Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -1,10 +1,11 @@ LIB = ${LIB_PREFIX}objfw${LIB_SUFFIX} LIB_MAJOR = 1 LIB_MINOR = 0 -SRCS = OFConstCString.m \ +SRCS = OFArray.m \ + OFConstCString.m \ OFCString.m \ OFConstWideCString.m \ OFExceptions.m \ OFHashes.m \ OFFile.m \ ADDED src/OFArray.h Index: src/OFArray.h ================================================================== --- src/OFArray.h +++ src/OFArray.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2008 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of libobjfw. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE included in + * the packaging of this file. + */ + +#import + +#import "OFObject.h" + +@interface OFArray: OFObject +{ + void *data; + size_t itemsize; + size_t size; +} + +- initWithItemSize: (size_t)is; +- (size_t)size; +- (void*)item: (size_t)item; +- (void*)last; +- add: (void*)item; +- addNItems: (size_t)nitems + fromCArray: (void*)carray; +- removeLastNItems: (size_t)nitems; +@end ADDED src/OFArray.m Index: src/OFArray.m ================================================================== --- src/OFArray.m +++ src/OFArray.m @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2008 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of libobjfw. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE included in + * the packaging of this file. + */ + +#import "OFArray.h" + +#import "OFExceptions.h" +#import "OFMacros.h" + +@implementation OFArray +- initWithItemSize: (size_t)is +{ + if ((self = [super init])) { + data = NULL; + itemsize = is; + size = 0; + } + + return self; +} + +- (size_t)size +{ + return size; +} + +- (size_t)itemsize +{ + return itemsize; +} + +- (void*)item: (size_t)item +{ + /* FIXME */ + OF_NOT_IMPLEMENTED(NULL) +} + +- (void*)last +{ + /* FIXME */ + OF_NOT_IMPLEMENTED(NULL) +} + +- add: (void*)item +{ + /* FIXME */ + OF_NOT_IMPLEMENTED(self) +} + +- addNItems: (size_t)nitems + fromCArray: (void*)carray +{ + /* FIXME */ + OF_NOT_IMPLEMENTED(self) +} + +- removeLastNItems: (size_t)nitems +{ + /* FIXME */ + OF_NOT_IMPLEMENTED(self) +} +@end Index: src/OFFile.m ================================================================== --- src/OFFile.m +++ src/OFFile.m @@ -104,21 +104,14 @@ } - (uint8_t*)readWithSize: (size_t)size andNItems: (size_t)nitems { - size_t memsize; uint8_t *ret; - memsize = nitems * size; - - if (size > SIZE_MAX / nitems) { - [[OFOverflowException newWithObject: self] raise]; - return NULL; - } - - ret = [self getMemWithSize: memsize]; + ret = [self getMemForNItems: nitems + withSize: size]; @try { [self readIntoBuffer: ret withSize: size andNItems: nitems]; Index: src/OFObject.h ================================================================== --- src/OFObject.h +++ src/OFObject.h @@ -22,10 +22,15 @@ struct __ofobject_allocated_mem *__mem_pool; } - init; - (void*)getMemWithSize: (size_t)size; +- (void*)getMemForNItems: (size_t)nitems + withSize: (size_t)size; - (void*)resizeMem: (void*)ptr toSize: (size_t)size; +- (void*)resizeMem: (void*)ptr + toNItems: (size_t)nitems + ofSize: (size_t)size; - freeMem: (void*)ptr; - free; @end Index: src/OFObject.m ================================================================== --- src/OFObject.m +++ src/OFObject.m @@ -72,10 +72,24 @@ __mem_pool = iter; return iter->ptr; } + +- (void*)getMemForNItems: (size_t)nitems + withSize: (size_t)size +{ + size_t memsize; + + if (size > SIZE_MAX / nitems) { + [[OFOverflowException newWithObject: self] raise]; + return NULL; + } + + memsize = nitems * size; + return [self getMemWithSize: memsize]; +} - (void*)resizeMem: (void*)ptr toSize: (size_t)size { struct __ofobject_allocated_mem *iter; @@ -83,11 +97,11 @@ for (iter = __mem_pool; iter != NULL; iter = iter->prev) { if (iter->ptr == ptr) { if ((ptr = realloc(iter->ptr, size)) == NULL) { [[OFNoMemException newWithObject: self andSize: size] raise]; - return NULL; + return iter->ptr; } iter->ptr = ptr; return ptr; } @@ -95,10 +109,26 @@ [[OFMemNotPartOfObjException newWithObject: self andPointer: ptr] raise]; return NULL; } + +- (void*)resizeMem: (void*)ptr + toNItems: (size_t)nitems + ofSize: (size_t)size +{ + size_t memsize; + + if (size > SIZE_MAX / nitems) { + [[OFOverflowException newWithObject: self] raise]; + return ptr; + } + + memsize = nitems * size; + return [self resizeMem: ptr + toSize: memsize]; +} - freeMem: (void*)ptr; { struct __ofobject_allocated_mem *iter;