// Insert values into the table from a CFDictionary. // // If the number of key-values pairs in the dictionary is zero, insert is ignored. // // Example: // // NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: @"Mirek", @"name", // @"Rusin", @"surname"]; // // SQLite3InsertWithDictionary(connection, CFSTR("users"), (CFDictionaryRef)attributes); // // TODO: // - sanitize table name and keys // - return values for binding inline SQLite3Status SQLite3InsertWithTableNameAndDictionary(SQLite3ConnectionRef connection, CFStringRef table, CFDictionaryRef dictionary) { SQLite3Status status = kSQLite3StatusError; CFIndex n = CFDictionaryGetCount(dictionary); const void **keys = CFAllocatorAllocate(connection->allocator, sizeof(void *) * n, 0); const void **values = CFAllocatorAllocate(connection->allocator, sizeof(void *) * n, 0); CFDictionaryGetKeysAndValues(dictionary, keys, values); CFArrayRef keysArray = CFArrayCreate(connection->allocator, keys, n, &kCFTypeArrayCallBacks); CFArrayRef valuesArray = CFArrayCreate(connection->allocator, values, n, &kCFTypeArrayCallBacks); CFStringRef keysString = CFStringCreateByCombiningStrings(connection->allocator, keysArray, CFSTR(", ")); const char *valuesCString = _SQLite3CreateValuesPlaceholderCString(connection->allocator, n); CFStringRef sql = CFStringCreateWithFormat(connection->allocator, NULL, CFSTR("INSERT INTO %@(%@) VALUES(%s)"), table, keysString, valuesCString); SQLite3StatementRef statement = SQLite3StatementCreate(connection, sql); if (statement) { SQLite3StatementBindArray(statement, valuesArray); status = SQLite3StatementStep(statement); SQLite3StatementReset(statement); SQLite3StatementClearBindings(statement); SQLite3StatementFinalize(statement); SQLite3StatementRelease(statement); } CFRelease(sql); CFAllocatorDeallocate(connection->allocator, (void *)valuesCString); CFRelease(keysString); CFRelease(valuesArray); CFRelease(keysArray); CFAllocatorDeallocate(connection->allocator, keys); CFAllocatorDeallocate(connection->allocator, values); return status; }
inline int __JSONParserAppendMapEnd(void *context) { int success = 0; __JSONRef json = (__JSONRef)context; __JSONStackEntryRef entry = __JSONStackPop(json->stack); if (entry) { if (entry->keysIndex == entry->valuesIndex) { CFTypeRef *keys = __JSONStackEntryCreateKeys(entry, json->elements); if (keys) { CFTypeRef *values = __JSONStackEntryCreateValues(entry, json->elements); if (values) { json->elements[entry->index] = CFDictionaryCreate(json->allocator, keys, values, entry->keysIndex, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFAllocatorDeallocate(entry->allocator, values); success = 1; } CFAllocatorDeallocate(entry->allocator, keys); } } else { // TODO: The number of keys and values does not match } __JSONStackEntryRelease(entry); } else { // TODO: Container on the stack can't be NULL (too deep?) } return success; }
CFComparisonResult CFStringCompareWithOptionsAndLocale (CFStringRef str1, CFStringRef str2, CFRange rangeToCompare, CFStringCompareFlags compareOptions, CFLocaleRef locale) { CFComparisonResult ret; UniChar *string1; UniChar *string2; CFIndex length1; CFIndex length2; CFAllocatorRef alloc; UCollator *ucol; alloc = CFAllocatorGetDefault (); length1 = rangeToCompare.length; string1 = CFAllocatorAllocate (alloc, (length1) * sizeof(UniChar), 0); CFStringGetCharacters (str1, rangeToCompare, string1); length2 = CFStringGetLength (str2); string2 = CFAllocatorAllocate (alloc, (length2) * sizeof(UniChar), 0); CFStringGetCharacters (str2, CFRangeMake(0, length2), string2); ucol = CFStringICUCollatorOpen (compareOptions, locale); ret = ucol_strcoll (ucol, string2, length2, string1, length1); CFStringICUCollatorClose (ucol); CFAllocatorDeallocate (alloc, string1); CFAllocatorDeallocate (alloc, string2); return ret; }
CFArrayRef /* of SCNetworkSetRef's */ SCNetworkSetCopyAll(SCPreferencesRef prefs) { CFMutableArrayRef array; CFIndex n; CFStringRef path; CFDictionaryRef sets; path = SCPreferencesPathKeyCreateSets(NULL); sets = SCPreferencesPathGetValue(prefs, path); CFRelease(path); if ((sets != NULL) && !isA_CFDictionary(sets)) { return NULL; } array = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); n = (sets != NULL) ? CFDictionaryGetCount(sets) : 0; if (n > 0) { CFIndex i; const void * keys_q[N_QUICK]; const void ** keys = keys_q; const void * vals_q[N_QUICK]; const void ** vals = vals_q; if (n > (CFIndex)(sizeof(keys_q) / sizeof(CFTypeRef))) { keys = CFAllocatorAllocate(NULL, n * sizeof(CFTypeRef), 0); vals = CFAllocatorAllocate(NULL, n * sizeof(CFPropertyListRef), 0); } CFDictionaryGetKeysAndValues(sets, keys, vals); for (i = 0; i < n; i++) { SCNetworkSetPrivateRef setPrivate; if (!isA_CFDictionary(vals[i])) { SC_log(LOG_INFO, "error w/set \"%@\"", keys[i]); continue; } setPrivate = __SCNetworkSetCreatePrivate(NULL, prefs, keys[i]); assert(setPrivate != NULL); // mark set as "old" (already established) setPrivate->established = TRUE; CFArrayAppendValue(array, (SCNetworkSetRef)setPrivate); CFRelease(setPrivate); } if (keys != keys_q) { CFAllocatorDeallocate(NULL, keys); CFAllocatorDeallocate(NULL, vals); } } return array; }
static void writeDataFinalize(struct _CFStream *stream, void *info) { _CFWriteDataStreamContext *ctxt = (_CFWriteDataStreamContext *)info; if (ctxt->bufferAllocator != kCFAllocatorNull) { _CFStreamByteBuffer *buf = ctxt->firstBuf->next, *next; while (buf != NULL) { next = buf->next; CFAllocatorDeallocate(ctxt->bufferAllocator, buf); buf = next; } CFRelease(ctxt->bufferAllocator); } CFAllocatorDeallocate(CFGetAllocator(stream), ctxt); }
/* extern */ void HttpContextRelease(HttpContextRef context) { if (context != NULL) { // Decrease the retain count. context->_retainCount--; // Don't dispose until the count goes to zero. if (context->_retainCount > 0) return; // The streams and timers retain the context. If any of these are still set, it means // that somehow we hit a retain count of zero, but are still held by them! This would // happen if the context was over-released. assert(!context->_inStream && !context->_outStream && !context->_timer); // Hold locally so deallocation can happen and then safely release. CFAllocatorRef alloc = context->_alloc; // Release request if (context->_request != NULL) { // Release uri if (context->_request->_uri != NULL ) CFAllocatorDeallocate(alloc, context->_request->_uri); // Free memory allocated for headers if (context->_request->_headers != NULL) CFAllocatorDeallocate(alloc, context->_request->_headers); // Free memory in use by the request CFAllocatorDeallocate(alloc, context->_request); } // Release the recieve buffer if there is one. if (context->_rcvdBytes != NULL) CFRelease(context->_rcvdBytes); // Release the send buffer if there is one. if (context->_sendBytes != NULL) CFRelease(context->_sendBytes); // Free the memory in use by the context. CFAllocatorDeallocate(alloc, context); // Release the allocator. if (alloc) CFRelease(alloc); } }
__private_extern__ int PPPSetOption(int ref, u_int8_t *serviceid, u_int32_t option, void *data, u_int32_t dataLen) { void *buf; u_long bufLen; int status; bufLen = sizeof(struct ppp_opt_hdr) + dataLen; buf = CFAllocatorAllocate(NULL, bufLen, 0); bzero((struct ppp_opt_hdr *)buf, sizeof(struct ppp_opt_hdr)); ((struct ppp_opt_hdr *)buf)->o_type = option; bcopy(data, ((struct ppp_opt *)buf)->o_data, dataLen); status = PPPExec(ref, serviceid, PPP_SETOPTION, buf, bufLen, NULL, NULL); if (status != 0) { fprintf(stderr, "PPPExec(PPP_SETOPTION) failed: status = %d\n", status); } CFAllocatorDeallocate(NULL, buf); return status; }
__private_extern__ int PPPGetOption(int ref, u_int8_t *serviceid, u_int32_t option, void **data, u_int32_t *dataLen) { struct ppp_opt_hdr opt; void *replyBuf = NULL; u_int32_t replyBufLen = 0; int status; bzero(&opt, sizeof(opt)); opt.o_type = option; status = PPPExec(ref, serviceid, PPP_GETOPTION, (void *)&opt, sizeof(opt), &replyBuf, &replyBufLen); if (status != 0) { fprintf(stderr, "PPPExec(PPP_GETOPTION) failed: status = %d\n", status); *data = NULL; *dataLen = 0; return status; } if (replyBuf && (replyBufLen > sizeof(struct ppp_opt_hdr))) { *dataLen = replyBufLen - sizeof(struct ppp_opt_hdr); *data = CFAllocatorAllocate(NULL, *dataLen, 0); bcopy(((struct ppp_opt *)replyBuf)->o_data, *data, *dataLen); } if (replyBuf) CFAllocatorDeallocate(NULL, replyBuf); return status; }
static Boolean _CFReadBytesFromPathAndGetFD(CFAllocatorRef alloc, const char *path, void **bytes, CFIndex *length, CFIndex maxLength, int extraOpenFlags, int *fd) { // maxLength is the number of bytes desired, or 0 if the whole file is desired regardless of length. struct statinfo statBuf; *bytes = NULL; int no_hang_fd = openAutoFSNoWait(); *fd = open(path, O_RDONLY|extraOpenFlags|CF_OPENFLGS, 0666); if (*fd < 0) { closeAutoFSNoWait(no_hang_fd); return false; } if (fstat(*fd, &statBuf) < 0) { int saveerr = thread_errno(); close(*fd); *fd = -1; closeAutoFSNoWait(no_hang_fd); thread_set_errno(saveerr); return false; } if ((statBuf.st_mode & S_IFMT) != S_IFREG) { close(*fd); *fd = -1; closeAutoFSNoWait(no_hang_fd); thread_set_errno(EACCES); return false; } if (statBuf.st_size == 0) { *bytes = CFAllocatorAllocate(alloc, 4, 0); // don't return constant string -- it's freed! if (__CFOASafe) __CFSetLastAllocationEventName(*bytes, "CFUtilities (file-bytes)"); *length = 0; } else { CFIndex desiredLength; if ((maxLength >= statBuf.st_size) || (maxLength == 0)) { desiredLength = statBuf.st_size; } else { desiredLength = maxLength; } *bytes = CFAllocatorAllocate(alloc, desiredLength, 0); if (!bytes) { close(*fd); *fd = -1; closeAutoFSNoWait(no_hang_fd); return false; } if (__CFOASafe) __CFSetLastAllocationEventName(*bytes, "CFUtilities (file-bytes)"); // fcntl(fd, F_NOCACHE, 1); if (read(*fd, *bytes, desiredLength) < 0) { CFAllocatorDeallocate(alloc, *bytes); close(*fd); *fd = -1; closeAutoFSNoWait(no_hang_fd); return false; } *length = desiredLength; } closeAutoFSNoWait(no_hang_fd); return true; }
CFMutableSetRef CFSetCreateMutableCopy (CFAllocatorRef allocator, CFIndex capacity, CFSetRef set) { if (CF_IS_OBJC (_kCFSetTypeID, set)) { CFMutableSetRef result; const CFIndex count = CFSetGetCount (set); void **values = (void **) CFAllocatorAllocate (allocator, sizeof (void *) * count, 0); CFIndex i; CFSetGetValues (set, (const void **) values); result = CFSetCreateMutable (allocator, count, &kCFTypeSetCallBacks); for (i = 0; i < count; i++) GSHashTableAddValue ((GSHashTableRef) result, values[i], values[i]); CFAllocatorDeallocate (allocator, (void *) values); return result; } return (CFMutableSetRef) GSHashTableCreateMutableCopy (allocator, (GSHashTableRef) set, capacity); }
void CFTreeSortChildren(CFTreeRef tree, CFComparatorFunction comparator, void *context) { CFIndex children; __CFGenericValidateType(tree, __kCFTreeTypeID); CFAssert1(NULL != comparator, __kCFLogAssertion, "%s(): pointer to comparator function may not be NULL", __PRETTY_FUNCTION__); children = CFTreeGetChildCount(tree); if (1 < children) { CFIndex idx; CFTreeRef nextChild; struct _tcompareContext ctx; CFTreeRef *list, buffer[128]; list = (children < 128) ? buffer : (CFTreeRef *)CFAllocatorAllocate(kCFAllocatorSystemDefault, children * sizeof(CFTreeRef), 0); // XXX_PCB GC OK if (__CFOASafe && list != buffer) __CFSetLastAllocationEventName(tree->_callbacks, "CFTree (temp)"); nextChild = tree->_child; for (idx = 0; NULL != nextChild; idx++) { list[idx] = nextChild; nextChild = nextChild->_sibling; } ctx.func = comparator; ctx.context = context; CFQSortArray(list, children, sizeof(CFTreeRef), (CFComparatorFunction)__CFTreeCompareValues, &ctx); __CFAssignWithWriteBarrier((void **)&tree->_child, list[0]); for (idx = 1; idx < children; idx++) { __CFAssignWithWriteBarrier((void **)&list[idx - 1]->_sibling, list[idx]); } list[idx - 1]->_sibling = NULL; tree->_rightmostChild = list[children - 1]; if (list != buffer) CFAllocatorDeallocate(kCFAllocatorSystemDefault, list); // XXX_PCB GC OK } }
CFArrayRef SCPreferencesCopyKeyList(SCPreferencesRef prefs) { CFAllocatorRef allocator; CFArrayRef keys; SCPreferencesPrivateRef prefsPrivate = (SCPreferencesPrivateRef)prefs; CFIndex prefsCnt; const void ** prefsKeys; if (prefs == NULL) { /* sorry, you must provide a session */ _SCErrorSet(kSCStatusNoPrefsSession); return NULL; } __SCPreferencesAccess(prefs); allocator = CFGetAllocator(prefs); prefsCnt = CFDictionaryGetCount(prefsPrivate->prefs); if (prefsCnt > 0) { prefsKeys = CFAllocatorAllocate(allocator, prefsCnt * sizeof(CFStringRef), 0); CFDictionaryGetKeysAndValues(prefsPrivate->prefs, prefsKeys, NULL); keys = CFArrayCreate(allocator, prefsKeys, prefsCnt, &kCFTypeArrayCallBacks); CFAllocatorDeallocate(allocator, prefsKeys); } else { keys = CFArrayCreate(allocator, NULL, 0, &kCFTypeArrayCallBacks); } return keys; }
CFArrayRef /* of SCNetworkInterfaceRef's */ SCBondStatusGetMemberInterfaces(SCBondStatusRef bondStatus) { SCBondStatusPrivateRef statusPrivate = (SCBondStatusPrivateRef)bondStatus; if (!isA_SCBondStatus(bondStatus)) { return NULL; } if (statusPrivate->interfaces == NULL) { const void * keys_q[N_QUICK]; const void ** keys = keys_q; CFIndex n; n = CFDictionaryGetCount(statusPrivate->status_interfaces); if (n > (CFIndex)(sizeof(keys_q) / sizeof(CFTypeRef))) { keys = CFAllocatorAllocate(NULL, n * sizeof(CFTypeRef), 0); } CFDictionaryGetKeysAndValues(statusPrivate->status_interfaces, keys, NULL); statusPrivate->interfaces = CFArrayCreate(NULL, keys, n, &kCFTypeArrayCallBacks); if (keys != keys_q) { CFAllocatorDeallocate(NULL, keys); } } return statusPrivate->interfaces; }
inline CFIndex __JSONStackEntryRelease(__JSONStackEntryRef entry) { CFIndex retainCount = 0; if (entry) { if ((retainCount = --entry->retainCount) == 0) { CFAllocatorRef allocator = entry->allocator; if (entry->values) CFAllocatorDeallocate(allocator, entry->values); if (entry->keys) CFAllocatorDeallocate(allocator, entry->keys); CFAllocatorDeallocate(allocator, entry); if (allocator) CFRelease(allocator); } } return retainCount; }
__private_extern__ int PPPStatus(int ref, u_int8_t *serviceid, struct ppp_status **stat) { void *replyBuf = NULL; u_int32_t replyBufLen = 0; int status; status = PPPExec(ref, serviceid, PPP_STATUS, NULL, 0, &replyBuf, &replyBufLen); if (status != 0) { fprintf(stderr, "PPPExec(PPP_STATUS) failed: status = %d\n", status); return status; } if (replyBuf && (replyBufLen == sizeof(struct ppp_status))) { *stat = (struct ppp_status *)replyBuf; } else { if (replyBuf) CFAllocatorDeallocate(NULL, replyBuf); *stat = NULL; status = -1; } return status; }
void CFPreferencesFlushCaches(void) { CFAllocatorRef alloc = __CFPreferencesAllocator(); __CFSpinLock(&__CFApplicationPreferencesLock); if (__CFStandardUserPreferences) { _CFApplicationPreferences **prefsArray, *prefsBuf[32]; CFIndex idx, count = CFDictionaryGetCount(__CFStandardUserPreferences); if (count < 32) { prefsArray = prefsBuf; } else { prefsArray = (_CFApplicationPreferences **)CFAllocatorAllocate(alloc, count * sizeof(_CFApplicationPreferences *), 0); } CFDictionaryGetKeysAndValues(__CFStandardUserPreferences, NULL, (const void **)prefsArray); __CFSpinUnlock(&__CFApplicationPreferencesLock); // DeallocateApplicationPreferences needs the lock for (idx = 0; idx < count; idx ++) { _CFApplicationPreferences *appPrefs = prefsArray[idx]; _CFApplicationPreferencesSynchronize(appPrefs); _CFDeallocateApplicationPreferences(appPrefs); } __CFSpinLock(&__CFApplicationPreferencesLock); CFRelease(__CFStandardUserPreferences); __CFStandardUserPreferences = NULL; if(prefsArray != prefsBuf) CFAllocatorDeallocate(alloc, prefsArray); } __CFSpinUnlock(&__CFApplicationPreferencesLock); _CFPreferencesPurgeDomainCache(); }
CFArrayRef CMCreateArrayWithMonoStringArray(CFAllocatorRef allocator, MonoArray *monoArray) { CFArrayRef array = NULL; if (monoArray) { uintptr_t n = mono_array_length(monoArray); CFTypeRef *values = CFAllocatorAllocate(allocator, n * sizeof(CFTypeRef), 0); if (values) { for (uintptr_t i = 0; i < n; i++) { values[i] = kCFNull; MonoString *monoString = mono_array_get(monoArray, MonoString*, i); if (monoString) { char *utf8MonoString = mono_string_to_utf8(monoString); if (utf8MonoString) { CFStringRef string = CFStringCreateWithCString(allocator, utf8MonoString, kCFStringEncodingUTF8); if (string) { values[i] = string; } mono_free(utf8MonoString); } } } array = CFArrayCreate(allocator, values, n, &kCFTypeArrayCallBacks); for (uintptr_t i = 0; i < n; i++) { if (values[i] != kCFNull) { CFRelease(values[i]); } } CFAllocatorDeallocate(allocator, values); } } return array; }
static CFStringRef __CFBinaryHeapCopyDescription(CFTypeRef cf) { CFBinaryHeapRef heap = (CFBinaryHeapRef)cf; CFMutableStringRef result; CFIndex idx; CFIndex cnt; const void **list, *buffer[256]; cnt = __CFBinaryHeapCount(heap); result = CFStringCreateMutable(CFGetAllocator(heap), 0); CFStringAppendFormat(result, NULL, CFSTR("<CFBinaryHeap %p [%p]>{count = %lu, capacity = %lu, objects = (\n"), cf, CFGetAllocator(heap), (unsigned long)cnt, (unsigned long)__CFBinaryHeapCapacity(heap)); list = (cnt <= 128) ? (const void **)buffer : (const void **)CFAllocatorAllocate(kCFAllocatorSystemDefault, cnt * sizeof(void *), 0); // GC OK if (__CFOASafe && list != buffer) __CFSetLastAllocationEventName(list, "CFBinaryHeap (temp)"); CFBinaryHeapGetValues(heap, list); for (idx = 0; idx < cnt; idx++) { CFStringRef desc = NULL; const void *item = list[idx]; if (NULL != heap->_callbacks.copyDescription) { desc = heap->_callbacks.copyDescription(item); } if (NULL != desc) { CFStringAppendFormat(result, NULL, CFSTR("\t%lu : %@\n"), (unsigned long)idx, desc); CFRelease(desc); } else { CFStringAppendFormat(result, NULL, CFSTR("\t%lu : <%p>\n"), (unsigned long)idx, item); } } CFStringAppend(result, CFSTR(")}")); if (list != buffer) CFAllocatorDeallocate(CFGetAllocator(heap), list); // GC OK return result; }
CFArrayRef CFPreferencesCopyKeyList(CFStringRef appName, CFStringRef user, CFStringRef host) { CFPreferencesDomainRef domain; CFAssert1(appName != NULL && user != NULL && host != NULL, __kCFLogAssertion, "%s(): Cannot access preferences for a NULL application name, user, or host", __PRETTY_FUNCTION__); domain = _CFPreferencesStandardDomain(appName, user, host); if (!domain) { return NULL; } else { CFArrayRef result; CFAllocatorRef alloc = __CFPreferencesAllocator(); CFDictionaryRef d = _CFPreferencesDomainDeepCopyDictionary(domain); CFIndex count = d ? CFDictionaryGetCount(d) : 0; CFTypeRef *keys = (CFTypeRef *)CFAllocatorAllocate(alloc, count * sizeof(CFTypeRef), 0); if (d) CFDictionaryGetKeysAndValues(d, keys, NULL); if (count == 0) { result = NULL; } else { result = CFArrayCreate(alloc, keys, count, &kCFTypeArrayCallBacks); } CFAllocatorDeallocate(alloc, keys); if (d) CFRelease(d); return result; } }
static Boolean __CFBinaryHeapEqual(CFTypeRef cf1, CFTypeRef cf2) { CFBinaryHeapRef heap1 = (CFBinaryHeapRef)cf1; CFBinaryHeapRef heap2 = (CFBinaryHeapRef)cf2; CFComparisonResult (*compare)(const void *, const void *, void *); CFIndex idx; CFIndex cnt; const void **list1, **list2, *buffer[256]; cnt = __CFBinaryHeapCount(heap1); if (cnt != __CFBinaryHeapCount(heap2)) return false; compare = heap1->_callbacks.compare; if (compare != heap2->_callbacks.compare) return false; if (0 == cnt) return true; /* after function comparison */ list1 = (cnt <= 128) ? (const void **)buffer : (const void **)CFAllocatorAllocate(kCFAllocatorSystemDefault, 2 * cnt * sizeof(void *), 0); // GC OK if (__CFOASafe && list1 != buffer) __CFSetLastAllocationEventName(list1, "CFBinaryHeap (temp)"); list2 = (cnt <= 128) ? buffer + 128 : list1 + cnt; CFBinaryHeapGetValues(heap1, list1); CFBinaryHeapGetValues(heap2, list2); for (idx = 0; idx < cnt; idx++) { const void *val1 = list1[idx]; const void *val2 = list2[idx]; // CF: which context info should be passed in? both? // CF: if the context infos are not equal, should the heaps not be equal? if (val1 != val2) { if (NULL == compare) return false; if (!compare(val1, val2, heap1->_context.info)) return false; } } if (list1 != buffer) CFAllocatorDeallocate(CFGetAllocator(heap1), list1); // GC OK return true; }
static void __CFICUThreadDataDestructor(void *context) { __CFICUThreadData * data = (__CFICUThreadData *)context; if (NULL != data->_converters) { // scan to make sure deallocation UConverter **converter = data->_converters; UConverter **limit = converter + data->_numSlots; while (converter < limit) { if (NULL != converter) ucnv_close(*converter); ++converter; } CFAllocatorDeallocate(NULL, data->_converters); } CFAllocatorDeallocate(NULL, data); }
static char *_CFSearchForNameInPath(CFAllocatorRef alloc, const char *name, char *path) { struct stat statbuf; char *nname = CFAllocatorAllocate(alloc, strlen(name) + strlen(path) + 2, 0); if (__CFOASafe) __CFSetLastAllocationEventName(nname, "CFUtilities (temp)"); for (;;) { char *p = (char *)strchr(path, PATH_LIST_SEP); if (NULL != p) { *p = '\0'; } nname[0] = '\0'; strcat(nname, path); strcat(nname, "/"); strcat(nname, name); // Could also do access(us, X_OK) == 0 in next condition, // for executable-only searching if (0 == stat(nname, &statbuf) && (statbuf.st_mode & S_IFMT) == S_IFREG) { if (p != NULL) { *p = PATH_LIST_SEP; } return nname; } if (NULL == p) { break; } *p = PATH_LIST_SEP; path = p + 1; } CFAllocatorDeallocate(alloc, nname); return NULL; }
CF_PRIVATE CFStringEncoding *__CFStringEncodingCreateICUEncodings(CFAllocatorRef allocator, CFIndex *numberOfIndex) { CFIndex count = ucnv_countAvailable(); CFIndex numEncodings = 0; CFStringEncoding *encodings; CFStringEncoding encoding; CFIndex index; if (0 == count) return NULL; encodings = (CFStringEncoding *)CFAllocatorAllocate(NULL, sizeof(CFStringEncoding) * count, 0); for (index = 0;index < count;index++) { encoding = __CFStringEncodingGetFromICUName(ucnv_getAvailableName(index)); if (kCFStringEncodingInvalidId != encoding) encodings[numEncodings++] = encoding; } if (0 == numEncodings) { CFAllocatorDeallocate(allocator, encodings); encodings = NULL; } *numberOfIndex = numEncodings; return encodings; }
static unsigned __stdcall __CFWinThreadFunc(void *arg) { struct _args *args = (struct _args*)arg; ((void (*)(void *))args->func)(args->arg); CloseHandle(args->handle); CFAllocatorDeallocate(kCFAllocatorSystemDefault, arg); _endthreadex(0); return 0; }
CF_PRIVATE void _freeInputStream(_CFXMLInputStream *stream) { if (stream->data) CFRelease(stream->data); if (stream->url) CFRelease(stream->url); if (stream->charBuffer) CFAllocatorDeallocate(stream->allocator, stream->charBuffer); if (stream->nameSet) CFRelease(stream->nameSet); if (stream->tempString) CFRelease(stream->tempString); CFRelease(stream->allocator); }
extern pascal void CFQDeallocate(void *blockPtr) // See comment in header. { // CFAllocatorDeallocate handles blockPtr being NULL. // NULL is a synonym for kCFAllocatorDefault. CFAllocatorDeallocate(NULL, blockPtr); }
struct sockaddr * _SC_string_to_sockaddr(const char *str, sa_family_t af, void *buf, size_t bufLen) { union { void *buf; struct sockaddr *sa; struct sockaddr_in *sin; struct sockaddr_in6 *sin6; } addr; if (buf == NULL) { bufLen = sizeof(struct sockaddr_storage); addr.buf = CFAllocatorAllocate(NULL, bufLen, 0); } else { addr.buf = buf; } bzero(addr.buf, bufLen); if (((af == AF_UNSPEC) || (af == AF_INET)) && (bufLen >= sizeof(struct sockaddr_in)) && inet_aton(str, &addr.sin->sin_addr) == 1) { // if IPv4 address addr.sin->sin_len = sizeof(struct sockaddr_in); addr.sin->sin_family = AF_INET; } else if (((af == AF_UNSPEC) || (af == AF_INET6)) && (bufLen >= sizeof(struct sockaddr_in6)) && inet_pton(AF_INET6, str, &addr.sin6->sin6_addr) == 1) { // if IPv6 address char *p; addr.sin6->sin6_len = sizeof(struct sockaddr_in6); addr.sin6->sin6_family = AF_INET6; p = strchr(buf, '%'); if (p != NULL) { addr.sin6->sin6_scope_id = if_nametoindex(p + 1); } if (IN6_IS_ADDR_LINKLOCAL(&addr.sin6->sin6_addr) || IN6_IS_ADDR_MC_LINKLOCAL(&addr.sin6->sin6_addr)) { uint16_t if_index; if_index = ntohs(addr.sin6->sin6_addr.__u6_addr.__u6_addr16[1]); addr.sin6->sin6_addr.__u6_addr.__u6_addr16[1] = 0; if (addr.sin6->sin6_scope_id == 0) { // use the scope id that was embedded in the [link local] IPv6 address addr.sin6->sin6_scope_id = if_index; } } } else { if (addr.buf != buf) { CFAllocatorDeallocate(NULL, addr.buf); } addr.buf = NULL; } return addr.sa; }
inline __JSONStackRef __JSONStackRelease(__JSONStackRef stack) { if (stack) { if ( --stack->retainCount == 0) { CFAllocatorRef allocator = stack->allocator; if (stack->stack) { while (--stack->index >= 0) __JSONStackEntryRelease(stack->stack[stack->index]); if (stack->stack) CFAllocatorDeallocate(allocator, stack->stack); stack->stack = NULL; } CFAllocatorDeallocate(allocator, stack); stack = NULL; if (allocator) CFRelease(allocator); } } return stack; }
Boolean CFMessagePortSetName(CFMessagePortRef ms, CFStringRef name) { CFAllocatorRef allocator = CFGetAllocator(ms); uint8_t *utfname = NULL; __CFGenericValidateType(ms, __kCFMessagePortTypeID); // if (__CFMessagePortIsRemote(ms)) return false; //#warning CF: make this an assertion // and assert than newName is non-NULL name = __CFMessagePortSanitizeStringName(allocator, name, &utfname, NULL); if (NULL == name) { return false; } __CFSpinLock(&__CFAllMessagePortsLock); if (NULL != name) { CFMessagePortRef existing; if (NULL != __CFAllLocalMessagePorts && CFDictionaryGetValueIfPresent(__CFAllLocalMessagePorts, name, (const void **)&existing)) { __CFSpinUnlock(&__CFAllMessagePortsLock); CFRelease(name); CFAllocatorDeallocate(allocator, utfname); return false; } } if (NULL != name && (NULL == ms->_name || !CFEqual(ms->_name, name))) { if (!__CFMessagePortNativeSetNameLocal(ms->_port, utfname)) { __CFSpinUnlock(&__CFAllMessagePortsLock); CFRelease(name); CFAllocatorDeallocate(allocator, utfname); return false; } if (NULL == __CFAllLocalMessagePorts) { __CFAllLocalMessagePorts = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeDictionaryKeyCallBacks, NULL); } if (NULL != ms->_name) { CFDictionaryRemoveValue(__CFAllLocalMessagePorts, ms->_name); CFRelease(ms->_name); } ms->_name = name; CFDictionaryAddValue(__CFAllLocalMessagePorts, name, ms); } __CFSpinUnlock(&__CFAllMessagePortsLock); CFAllocatorDeallocate(allocator, utfname); return true; }
static void __CFBinaryHeapDeallocate(CFTypeRef cf) { CFBinaryHeapRef heap = (CFBinaryHeapRef)cf; CFAllocatorRef allocator = CFGetAllocator(heap); // CF: should make the heap mutable here first, a la CFArrayDeallocate CFBinaryHeapRemoveAllValues(heap); // CF: does not release the context info if (__CFBinaryHeapMutableVariety(heap) == kCFBinaryHeapMutable) { CFAllocatorDeallocate(allocator, heap->_buckets); } }