Index: ObjFW.xcodeproj/project.pbxproj ================================================================== --- ObjFW.xcodeproj/project.pbxproj +++ ObjFW.xcodeproj/project.pbxproj @@ -417,10 +417,12 @@ 4BF33B10133807A20059CEF7 /* OFXMLParserTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B6EF67E1235358D0076B512 /* OFXMLParserTests.m */; }; 4BF33B11133807A20059CEF7 /* PropertiesTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B6EF67F1235358D0076B512 /* PropertiesTests.m */; }; 4BF33B12133807A20059CEF7 /* TestsAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B6EF6811235358D0076B512 /* TestsAppDelegate.m */; }; 4BF33B4713380CE20059CEF7 /* testfile.txt in CopyFiles */ = {isa = PBXBuildFile; fileRef = 4BF33B4313380CD40059CEF7 /* testfile.txt */; }; 4BF33B4813380D2D0059CEF7 /* testfile.bin in CopyFiles */ = {isa = PBXBuildFile; fileRef = 4BF33B4213380CD40059CEF7 /* testfile.bin */; }; + 4BF48CE118A95F83000E8D04 /* OFBigDataArray.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BF48CDF18A95F83000E8D04 /* OFBigDataArray.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 4BF48CE218A95F83000E8D04 /* OFBigDataArray.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BF48CE018A95F83000E8D04 /* OFBigDataArray.m */; }; 4BFF3714177E17C100192782 /* OFRemoveItemFailedException.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BFF3710177E17C100192782 /* OFRemoveItemFailedException.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4BFF3715177E17C100192782 /* OFRemoveItemFailedException.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BFF3711177E17C100192782 /* OFRemoveItemFailedException.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -886,10 +888,12 @@ 4BF1BCCE11C9663F0025511F /* OFXMLAttribute.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFXMLAttribute.h; path = src/OFXMLAttribute.h; sourceTree = ""; }; 4BF1BCCF11C9663F0025511F /* OFXMLAttribute.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFXMLAttribute.m; path = src/OFXMLAttribute.m; sourceTree = ""; }; 4BF33AF0133807310059CEF7 /* Tests */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Tests; sourceTree = BUILT_PRODUCTS_DIR; }; 4BF33B4213380CD40059CEF7 /* testfile.bin */ = {isa = PBXFileReference; lastKnownFileType = archive.macbinary; name = testfile.bin; path = tests/testfile.bin; sourceTree = ""; }; 4BF33B4313380CD40059CEF7 /* testfile.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = testfile.txt; path = tests/testfile.txt; sourceTree = ""; }; + 4BF48CDF18A95F83000E8D04 /* OFBigDataArray.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFBigDataArray.h; path = src/OFBigDataArray.h; sourceTree = ""; }; + 4BF48CE018A95F83000E8D04 /* OFBigDataArray.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFBigDataArray.m; path = src/OFBigDataArray.m; sourceTree = ""; }; 4BFBDD1610A0724800051AFB /* unicode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = unicode.m; path = src/unicode.m; sourceTree = ""; }; 4BFF3710177E17C100192782 /* OFRemoveItemFailedException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFRemoveItemFailedException.h; path = src/exceptions/OFRemoveItemFailedException.h; sourceTree = ""; }; 4BFF3711177E17C100192782 /* OFRemoveItemFailedException.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFRemoveItemFailedException.m; path = src/exceptions/OFRemoveItemFailedException.m; sourceTree = ""; }; /* End PBXFileReference section */ @@ -1167,10 +1171,12 @@ 4B9BB7BB141CDE2D000AD1CC /* OFArray_subarray.h */, 4B9BB7BC141CDE2D000AD1CC /* OFArray_subarray.m */, 4B67995C1099E7C50041064A /* OFAutoreleasePool.h */, 4B67995D1099E7C50041064A /* OFAutoreleasePool.m */, 4B1473CA17E6391900B46BB8 /* OFAutoreleasePool+Private.h */, + 4BF48CDF18A95F83000E8D04 /* OFBigDataArray.h */, + 4BF48CE018A95F83000E8D04 /* OFBigDataArray.m */, 4BD86D801237A6C600ED9912 /* OFBlock.h */, 4BD86D811237A6C600ED9912 /* OFBlock.m */, 4BAF5F46123460C900F4E111 /* OFCollection.h */, 4B6743F7163C395900EB1E59 /* OFCondition.h */, 4B6743F8163C395900EB1E59 /* OFCondition.m */, @@ -1489,10 +1495,11 @@ files = ( 4B3D23BC1337FC8300DD29B8 /* OFApplication.h in Headers */, 4B3D23BD1337FC8300DD29B8 /* OFArray.h in Headers */, 4B9BB7BF141CDE2D000AD1CC /* OFArray_subarray.h in Headers */, 4B3D23BE1337FC8300DD29B8 /* OFAutoreleasePool.h in Headers */, + 4BF48CE118A95F83000E8D04 /* OFBigDataArray.h in Headers */, 4B3D23BF1337FC8300DD29B8 /* OFBlock.h in Headers */, 4B3D23C01337FC8300DD29B8 /* OFCollection.h in Headers */, 4B674400163C395900EB1E59 /* OFCondition.h in Headers */, 4B3D23C11337FC8300DD29B8 /* OFConstantString.h in Headers */, 4B45355313DCFE1E0037AB4D /* OFCountedSet.h in Headers */, @@ -1860,10 +1867,11 @@ 4B3D238C1337FC0D00DD29B8 /* OFArray.m in Sources */, 4B2B3E7E140D430500EC2F7C /* OFArray_adjacent.m in Sources */, 4B9BB7BE141CDE2D000AD1CC /* OFArray_adjacentSubarray.m in Sources */, 4B9BB7C0141CDE2D000AD1CC /* OFArray_subarray.m in Sources */, 4B3D238D1337FC0D00DD29B8 /* OFAutoreleasePool.m in Sources */, + 4BF48CE218A95F83000E8D04 /* OFBigDataArray.m in Sources */, 4B3D238E1337FC0D00DD29B8 /* OFBlock.m in Sources */, 4B674401163C395900EB1E59 /* OFCondition.m in Sources */, 4B3D238F1337FC0D00DD29B8 /* OFConstantString.m in Sources */, 4B45355413DCFE1E0037AB4D /* OFCountedSet.m in Sources */, 4BA85BCB140ECCE800E91D51 /* OFCountedSet_hashtable.m in Sources */, Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -11,10 +11,11 @@ SRCS = OFApplication.m \ OFArray.m \ OFArray_subarray.m \ OFAutoreleasePool.m \ + OFBigDataArray.m \ OFBlock.m \ OFConstantString.m \ OFCountedSet.m \ OFDataArray.m \ OFDataArray+Hashing.m \ ADDED src/OFBigDataArray.h Index: src/OFBigDataArray.h ================================================================== --- src/OFBigDataArray.h +++ src/OFBigDataArray.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "OFDataArray.h" + +/*! + * @brief A class for storing arbitrary big data in an array. + * + * The OFBigDataArray class is a class for storing arbitrary data in an array + * and is designed to store large hunks of data. Therefore, it allocates + * memory in pages rather than a chunk of memory for each item. + */ +@interface OFBigDataArray: OFDataArray +{ + size_t _size; +} +@end ADDED src/OFBigDataArray.m Index: src/OFBigDataArray.m ================================================================== --- src/OFBigDataArray.m +++ src/OFBigDataArray.m @@ -0,0 +1,218 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#include + +#import "OFBigDataArray.h" +#import "OFSystemInfo.h" + +#import "OFInvalidArgumentException.h" +#import "OFOutOfMemoryException.h" +#import "OFOutOfRangeException.h" + +@implementation OFBigDataArray +- init +{ + return [self initWithItemSize: 1 + capacity: 0]; +} + +- initWithItemSize: (size_t)itemSize +{ + return [self initWithItemSize: itemSize + capacity: 0]; +} + +- initWithItemSize: (size_t)itemSize + capacity: (size_t)capacity +{ + self = [super init]; + + @try { + size_t size, lastPageByte; + + if (itemSize == 0) + @throw [OFInvalidArgumentException exception]; + + if (capacity > SIZE_MAX / itemSize) + @throw [OFOutOfRangeException exception]; + + lastPageByte = [OFSystemInfo pageSize] - 1; + size = (capacity * itemSize + lastPageByte) & ~lastPageByte; + + if (size == 0) + size = lastPageByte + 1; + + _items = [self allocMemoryWithSize: size]; + + _itemSize = itemSize; + _capacity = size / itemSize; + _size = size; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (void)addItem: (const void*)item +{ + if (SIZE_MAX - _count < 1 || _count + 1 > SIZE_MAX / _itemSize) + @throw [OFOutOfRangeException exception]; + + if (_count + 1 > _capacity) { + size_t size, lastPageByte; + + lastPageByte = [OFSystemInfo pageSize] - 1; + size = ((_count + 1) * _itemSize + lastPageByte) & + ~lastPageByte; + + _items = [self resizeMemory: _items + size: size]; + + _capacity = size / _itemSize; + _size = size; + } + + memcpy(_items + _count * _itemSize, item, _itemSize); + + _count++; +} + +- (void)addItems: (const void*)items + count: (size_t)count +{ + if (count > SIZE_MAX - _count || _count + count > SIZE_MAX / _itemSize) + @throw [OFOutOfRangeException exception]; + + if (_count + count > _capacity) { + size_t size, lastPageByte; + + lastPageByte = [OFSystemInfo pageSize] - 1; + size = ((_count + count) * _itemSize + lastPageByte) & + ~lastPageByte; + + _items = [self resizeMemory: _items + size: size]; + + _capacity = size / _itemSize; + _size = size; + } + + memcpy(_items + _count * _itemSize, items, count * _itemSize); + + _count += count; +} + +- (void)insertItems: (const void*)items + atIndex: (size_t)index + count: (size_t)count +{ + if (count > SIZE_MAX - _count || index > _count || + _count + count > SIZE_MAX / _itemSize) + @throw [OFOutOfRangeException exception]; + + if (_count + count > _capacity) { + size_t size, lastPageByte; + + lastPageByte = [OFSystemInfo pageSize] - 1; + size = ((_count + count) * _itemSize + lastPageByte) & + ~lastPageByte; + + _items = [self resizeMemory: _items + size: size]; + + _capacity = size / _itemSize; + _size = size; + } + + memmove(_items + (index + count) * _itemSize, + _items + index * _itemSize, (_count - index) * _itemSize); + memcpy(_items + index * _itemSize, items, count * _itemSize); + + _count += count; +} + +- (void)removeItemsInRange: (of_range_t)range +{ + size_t lastPageByte, size; + + if (range.length > SIZE_MAX - range.location || + range.location + range.length > _count) + @throw [OFOutOfRangeException exception]; + + memmove(_items + range.location * _itemSize, + _items + (range.location + range.length) * _itemSize, + (_count - range.location - range.length) * _itemSize); + + _count -= range.length; + lastPageByte = [OFSystemInfo pageSize] - 1; + size = (_count * _itemSize + lastPageByte) & ~lastPageByte; + + if (_size != size && size > lastPageByte) { + @try { + _items = [self resizeMemory: _items + size: size]; + _capacity = size / _itemSize; + _size = size; + } @catch (OFOutOfMemoryException *e) { + /* We don't care, as we only made it smaller */ + } + } +} + +- (void)removeLastItem +{ + size_t lastPageByte, size; + + if (_count == 0) + return; + + _count--; + lastPageByte = [OFSystemInfo pageSize] - 1; + size = (_count * _itemSize + lastPageByte) & ~lastPageByte; + + if (_size != size && size > lastPageByte) { + @try { + _items = [self resizeMemory: _items + size: size]; + _capacity = size / _itemSize; + _size = size; + } @catch (OFOutOfMemoryException *e) { + /* We don't care, as we only made it smaller */ + } + } +} + +- (void)removeAllItems +{ + size_t pageSize = [OFSystemInfo pageSize]; + + @try { + _items = [self resizeMemory: _items + size: pageSize]; + _capacity = pageSize / _itemSize; + _size = pageSize; + } @catch (OFOutOfMemoryException *e) { + /* We don't care, as we only made it smaller */ + } + + _count = 0; +} +@end Index: src/OFDataArray.h ================================================================== --- src/OFDataArray.h +++ src/OFDataArray.h @@ -316,22 +316,9 @@ * * @param path The path of the file to write to */ - (void)writeToFile: (OFString*)path; #endif -@end - -/*! - * @brief A class for storing arbitrary big data in an array. - * - * The OFBigDataArray class is a class for storing arbitrary data in an array - * and is designed to store large hunks of data. Therefore, it allocates - * memory in pages rather than a chunk of memory for each item. - */ -@interface OFBigDataArray: OFDataArray -{ - size_t _size; -} @end #import "OFDataArray+Hashing.h" #import "OFDataArray+MessagePackValue.h" Index: src/OFDataArray.m ================================================================== --- src/OFDataArray.m +++ src/OFDataArray.m @@ -685,198 +685,6 @@ [data addItems: _items count: _count]; return data; } -@end - -@implementation OFBigDataArray -- init -{ - return [self initWithItemSize: 1 - capacity: 0]; -} - -- initWithItemSize: (size_t)itemSize -{ - return [self initWithItemSize: itemSize - capacity: 0]; -} - -- initWithItemSize: (size_t)itemSize - capacity: (size_t)capacity -{ - self = [super init]; - - @try { - size_t size, lastPageByte; - - if (itemSize == 0) - @throw [OFInvalidArgumentException exception]; - - if (capacity > SIZE_MAX / itemSize) - @throw [OFOutOfRangeException exception]; - - lastPageByte = [OFSystemInfo pageSize] - 1; - size = (capacity * itemSize + lastPageByte) & ~lastPageByte; - - if (size == 0) - size = lastPageByte + 1; - - _items = [self allocMemoryWithSize: size]; - - _itemSize = itemSize; - _capacity = size / itemSize; - _size = size; - } @catch (id e) { - [self release]; - @throw e; - } - - return self; -} - -- (void)addItem: (const void*)item -{ - if (SIZE_MAX - _count < 1 || _count + 1 > SIZE_MAX / _itemSize) - @throw [OFOutOfRangeException exception]; - - if (_count + 1 > _capacity) { - size_t size, lastPageByte; - - lastPageByte = [OFSystemInfo pageSize] - 1; - size = ((_count + 1) * _itemSize + lastPageByte) & - ~lastPageByte; - - _items = [self resizeMemory: _items - size: size]; - - _capacity = size / _itemSize; - _size = size; - } - - memcpy(_items + _count * _itemSize, item, _itemSize); - - _count++; -} - -- (void)addItems: (const void*)items - count: (size_t)count -{ - if (count > SIZE_MAX - _count || _count + count > SIZE_MAX / _itemSize) - @throw [OFOutOfRangeException exception]; - - if (_count + count > _capacity) { - size_t size, lastPageByte; - - lastPageByte = [OFSystemInfo pageSize] - 1; - size = ((_count + count) * _itemSize + lastPageByte) & - ~lastPageByte; - - _items = [self resizeMemory: _items - size: size]; - - _capacity = size / _itemSize; - _size = size; - } - - memcpy(_items + _count * _itemSize, items, count * _itemSize); - - _count += count; -} - -- (void)insertItems: (const void*)items - atIndex: (size_t)index - count: (size_t)count -{ - if (count > SIZE_MAX - _count || index > _count || - _count + count > SIZE_MAX / _itemSize) - @throw [OFOutOfRangeException exception]; - - if (_count + count > _capacity) { - size_t size, lastPageByte; - - lastPageByte = [OFSystemInfo pageSize] - 1; - size = ((_count + count) * _itemSize + lastPageByte) & - ~lastPageByte; - - _items = [self resizeMemory: _items - size: size]; - - _capacity = size / _itemSize; - _size = size; - } - - memmove(_items + (index + count) * _itemSize, - _items + index * _itemSize, (_count - index) * _itemSize); - memcpy(_items + index * _itemSize, items, count * _itemSize); - - _count += count; -} - -- (void)removeItemsInRange: (of_range_t)range -{ - size_t lastPageByte, size; - - if (range.length > SIZE_MAX - range.location || - range.location + range.length > _count) - @throw [OFOutOfRangeException exception]; - - memmove(_items + range.location * _itemSize, - _items + (range.location + range.length) * _itemSize, - (_count - range.location - range.length) * _itemSize); - - _count -= range.length; - lastPageByte = [OFSystemInfo pageSize] - 1; - size = (_count * _itemSize + lastPageByte) & ~lastPageByte; - - if (_size != size && size > lastPageByte) { - @try { - _items = [self resizeMemory: _items - size: size]; - _capacity = size / _itemSize; - _size = size; - } @catch (OFOutOfMemoryException *e) { - /* We don't care, as we only made it smaller */ - } - } -} - -- (void)removeLastItem -{ - size_t lastPageByte, size; - - if (_count == 0) - return; - - _count--; - lastPageByte = [OFSystemInfo pageSize] - 1; - size = (_count * _itemSize + lastPageByte) & ~lastPageByte; - - if (_size != size && size > lastPageByte) { - @try { - _items = [self resizeMemory: _items - size: size]; - _capacity = size / _itemSize; - _size = size; - } @catch (OFOutOfMemoryException *e) { - /* We don't care, as we only made it smaller */ - } - } -} - -- (void)removeAllItems -{ - size_t pageSize = [OFSystemInfo pageSize]; - - @try { - _items = [self resizeMemory: _items - size: pageSize]; - _capacity = pageSize / _itemSize; - _size = pageSize; - } @catch (OFOutOfMemoryException *e) { - /* We don't care, as we only made it smaller */ - } - - _count = 0; -} @end Index: src/OFXMLParser.m ================================================================== --- src/OFXMLParser.m +++ src/OFXMLParser.m @@ -22,11 +22,11 @@ #import "OFXMLParser.h" #import "OFString.h" #import "OFArray.h" #import "OFDictionary.h" -#import "OFDataArray.h" +#import "OFBigDataArray.h" #import "OFXMLAttribute.h" #import "OFStream.h" #ifdef OF_HAVE_FILES # import "OFFile.h" #endif Index: tests/OFDataArrayTests.m ================================================================== --- tests/OFDataArrayTests.m +++ tests/OFDataArrayTests.m @@ -17,10 +17,11 @@ #include "config.h" #include #import "OFDataArray.h" +#import "OFBigDataArray.h" #import "OFString.h" #import "OFAutoreleasePool.h" #import "OFOutOfRangeException.h"