Index: src/OFData.m ================================================================== --- src/OFData.m +++ src/OFData.m @@ -195,24 +195,21 @@ # if ULLONG_MAX > SIZE_MAX if (size > SIZE_MAX) @throw [OFOutOfRangeException exception]; # endif - if ((buffer = malloc((size_t)size)) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: (size_t)size]; - + buffer = of_malloc(1, (size_t)size); file = [[OFFile alloc] initWithPath: path mode: @"r"]; @try { [file readIntoBuffer: buffer exactLength: (size_t)size]; } @finally { [file release]; } } @catch (id e) { - free(buffer); + of_free(buffer); [self release]; @throw e; } @@ -219,11 +216,11 @@ @try { self = [self initWithItemsNoCopy: buffer count: (size_t)size freeWhenDone: true]; } @catch (id e) { - free(buffer); + of_free(buffer); @throw e; } return self; } @@ -381,11 +378,11 @@ } - (void)dealloc { if (_freeWhenDone) - free(_items); + of_free(_items); [_parentData release]; [super dealloc]; } Index: src/OFDate.m ================================================================== --- src/OFDate.m +++ src/OFDate.m @@ -777,14 +777,11 @@ } # endif #endif pageSize = [OFSystemInfo pageSize]; - if ((buffer = malloc(pageSize)) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: pageSize]; - + buffer = of_malloc(1, pageSize); @try { #ifndef OF_WINDOWS if (strftime(buffer, pageSize, format.UTF8String, &tm) == 0) @throw [OFOutOfRangeException exception]; @@ -795,11 +792,11 @@ @throw [OFOutOfRangeException exception]; ret = [OFString stringWithUTF16String: buffer]; #endif } @finally { - free(buffer); + of_free(buffer); } return ret; } @@ -840,14 +837,11 @@ } # endif #endif pageSize = [OFSystemInfo pageSize]; - if ((buffer = malloc(pageSize)) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: pageSize]; - + buffer = of_malloc(1, pageSize); @try { #ifndef OF_WINDOWS if (strftime(buffer, pageSize, format.UTF8String, &tm) == 0) @throw [OFOutOfRangeException exception]; @@ -858,11 +852,11 @@ @throw [OFOutOfRangeException exception]; ret = [OFString stringWithUTF16String: buffer]; #endif } @finally { - free(buffer); + of_free(buffer); } return ret; } Index: src/OFFile.m ================================================================== --- src/OFFile.m +++ src/OFFile.m @@ -96,11 +96,11 @@ handle->next->previous = handle->previous; if (firstHandle == handle) firstHandle = handle->next; - free(handle); + of_free(handle); } OF_DESTRUCTOR() { for (of_file_handle_t iter = firstHandle; iter != NULL; @@ -241,14 +241,11 @@ @throw [OFOpenItemFailedException exceptionWithPath: path mode: mode errNo: errno]; #else - if ((handle = malloc(sizeof(*handle))) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: sizeof(*handle)]; - + handle = of_malloc(1, sizeof(*handle)); @try { if ((flags = parseMode(mode.UTF8String, &handle->append)) == -1) @throw [OFInvalidArgumentException exception]; @@ -306,11 +303,11 @@ if (firstHandle != NULL) firstHandle->previous = handle; firstHandle = handle; } @catch (id e) { - free(handle); + of_free(handle); @throw e; } #endif objc_autoreleasePoolPop(pool); Index: src/OFFileManager.m ================================================================== --- src/OFFileManager.m +++ src/OFFileManager.m @@ -663,14 +663,11 @@ size_t pageSize = [OFSystemInfo pageSize]; OFStream *sourceStream = nil; OFStream *destinationStream = nil; char *buffer; - if ((buffer = malloc(pageSize)) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: pageSize]; - + buffer = of_malloc(1, pageSize); @try { sourceStream = [[OFURLHandler handlerForURL: source] openItemAtURL: source mode: @"r"]; destinationStream = [[OFURLHandler handlerForURL: @@ -719,11 +716,11 @@ @throw e; } @finally { [sourceStream close]; [destinationStream close]; - free(buffer); + of_free(buffer); } } else if ([type isEqual: of_file_type_symbolic_link]) { @try { OFString *linkDestination = attributes.fileSymbolicLinkDestination; Index: src/OFHTTPClient.m ================================================================== --- src/OFHTTPClient.m +++ src/OFHTTPClient.m @@ -508,23 +508,20 @@ lineC = line.UTF8String; if ((tmp = strchr(lineC, ':')) == NULL) @throw [OFInvalidServerReplyException exception]; - if ((keyC = malloc(tmp - lineC + 1)) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: tmp - lineC + 1]; - + keyC = of_malloc(1, tmp - lineC + 1); memcpy(keyC, lineC, tmp - lineC); keyC[tmp - lineC] = '\0'; normalizeKey(keyC); @try { key = [OFString stringWithUTF8StringNoCopy: keyC freeWhenDone: true]; } @catch (id e) { - free(keyC); + of_free(keyC); @throw e; } do { tmp++; Index: src/OFObject+KeyValueCoding.m ================================================================== --- src/OFObject+KeyValueCoding.m +++ src/OFObject+KeyValueCoding.m @@ -48,24 +48,21 @@ if ((keyLength = key.UTF8StringLength) < 1) { objc_autoreleasePoolPop(pool); return [self valueForUndefinedKey: key]; } - if ((name = malloc(keyLength + 3)) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: keyLength + 3]; - + name = of_malloc(1, keyLength + 3); @try { memcpy(name, "is", 2); memcpy(name + 2, key.UTF8String, keyLength); name[keyLength + 2] = '\0'; name[2] = of_ascii_toupper(name[2]); selector = sel_registerName(name); } @finally { - free(name); + of_free(name); } methodSignature = [self methodSignatureForSelector: selector]; if (methodSignature == NULL) { @@ -163,24 +160,21 @@ [self setValue: value forUndefinedKey: key]; return; } - if ((name = malloc(keyLength + 5)) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: keyLength + 5]; - + name = of_malloc(1, keyLength + 5); @try { memcpy(name, "set", 3); memcpy(name + 3, key.UTF8String, keyLength); memcpy(name + keyLength + 3, ":", 2); name[3] = of_ascii_toupper(name[3]); selector = sel_registerName(name); } @finally { - free(name); + of_free(name); } methodSignature = [self methodSignatureForSelector: selector]; if (methodSignature == nil || Index: src/OFObject.h ================================================================== --- src/OFObject.h +++ src/OFObject.h @@ -1327,10 +1327,65 @@ #endif #ifdef __cplusplus extern "C" { #endif +/** + * @brief Allocates memory for the specified number of items. + * + * Throws @ref OFOutOfMemoryException if allocating failed and + * @ref OFOutOfRangeException if the requested size exceeds the address space. + * + * @param count The number of items to allocate + * @param size The size of each item to allocate + * @return A pointer to the allocated memory. May return NULL if the specified + * size or count is 0. + */ +extern void *_Nullable of_malloc(size_t count, size_t size) + OF_WARN_UNUSED_RESULT; + +/** + * @brief Allocates memory for the specified number of items and initializes it + * with zeros. + * + * Throws @ref OFOutOfMemoryException if allocating failed and + * @ref OFOutOfRangeException if the requested size exceeds the address space. + * + * @param size The size of each item to allocate + * @param count The number of items to allocate + * @return A pointer to the allocated memory. May return NULL if the specified + * size or count is 0. + */ +extern void *_Nullable of_calloc(size_t count, size_t size) + OF_WARN_UNUSED_RESULT; + +/** + * @brief Resizes memory to the specific number of items of the specified size. + * + * If the pointer is NULL, this is equivalent to allocating memory. + * If the size or number of items is 0, this is equivalent to freeing memory. + * + * Throws @ref OFOutOfMemoryException if allocating failed and + * @ref OFOutOfRangeException if the requested size exceeds the address space. + * + * @param pointer A pointer to the already allocated memory + * @param size The size of each item to resize to + * @param count The number of items to resize to + * @return A pointer to the resized memory chunk + */ +extern void *_Nullable of_realloc(void *_Nullable pointer, size_t count, + size_t size) OF_WARN_UNUSED_RESULT; + +/** + * @brief Frees allocated memory. + * + * Does nothing if the pointer is NULL. + * + * @param pointer A pointer to the allocated memory + */ +extern void of_free(void *_Nullable pointer); + #ifdef OF_APPLE_RUNTIME extern void *_Null_unspecified objc_autoreleasePoolPush(void); extern void objc_autoreleasePoolPop(void *_Null_unspecified pool); # ifndef __OBJC2__ extern id _Nullable objc_constructInstance(Class _Nullable class_, Index: src/OFObject.m ================================================================== --- src/OFObject.m +++ src/OFObject.m @@ -105,10 +105,69 @@ static struct { Class isa; } allocFailedException; uint32_t of_hash_seed; + +void * +of_malloc(size_t count, size_t size) +{ + void *pointer; + + if OF_UNLIKELY (count == 0 || size == 0) + return NULL; + + if OF_UNLIKELY (count > SIZE_MAX / size) + @throw [OFOutOfRangeException exception]; + + if OF_UNLIKELY ((pointer = malloc(count * size)) == NULL) + @throw [OFOutOfMemoryException + exceptionWithRequestedSize: size]; + + return pointer; +} + +void * +of_calloc(size_t count, size_t size) +{ + void *pointer; + + if OF_UNLIKELY (count == 0 || size == 0) + return NULL; + + /* Not all calloc implementations check for overflow. */ + if OF_UNLIKELY (count > SIZE_MAX / size) + @throw [OFOutOfRangeException exception]; + + if OF_UNLIKELY ((pointer = calloc(count, size)) == NULL) + @throw [OFOutOfMemoryException + exceptionWithRequestedSize: size]; + + return pointer; +} + +void * +of_realloc(void *pointer, size_t count, size_t size) +{ + if OF_UNLIKELY (count == 0 || size == 0) + return NULL; + + if OF_UNLIKELY (count > SIZE_MAX / size) + @throw [OFOutOfRangeException exception]; + + if OF_UNLIKELY ((pointer = realloc(pointer, count * size)) == NULL) + @throw [OFOutOfMemoryException + exceptionWithRequestedSize: size]; + + return pointer; +} + +void +of_free(void *pointer) +{ + free(pointer); +} #if !defined(HAVE_ARC4RANDOM) && !defined(HAVE_GETRANDOM) static void initRandom(void) { Index: src/OFSecureData.m ================================================================== --- src/OFSecureData.m +++ src/OFSecureData.m @@ -126,11 +126,11 @@ # endif page = preallocatedPages[numPreallocatedPages]; if (numPreallocatedPages == 0) { - free(preallocatedPages); + of_free(preallocatedPages); preallocatedPages = NULL; # if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS) OF_ENSURE(of_tlskey_set(preallocatedPagesKey, preallocatedPages)); # endif @@ -138,18 +138,12 @@ return page; } } - if ((page = malloc(sizeof(*page))) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: sizeof(*page)]; - - if ((page->map = calloc(1, mapSize)) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: mapSize]; - + page = of_malloc(1, sizeof(*page)); + page->map = of_calloc(1, mapSize); page->page = mapPages(1); of_explicit_memset(page->page, 0, pageSize); # if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS) lastPage = of_tlskey_get(lastPageKey); @@ -186,11 +180,11 @@ for (size_t i = 0; i < mapSize; i++) if (map[i] != 0) return; unmapPages(page->page, 1); - free(page->map); + of_free(page->map); if (page->previous != NULL) page->previous->next = page->next; if (page->next != NULL) page->next->previous = page->previous; @@ -205,11 +199,11 @@ OF_ENSURE(of_tlskey_set(firstPageKey, page->next)); if (of_tlskey_get(lastPageKey) == page) OF_ENSURE(of_tlskey_set(lastPageKey, page->previous)); # endif - free(page); + of_free(page); } static void * allocateMemory(struct page *page, size_t bytes) { @@ -286,15 +280,11 @@ # endif if (preallocatedPages != NULL) @throw [OFInvalidArgumentException exception]; - preallocatedPages = calloc(numPages, sizeof(struct page)); - if (preallocatedPages == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: numPages * sizeof(struct page)]; - + preallocatedPages = of_calloc(numPages, sizeof(struct page)); # if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS) of_tlskey_set(preallocatedPagesKey, preallocatedPages); # endif for (size_t i = 0; i < numPages; i++) Index: src/OFStream.m ================================================================== --- src/OFStream.m +++ src/OFStream.m @@ -634,24 +634,21 @@ char *buffer; if OF_UNLIKELY (count > SIZE_MAX / itemSize) @throw [OFOutOfRangeException exception]; - if OF_UNLIKELY ((buffer = malloc(count * itemSize)) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: count * itemSize]; - + buffer = of_malloc(count, itemSize); @try { [self readIntoBuffer: buffer exactLength: count * itemSize]; ret = [OFData dataWithItemsNoCopy: buffer itemSize: itemSize count: count freeWhenDone: true]; } @catch (id e) { - free(buffer); + of_free(buffer); @throw e; } return ret; } Index: src/OFString+JSONParsing.m ================================================================== --- src/OFString+JSONParsing.m +++ src/OFString+JSONParsing.m @@ -146,18 +146,17 @@ char delimiter = **pointer; if (++(*pointer) + 1 >= stop) return nil; - if ((buffer = malloc(stop - *pointer)) == NULL) - return nil; + buffer = of_malloc(1, stop - *pointer); while (*pointer < stop) { /* Parse escape codes */ if (**pointer == '\\') { if (++(*pointer) >= stop) { - free(buffer); + of_free(buffer); return nil; } switch (**pointer) { case '"': @@ -192,26 +191,26 @@ of_unichar_t c; size_t l; c1 = parseUnicodeEscape(*pointer - 1, stop); if (c1 == 0xFFFF) { - free(buffer); + of_free(buffer); return nil; } /* Low surrogate */ if ((c1 & 0xFC00) == 0xDC00) { - free(buffer); + of_free(buffer); return nil; } /* Normal character */ if ((c1 & 0xFC00) != 0xD800) { l = of_string_utf8_encode(c1, buffer + i); if (l == 0) { - free(buffer); + of_free(buffer); return nil; } i += l; *pointer += 5; @@ -224,20 +223,20 @@ * surrogate and now need to get the other one * in order to produce UTF-8 and not CESU-8. */ c2 = parseUnicodeEscape(*pointer + 5, stop); if (c2 == 0xFFFF) { - free(buffer); + of_free(buffer); return nil; } c = (((c1 & 0x3FF) << 10) | (c2 & 0x3FF)) + 0x10000; l = of_string_utf8_encode(c, buffer + i); if (l == 0) { - free(buffer); + of_free(buffer); return nil; } i += l; *pointer += 11; @@ -255,11 +254,11 @@ case '\n': (*pointer)++; (*line)++; break; default: - free(buffer); + of_free(buffer); return nil; } /* End of string found */ } else if (**pointer == delimiter) { OFString *ret; @@ -266,39 +265,38 @@ @try { ret = [OFString stringWithUTF8String: buffer length: i]; } @finally { - free(buffer); + of_free(buffer); } (*pointer)++; return ret; /* Newlines in strings are disallowed */ } else if (**pointer == '\n' || **pointer == '\r') { (*line)++; - free(buffer); + of_free(buffer); return nil; } else { buffer[i++] = **pointer; (*pointer)++; } } - free(buffer); + of_free(buffer); return nil; } static inline OFString * parseIdentifier(const char **pointer, const char *stop) { char *buffer; size_t i = 0; - if ((buffer = malloc(stop - *pointer)) == NULL) - return nil; + buffer = of_malloc(1, stop - *pointer); while (*pointer < stop) { if ((**pointer >= 'a' && **pointer <= 'z') || (**pointer >= 'A' && **pointer <= 'Z') || (**pointer >= '0' && **pointer <= '9') || @@ -310,31 +308,31 @@ of_char16_t c1, c2; of_unichar_t c; size_t l; if (++(*pointer) >= stop || **pointer != 'u') { - free(buffer); + of_free(buffer); return nil; } c1 = parseUnicodeEscape(*pointer - 1, stop); if (c1 == 0xFFFF) { - free(buffer); + of_free(buffer); return nil; } /* Low surrogate */ if ((c1 & 0xFC00) == 0xDC00) { - free(buffer); + of_free(buffer); return nil; } /* Normal character */ if ((c1 & 0xFC00) != 0xD800) { l = of_string_utf8_encode(c1, buffer + i); if (l == 0) { - free(buffer); + of_free(buffer); return nil; } i += l; *pointer += 5; @@ -347,37 +345,37 @@ * surrogate and now need to get the other one in order * to produce UTF-8 and not CESU-8. */ c2 = parseUnicodeEscape(*pointer + 5, stop); if (c2 == 0xFFFF) { - free(buffer); + of_free(buffer); return nil; } c = (((c1 & 0x3FF) << 10) | (c2 & 0x3FF)) + 0x10000; l = of_string_utf8_encode(c, buffer + i); if (l == 0) { - free(buffer); + of_free(buffer); return nil; } i += l; *pointer += 11; } else { OFString *ret; if (i == 0 || (buffer[0] >= '0' && buffer[0] <= '9')) { - free(buffer); + of_free(buffer); return nil; } @try { ret = [OFString stringWithUTF8String: buffer length: i]; } @finally { - free(buffer); + of_free(buffer); } return ret; } } @@ -384,10 +382,11 @@ /* * It is never possible to end with an identifier, thus we should never * reach stop. */ + of_free(buffer); return nil; } static inline OFMutableArray * parseArray(const char **pointer, const char *stop, size_t *line, Index: src/OFString+URLEncoding.m ================================================================== --- src/OFString+URLEncoding.m +++ src/OFString+URLEncoding.m @@ -82,18 +82,16 @@ - (OFString *)stringByURLDecoding { void *pool = objc_autoreleasePoolPush(); const char *string = self.UTF8String; size_t length = self.UTF8StringLength; - char *retCString, *retCString2; + char *retCString; char byte = 0; int state = 0; size_t i = 0; - if ((retCString = malloc(length + 1)) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: length + 1]; + retCString = of_malloc(1, length + 1); while (length--) { char c = *string++; switch (state) { @@ -112,11 +110,11 @@ else if (c >= 'A' && c <= 'F') byte += (c - 'A' + 10) << shift; else if (c >= 'a' && c <= 'f') byte += (c - 'a' + 10) << shift; else { - free(retCString); + of_free(retCString); @throw [OFInvalidFormatException exception]; } if (++state == 3) { retCString[i++] = byte; @@ -130,18 +128,20 @@ retCString[i] = '\0'; objc_autoreleasePoolPop(pool); if (state != 0) { - free(retCString); + of_free(retCString); @throw [OFInvalidFormatException exception]; } - /* We don't care if it fails, as we only made it smaller. */ - if ((retCString2 = realloc(retCString, i + 1)) == NULL) - retCString2 = retCString; + @try { + retCString = of_realloc(retCString, 1, i + 1); + } @catch (OFOutOfMemoryException *e) { + /* We don't care if it fails, as we only made it smaller. */ + } - return [OFString stringWithUTF8StringNoCopy: retCString2 + return [OFString stringWithUTF8StringNoCopy: retCString length: i freeWhenDone: true]; } @end Index: src/OFString+XMLEscaping.m ================================================================== --- src/OFString+XMLEscaping.m +++ src/OFString+XMLEscaping.m @@ -44,13 +44,11 @@ retLength = length; /* * We can't use allocMemoryWithSize: here as it might be a @"" literal */ - if ((retCString = malloc(retLength)) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: retLength]; + retCString = of_malloc(1, retLength); for (size_t i = 0; i < length; i++) { switch (string[i]) { case '<': append = "<"; @@ -80,20 +78,17 @@ append = NULL; appendLen = 0; } if (append != NULL) { - char *newRetCString; - - if ((newRetCString = realloc(retCString, - retLength + appendLen)) == NULL) { - free(retCString); - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: retLength + - appendLen]; - } - retCString = newRetCString; + @try { + retCString = of_realloc(retCString, 1, + retLength + appendLen); + } @catch (id e) { + of_free(retCString); + @throw e; + } retLength += appendLen - 1; memcpy(retCString + j, append, appendLen); j += appendLen; } else @@ -105,10 +100,10 @@ @try { ret = [OFString stringWithUTF8String: retCString length: retLength]; } @finally { - free(retCString); + of_free(retCString); } return ret; } @end Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -840,11 +840,11 @@ @try { ret = [self initWithUTF8String: UTF8String]; } @finally { if (freeWhenDone) - free(UTF8String); + of_free(UTF8String); } return ret; } @@ -857,11 +857,11 @@ @try { ret = [self initWithUTF8String: UTF8String length: UTF8StringLength]; } @finally { if (freeWhenDone) - free(UTF8String); + of_free(UTF8String); } return ret; } @@ -1027,22 +1027,19 @@ * to use -[initWithUTF8StringNoCopy:length:freeWhenDone:]. */ if (SIZE_MAX - (size_t)fileSize < 1) @throw [OFOutOfRangeException exception]; - if ((tmp = malloc((size_t)fileSize + 1)) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: (size_t)fileSize]; - + tmp = of_malloc(1, (size_t)fileSize + 1); @try { file = [[OFFile alloc] initWithPath: path mode: @"r"]; [file readIntoBuffer: tmp exactLength: (size_t)fileSize]; } @catch (id e) { - free(tmp); + of_free(tmp); @throw e; } @finally { [file release]; } @@ -1060,11 +1057,11 @@ @try { self = [self initWithCString: tmp encoding: encoding length: (size_t)fileSize]; } @finally { - free(tmp); + of_free(tmp); } } return self; } @@ -1858,14 +1855,11 @@ pool = objc_autoreleasePoolPush(); searchCharacters = string.characters; - if ((characters = malloc(range.length * sizeof(of_unichar_t))) == NULL) - @throw [OFOutOfMemoryException exceptionWithRequestedSize: - range.length * sizeof(of_unichar_t)]; - + characters = of_malloc(range.length, sizeof(of_unichar_t)); @try { [self getCharacters: characters inRange: range]; if (options & OF_STRING_SEARCH_BACKWARDS) { @@ -1891,11 +1885,11 @@ searchLength); } } } } @finally { - free(characters); + of_free(characters); } objc_autoreleasePoolPop(pool); return of_range(OF_NOT_FOUND, 0); @@ -1929,14 +1923,11 @@ return OF_NOT_FOUND; if (range.length > SIZE_MAX / sizeof(of_unichar_t)) @throw [OFOutOfRangeException exception]; - if ((characters = malloc(range.length * sizeof(of_unichar_t))) == NULL) - @throw [OFOutOfMemoryException exceptionWithRequestedSize: - range.length * sizeof(of_unichar_t)]; - + characters = of_malloc(range.length, sizeof(of_unichar_t)); @try { [self getCharacters: characters inRange: range]; if (options & OF_STRING_SEARCH_BACKWARDS) { @@ -1956,11 +1947,11 @@ @selector(characterIsMember:), characters[i])) return range.location + i; } } @finally { - free(characters); + of_free(characters); } return OF_NOT_FOUND; } Index: src/OFValue.m ================================================================== --- src/OFValue.m +++ src/OFValue.m @@ -181,18 +181,16 @@ if (strcmp([object objCType], objCType) != 0) return false; size = of_sizeof_type_encoding(objCType); - if ((value = malloc(size)) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: size]; - - if ((otherValue = malloc(size)) == NULL) { - free(value); - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: size]; + value = of_malloc(1, size); + @try { + otherValue = of_malloc(1, size); + } @catch (id e) { + of_free(value); + @throw e; } @try { [self getValue: value size: size]; @@ -199,12 +197,12 @@ [object getValue: otherValue size: size]; ret = (memcmp(value, otherValue, size) == 0); } @finally { - free(value); - free(otherValue); + of_free(value); + of_free(otherValue); } return ret; } @@ -212,14 +210,11 @@ { size_t size = of_sizeof_type_encoding(self.objCType); unsigned char *value; uint32_t hash; - if ((value = malloc(size)) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: size]; - + value = of_malloc(1, size); @try { [self getValue: value size: size]; OF_HASH_INIT(hash); @@ -227,11 +222,11 @@ for (size_t i = 0; i < size; i++) OF_HASH_ADD(hash, value[i]); OF_HASH_FINALIZE(hash); } @finally { - free(value); + of_free(value); } return hash; } @@ -316,14 +311,11 @@ OFMutableString *ret = [OFMutableString stringWithString: @""]; [ret makeImmutable]; return ret; } @end Index: src/huffman_tree.m ================================================================== --- src/huffman_tree.m +++ src/huffman_tree.m @@ -28,14 +28,11 @@ static struct of_huffman_tree * newTree(void) { struct of_huffman_tree *tree; - if ((tree = malloc(sizeof(*tree))) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: sizeof(*tree)]; - + tree = of_malloc(1, sizeof(*tree)); tree->leaves[0] = tree->leaves[1] = NULL; tree->value = 0xFFFF; return tree; } @@ -70,24 +67,14 @@ @try { for (uint16_t i = 0; i < count; i++) { uint_fast8_t length = lengths[i]; if OF_UNLIKELY (length > maxBit) { - size_t size = (length + 1) * sizeof(uint16_t); - uint16_t *new; - - if ((new = realloc(lengthCount, size)) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: size]; - - lengthCount = new; - - if ((new = realloc(nextCode, size)) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: size]; - - nextCode = new; + lengthCount = of_realloc(lengthCount, + length + 1, sizeof(uint16_t)); + nextCode = of_realloc(nextCode, + length + 1, sizeof(uint16_t)); for (uint_fast8_t j = maxBit + 1; j <= length; j++) { lengthCount[j] = 0; nextCode[j] = 0; @@ -115,12 +102,12 @@ if (length > 0) insertTree(tree, nextCode[length]++, length, i); } } @finally { - free(lengthCount); - free(nextCode); + of_free(lengthCount); + of_free(nextCode); } return tree; } @@ -139,7 +126,7 @@ { for (uint_fast8_t i = 0; i < 2; i++) if OF_LIKELY (tree->leaves[i] != NULL) of_huffman_tree_release(tree->leaves[i]); - free(tree); + of_free(tree); }