Skip to content

Commit

Permalink
Provide a way to override frame memory allocator functions
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilioPeJu committed Oct 5, 2023
1 parent 076bd45 commit a6b6dec
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
4 changes: 2 additions & 2 deletions ADApp/ADSrc/NDArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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;
}

Expand Down
12 changes: 12 additions & 0 deletions ADApp/ADSrc/NDArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
7 changes: 5 additions & 2 deletions ADApp/ADSrc/NDArrayPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a6b6dec

Please sign in to comment.