But note: There is extra memoeymemory being used.
// LetsLet's examine this specific call:
Memory_Pool memory;
if (mp_init(&memory, sizeof(void*) * 4, 5) != MEMORY_POOL_SUCCESS)
{
exit(1);
}
if((mp->memory = malloc(size * slots)) == NULL)
return MEMORY_POOL_ERROR;
mp->head----->Random
mp->memory---> ***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
mp->head = NULL;
mp->head-----|
mp->memory---> ***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
char *end = (char *)mp->memory + size * slots;
mp->head-----|
mp->memory----------->***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
char *end----------------->
for(char *ite = mp->memory; ite < end; ite += size)
mp->head----->Random
mp->memory---> ***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
mp->head = NULL;
mp->head-----|
mp->memory---> ***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
char *end = (char *)mp->memory + size * slots;
mp->head-----|
mp->memory----------->***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
char *end----------------->
for(char *ite = mp->memory; ite < end; ite += size)
mp_release(mp, ite);
// Iteration 1:
mp->head----------------|
\/
mp->memory----------->**( null )*
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
char *end----------------->
// Iteration 2:
mp->memory----------->**( null )*
* /\ *
* | *
mp->head--------------*-| | *
* \/ | *
**( * )*
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
char *end----------------->
// Iteration 3:
mp->memory----------->**( null )*
* /\ *
* | *
* *
* | *
**( * )*
* /\ *
* | *
mp->head--------------*-| | *
* \/ | *
**( * )*
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
char *end----------------->
// Iteration 4:
mp->memory----------->**( null )*
* /\ *
* | *
* | *
* | *
**( * )*
* /\ *
* | *
* | *
* | *
**( * )*
* /\ *
* | *
mp->head--------------*-| | *
* \/ | *
**( * )*
* *
* *
* *
* *
***********
char *end----------------->
// Iteration 5:
mp->memory----------->**( null )*
* /\ *
* | *
* | *
* | *
**( * )*
* /\ *
* | *
* | *
* | *
**( * )*
* /\ *
* | *
* | *
* | *
**( * )*
* /\ *
* | *
mp->head--------------*-| | *
* \/ | *
**( * )*
char *end----------------->
return MEMORY_POOL_SUCCESS;
// Iteration 1:
mp->head----------------|
\/
mp->memory----------->**( null )*
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
char *end----------------->
// Iteration 2:
mp->memory----------->**( null )*
* /\ *
* | *
mp->head--------------*-| | *
* \/ | *
**( * )*
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
char *end----------------->
// Iteration 3:
mp->memory----------->**( null )*
* /\ *
* | *
* *
* | *
**( * )*
* /\ *
* | *
mp->head--------------*-| | *
* \/ | *
**( * )*
* *
* *
* *
* *
***********
* *
* *
* *
* *
***********
char *end----------------->
// Iteration 4:
mp->memory----------->**( null )*
* /\ *
* | *
* | *
* | *
**( * )*
* /\ *
* | *
* | *
* | *
**( * )*
* /\ *
* | *
mp->head--------------*-| | *
* \/ | *
**( * )*
* *
* *
* *
* *
***********
char *end----------------->
// Iteration 5:
mp->memory----------->**( null )*
* /\ *
* | *
* | *
* | *
**( * )*
* /\ *
* | *
* | *
* | *
**( * )*
* /\ *
* | *
* | *
* | *
**( * )*
* /\ *
* | *
mp->head--------------*-| | *
* \/ | *
**( * )*
char *end----------------->
return MEMORY_POOL_SUCCESS;
}