Index: src/OFFloatMatrix.h ================================================================== --- src/OFFloatMatrix.h +++ src/OFFloatMatrix.h @@ -23,10 +23,11 @@ /** * \brief A class for storing and manipulating matrices of floats. */ @interface OFFloatMatrix: OFObject { +@public size_t rows, columns; float *data; } /** Index: src/OFFloatMatrix.m ================================================================== --- src/OFFloatMatrix.m +++ src/OFFloatMatrix.m @@ -14,19 +14,21 @@ * file. */ #include "config.h" +#include #include #include #import "OFFloatMatrix.h" #import "OFFloatVector.h" #import "OFString.h" #import "OFInvalidArgumentException.h" #import "OFNotImplementedException.h" +#import "OFOutOfMemoryException.h" #import "OFOutOfRangeException.h" #import "macros.h" @implementation OFFloatMatrix @@ -67,16 +69,19 @@ @try { rows = rows_; columns = columns_; - if (SIZE_MAX / rows < columns) + if (SIZE_MAX / rows < columns || + SIZE_MAX / rows * columns < sizeof(float)) @throw [OFOutOfRangeException newWithClass: isa]; - data = [self allocMemoryForNItems: rows * columns - withSize: sizeof(float)]; + if ((data = malloc(rows * columns * sizeof(float))) == NULL) + @throw [OFOutOfMemoryException + newWithClass: isa + requestedSize: rows * columns * sizeof(float)]; memset(data, 0, rows * columns * sizeof(float)); if (rows == columns) { size_t i; @@ -117,16 +122,18 @@ size_t i; rows = rows_; columns = columns_; - if (SIZE_MAX / rows < columns) - @throw [OFOutOfRangeException - newWithClass: isa]; + if (SIZE_MAX / rows < columns || + SIZE_MAX / rows * columns < sizeof(float)) + @throw [OFOutOfRangeException newWithClass: isa]; - data = [self allocMemoryForNItems: rows * columns - withSize: sizeof(float)]; + if ((data = malloc(rows * columns * sizeof(float))) == NULL) + @throw [OFOutOfMemoryException + newWithClass: isa + requestedSize: rows * columns * sizeof(float)]; for (i = 0; i < rows; i++) { size_t j; for (j = i; j < rows * columns; j += rows) @@ -137,10 +144,17 @@ @throw e; } return self; } + +- (void)dealloc +{ + free(data); + + [super dealloc]; +} - (void)setValue: (float)value forRow: (size_t)row column: (size_t)column { @@ -312,12 +326,14 @@ if (rows != matrix->columns) @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; - newData = [self allocMemoryForNItems: matrix->rows * columns - withSize: sizeof(float)]; + if ((newData = malloc(matrix->rows * columns * sizeof(float))) == NULL) + @throw [OFOutOfMemoryException + newWithClass: isa + requestedSize: matrix->rows * columns * sizeof(float)]; base1 = 0; base2 = 0; for (i = 0; i < columns; i++) { @@ -342,21 +358,25 @@ base1 += rows; base2 += matrix->rows; } - [self freeMemory: data]; + free(data); data = newData; rows = matrix->rows; } - (void)transpose { - float *newData = [self allocMemoryForNItems: rows * columns - withSize: sizeof(float)]; + float *newData; size_t i, k; + + if ((newData = malloc(rows * columns * sizeof(float))) == NULL) + @throw [OFOutOfMemoryException newWithClass: isa + requestedSize: rows * columns * + sizeof(float)]; rows ^= columns; columns ^= rows; rows ^= columns; @@ -365,11 +385,11 @@ for (j = i; j < rows * columns; j += rows) newData[j] = data[k++]; } - [self freeMemory: data]; + free(data); data = newData; } - (void)translateWithVector: (OFFloatVector*)vector { Index: src/OFFloatVector.m ================================================================== --- src/OFFloatVector.m +++ src/OFFloatVector.m @@ -14,24 +14,34 @@ * file. */ #include "config.h" +#include #include #include #import "OFFloatVector.h" #import "OFFloatMatrix.h" #import "OFString.h" #import "OFInvalidArgumentException.h" #import "OFNotImplementedException.h" +#import "OFOutOfMemoryException.h" #import "OFOutOfRangeException.h" #import "macros.h" +static Class floatMatrix = Nil; + @implementation OFFloatVector ++ (void)initialize +{ + if (self == [OFFloatVector class]) + floatMatrix = [OFFloatMatrix class]; +} + + vectorWithDimension: (size_t)dimension { return [[[self alloc] initWithDimension: dimension] autorelease]; } @@ -61,12 +71,17 @@ self = [super init]; @try { dimension = dimension_; - data = [self allocMemoryForNItems: dimension - withSize: sizeof(float)]; + if (SIZE_MAX / dimension < sizeof(float)) + @throw [OFOutOfRangeException newWithClass: isa]; + + if ((data = malloc(dimension * sizeof(float))) == NULL) + @throw [OFOutOfMemoryException + newWithClass: isa + requestedSize: dimension * sizeof(float)]; memset(data, 0, dimension * sizeof(float)); } @catch (id e) { [self release]; @throw e; @@ -96,12 +111,17 @@ @try { size_t i; dimension = dimension_; - data = [self allocMemoryForNItems: dimension - withSize: sizeof(float)]; + if (SIZE_MAX / dimension < sizeof(float)) + @throw [OFOutOfRangeException newWithClass: isa]; + + if ((data = malloc(dimension * sizeof(float))) == NULL) + @throw [OFOutOfMemoryException + newWithClass: isa + requestedSize: dimension * sizeof(float)]; for (i = 0; i < dimension; i++) data[i] = (float)va_arg(arguments, double); } @catch (id e) { [self release]; @@ -108,10 +128,17 @@ @throw e; } return self; } + +- (void)dealloc +{ + free(data); + + [super dealloc]; +} - (void)setValue: (float)value atIndex: (size_t)index { if (index >= dimension) @@ -319,37 +346,34 @@ data[i] /= magnitude; } - (void)multiplyWithMatrix: (OFFloatMatrix*)matrix { - size_t rows, columns; - float *cArray, *newData; + float *newData; size_t i, j, k; - columns = [matrix columns]; - - if (dimension != columns) + if (matrix->isa != floatMatrix || dimension != matrix->columns) @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; - rows = [matrix rows]; - cArray = [matrix cArray]; - - newData = [self allocMemoryForNItems: rows - withSize: sizeof(float)]; - memset(newData, 0, rows * sizeof(float)); - - for (i = j = k = 0; i < rows * columns; i++) { - newData[j] += cArray[i] * data[k]; - - if (++j == rows) { + if ((newData = malloc(matrix->rows * sizeof(float))) == NULL) + @throw [OFOutOfMemoryException + newWithClass: isa + requestedSize: matrix->rows * sizeof(float)]; + + memset(newData, 0, matrix->rows * sizeof(float)); + + for (i = j = k = 0; i < matrix->rows * matrix->columns; i++) { + newData[j] += matrix->data[i] * data[k]; + + if (++j == matrix->rows) { k++; j = 0; } } - [self freeMemory: data]; + free(data); data = newData; - dimension = rows; + dimension = matrix->rows; } @end Index: src/OFObject.m ================================================================== --- src/OFObject.m +++ src/OFObject.m @@ -85,11 +85,11 @@ #define PRE_IVAR ((struct pre_ivar*)(void*)((char*)self - PRE_IVAR_ALIGN)) static struct { Class isa; } alloc_failed_exception; -static Class autoreleasepool = Nil; +static Class autoreleasePool = Nil; static SEL cxx_construct = NULL; static SEL cxx_destruct = NULL; size_t of_pagesize; @@ -785,14 +785,14 @@ { /* * Cache OFAutoreleasePool since class lookups are expensive with the * GNU runtime. */ - if (autoreleasepool == Nil) - autoreleasepool = [OFAutoreleasePool class]; + if (autoreleasePool == Nil) + autoreleasePool = [OFAutoreleasePool class]; - [autoreleasepool addObject: self]; + [autoreleasePool addObject: self]; return self; } - (void)dealloc