Commit 24d0aa0e authored by Niels Möller's avatar Niels Möller
Browse files

Made lsh_malloc and lsh_free internal functions. Other files should

always use lsh_string_alloc, lsh_space_alloc or lsh_object_alloc instead.

Rev: src/xalloc.c:1.9
Rev: src/xalloc.h:1.8
parent 4fd550a7
......@@ -26,8 +26,13 @@
#include "xalloc.h"
#include "werror.h"
#include <stdlib.h>
#ifdef DEBUG_ALLOC
#define lsh_free debug_free
#define lsh_malloc debug_malloc
/* There are two sets of allocation functions: Low level allocation *
* that can allocate memory for any purpose, and object allocators
* that assume that the allocated object begins with a type field. */
......@@ -38,7 +43,7 @@
#define SIZE_IN_INTS(x) (((x) + sizeof(int)-1) / sizeof(int))
void *debug_malloc(size_t real_size)
static void *debug_malloc(size_t real_size)
{
static int count = 4711;
int *res;
......@@ -58,7 +63,7 @@ void *debug_malloc(size_t real_size)
return (void *) (res + 2);
}
void debug_free(void *m)
static void debug_free(void *m)
{
if (m)
{
......@@ -74,7 +79,13 @@ void debug_free(void *m)
free(p-2);
}
}
#endif /* DEBUG_ALLOC */
#else /* !DEBUG_ALLOC */
#define lsh_free free
#define lsh_malloc malloc
#endif /* !DEBUG_ALLOC */
static void *xalloc(size_t size)
{
......@@ -127,15 +138,36 @@ void lsh_object_free(void *p)
void lsh_object_check(void *p, size_t size)
{
struct lsh_object *self = (struct lsh_object *) p;
if (self->size != size)
fatal("Type error!\n");
switch(self->alloc_method)
{
case LSH_ALLOC_HEAP:
if (self->size != size)
fatal("Type error!\n");
break;
case LSH_ALLOC_STATIC:
case LSH_ALLOC_STACK:
break;
default:
fatal("Type error!\n");
}
}
void lsh_object_check_subtype(void *p, size_t size)
{
struct lsh_object *self = (struct lsh_object *) p;
if (self->size < size)
fatal("Type error!\n");
switch(self->alloc_method)
{
case LSH_ALLOC_HEAP:
if (self->size < size)
fatal("Type error!\n");
break;
case LSH_ALLOC_STATIC:
case LSH_ALLOC_STACK:
break;
default:
fatal("Type error!\n");
}
}
#endif /* DEBUG_ALLOC */
......
......@@ -60,20 +60,14 @@ void lsh_space_free(void *p);
#ifdef DEBUG_ALLOC
void *debug_malloc(size_t size);
void debug_free(void *m);
void lsh_object_check(void *m, size_t size);
void lsh_object_check_subtype(void *m, size_t size);
#define lsh_free debug_free
#define lsh_malloc debug_malloc
#define MDEBUG(x) lsh_object_check((x), sizeof(*(x)))
#define MDEBUG_SUBTYPE(x) lsh_object_check_subtype((x), sizeof(*(x)))
#else /* !DEBUG_ALLOC */
#define lsh_free free
#define lsh_malloc malloc
#define MDEBUG(x)
#define MDEBUG_SUBTYPE(x)
......
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