86 int alignmentBit = 0x1;
92 if (Alignment < (
int)
sizeof(
unsigned char *)) {
93 Alignment = (int)
sizeof(
unsigned char *);
96 for (i = 0; i < 31; i++) {
97 if (Alignment == alignmentBit) {
119 if (ItemSize <= Alignment) {
127 if (ItemSize % Alignment != 0) {
133 ItemSize = ((alignmentCount + 1) * Alignment);
154 if (Alignment == 0) {
160 MemPoolSize =
sizeof(
MemPool_t) -
sizeof(
unsigned char)
163 MemPool = (
MemPool_t *)malloc(MemPoolSize);
168 MemPool->
Lock = xSemaphoreCreateMutex();
169 if (MemPool->
Lock == NULL) {
180 for (i = 0; i < ItemCount; i++) {
201 int AdditionalPoolSize;
206 AdditionalPoolSize = ItemCount * MemPool->
ItemSize;
208 ptr = (
unsigned char *)malloc(AdditionalPoolSize);
213 for (i = 0; i < ItemCount; i++) {
217 xSemaphoreTake(MemPool->
Lock, portMAX_DELAY);
221 xSemaphoreGive(MemPool->
Lock);
231 void *PreallocatedMemory,
232 int PreallocatedMemorySize,
244 if (Alignment == 0) {
250 MemPoolSize =
sizeof(
MemPool_t) -
sizeof(
unsigned char);
252 MemPool = (
MemPool_t *)malloc(MemPoolSize);
257 MemPool->
Lock = xSemaphoreCreateMutex();
258 if (MemPool->
Lock == NULL) {
267 ptr = (
unsigned char *)PreallocatedMemory;
269 while (PreallocatedMemorySize >= ItemSize) {
274 PreallocatedMemorySize -= MemPool->
ItemSize;
282 void *PreallocatedMemory,
283 int PreallocatedMemorySize)
292 ptr = (
unsigned char *)PreallocatedMemory;
294 while (PreallocatedMemorySize >= MemPool->
ItemSize) {
298 xSemaphoreTake(MemPool->
Lock, portMAX_DELAY);
302 xSemaphoreGive(MemPool->
Lock);
321 xSemaphoreTake(MemPool->
Lock, portMAX_DELAY);
324 xSemaphoreGive(MemPool->
Lock);
330 xSemaphoreGive(MemPool->
Lock);
332 ptr = ((
unsigned char *)Node) + MemPool->
Alignment;
348 ptr = ((
unsigned char *)memory) - MemPool->
Alignment;
352 xSemaphoreTake(MemPool->
Lock, portMAX_DELAY);
356 xSemaphoreGive(MemPool->
Lock);
MemoryPool_t CreateMemoryPoolStatic(int ItemSize, void *PreallocatedMemory, int PreallocatedMemorySize, int Alignment)
void MemoryPoolFree(MemoryPool_t pool, void *memory)
int AddExtraMemoryToPool(MemoryPool_t pool, int ItemCount)
SlNode_t * PopOffStack(Stack_t *Stack)
int AddExtraMemoryToPoolStatic(MemoryPool_t pool, void *PreallocatedMemory, int PreallocatedMemorySize)
struct MemPool_t_ MemPool_t
void PushOnStack(Stack_t *Stack, SlNode_t *Node)
static int CalculateAndVerifyAlignment(int Alignment)
MemoryPool_t CreateMemoryPool(int ItemSize, int ItemCount, int Alignment)
static int CalculateItemSize(int ItemSize, int Alignment)
void * MemoryPoolAllocate(MemoryPool_t pool)
void InitStack(Stack_t *Stack)