2011-04-29 15:41:02 +04:00
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
#define bool char
|
|
|
|
|
#define TRUE 1
|
|
|
|
|
#define FALSE 0
|
|
|
|
|
|
|
|
|
|
struct object_s {
|
|
|
|
|
char val;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct block_s {
|
|
|
|
|
struct block_s *next;
|
|
|
|
|
struct block_s *prev;
|
|
|
|
|
struct object_s obj;
|
|
|
|
|
};
|
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
struct block_pool {
|
|
|
|
|
size_t nblocks;
|
|
|
|
|
struct block_s *pool;
|
|
|
|
|
struct block_s *free_pool;
|
|
|
|
|
struct block_s *busy_pool;
|
|
|
|
|
};
|
2011-04-29 15:41:02 +04:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* инициализация пула
|
|
|
|
|
*/
|
2011-04-30 00:55:40 +04:00
|
|
|
|
void pool_init(struct block_pool *bp, size_t nblocks)
|
2011-04-29 15:41:02 +04:00
|
|
|
|
{
|
|
|
|
|
size_t i;
|
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
bp->nblocks = nblocks;
|
|
|
|
|
|
|
|
|
|
bp->pool = (struct block_s *)malloc(nblocks * sizeof(struct block_s));
|
|
|
|
|
|
|
|
|
|
size_t max_i = nblocks - 1;
|
2011-04-29 15:41:02 +04:00
|
|
|
|
for (i = 1; i < max_i; i++) {
|
2011-04-30 00:55:40 +04:00
|
|
|
|
bp->pool[i].prev = &bp->pool[i - 1];
|
|
|
|
|
bp->pool[i].next = &bp->pool[i + 1];
|
2011-04-29 15:41:02 +04:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
bp->pool[0].prev = NULL;
|
|
|
|
|
bp->pool[0].next = &bp->pool[1];
|
2011-04-29 15:41:02 +04:00
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
bp->pool[nblocks - 1].prev = &bp->pool[nblocks - 2];
|
|
|
|
|
bp->pool[nblocks - 1].next = NULL;
|
2011-04-29 15:41:02 +04:00
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
bp->free_pool = &bp->pool[0];
|
|
|
|
|
bp->busy_pool = NULL;
|
2011-04-29 15:41:02 +04:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
void pool_destroy(struct block_pool *bp)
|
2011-04-29 15:41:02 +04:00
|
|
|
|
{
|
2011-04-30 00:55:40 +04:00
|
|
|
|
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)
|
2011-04-29 15:41:02 +04:00
|
|
|
|
return NULL;
|
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
bp->free_pool->prev = bp->free_pool->next; // save free_pool->next in free_pool->prev
|
2011-04-29 18:45:44 +04:00
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
bp->free_pool->next = bp->busy_pool;
|
2011-04-29 18:45:44 +04:00
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
if (bp->busy_pool)
|
|
|
|
|
bp->busy_pool->prev = bp->free_pool;
|
2011-04-29 15:41:02 +04:00
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
bp->busy_pool = bp->free_pool;
|
2011-04-29 15:41:02 +04:00
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
bp->free_pool = bp->free_pool->prev;
|
2011-04-29 18:45:44 +04:00
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
bp->busy_pool->prev = NULL; // saved in free_pool->prev to NULL
|
2011-04-29 15:41:02 +04:00
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
return &bp->busy_pool->obj;
|
2011-04-29 15:41:02 +04:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
inline void pool_free(struct block_pool *bp, void *p)
|
2011-04-29 15:41:02 +04:00
|
|
|
|
{
|
2011-04-29 18:45:44 +04:00
|
|
|
|
if (!p)
|
|
|
|
|
return;
|
|
|
|
|
|
2011-04-29 15:41:02 +04:00
|
|
|
|
struct block_s *b1 = (struct block_s *)(p - 2 * sizeof(struct block_s *)),
|
2011-04-29 22:18:10 +04:00
|
|
|
|
*b2 = b1->next;
|
2011-04-29 15:41:02 +04:00
|
|
|
|
|
2011-04-29 22:19:07 +04:00
|
|
|
|
if (b1->prev)
|
|
|
|
|
b1->prev->next = b2;
|
2011-04-29 15:41:02 +04:00
|
|
|
|
if (b2)
|
2011-04-29 22:19:07 +04:00
|
|
|
|
b2->prev = b1->prev;
|
2011-04-29 15:41:02 +04:00
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
if (bp->free_pool) {
|
|
|
|
|
b1->next = bp->free_pool;
|
|
|
|
|
bp->free_pool->prev = b1;
|
2011-04-29 15:41:02 +04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
b1->next = NULL;
|
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
bp->free_pool = b2;
|
2011-04-29 15:41:02 +04:00
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
if (bp->busy_pool == b1)
|
|
|
|
|
bp->busy_pool = b2;
|
2011-04-29 15:41:02 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
unsigned long long i;
|
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
struct block_pool bp;
|
2011-04-29 18:45:44 +04:00
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
size_t nblocks = 0xffffff;
|
2011-04-30 00:35:53 +04:00
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
pool_init(&bp, nblocks);
|
2011-04-29 15:41:02 +04:00
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
void **mem = malloc(nblocks * sizeof(void *));
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < nblocks; i++)
|
|
|
|
|
if (!(mem[i] = pool_alloc(&bp))) {
|
2011-04-29 18:45:44 +04:00
|
|
|
|
printf("не удалось выделить память под %lld блок\n", i);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
for (i = 0; i < nblocks; i++)
|
|
|
|
|
pool_free(&bp, mem[i]);
|
2011-04-29 15:41:02 +04:00
|
|
|
|
|
2011-04-30 00:35:53 +04:00
|
|
|
|
free(mem);
|
|
|
|
|
|
2011-04-30 00:55:40 +04:00
|
|
|
|
pool_destroy(&bp);
|
|
|
|
|
|
2011-04-29 15:41:02 +04:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|