From 24aca132c32fa3e54d5cd5b049b919eafe741052 Mon Sep 17 00:00:00 2001 From: Kolan Sh Date: Fri, 29 Apr 2011 18:45:44 +0400 Subject: [PATCH] =?UTF-8?q?pool=5Falloc()=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=20=D0=B8=20=D0=BE=D0=BF=D1=82=D0=B8=D0=BC=D0=B8=D0=B7?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B0,=20pool=5Ffree()=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B3=D0=B0=D0=B5=D1=82=20=D0=BF=D0=BE=D0=BA=D0=B0?= =?UTF-8?q?=20=D1=87=D1=82=D0=BE=20=D0=B8=20=D0=BD=D0=B5=20=D0=BE=D0=BF?= =?UTF-8?q?=D1=82=D0=B8=D0=BC=D0=B8=D0=B7=D0=B8=D1=80=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- c/malloc_speed_test/pool_list.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) 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; }