diff --git a/ADApp/ADSrc/NDArray.cpp b/ADApp/ADSrc/NDArray.cpp index 2447a9bc6..7d06e9dcb 100644 --- a/ADApp/ADSrc/NDArray.cpp +++ b/ADApp/ADSrc/NDArray.cpp @@ -64,7 +64,7 @@ NDArray::NDArray(int nDims, size_t *dims, NDDataType_t dataType, size_t dataSize if (pData) { this->pData = pData; } else { - this->pData = malloc(dataSize); + this->pData = frame_malloc(dataSize); this->dataSize = dataSize; } } @@ -73,7 +73,7 @@ NDArray::NDArray(int nDims, size_t *dims, NDDataType_t dataType, size_t dataSize * Frees the data array, deletes all attributes, frees the attribute list and destroys the mutex. */ NDArray::~NDArray() { - if (this->pData) free(this->pData); + if (this->pData) frame_free(this->pData); delete this->pAttributeList; } diff --git a/ADApp/ADSrc/NDArray.h b/ADApp/ADSrc/NDArray.h index f7ad79b69..0cd127d26 100644 --- a/ADApp/ADSrc/NDArray.h +++ b/ADApp/ADSrc/NDArray.h @@ -25,6 +25,18 @@ /** The maximum number of dimensions in an NDArray */ #define ND_ARRAY_MAX_DIMS 10 +/** This provides a way of overriding the default memory management functions **/ +typedef void *(*malloc_func_t)(size_t size); +typedef void (*free_func_t)(void *ptr); +extern malloc_func_t frame_malloc; +extern free_func_t frame_free; + +inline void setADFrameMemoryFunctions(malloc_func_t malloc_f, free_func_t free_f) +{ + frame_malloc = malloc_f; + frame_free = free_f; +} + /** Enumeration of color modes for NDArray attribute "colorMode" */ typedef enum { diff --git a/ADApp/ADSrc/NDArrayPool.cpp b/ADApp/ADSrc/NDArrayPool.cpp index b28122e53..217d71572 100644 --- a/ADApp/ADSrc/NDArrayPool.cpp +++ b/ADApp/ADSrc/NDArrayPool.cpp @@ -29,6 +29,9 @@ static const char *driverName = "NDArrayPool"; +malloc_func_t frame_malloc = malloc; +free_func_t frame_free = free; + /** eraseNDAttributes is a global flag the controls whether NDArray::clearAttributes() is called * each time a new array is allocated with NDArrayPool->alloc(). @@ -139,7 +142,7 @@ NDArray* NDArrayPool::alloc(int ndims, size_t *dims, NDDataType_t dataType, size if (pData || (pListElement->dataSize_ > (dataSize * THRESHOLD_SIZE_RATIO))) { // We found an array but it is too large. Set the size to 0 so it will be allocated below. memorySize_ -= pArray->dataSize; - free(pArray->pData); + frame_free(pArray->pData); pArray->pData = NULL; } freeList_.erase(pListElement); @@ -193,7 +196,7 @@ NDArray* NDArrayPool::alloc(int ndims, size_t *dims, NDDataType_t dataType, size "%s: error: reached limit of %ld memory (%d buffers)\n", functionName, (long)maxMemory_, numBuffers_); } else { - pArray->pData = malloc(dataSize); + pArray->pData = frame_malloc(dataSize); if (pArray->pData) { pArray->dataSize = dataSize; pArray->compressedSize = dataSize;