@@ -40,12 +40,11 @@ + (instancetype)dataWithCapacity: (size_t)capacity { return [[[self alloc] initWithCapacity: capacity] autorelease]; } -+ (instancetype)dataWithItemSize: (size_t)itemSize - capacity: (size_t)capacity ++ (instancetype)dataWithItemSize: (size_t)itemSize capacity: (size_t)capacity { return [[[self alloc] initWithItemSize: itemSize capacity: capacity] autorelease]; } @@ -81,20 +80,19 @@ { return [self initWithItemSize: 1 capacity: capacity]; } -- (instancetype)initWithItemSize: (size_t)itemSize - capacity: (size_t)capacity +- (instancetype)initWithItemSize: (size_t)itemSize capacity: (size_t)capacity { self = [super init]; @try { if (itemSize == 0) @throw [OFInvalidArgumentException exception]; - _items = of_alloc(capacity, itemSize); + _items = OFAllocMemory(capacity, itemSize); _itemSize = itemSize; _capacity = capacity; _freeWhenDone = true; } @catch (id e) { [self release]; @@ -106,13 +104,11 @@ - (instancetype)initWithItems: (const void *)items count: (size_t)count itemSize: (size_t)itemSize { - self = [super initWithItems: items - count: count - itemSize: itemSize]; + self = [super initWithItems: items count: count itemSize: itemSize]; _capacity = _count; return self; } @@ -120,16 +116,14 @@ - (instancetype)initWithItemsNoCopy: (void *)items count: (size_t)count itemSize: (size_t)itemSize freeWhenDone: (bool)freeWhenDone { - self = [self initWithItems: items - count: count - itemSize: itemSize]; + self = [self initWithItems: items count: count itemSize: itemSize]; if (freeWhenDone) - free(items); + OFFreeMemory(items); return self; } - (instancetype)initWithStringRepresentation: (OFString *)string @@ -168,11 +162,11 @@ return NULL; return _items + (_count - 1) * _itemSize; } -- (OFData *)subdataWithRange: (of_range_t)range +- (OFData *)subdataWithRange: (OFRange)range { if (range.length > SIZE_MAX - range.location || range.location + range.length > _count) @throw [OFOutOfRangeException exception]; @@ -185,35 +179,31 @@ { if (SIZE_MAX - _count < 1) @throw [OFOutOfRangeException exception]; if (_count + 1 > _capacity) { - _items = of_realloc(_items, _count + 1, _itemSize); + _items = OFResizeMemory(_items, _count + 1, _itemSize); _capacity = _count + 1; } memcpy(_items + _count * _itemSize, item, _itemSize); _count++; } -- (void)insertItem: (const void *)item - atIndex: (size_t)idx +- (void)insertItem: (const void *)item atIndex: (size_t)idx { - [self insertItems: item - atIndex: idx - count: 1]; + [self insertItems: item atIndex: idx count: 1]; } -- (void)addItems: (const void *)items - count: (size_t)count +- (void)addItems: (const void *)items count: (size_t)count { if (count > SIZE_MAX - _count) @throw [OFOutOfRangeException exception]; if (_count + count > _capacity) { - _items = of_realloc(_items, _count + count, _itemSize); + _items = OFResizeMemory(_items, _count + count, _itemSize); _capacity = _count + count; } memcpy(_items + _count * _itemSize, items, count * _itemSize); _count += count; @@ -225,11 +215,11 @@ { if (count > SIZE_MAX - _count || idx > _count) @throw [OFOutOfRangeException exception]; if (_count + count > _capacity) { - _items = of_realloc(_items, _count + count, _itemSize); + _items = OFResizeMemory(_items, _count + count, _itemSize); _capacity = _count + count; } memmove(_items + (idx + count) * _itemSize, _items + idx * _itemSize, (_count - idx) * _itemSize); @@ -242,24 +232,24 @@ { if (count > SIZE_MAX - _count) @throw [OFOutOfRangeException exception]; if (_count + count > _capacity) { - _items = of_realloc(_items, _count + count, _itemSize); + _items = OFResizeMemory(_items, _count + count, _itemSize); _capacity = _count + count; } memset(_items + _count * _itemSize, '\0', count * _itemSize); _count += count; } - (void)removeItemAtIndex: (size_t)idx { - [self removeItemsInRange: of_range(idx, 1)]; + [self removeItemsInRange: OFRangeMake(idx, 1)]; } -- (void)removeItemsInRange: (of_range_t)range +- (void)removeItemsInRange: (OFRange)range { if (range.length > SIZE_MAX - range.location || range.location + range.length > _count) @throw [OFOutOfRangeException exception]; @@ -267,11 +257,11 @@ _items + (range.location + range.length) * _itemSize, (_count - range.location - range.length) * _itemSize); _count -= range.length; @try { - _items = of_realloc(_items, _count, _itemSize); + _items = OFResizeMemory(_items, _count, _itemSize); _capacity = _count; } @catch (OFOutOfMemoryException *e) { /* We don't really care, as we only made it smaller */ } } @@ -281,20 +271,20 @@ if (_count == 0) return; _count--; @try { - _items = of_realloc(_items, _count, _itemSize); + _items = OFResizeMemory(_items, _count, _itemSize); _capacity = _count; } @catch (OFOutOfMemoryException *e) { /* We don't care, as we only made it smaller */ } } - (void)removeAllItems { - free(_items); + OFFreeMemory(_items); _items = NULL; _count = 0; _capacity = 0; } @@ -307,15 +297,15 @@ - (void)makeImmutable { if (_capacity != _count) { @try { - _items = of_realloc(_items, _count, _itemSize); + _items = OFResizeMemory(_items, _count, _itemSize); _capacity = _count; } @catch (OFOutOfMemoryException *e) { /* We don't care, as we only made it smaller */ } } object_setClass(self, [OFData class]); } @end