diff --git a/c/malloc_speed_test/pool_list.c b/c/malloc_speed_test/pool_list.c index fa1f42a..2501e24 100644 --- a/c/malloc_speed_test/pool_list.c +++ b/c/malloc_speed_test/pool_list.c @@ -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; }