@@ -61,16 +61,16 @@ return [[[self alloc] initWithItems: items count: count] autorelease]; } + (instancetype)dataWithItems: (const void *)items - itemSize: (size_t)itemSize count: (size_t)count + itemSize: (size_t)itemSize { return [[[self alloc] initWithItems: items - itemSize: itemSize - count: count] autorelease]; + count: count + itemSize: itemSize] autorelease]; } + (instancetype)dataWithItemsNoCopy: (void *)items count: (size_t)count freeWhenDone: (bool)freeWhenDone @@ -79,17 +79,17 @@ count: count freeWhenDone: freeWhenDone] autorelease]; } + (instancetype)dataWithItemsNoCopy: (void *)items - itemSize: (size_t)itemSize count: (size_t)count + itemSize: (size_t)itemSize freeWhenDone: (bool)freeWhenDone { return [[[self alloc] initWithItemsNoCopy: items - itemSize: itemSize count: count + itemSize: itemSize freeWhenDone: freeWhenDone] autorelease]; } #ifdef OF_HAVE_FILES + (instancetype)dataWithContentsOfFile: (OFString *)path @@ -116,28 +116,28 @@ - (instancetype)initWithItems: (const void *)items count: (size_t)count { return [self initWithItems: items - itemSize: 1 - count: count]; + count: count + itemSize: 1]; } - (instancetype)initWithItems: (const void *)items - itemSize: (size_t)itemSize count: (size_t)count + itemSize: (size_t)itemSize { self = [super init]; @try { if (itemSize == 0) @throw [OFInvalidArgumentException exception]; - _items = [self allocMemoryWithSize: itemSize - count: count]; + _items = of_malloc(count, itemSize); + _count = count; _itemSize = itemSize; - _count = count; + _freeWhenDone = true; memcpy(_items, items, count * itemSize); } @catch (id e) { [self release]; @throw e; @@ -149,29 +149,29 @@ - (instancetype)initWithItemsNoCopy: (void *)items count: (size_t)count freeWhenDone: (bool)freeWhenDone { return [self initWithItemsNoCopy: items - itemSize: 1 count: count + itemSize: 1 freeWhenDone: freeWhenDone]; } - (instancetype)initWithItemsNoCopy: (void *)items - itemSize: (size_t)itemSize count: (size_t)count + itemSize: (size_t)itemSize freeWhenDone: (bool)freeWhenDone { self = [super init]; @try { if (itemSize == 0) @throw [OFInvalidArgumentException exception]; _items = (unsigned char *)items; - _itemSize = itemSize; _count = count; + _itemSize = itemSize; _freeWhenDone = freeWhenDone; } @catch (id e) { [self release]; @throw e; } @@ -194,14 +194,11 @@ # 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((size_t)size, 1); file = [[OFFile alloc] initWithPath: path mode: @"r"]; @try { [file readIntoBuffer: buffer exactLength: (size_t)size]; @@ -244,27 +241,33 @@ exceptionWithURL: URL]; stream = [URLHandler openItemAtURL: URL mode: @"r"]; - _itemSize = 1; _count = 0; + _itemSize = 1; + _freeWhenDone = true; pageSize = [OFSystemInfo pageSize]; - buffer = [self allocMemoryWithSize: pageSize]; - - while (!stream.atEndOfStream) { - size_t length = [stream readIntoBuffer: buffer - length: pageSize]; - - if (SIZE_MAX - _count < length) - @throw [OFOutOfRangeException exception]; - - _items = [self resizeMemory: _items - size: _count + length]; - memcpy(_items + _count, buffer, length); - _count += length; + buffer = of_malloc(1, pageSize); + + @try { + while (!stream.atEndOfStream) { + size_t length = [stream + readIntoBuffer: buffer + length: pageSize]; + + if (SIZE_MAX - _count < length) + @throw [OFOutOfRangeException + exception]; + + _items = of_realloc(_items, _count + length, 1); + memcpy(_items + _count, buffer, length); + _count += length; + } + } @finally { + free(buffer); } objc_autoreleasePoolPop(pool); } @catch (id e) { [self release]; @@ -286,13 +289,14 @@ if (count % 2 != 0) @throw [OFInvalidFormatException exception]; count /= 2; - _items = [self allocMemoryWithSize: count]; + _items = of_malloc(count, 1); + _count = count; _itemSize = 1; - _count = count; + _freeWhenDone = true; cString = [string cStringWithEncoding: OF_STRING_ENCODING_ASCII]; for (size_t i = 0; i < count; i++) { @@ -429,12 +433,12 @@ } - (id)mutableCopy { return [[OFMutableData alloc] initWithItems: _items - itemSize: _itemSize - count: _count]; + count: _count + itemSize: _itemSize]; } - (bool)isEqual: (id)object { OFData *data; @@ -486,11 +490,11 @@ return OF_ORDERED_DESCENDING; else return OF_ORDERED_ASCENDING; } -- (uint32_t)hash +- (unsigned long)hash { uint32_t hash; OF_HASH_INIT(hash); @@ -509,12 +513,12 @@ if (range.length > SIZE_MAX - range.location || range.location + range.length > _count) @throw [OFOutOfRangeException exception]; ret = [OFData dataWithItemsNoCopy: _items + (range.location * _itemSize) - itemSize: _itemSize count: range.length + itemSize: _itemSize freeWhenDone: false]; ret->_parentData = [(_parentData != nil ? _parentData : self) copy]; return ret; }