1/*
2 * Copyright (c) 2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#include <errno.h>
25#include <libkern/OSMemoryNotification.h>
26#include <mach/mach.h>
27#include <notify.h>
28#include <stdlib.h>
29#include <sys/time.h>
30#include <dispatch/dispatch.h>
31#include <dispatch/private.h>
32
33const char *kOSMemoryNotificationName = "com.apple.system.memorystatus";
34
35struct _OSMemoryNotification {
36	int token;
37	mach_port_t port;
38};
39
40int
41OSMemoryNotificationCreate(OSMemoryNotificationRef *note)
42{
43	OSMemoryNotificationRef ref = malloc(sizeof(struct _OSMemoryNotification));
44
45	if (NULL == ref) {
46		return ENOMEM;
47	}
48
49	if (NOTIFY_STATUS_OK != notify_register_mach_port(kOSMemoryNotificationName, &ref->port, 0, &ref->token))
50		return ENOMEM;
51
52	*note = ref;
53
54	return 0;
55}
56
57int
58OSMemoryNotificationDestroy(OSMemoryNotificationRef note)
59{
60	if (NOTIFY_STATUS_OK != notify_cancel(note->token))
61		return EINVAL;
62
63	if (KERN_SUCCESS != mach_port_deallocate(mach_task_self(), note->port))
64		return EINVAL;
65
66	free(note);
67
68	return 0;
69}
70
71int
72OSMemoryNotificationWait(OSMemoryNotificationRef note, OSMemoryNotificationLevel *level)
73{
74	return OSMemoryNotificationTimedWait(note, level, NULL);
75}
76
77static mach_msg_timeout_t
78abs_to_timeout(const struct timeval *abstime)
79{
80	struct timeval rel, now;
81
82	gettimeofday(&now, NULL);
83
84	rel.tv_usec = abstime->tv_usec - now.tv_usec;
85	rel.tv_sec = abstime->tv_sec - now.tv_sec;
86	if (rel.tv_usec < 0) {
87		rel.tv_usec += 1000000;
88		rel.tv_sec--;
89	}
90	if (((int)rel.tv_sec < 0) || ((rel.tv_sec == 0) && (rel.tv_usec == 0))) {
91		return 0;
92	}
93	return rel.tv_sec * 1000 + rel.tv_usec / 1000;
94}
95
96int
97OSMemoryNotificationTimedWait(OSMemoryNotificationRef note, OSMemoryNotificationLevel *level, const struct timeval *abstime)
98{
99    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
100    dispatch_retain(sema);
101
102    dispatch_source_t memoryNotificationSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_VM, 0, DISPATCH_VM_PRESSURE, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
103    dispatch_source_set_event_handler(memoryNotificationSource, ^{
104        dispatch_semaphore_signal(sema);
105        dispatch_release(sema);
106    });
107
108    dispatch_resume(memoryNotificationSource);
109
110    dispatch_time_t waitTime = DISPATCH_TIME_FOREVER;
111    if (abstime) {
112        mach_msg_timeout_t timeout = abs_to_timeout(abstime);
113        waitTime = dispatch_time(DISPATCH_TIME_NOW, timeout * 1e6);
114    }
115
116    long ret = dispatch_semaphore_wait(sema, waitTime);
117
118    dispatch_release(sema);
119    dispatch_release(memoryNotificationSource);
120
121    if (ret != 0) {
122        return ETIMEDOUT;
123    }
124
125    if (level) {
126        *level = OSMemoryNotificationLevelUrgent;
127    }
128
129    return 0;
130}
131
132OSMemoryNotificationLevel OSMemoryNotificationCurrentLevel(void)
133{
134	return OSMemoryNotificationLevelNormal;
135}
136