|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- /*
- * RealTime Memory Pool, heavily based on work by Nedko Arnaudov
- * Copyright (C) 2006-2009 Nedko Arnaudov <nedko@arnaudov.name>
- * Copyright (C) 2013-2014 Filipe Coelho <falktx@falktx.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * For a full copy of the GNU General Public License see the GPL.txt file
- */
-
- #include "list.h"
- #include "rtmempool.h"
- #include "rtmempool-lv2.h"
-
- #include <assert.h>
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- // ------------------------------------------------------------------------------------------------
-
- typedef struct list_head k_list_head;
-
- // ------------------------------------------------------------------------------------------------
-
- typedef struct _RtMemPool
- {
- char name[RTSAFE_MEMORY_POOL_NAME_MAX];
-
- size_t dataSize;
- size_t minPreallocated;
- size_t maxPreallocated;
-
- k_list_head used;
- unsigned int usedCount;
-
- k_list_head unused;
- unsigned int unusedCount;
-
- bool enforceThreadSafety;
-
- // next members are initialized/used only if enforceThreadSafety is true
- pthread_mutex_t mutex;
- unsigned int unusedCount2;
- k_list_head pending;
- size_t usedSize;
-
- } RtMemPool;
-
- // ------------------------------------------------------------------------------------------------
- // adjust unused list size
-
- static void rtsafe_memory_pool_sleepy(RtMemPool* poolPtr)
- {
- k_list_head* nodePtr;
- unsigned int count;
-
- if (poolPtr->enforceThreadSafety)
- {
- pthread_mutex_lock(&poolPtr->mutex);
-
- count = poolPtr->unusedCount2;
-
- assert(poolPtr->minPreallocated < poolPtr->maxPreallocated);
-
- while (count < poolPtr->minPreallocated)
- {
- nodePtr = malloc(sizeof(k_list_head) + poolPtr->dataSize);
-
- if (nodePtr == NULL)
- {
- break;
- }
-
- list_add_tail(nodePtr, &poolPtr->pending);
-
- count++;
-
- poolPtr->usedSize += poolPtr->dataSize;
- }
-
- while (count > poolPtr->maxPreallocated && ! list_empty(&poolPtr->pending))
- {
- nodePtr = poolPtr->pending.next;
-
- list_del(nodePtr);
-
- free(nodePtr);
-
- count--;
-
- poolPtr->usedSize -= poolPtr->dataSize;
- }
-
- pthread_mutex_unlock(&poolPtr->mutex);
- }
- else
- {
- while (poolPtr->unusedCount < poolPtr->minPreallocated)
- {
- nodePtr = malloc(sizeof(k_list_head) + poolPtr->dataSize);
-
- if (nodePtr == NULL)
- {
- return;
- }
-
- list_add_tail(nodePtr, &poolPtr->unused);
- poolPtr->unusedCount++;
- poolPtr->usedSize += poolPtr->dataSize;
- }
-
- while (poolPtr->unusedCount > poolPtr->maxPreallocated)
- {
- assert(! list_empty(&poolPtr->unused));
-
- nodePtr = poolPtr->unused.next;
-
- list_del(nodePtr);
- poolPtr->unusedCount--;
-
- free(nodePtr);
- poolPtr->usedSize -= poolPtr->dataSize;
- }
- }
- }
-
- // ------------------------------------------------------------------------------------------------
-
- static bool rtsafe_memory_pool_create2(RtMemPool_Handle* handlePtr,
- const char* poolName,
- size_t dataSize,
- size_t minPreallocated,
- size_t maxPreallocated,
- int enforceThreadSafety)
- {
- assert(minPreallocated <= maxPreallocated);
- assert(poolName == NULL || strlen(poolName) < RTSAFE_MEMORY_POOL_NAME_MAX);
-
- RtMemPool* poolPtr;
-
- poolPtr = malloc(sizeof(RtMemPool));
-
- if (poolPtr == NULL)
- {
- return false;
- }
-
- if (poolName != NULL)
- {
- strcpy(poolPtr->name, poolName);
- }
- else
- {
- sprintf(poolPtr->name, "%p", poolPtr);
- }
-
- poolPtr->dataSize = dataSize;
- poolPtr->minPreallocated = minPreallocated;
- poolPtr->maxPreallocated = maxPreallocated;
-
- INIT_LIST_HEAD(&poolPtr->used);
- poolPtr->usedCount = 0;
-
- INIT_LIST_HEAD(&poolPtr->unused);
- poolPtr->unusedCount = 0;
-
- poolPtr->enforceThreadSafety = (enforceThreadSafety != 0);
-
- if (poolPtr->enforceThreadSafety)
- {
- if (pthread_mutex_init(&poolPtr->mutex, NULL) != 0)
- {
- free(poolPtr);
- return false;
- }
-
- INIT_LIST_HEAD(&poolPtr->pending);
- }
-
- poolPtr->unusedCount2 = 0;
- poolPtr->usedSize = 0;
-
- rtsafe_memory_pool_sleepy(poolPtr);
- *handlePtr = (RtMemPool_Handle)poolPtr;
-
- return true;
- }
-
- // ------------------------------------------------------------------------------------------------
-
- static unsigned char rtsafe_memory_pool_create_old(const char* poolName, size_t dataSize, size_t minPreallocated, size_t maxPreallocated, RtMemPool_Handle* handlePtr)
- {
- return rtsafe_memory_pool_create2(handlePtr, poolName, dataSize, minPreallocated, maxPreallocated, 0);
- }
-
- // ------------------------------------------------------------------------------------------------
-
- bool rtsafe_memory_pool_create(RtMemPool_Handle* handlePtr,
- const char* poolName,
- size_t dataSize,
- size_t minPreallocated,
- size_t maxPreallocated)
- {
- return rtsafe_memory_pool_create2(handlePtr, poolName, dataSize, minPreallocated, maxPreallocated, 0);
- }
-
- // ------------------------------------------------------------------------------------------------
-
- bool rtsafe_memory_pool_create_safe(RtMemPool_Handle* handlePtr,
- const char* poolName,
- size_t dataSize,
- size_t minPreallocated,
- size_t maxPreallocated)
- {
- return rtsafe_memory_pool_create2(handlePtr, poolName, dataSize, minPreallocated, maxPreallocated, 1);
- }
-
- // ------------------------------------------------------------------------------------------------
-
- void rtsafe_memory_pool_destroy(RtMemPool_Handle handle)
- {
- assert(handle);
-
- k_list_head* nodePtr;
- RtMemPool* poolPtr = (RtMemPool*)handle;
-
- // caller should deallocate all chunks prior releasing pool itself
- if (poolPtr->usedCount != 0)
- {
- assert(0);
- }
-
- while (poolPtr->unusedCount != 0)
- {
- assert(! list_empty(&poolPtr->unused));
-
- nodePtr = poolPtr->unused.next;
-
- list_del(nodePtr);
- poolPtr->unusedCount--;
-
- free(nodePtr);
- }
-
- assert(list_empty(&poolPtr->unused));
-
- if (poolPtr->enforceThreadSafety)
- {
- while (! list_empty(&poolPtr->pending))
- {
- nodePtr = poolPtr->pending.next;
-
- list_del(nodePtr);
-
- free(nodePtr);
- }
-
- int ret = pthread_mutex_destroy(&poolPtr->mutex);
-
- #ifdef DEBUG
- assert(ret == 0);
- #else
- // unused
- (void)ret;
- #endif
- }
-
- free(poolPtr);
- }
-
- // ------------------------------------------------------------------------------------------------
- // find entry in unused list, fail if it is empty
-
- void* rtsafe_memory_pool_allocate_atomic(RtMemPool_Handle handle)
- {
- assert(handle);
-
- k_list_head* nodePtr;
- RtMemPool* poolPtr = (RtMemPool*)handle;
-
- if (list_empty(&poolPtr->unused))
- {
- return NULL;
- }
-
- nodePtr = poolPtr->unused.next;
- list_del(nodePtr);
-
- poolPtr->unusedCount--;
- poolPtr->usedCount++;
-
- list_add_tail(nodePtr, &poolPtr->used);
-
- if (poolPtr->enforceThreadSafety && pthread_mutex_trylock(&poolPtr->mutex) == 0)
- {
- while (poolPtr->unusedCount < poolPtr->minPreallocated && ! list_empty(&poolPtr->pending))
- {
- nodePtr = poolPtr->pending.next;
-
- list_del(nodePtr);
- list_add_tail(nodePtr, &poolPtr->unused);
-
- poolPtr->unusedCount++;
- }
-
- poolPtr->unusedCount2 = poolPtr->unusedCount;
-
- pthread_mutex_unlock(&poolPtr->mutex);
- }
-
- return (nodePtr + 1);
- }
-
- // ------------------------------------------------------------------------------------------------
-
- void* rtsafe_memory_pool_allocate_sleepy(RtMemPool_Handle handle)
- {
- assert(handle);
-
- void* data;
- RtMemPool* poolPtr = (RtMemPool*)handle;
-
- do {
- rtsafe_memory_pool_sleepy(poolPtr);
- data = rtsafe_memory_pool_allocate_atomic((RtMemPool_Handle)poolPtr);
- }
- while (data == NULL);
-
- return data;
- }
-
- // ------------------------------------------------------------------------------------------------
- // move from used to unused list
-
- void rtsafe_memory_pool_deallocate(RtMemPool_Handle handle, void* memoryPtr)
- {
- assert(handle);
-
- k_list_head* nodePtr;
- RtMemPool* poolPtr = (RtMemPool*)handle;
-
- list_del((k_list_head*)memoryPtr - 1);
- list_add_tail((k_list_head*)memoryPtr - 1, &poolPtr->unused);
- poolPtr->usedCount--;
- poolPtr->unusedCount++;
-
- if (poolPtr->enforceThreadSafety && pthread_mutex_trylock(&poolPtr->mutex) == 0)
- {
- while (poolPtr->unusedCount > poolPtr->maxPreallocated)
- {
- assert(! list_empty(&poolPtr->unused));
-
- nodePtr = poolPtr->unused.next;
-
- list_del(nodePtr);
- list_add_tail(nodePtr, &poolPtr->pending);
- poolPtr->unusedCount--;
- }
-
- poolPtr->unusedCount2 = poolPtr->unusedCount;
-
- pthread_mutex_unlock(&poolPtr->mutex);
- }
- }
-
- void lv2_rtmempool_init(LV2_RtMemPool_Pool* poolPtr)
- {
- poolPtr->create = rtsafe_memory_pool_create;
- poolPtr->destroy = rtsafe_memory_pool_destroy;
- poolPtr->allocate_atomic = rtsafe_memory_pool_allocate_atomic;
- poolPtr->allocate_sleepy = rtsafe_memory_pool_allocate_sleepy;
- poolPtr->deallocate = rtsafe_memory_pool_deallocate;
- }
-
- void lv2_rtmempool_init_deprecated(LV2_RtMemPool_Pool_Deprecated* poolPtr)
- {
- poolPtr->create = rtsafe_memory_pool_create_old;
- poolPtr->destroy = rtsafe_memory_pool_destroy;
- poolPtr->allocate_atomic = rtsafe_memory_pool_allocate_atomic;
- poolPtr->allocate_sleepy = rtsafe_memory_pool_allocate_sleepy;
- poolPtr->deallocate = rtsafe_memory_pool_deallocate;
- }
|