pool_alloc() готова и оптимизирована, pool_free() лагает пока что и не оптимизирована

This commit is contained in:
Kolan Sh 2011-04-29 18:45:44 +04:00
parent 58d938d391
commit 24aca132c3
1 changed files with 22 additions and 11 deletions

View File

@ -48,34 +48,37 @@ void init()
#define MAX_B (pool + MAX_I * sizeof(struct block_s))
inline void *alloc()
inline void *pool_alloc()
{
if (!free_pool)
return NULL;
if (busy_pool) {
free_pool->next = busy_pool;
free_pool->prev = free_pool->next; // save free_pool->next in free_pool->prev
free_pool->next = busy_pool;
if (busy_pool)
busy_pool->prev = free_pool;
}
else
free_pool->next = NULL;
busy_pool = free_pool;
free_pool = free_pool->next;
free_pool = free_pool->prev;
busy_pool->prev = NULL; // saved in free_pool->prev to NULL
return &busy_pool->obj;
}
inline void free(void *p)
inline void pool_free(void *p)
{
if (!p)
return;
struct block_s *b1 = (struct block_s *)(p - 2 * sizeof(struct block_s *)),
*b0 = b1->prev,
*b2 = b1->next,
*f1 = free_pool;
// move one busy block to free list
// cut block from free list
if (b0)
b0->next = b2;
@ -102,10 +105,18 @@ int main()
{
unsigned long long i;
void *mem[MAX_I];
init();
for (i = 0; i < MAX_I; i++)
alloc();
if (!(mem[i] = pool_alloc())) {
printf("не удалось выделить память под %lld блок\n", i);
break;
}
for (i = 0; i < MAX_I; i++)
pool_free(mem[i]);
return 0;
}