Skip to content

Commit 3c91128

Browse files
MOre
1 parent a9edf34 commit 3c91128

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

Data_Struct_Implementation/memoryPoolAllocator/README.md

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ The memory pool data structure that contains metadata about the pool
1818
and maintain a list of free blocks to be allocated.
1919
*/
2020
typedef 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
7376
void *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

100111
void poolFree(pool *p, void *ptr)
@@ -109,6 +120,7 @@ void poolFree(pool *p, void *ptr)
109120

110121
void poolFreeAll(pool *p)
111122
{
123+
// reinitialize all
112124
p->used = p->blockSize - 1;
113125
p->block = -1;
114126
p->freed = NULL;

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@
5959
5. [Array of Bits (bit sets/bit map)](Data_Struct_Implementation/bitsArray/README.md)
6060
6. Low Pass Filter
6161
7. [Memory map IO register manipulation](Data_Struct_Implementation/memoryMap/memory_map_io.md)
62-
8. Timer list
63-
9. [Memory Pool Allocation](Data_Struct_Implementation\memoryPoolAllocator\README.md)
62+
8. [Simple One-layer Timer list/wheel](Data_Struct_Implementation/timerWheel/README.md)
63+
9. [Memory Pool Allocation](Data_Struct_Implementation\memoryPoolAllocator\README.md)
6464
10. Simple Slab Allocator
6565
11. Data Structure
6666
1. [Ring Buffer](./Data_Struct_Implementation/circularRingBuffer/README.md)

0 commit comments

Comments
 (0)