добавлена структура block_pool, цель - создание множества пулов

This commit is contained in:
Kolan Sh 2011-04-30 00:55:40 +04:00
parent 1a34fea5ff
commit d6f1dbb321
1 changed files with 55 additions and 39 deletions

View File

@ -2,8 +2,6 @@
#include <stdio.h>
#define MAX_I 0x00ffffff
#define bool char
#define TRUE 1
#define FALSE 0
@ -18,56 +16,69 @@ struct block_s {
struct object_s obj;
};
static struct block_s pool[MAX_I];
static struct block_s *free_pool = NULL,
*busy_pool = NULL;
struct block_pool {
size_t nblocks;
struct block_s *pool;
struct block_s *free_pool;
struct block_s *busy_pool;
};
/*
* инициализация пула
*/
void init()
void pool_init(struct block_pool *bp, size_t nblocks)
{
size_t i;
size_t max_i = MAX_I - 1;
bp->nblocks = nblocks;
bp->pool = (struct block_s *)malloc(nblocks * sizeof(struct block_s));
size_t max_i = nblocks - 1;
for (i = 1; i < max_i; i++) {
pool[i].prev = &pool[i - 1];
pool[i].next = &pool[i + 1];
bp->pool[i].prev = &bp->pool[i - 1];
bp->pool[i].next = &bp->pool[i + 1];
}
pool[0].prev = NULL;
pool[0].next = &pool[1];
bp->pool[0].prev = NULL;
bp->pool[0].next = &bp->pool[1];
pool[MAX_I - 1].prev = &pool[MAX_I - 2];
pool[MAX_I - 1].next = NULL;
bp->pool[nblocks - 1].prev = &bp->pool[nblocks - 2];
bp->pool[nblocks - 1].next = NULL;
free_pool = &pool[0];
busy_pool = NULL;
bp->free_pool = &bp->pool[0];
bp->busy_pool = NULL;
}
inline void *pool_alloc()
void pool_destroy(struct block_pool *bp)
{
if (!free_pool)
free(bp->pool);
bp->nblocks = 0;
bp->pool = bp->free_pool = bp->busy_pool = NULL;
}
inline void *pool_alloc(struct block_pool *bp)
{
if (!bp->free_pool)
return NULL;
free_pool->prev = free_pool->next; // save free_pool->next in free_pool->prev
bp->free_pool->prev = bp->free_pool->next; // save free_pool->next in free_pool->prev
free_pool->next = busy_pool;
bp->free_pool->next = bp->busy_pool;
if (busy_pool)
busy_pool->prev = free_pool;
if (bp->busy_pool)
bp->busy_pool->prev = bp->free_pool;
busy_pool = free_pool;
bp->busy_pool = bp->free_pool;
free_pool = free_pool->prev;
bp->free_pool = bp->free_pool->prev;
busy_pool->prev = NULL; // saved in free_pool->prev to NULL
bp->busy_pool->prev = NULL; // saved in free_pool->prev to NULL
return &busy_pool->obj;
return &bp->busy_pool->obj;
}
inline void pool_free(void *p)
inline void pool_free(struct block_pool *bp, void *p)
{
if (!p)
return;
@ -80,39 +91,44 @@ inline void pool_free(void *p)
if (b2)
b2->prev = b1->prev;
if (free_pool) {
b1->next = free_pool;
free_pool->prev = b1;
if (bp->free_pool) {
b1->next = bp->free_pool;
bp->free_pool->prev = b1;
}
else
b1->next = NULL;
free_pool = b2;
bp->free_pool = b2;
if (busy_pool == b1)
busy_pool = b2;
if (bp->busy_pool == b1)
bp->busy_pool = b2;
}
int main()
{
unsigned long long i;
struct block_pool bp;
init();
size_t nblocks = 0xffffff;
void **mem = malloc(MAX_I * sizeof(void *));
pool_init(&bp, nblocks);
for (i = 0; i < MAX_I; i++)
if (!(mem[i] = pool_alloc())) {
void **mem = malloc(nblocks * sizeof(void *));
for (i = 0; i < nblocks; i++)
if (!(mem[i] = pool_alloc(&bp))) {
printf("не удалось выделить память под %lld блок\n", i);
break;
}
for (i = 0; i < MAX_I; i++)
pool_free(mem[i]);
for (i = 0; i < nblocks; i++)
pool_free(&bp, mem[i]);
free(mem);
pool_destroy(&bp);
return 0;
}