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

Improved sanity checks.

Rev: src/xalloc.c:1.6
Rev: src/xalloc.h:1.5
parent 07e8b766
...@@ -28,13 +28,14 @@ ...@@ -28,13 +28,14 @@
#ifdef DEBUG_ALLOC #ifdef DEBUG_ALLOC
void *debug_malloc(size_t size) void *debug_malloc(size_t real_size)
{ {
static int count = 4711; static int count = 4711;
int *res; int *res;
int size;
/* Count size in ints, and round up */ /* Count size in ints, and round up */
size = (size + sizeof(int)-1) / sizeof(int); size = (real_size + sizeof(int)-1) / sizeof(int);
res = malloc((size + 3)*sizeof(int)); res = malloc((size + 3)*sizeof(int));
...@@ -43,23 +44,49 @@ void *debug_malloc(size_t size) ...@@ -43,23 +44,49 @@ void *debug_malloc(size_t size)
res[0] = count; res[0] = count;
res[1] = size; res[1] = size;
((struct lsh_object *) (res + 2))->type = real_size;
res[size+2] = ~count; res[size+2] = ~count;
count++; count++;
return (void *) (res + 2); return (void *) (res + 2);
} }
void debug_free(void *m) void debug_check_object(void *m, UINT32 expected_size)
{ {
int *p = (int *) m; int real_size = ((struct lsh_object *) m)->type;
size_t size = p[-1]; if (real_size)
{ /* Heap allocated object */
int *p = (int *) m;
size_t size = p[-1];
if (~p[-2] != p[size]) if (expected_size > real_size)
fatal("Memory currupted!\n"); 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");
}
}
p[-2] = p[size] = 0; void debug_free(void *m)
{
free(p-2); int real_size = ((struct lsh_object *) m)->type;
if (real_size)
{ /* Heap allocated object */
int *p = (int *) m;
size_t size = p[-1];
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 #endif
...@@ -77,6 +104,7 @@ struct lsh_string *lsh_string_alloc(UINT32 length) ...@@ -77,6 +104,7 @@ struct lsh_string *lsh_string_alloc(UINT32 length)
struct lsh_string *packet struct lsh_string *packet
= xalloc(sizeof(struct lsh_string) - 1 + length); = xalloc(sizeof(struct lsh_string) - 1 + length);
packet->length = length; packet->length = length;
packet->sequence_number = 0;
return packet; return packet;
} }
......
...@@ -50,14 +50,20 @@ struct lsh_string *lsh_string_alloc(UINT32 size); ...@@ -50,14 +50,20 @@ struct lsh_string *lsh_string_alloc(UINT32 size);
void lsh_string_free(struct lsh_string *packet); void lsh_string_free(struct lsh_string *packet);
#ifdef DEBUG_ALLOC #ifdef DEBUG_ALLOC
void *debug_malloc(size_t size); void *debug_malloc(size_t size);
void debug_free(void *m); void debug_free(void *m);
void debug_check_object(void *m, UINT32 size);
#define lsh_free debug_free #define lsh_free debug_free
#define lsh_malloc debug_malloc #define lsh_malloc debug_malloc
#else #define MDEBUG(x) debug_check_object(x, sizeof(*(x)))
#else /* !DEBUG_ALLOC */
#define lsh_free free #define lsh_free free
#define lsh_malloc malloc #define lsh_malloc malloc
#endif #define MDEBUG(x)
#endif /* !DEBUG_ALLOC */
#endif /* LSH_XALLOC_H_INCLUDED */ #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