1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

o pool-debug.c contains an alternative implementation of pool that gets

a seperate chunk of memory from dbg_malloc for each pool_alloc.  This
   will allow the bounds checking code in dbg_malloc to do it's stuff.

o  The normal implementation moved to pool-fast.c

o  pool.c now just contains a #ifdef and includes the appropriate .c file.

Alasdair, could you make sure that gcc -MM get's passed all the
CFLAGS please, otherwise the dependencies get calculated incorrectly.
This commit is contained in:
Joe Thornber 2001-11-28 09:13:00 +00:00
parent f8107626d0
commit 4a8d120415
3 changed files with 383 additions and 221 deletions

151
lib/mm/pool-debug.c Normal file
View File

@ -0,0 +1,151 @@
/*
* Copyright (C) 2001 Sistina Software (UK) Limited.
*
* This file is released under the LGPL.
*/
#include "pool.h"
#include "dbg_malloc.h"
#include "log.h"
#include <assert.h>
struct block {
struct block *next;
char data[0];
};
struct pool {
struct block *blocks;
struct block *tail;
};
/* by default things come out aligned for doubles */
#define DEFAULT_ALIGNMENT __alignof__ (double)
struct pool *pool_create(size_t chunk_hint)
{
struct pool *mem = dbg_malloc(sizeof(*mem));
if (!mem) {
stack;
return NULL;
}
mem->blocks = mem->tail = NULL;
return mem;
}
static void _free_blocks(struct block *b)
{
struct block *n;
while (b) {
n = b->next;
dbg_free(b);
b = n;
}
}
void pool_destroy(struct pool *p)
{
_free_blocks(p->blocks);
dbg_free(p);
}
void *pool_alloc(struct pool *p, size_t s)
{
return pool_alloc_aligned(p, s, DEFAULT_ALIGNMENT);
}
void *pool_alloc_aligned(struct pool *p, size_t s, unsigned alignment)
{
/* FIXME: I'm currently ignoring the alignment arg. */
size_t len = sizeof(struct block) + s;
struct block *b = dbg_malloc(len);
if (!b) {
stack;
return NULL;
}
b->next = NULL;
if (p->tail) {
p->tail->next = b;
p->tail = b;
} else
p->blocks = p->tail = b;
return &b->data;
}
void pool_empty(struct pool *p)
{
_free_blocks(p->blocks);
p->blocks = p->tail = NULL;
}
void pool_free(struct pool *p, void *ptr)
{
struct block *b, *prev = NULL;
for (b = p->blocks; b; b = b->next) {
prev = b;
if ((void *) &b->data == ptr)
break;
}
/*
* If this fires then you tried to free a
* pointer that either wasn't from this
* pool, or isn't the start of a block.
*/
assert(b);
if (!b) {
log_err("pool asked to free a pointer that was not "
"allocated from this pool.");
assert(0);
}
_free_blocks(b);
if (prev) {
p->tail = prev;
prev->next = NULL;
} else
p->blocks = p->tail = NULL;
}
void *pool_begin_object(struct pool *p, size_t hint, unsigned align)
{
log_err("pool_begin_object not implemented for pool-debug");
assert(0);
}
void *pool_grow_object(struct pool *p, unsigned char *buffer, size_t n)
{
log_err("pool_grow_object not implemented for pool-debug");
assert(0);
}
void *pool_end_object(struct pool *p)
{
log_err("pool_end_object not implemented for pool-debug");
assert(0);
}
void pool_abandon_object(struct pool *p)
{
log_err("pool_abandon_object not implemented for pool-debug");
}
char *pool_strdup(struct pool *p, const char *str)
{
char *ret = pool_alloc(p, strlen(str) + 1);
if (ret)
strcpy(ret, str);
return ret;
}

227
lib/mm/pool-fast.c Normal file
View File

@ -0,0 +1,227 @@
/*
* Copyright (C) 2001 Sistina Software (UK) Limited.
*
* This file is released under the LGPL.
*/
#include <stdlib.h>
#include <string.h>
#include "pool.h"
#include "dbg_malloc.h"
#include "log.h"
struct chunk {
char *begin, *end;
struct chunk *prev;
};
struct pool {
struct chunk *chunk, *spare_chunk; /* spare_chunk is a one entry free
list to stop 'bobbling' */
size_t chunk_size;
size_t object_len;
unsigned object_alignment;
};
void _align_chunk(struct chunk *c, unsigned alignment);
struct chunk *_new_chunk(struct pool *p, size_t s);
/* by default things come out aligned for doubles */
#define DEFAULT_ALIGNMENT __alignof__ (double)
struct pool *pool_create(size_t chunk_hint)
{
size_t new_size = 1024;
struct pool *p = dbg_malloc(sizeof(*p));
if (!p) {
log_error("Couldn't create memory pool");
return 0;
}
memset(p, 0, sizeof(*p));
/* round chunk_hint up to the next power of 2 */
p->chunk_size = chunk_hint + sizeof(struct chunk);
while (new_size < p->chunk_size)
new_size <<= 1;
p->chunk_size = new_size;
return p;
}
void pool_destroy(struct pool *p)
{
struct chunk *c, *pr;
dbg_free(p->spare_chunk);
c = p->chunk;
while (c) {
pr = c->prev;
dbg_free(c);
c = pr;
}
dbg_free(p);
}
void *pool_alloc(struct pool *p, size_t s)
{
return pool_alloc_aligned(p, s, DEFAULT_ALIGNMENT);
}
void *pool_alloc_aligned(struct pool *p, size_t s, unsigned alignment)
{
struct chunk *c = p->chunk;
void *r;
/* realign begin */
if (c)
_align_chunk(c, alignment);
/* have we got room ? */
if(!c || (c->begin > c->end) || (c->end - c->begin < s)) {
/* allocate new chunk */
int needed = s + alignment + sizeof(struct chunk);
c = _new_chunk(p, (needed > p->chunk_size) ?
needed : p->chunk_size);
_align_chunk(c, alignment);
}
r = c->begin;
c->begin += s;
return r;
}
void pool_empty(struct pool *p)
{
struct chunk *c;
for (c = p->chunk; c && c->prev; c = c->prev)
;
if (p->chunk)
pool_free(p, (char *) (p->chunk + 1));
}
void pool_free(struct pool *p, void *ptr)
{
struct chunk *c = p->chunk;
while (c) {
if (((char *) c < (char *) ptr) &&
((char *) c->end > (char *) ptr)) {
c->begin = ptr;
break;
}
if (p->spare_chunk)
dbg_free(p->spare_chunk);
p->spare_chunk = c;
c = c->prev;
}
if (!c)
log_debug("pool_free asked to free a pointer "
"that wasn't in the pool, doing nothing");
else
p->chunk = c;
}
void *pool_begin_object(struct pool *p, size_t hint, unsigned align)
{
struct chunk *c = p->chunk;
p->object_len = 0;
p->object_alignment = align;
_align_chunk(c, align);
if (c->end - c->begin < hint) {
/* allocate a new chunk */
c = _new_chunk(p,
hint > (p->chunk_size - sizeof(struct chunk)) ?
hint + sizeof(struct chunk) + align :
p->chunk_size);
_align_chunk(c, align);
}
return c->begin;
}
void *pool_grow_object(struct pool *p, unsigned char *buffer, size_t n)
{
struct chunk *c = p->chunk;
if (c->end - (c->begin + p->object_len) < n) {
/* move into a new chunk */
if (p->object_len + n > (p->chunk_size / 2))
_new_chunk(p, (p->object_len + n) * 2);
else
_new_chunk(p, p->chunk_size);
_align_chunk(p->chunk, p->object_alignment);
memcpy(p->chunk->begin, c->begin, p->object_len);
c = p->chunk;
}
memcpy(c->begin + p->object_len, buffer, n);
p->object_len += n;
return c->begin;
}
void *pool_end_object(struct pool *p)
{
struct chunk *c = p->chunk;
void *r = c->begin;
c->begin += p->object_len;
p->object_len = 0u;
p->object_alignment = DEFAULT_ALIGNMENT;
return r;
}
void pool_abandon_object(struct pool *p)
{
p->object_len = 0;
p->object_alignment = DEFAULT_ALIGNMENT;
}
char *pool_strdup(struct pool *p, const char *str)
{
char *ret = pool_alloc(p, strlen(str) + 1);
if (ret)
strcpy(ret, str);
return ret;
}
void _align_chunk(struct chunk *c, unsigned alignment)
{
c->begin += alignment - ((unsigned long) c->begin & (alignment - 1));
}
struct chunk *_new_chunk(struct pool *p, size_t s)
{
struct chunk *c;
if (p->spare_chunk &&
((p->spare_chunk->end - (char *) p->spare_chunk) >= s)) {
/* reuse old chunk */
c = p->spare_chunk;
p->spare_chunk = 0;
} else {
c = dbg_malloc(s);
c->end = (char *) c + s;
}
c->prev = p->chunk;
c->begin = (char *) (c + 1);
p->chunk = c;
return c;
}
/*
* Local variables:
* c-file-style: "linux"
* End:
* vim:ai cin ts=8
*/

View File

@ -4,224 +4,8 @@
* This file is released under the LGPL.
*/
#include <stdlib.h>
#include <string.h>
#include "pool.h"
#include "dbg_malloc.h"
#include "log.h"
struct chunk {
char *begin, *end;
struct chunk *prev;
};
struct pool {
struct chunk *chunk, *spare_chunk; /* spare_chunk is a one entry free
list to stop 'bobbling' */
size_t chunk_size;
size_t object_len;
unsigned object_alignment;
};
void _align_chunk(struct chunk *c, unsigned alignment);
struct chunk *_new_chunk(struct pool *p, size_t s);
/* by default things come out aligned for doubles */
#define DEFAULT_ALIGNMENT __alignof__ (double)
struct pool *pool_create(size_t chunk_hint)
{
size_t new_size = 1024;
struct pool *p = dbg_malloc(sizeof(*p));
if (!p) {
log_error("Couldn't create memory pool");
return 0;
}
memset(p, 0, sizeof(*p));
/* round chunk_hint up to the next power of 2 */
p->chunk_size = chunk_hint + sizeof(struct chunk);
while (new_size < p->chunk_size)
new_size <<= 1;
p->chunk_size = new_size;
return p;
}
void pool_destroy(struct pool *p)
{
struct chunk *c, *pr;
dbg_free(p->spare_chunk);
c = p->chunk;
while (c) {
pr = c->prev;
dbg_free(c);
c = pr;
}
dbg_free(p);
}
void *pool_alloc(struct pool *p, size_t s)
{
return pool_alloc_aligned(p, s, DEFAULT_ALIGNMENT);
}
void *pool_alloc_aligned(struct pool *p, size_t s, unsigned alignment)
{
struct chunk *c = p->chunk;
void *r;
/* realign begin */
if (c)
_align_chunk(c, alignment);
/* have we got room ? */
if(!c || (c->begin > c->end) || (c->end - c->begin < s)) {
/* allocate new chunk */
int needed = s + alignment + sizeof(struct chunk);
c = _new_chunk(p, (needed > p->chunk_size) ?
needed : p->chunk_size);
_align_chunk(c, alignment);
}
r = c->begin;
c->begin += s;
return r;
}
void pool_empty(struct pool *p)
{
struct chunk *c;
for (c = p->chunk; c && c->prev; c = c->prev)
;
if (p->chunk)
pool_free(p, (char *) (p->chunk + 1));
}
void pool_free(struct pool *p, void *ptr)
{
struct chunk *c = p->chunk;
while (c) {
if (((char *) c < (char *) ptr) &&
((char *) c->end > (char *) ptr)) {
c->begin = ptr;
break;
}
if (p->spare_chunk)
dbg_free(p->spare_chunk);
p->spare_chunk = c;
c = c->prev;
}
if (!c)
log_debug("pool_free asked to free a pointer "
"that wasn't in the pool, doing nothing");
else
p->chunk = c;
}
void *pool_begin_object(struct pool *p, size_t hint, unsigned align)
{
struct chunk *c = p->chunk;
p->object_len = 0;
p->object_alignment = align;
_align_chunk(c, align);
if (c->end - c->begin < hint) {
/* allocate a new chunk */
c = _new_chunk(p,
hint > (p->chunk_size - sizeof(struct chunk)) ?
hint + sizeof(struct chunk) + align :
p->chunk_size);
_align_chunk(c, align);
}
return c->begin;
}
void *pool_grow_object(struct pool *p, unsigned char *buffer, size_t n)
{
struct chunk *c = p->chunk;
if (c->end - (c->begin + p->object_len) < n) {
/* move into a new chunk */
if (p->object_len + n > (p->chunk_size / 2))
_new_chunk(p, (p->object_len + n) * 2);
else
_new_chunk(p, p->chunk_size);
_align_chunk(p->chunk, p->object_alignment);
memcpy(p->chunk->begin, c->begin, p->object_len);
c = p->chunk;
}
memcpy(c->begin + p->object_len, buffer, n);
p->object_len += n;
return c->begin;
}
void *pool_end_object(struct pool *p)
{
struct chunk *c = p->chunk;
void *r = c->begin;
c->begin += p->object_len;
p->object_len = 0u;
p->object_alignment = DEFAULT_ALIGNMENT;
return r;
}
void pool_abandon_object(struct pool *p)
{
p->object_len = 0;
p->object_alignment = DEFAULT_ALIGNMENT;
}
char *pool_strdup(struct pool *p, const char *str)
{
char *ret = pool_alloc(p, strlen(str) + 1);
if (ret)
strcpy(ret, str);
return ret;
}
void _align_chunk(struct chunk *c, unsigned alignment)
{
c->begin += alignment - ((unsigned long) c->begin & (alignment - 1));
}
struct chunk *_new_chunk(struct pool *p, size_t s)
{
struct chunk *c;
if (p->spare_chunk &&
((p->spare_chunk->end - (char *) p->spare_chunk) >= s)) {
/* reuse old chunk */
c = p->spare_chunk;
p->spare_chunk = 0;
} else {
c = dbg_malloc(s);
c->end = (char *) c + s;
}
c->prev = p->chunk;
c->begin = (char *) (c + 1);
p->chunk = c;
return c;
}
/*
* Local variables:
* c-file-style: "linux"
* End:
* vim:ai cin ts=8
*/
#ifdef DEBUG_POOL
#include "pool-debug.c"
#else
#include "pool-fast.c"
#endif