2 * Copyright (c) 2003, 2004, 2006, 2011 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * May 1, 2003 Allan Nathanson <ajn@apple.com>
32 #include <CoreFoundation/CoreFoundation.h>
33 #include <SystemConfiguration/SystemConfiguration.h>
34 #include <SystemConfiguration/SCPrivate.h> // for SCLog()
39 static CFMutableDictionaryRef cached_keys
= NULL
;
40 static CFMutableDictionaryRef cached_set
= NULL
;
41 static CFMutableArrayRef cached_removals
= NULL
;
42 static CFMutableArrayRef cached_notifys
= NULL
;
49 cached_keys
= CFDictionaryCreateMutable(NULL
,
51 &kCFTypeDictionaryKeyCallBacks
,
52 &kCFTypeDictionaryValueCallBacks
);
53 cached_set
= CFDictionaryCreateMutable(NULL
,
55 &kCFTypeDictionaryKeyCallBacks
,
56 &kCFTypeDictionaryValueCallBacks
);
57 cached_removals
= CFArrayCreateMutable(NULL
,
59 &kCFTypeArrayCallBacks
);
60 cached_notifys
= CFArrayCreateMutable(NULL
,
62 &kCFTypeArrayCallBacks
);
70 cache_SCDynamicStoreCopyValue(SCDynamicStoreRef store
, CFStringRef key
)
72 CFPropertyListRef value
;
74 value
= CFDictionaryGetValue(cached_set
, key
);
76 // if we have "set" a new value
77 return (CFRetain(value
));
80 if (CFArrayContainsValue(cached_removals
,
81 CFRangeMake(0, CFArrayGetCount(cached_removals
)),
83 // if we have "removed" the key
84 _SCErrorSet(kSCStatusNoKey
);
88 value
= CFDictionaryGetValue(cached_keys
, key
);
90 // if we have a cached value
91 return (CFRetain(value
));
94 value
= SCDynamicStoreCopyValue(store
, key
);
96 CFDictionarySetValue(cached_keys
, key
, value
);
105 cache_SCDynamicStoreSetValue(SCDynamicStoreRef store
, CFStringRef key
, CFPropertyListRef value
)
109 i
= CFArrayGetFirstIndexOfValue(cached_removals
,
110 CFRangeMake(0, CFArrayGetCount(cached_removals
)),
112 if (i
!= kCFNotFound
) {
113 // if previously "removed"
114 CFArrayRemoveValueAtIndex(cached_removals
, i
);
117 CFDictionarySetValue(cached_set
, key
, value
);
124 cache_SCDynamicStoreRemoveValue(SCDynamicStoreRef store
, CFStringRef key
)
126 CFDictionaryRemoveValue(cached_set
, key
);
128 if (!CFArrayContainsValue(cached_removals
,
129 CFRangeMake(0, CFArrayGetCount(cached_removals
)),
131 CFArrayAppendValue(cached_removals
, key
);
140 cache_SCDynamicStoreNotifyValue(SCDynamicStoreRef store
, CFStringRef key
)
142 if (!CFArrayContainsValue(cached_notifys
,
143 CFRangeMake(0, CFArrayGetCount(cached_notifys
)),
145 CFArrayAppendValue(cached_notifys
, key
);
154 cache_write(SCDynamicStoreRef store
)
156 if ((CFDictionaryGetCount(cached_set
) > 0) ||
157 (CFArrayGetCount(cached_removals
) > 0) ||
158 (CFArrayGetCount(cached_notifys
) > 0)) {
159 if (!SCDynamicStoreSetMultiple(store
,
165 CFSTR("SCDynamicStoreSetMultiple() failed: %s"),
166 SCErrorString(SCError()));
178 CFRelease(cached_keys
);
179 CFRelease(cached_set
);
180 CFRelease(cached_removals
);
181 CFRelease(cached_notifys
);