2 * Copyright (c) 2003, 2004, 2006 Apple Computer, 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
87 value
= CFDictionaryGetValue(cached_keys
, key
);
89 // if we have a cached value
90 return (CFRetain(value
));
93 value
= SCDynamicStoreCopyValue(store
, key
);
95 CFDictionarySetValue(cached_keys
, key
, value
);
104 cache_SCDynamicStoreSetValue(SCDynamicStoreRef store
, CFStringRef key
, CFPropertyListRef value
)
108 i
= CFArrayGetFirstIndexOfValue(cached_removals
,
109 CFRangeMake(0, CFArrayGetCount(cached_removals
)),
111 if (i
!= kCFNotFound
) {
112 // if previously "removed"
113 CFArrayRemoveValueAtIndex(cached_removals
, i
);
116 CFDictionarySetValue(cached_set
, key
, value
);
123 cache_SCDynamicStoreRemoveValue(SCDynamicStoreRef store
, CFStringRef key
)
125 CFDictionaryRemoveValue(cached_set
, key
);
127 if (!CFArrayContainsValue(cached_removals
,
128 CFRangeMake(0, CFArrayGetCount(cached_removals
)),
130 CFArrayAppendValue(cached_removals
, key
);
139 cache_SCDynamicStoreNotifyValue(SCDynamicStoreRef store
, CFStringRef key
)
141 if (!CFArrayContainsValue(cached_notifys
,
142 CFRangeMake(0, CFArrayGetCount(cached_notifys
)),
144 CFArrayAppendValue(cached_notifys
, key
);
153 cache_write(SCDynamicStoreRef store
)
155 if ((CFDictionaryGetCount(cached_set
) > 0) ||
156 (CFArrayGetCount(cached_removals
) > 0) ||
157 (CFArrayGetCount(cached_notifys
) > 0)) {
158 if (!SCDynamicStoreSetMultiple(store
,
164 CFSTR("SCDynamicStoreSetMultiple() failed: %s"),
165 SCErrorString(SCError()));
177 CFRelease(cached_keys
);
178 CFRelease(cached_set
);
179 CFRelease(cached_removals
);
180 CFRelease(cached_notifys
);