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

New functions lsh_alloc_object() and lsh_alloc_space(). Handle objects

without object headers better.

Rev: src/xalloc.c:1.7
Rev: src/xalloc.h:1.7
parent e1ab97db
......@@ -28,14 +28,21 @@
#ifdef DEBUG_ALLOC
/* 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. */
/* NOTE: This doesn't work if there are types that require other
* alignment than int boundaries. But it doesn't matter much if the
* optional debug code isn't fully portable. */
#define SIZE_IN_INTS(x) (((x) + sizeof(int)-1) / sizeof(int))
void *debug_malloc(size_t real_size)
{
static int count = 4711;
int *res;
int size;
/* Count size in ints, and round up */
size = (real_size + sizeof(int)-1) / sizeof(int);
int size = SIZE_IN_INTS(real_size);
res = malloc((size + 3)*sizeof(int));
......@@ -43,55 +50,33 @@ void *debug_malloc(size_t real_size)
return NULL;
res[0] = count;
res[1] = size;
((struct lsh_object *) (res + 2))->type = real_size;
res[1] = real_size;
/* ((struct lsh_object *) (res + 2))->type = real_size; */
res[size+2] = ~count;
count++;
return (void *) (res + 2);
}
void debug_check_object(void *m, UINT32 expected_size)
{
int real_size = ((struct lsh_object *) m)->type;
if (real_size)
{ /* Heap allocated object */
int *p = (int *) m;
size_t size = p[-1];
if (expected_size > real_size)
fatal("Type error: pointing at too small an object!\n");
if (~p[-2] != p[size])
fatal("Memory corrupted!\n");
if (expected_size > size * sizeof(int))
fatal("Memory corrupted!\n");
}
}
void debug_free(void *m)
{
int real_size = ((struct lsh_object *) m)->type;
if (real_size)
{ /* Heap allocated object */
if (m)
{
int *p = (int *) m;
size_t size = p[-1];
int real_size = p[-1];
int size = SIZE_IN_INTS(real_size);
if (~p[-2] != p[size])
fatal("Memory corrupted!\n");
p[-2] = p[size] = 0;
free(p-2);
}
else
fatal("Freeing an object not allcoated on the heap!\n");
}
#endif /* DEBUG_ALLOC */
#endif
void *xalloc(size_t size)
static void *xalloc(size_t size)
{
void *res = lsh_malloc(size);
if (!res)
......@@ -101,14 +86,86 @@ void *xalloc(size_t size)
struct lsh_string *lsh_string_alloc(UINT32 length)
{
struct lsh_string *packet
struct lsh_string *s
= xalloc(sizeof(struct lsh_string) - 1 + length);
packet->length = length;
packet->sequence_number = 0;
return packet;
#ifdef DEBUG_ALLOC
s->header.magic = -1717;
#endif
s->length = length;
s->sequence_number = 0;
return s;
}
void lsh_string_free(struct lsh_string *s)
{
#ifdef DEBUG_ALLOC
if (s->header.magic != -1717)
fatal("lsh_string_free: Not string!\n");
#endif
lsh_free(s);
}
void *lsh_object_alloc(size_t size)
{
struct lsh_object *self = xalloc(size);
#ifdef DEBUG_ALLOC
self->size = size;
#endif
return (void *) self;
};
void lsh_object_free(void *p)
{
lsh_free(p);
};
#ifdef DEBUG_ALLOC
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");
}
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");
}
#endif /* DEBUG_ALLOC */
#ifdef DEBUG_ALLOC
void *lsh_space_alloc(size_t size)
{
int * p = xalloc(size + sizeof(int));
*p = -1919;
return (void *) (p + 1);
}
void lsh_space_free(void *p)
{
int *m = ((int *) p) - 1;
if (*m != -1919)
fatal("lsh_free_space: Type error!\n");
lsh_free(m);
}
void lsh_string_free(struct lsh_string *packet)
#else /* !DEBUG_ALLOC */
void *lsh_space_alloc(size_t size)
{
lsh_free(packet);
return xalloc(size);
}
void lsh_space_free(void *p)
{
lsh_free(p);
}
#endif /* !DEBUG_ALLOC */
......@@ -29,8 +29,9 @@
#include <stdlib.h>
#include "lsh_types.h"
#if 0
void *xalloc(size_t size);
#define NEW(x) ((x) = xalloc(sizeof(*(x))))
#endif
/* Allocation */
......@@ -50,21 +51,36 @@ void *xalloc(size_t size);
struct lsh_string *lsh_string_alloc(UINT32 size);
void lsh_string_free(struct lsh_string *packet);
void *lsh_object_alloc(size_t size);
void lsh_object_free(void *p);
void *lsh_space_alloc(size_t size);
void lsh_space_free(void *p);
#ifdef DEBUG_ALLOC
void *debug_malloc(size_t size);
void debug_free(void *m);
void debug_check_object(void *m, UINT32 size);
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) debug_check_object(x, sizeof(*(x)))
#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)
#endif /* !DEBUG_ALLOC */
#define NEW(x) ((x) = lsh_object_alloc(sizeof(*(x))))
#define NEW_SPACE(x) ((x) = lsh_space_alloc(sizeof(*(x))))
#endif /* LSH_XALLOC_H_INCLUDED */
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