66 lines
1.6 KiB
C
66 lines
1.6 KiB
C
|
#include <shadow.h>
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
void mt_init_shadow_pool_allocation(MtShadowPool *pool)
|
||
|
{
|
||
|
usz_t capacity = pool->capacity;
|
||
|
usz_t shadow_index = 0;
|
||
|
while(shadow_index < capacity)
|
||
|
{
|
||
|
pool->shadows[shadow_index].pool = pool;
|
||
|
pool->shadows[shadow_index].pool_next = &pool->shadows[shadow_index+1];
|
||
|
++shadow_index;
|
||
|
}
|
||
|
pool->shadows[capacity-1].pool_next = NULL;
|
||
|
}
|
||
|
|
||
|
void mt_init_shadow_pool(MtShadowPool *pool, usz_t capacity)
|
||
|
{
|
||
|
pool->capacity = capacity;
|
||
|
pool->continuation = NULL;
|
||
|
pool->shadows = calloc(sizeof(MtShadow), capacity);
|
||
|
pool->first_free = pool->shadows;
|
||
|
mt_init_shadow_pool_allocation(pool);
|
||
|
}
|
||
|
|
||
|
void mt_cleanup_shadow_pool(MtShadowPool *pool)
|
||
|
{
|
||
|
if(pool->continuation != NULL)
|
||
|
{
|
||
|
mt_cleanup_shadow_pool(pool->continuation);
|
||
|
free(pool->continuation);
|
||
|
}
|
||
|
free(pool->shadows);
|
||
|
}
|
||
|
|
||
|
void mt_free_shadow_pool(MtShadowPool *pool)
|
||
|
{
|
||
|
mt_cleanup_shadow_pool(pool);
|
||
|
free(pool);
|
||
|
}
|
||
|
|
||
|
MtShadowPool * mt_new_shadow_pool(usz_t capacity)
|
||
|
{
|
||
|
MtShadowPool *pool = malloc(sizeof(MtShadowPool));
|
||
|
mt_init_shadow_pool(pool, capacity);
|
||
|
return pool;
|
||
|
}
|
||
|
|
||
|
MtShadow * mt_pool_shadow(MtShadowPool *pool)
|
||
|
{
|
||
|
if(pool->first_free == NULL)
|
||
|
{
|
||
|
if(pool->continuation == NULL)
|
||
|
pool->continuation = mt_new_shadow_pool(pool->capacity * 2);
|
||
|
return mt_pool_shadow(pool->continuation);
|
||
|
}
|
||
|
MtShadow *result = pool->first_free;
|
||
|
pool->first_free = result->pool_next;
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
void mt_unpool_shadow(MtShadow *shadow)
|
||
|
{
|
||
|
shadow->pool_next = shadow->pool->first_free;
|
||
|
shadow->pool->first_free = shadow;
|
||
|
}
|