@@ -18,13 +18,13 @@ The memory pool data structure that contains metadata about the pool
1818and maintain a list of free blocks to be allocated.
1919*/
2020typedef struct {
21- uint32_t elementSize;
22- uint32_t blockSize;
23- uint32_t used;
24- int32_t block;
25- poolFreed *freed;
26- uint32_t blocksUsed;
27- uint8_t **blocks;
21+ uint32_t chunkSize; // size of each chunk
22+ uint32_t blockSize; // size of each block
23+ uint32_t used; // number of chunks in use in current block
24+ int32_t block; // current block index
25+ poolFreed *freed; // overall free chunks list
26+ uint32_t blocksUsed; // number of block allocated
27+ uint8_t **blocks; // block memory
2828} pool;
2929```
3030
@@ -37,18 +37,20 @@ typedef struct {
3737#endif
3838
3939/*
40- Pool Initialization function
40+ Pool Initialization function. Pool is only able to allocate chunSize objects
4141*/
42- void poolInitialize (pool * p, const uint32_t elementSize , const uint32_t blockSize)
42+ void poolInitialize (pool * p, const uint32_t chunkSize , const uint32_t blockSize)
4343{
4444 uint32_t i;
4545
46- p->elementSize = max(elementSize, sizeof(poolFreed));
46+ // define chunk and block parammeters
47+ p->chunkSize = max(chunkSize, sizeof(poolFreed));
4748 p->blockSize = blockSize;
4849
4950 // Clear the pool before any usage
5051 poolFreeAll(p);
5152
53+ // assign initial number of blocks
5254 p->blocksUsed = POOL_BLOCKS_INITIAL; // 1
5355 p->blocks = malloc(sizeof(uint8_t*) * p->blocksUsed);
5456
@@ -63,38 +65,47 @@ void poolFreePool(pool *p)
6365 if(p->blocks[ i] == NULL)
6466 break;
6567 else
66- free(p->blocks[ i] );
68+ free(p->blocks[ i] ); // free this block
6769 }
6870
71+ // free all block
6972 free(p->blocks);
7073}
7174
7275#ifndef DISABLE_MEMORY_POOLING
7376void * poolMalloc(pool * p)
7477{
78+ // if there is free chunk, reuse it
7579 if(p->freed != NULL) {
7680 void * recycle = p->freed;
7781 p->freed = p->freed->nextFree;
7882 return recycle;
7983 }
8084
85+ // else, we proceed to the chunk in current block
86+ // if block is full
8187 if(++p->used == p->blockSize) {
82- p->used = 0;
88+ p->used = 0; // reset used chunk number to 0
89+ // if we also run out of blocks
8390 if(++p->block == (int32_t)p->blocksUsed) {
8491 uint32_t i;
8592
93+ // assign 2 times block size
8694 p->blocksUsed <<= 1;
8795 p->blocks = realloc(p->blocks, sizeof(uint8_t*)* p->blocksUsed);
8896
97+ // initialize to be NULL
8998 for(i = p->blocksUsed >> 1; i < p->blocksUsed; ++i)
9099 p->blocks[i] = NULL;
91100 }
92101
102+ // assign memory for this free block
93103 if(p->blocks[p->block] == NULL)
94- p->blocks[p->block] = malloc(p->elementSize * p->blockSize);
104+ p->blocks[p->block] = malloc(p->chunkSize * p->blockSize);
95105 }
96106
97- return p->blocks[p->block] + p->used * p->elementSize;
107+ // return the next free chunk address
108+ return p->blocks[p->block] + p->used * p->chunkSize;
98109}
99110
100111void poolFree(pool * p, void * ptr)
@@ -109,6 +120,7 @@ void poolFree(pool *p, void *ptr)
109120
110121void poolFreeAll(pool * p)
111122{
123+ // reinitialize all
112124 p->used = p->blockSize - 1;
113125 p->block = -1;
114126 p->freed = NULL;
0 commit comments