Commit 7aa46689 authored by Niels Möller's avatar Niels Möller
Browse files

(debug_realloc, xrealloc, lsh_space_realloc): New functions.

(lsh_space_alloc) [!DEBUG_ALLOC]: Use xalloc.

Rev: src/xalloc.c:1.34.2.1
Rev: src/xalloc.h:1.30.2.1
parent 9558f082
......@@ -94,6 +94,39 @@ debug_free(const void *m)
free(p-2);
}
}
void *
debug_realloc(void *m, size_t real_size)
{
if (!real_size)
{
debug_free(m);
return NULL;
}
else if (!m)
return debug_malloc(real_size);
else
{
UNIT *p = (UNIT *) m;
UNIT *res;
UNIT old_size = p[-1];
UNIT size = SIZE_IN_UNITS(real_size);
if (~p[-2] != p[SIZE_IN_UNITS(old_size)])
fatal("Memory corrupted!\n");
res = realloc(p-2, (size + 3) * sizeof(UNIT));
if (!res)
return NULL;
res += 2;
res[-1] = real_size;
res[size] = ~res[-2];
return res;
}
}
#endif /* DEBUG_ALLOC */
static void *xalloc(size_t size)
......@@ -113,6 +146,15 @@ static void *xalloc(size_t size)
return res;
}
static void *xrealloc(void *p, size_t size)
{
void *res = lsh_realloc(p, size);
if (size && !res)
fatal("Virtual memory exhausted");
return res;
}
/* General allocator that can handle variable size objects */
struct lsh_object *
......@@ -249,17 +291,38 @@ void lsh_space_free(const void *p)
m = (UNIT *) p;
if (m[-1] != (UNIT) -1919)
fatal("lsh_free_space: Type error!\n");
fatal("lsh_space_free: Type error!\n");
lsh_free(m-1);
}
void *lsh_space_realloc(void *p, size_t size)
{
if (!size)
{
lsh_space_free(p);
return NULL;
}
else if (!p)
return lsh_space_alloc(size);
else
{
UNIT *m = p;
if (m[-1] != (UNIT) -1919)
fatal("lsh_space_realloc: Type error!\n");
m = xrealloc(m - 1, size + sizeof(UNIT));
return m + 1;
}
}
#else /* !DEBUG_ALLOC */
/* FIXME: Why not use macros for this? */
void *lsh_space_alloc(size_t size)
{
return lsh_malloc(size);
return xalloc(size);
}
void lsh_space_free(const void *p)
......
......@@ -33,10 +33,14 @@
#if DEBUG_ALLOC
#define lsh_free debug_free
#define lsh_malloc debug_malloc
#define lsh_realloc debug_realloc
void *
debug_malloc(size_t real_size);
void *
debug_realloc(void *m, size_t real_size);
void
debug_free(const void *m);
#else /* !DEBUG_ALLOC */
......@@ -69,6 +73,7 @@ struct lsh_object *
lsh_object_clone(struct lsh_object *o);
void *lsh_space_alloc(size_t size);
void *lsh_space_realloc(void *p, size_t size);
void lsh_space_free(const void *p);
#if DEBUG_ALLOC
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment