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

Bugfixes. New CAST macro.

Rev: src/lsh_types.h:1.19
Rev: src/xalloc.c:1.11
Rev: src/xalloc.h:1.11
parent dc535c06
......@@ -32,6 +32,8 @@
#include "include/crypto_types.h"
#include <stdlib.h>
#ifdef __GNUC__
#define NORETURN __attribute__ ((noreturn))
#define PRINTF_STYLE(f, a) __attribute__ ((format(printf, f, a)))
......@@ -76,11 +78,20 @@ struct lsh_object
char alloc_method;
char marked;
char dead;
};
/* NOTE: Static objects have a NULL isa-pointer, and can therefore not
* contain any references to non-static objects. This could be fixed,
* by using an argument to the STATIC_HEADER macro, but then one must
* use some class for lsh_class objects... */
#define STATIC_HEADER { NULL, NULL, LSH_ALLOC_STATIC, 0, 0 }
#define STACK_HEADER { NULL, NULL, LSH_ALLOC_STACK, 0, 0 }
struct lsh_class
{
struct lsh_object *super;
struct lsh_object super;
struct lsh_class *super_class;
size_t size;
......@@ -91,6 +102,9 @@ struct lsh_class
/* Particular classes may add their own methods here */
};
#define MARK_INSTANCE(c, i, f) ((c)->mark_instance((i), (f)))
#define FREE_INSTANCE(c, i) ((c)->free_instance((i)))
#ifdef DEBUG_ALLOC
#if 0
......@@ -107,9 +121,6 @@ struct lsh_string_header
int magic;
};
#define STATIC_HEADER { 0, LSH_ALLOC_STATIC, 0 }
#define STACK_HEADER { 0, LSH_ALLOC_STACK, 0 }
#else /* !DEBUG_ALLOC */
struct lsh_object
{
......@@ -119,9 +130,6 @@ struct lsh_object
struct lsh_string_header {};
#define STATIC_HEADER { LSH_ALLOC_STATIC, 0 }
#define STACK_HEADER { LSH_ALLOC_STACK, 0 }
#endif /* !DEBUG_ALLOC */
struct lsh_string
......
......@@ -118,13 +118,13 @@ void lsh_string_free(struct lsh_string *s)
struct lsh_object *lsh_object_alloc(struct lsh_class *class)
{
struct lsh_object *self = xalloc(class->size);
self->class = class;
self->alloc_method = LSH_ALLOC_HEAP;
struct lsh_object *instance = xalloc(class->size);
instance->isa = class;
instance->alloc_method = LSH_ALLOC_HEAP;
gc_register(o);
gc_register(instance);
return o;
return instance;
}
/* Should be called *only* by the gc */
......@@ -160,7 +160,8 @@ void lsh_object_free(void *p)
#endif
#ifdef DEBUG_ALLOC
void lsh_object_check(struct lsh_object *instance, struct lsh_class *class)
struct lsh_object *lsh_object_check(struct lsh_object *instance,
struct lsh_class *class)
{
if (instance->marked)
fatal("lsh_object_check: Unexpected marked object!\n");
......@@ -168,12 +169,14 @@ void lsh_object_check(struct lsh_object *instance, struct lsh_class *class)
if (instance->dead)
fatal("lsh_object_check: Reference to dead object!\n");
if (instance->class != class)
if (instance->isa != class)
fatal("lsh_object_check: Type error!\n");
return instance;
}
void lsh_object_check_subtype(struct lsh_object *instance,
struct lsh_class *class)
struct lsh_object *lsh_object_check_subtype(struct lsh_object *instance,
struct lsh_class *class)
{
struct lsh_class *type;
......@@ -183,9 +186,9 @@ void lsh_object_check_subtype(struct lsh_object *instance,
if (instance->dead)
fatal("lsh_object_check: Reference to dead object!\n");
for (type = instance->class; type; type=type->super_class)
for (type = instance->isa; type; type=type->super_class)
if (type == class)
return;
return instance;
fatal("lsh_object_check_subtype: Unexpected marked object!\n");
}
......
......@@ -47,8 +47,8 @@
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);
struct lsh_object *lsh_object_alloc(struct lsh_class *class);
void lsh_object_free(struct lsh_object *o);
void *lsh_space_alloc(size_t size);
void lsh_space_free(void *p);
......@@ -56,21 +56,47 @@ void lsh_space_free(void *p);
#ifdef DEBUG_ALLOC
void lsh_object_check(void *m, size_t size);
void lsh_object_check_subtype(void *m, size_t size);
struct lsh_object *lsh_object_check(struct lsh_object *instance,
struct lsh_class *class);
struct lsh_object *lsh_object_check_subtype(struct lsh_object *instance,
struct lsh_class *class);
#if 0
#define MDEBUG(x) lsh_object_check((x), sizeof(*(x)))
#define MDEBUG_SUBTYPE(x) lsh_object_check_subtype((x), sizeof(*(x)))
#define CHECK_TYPE(c, i) lsh_object_check((c), (struct lsh_object *) (i))
#define CHECK_SUBTYPE(c, i) \
lsh_object_check_subtype((c), (struct lsh_object *) (i))
#endif
#define CAST(class, var, o) \
struct class *(var) = (struct class *) \
(lsh_check_object(class##_class, (struct lsh_object *) (o))
#define CAST_SUBTYPE(class, var, o) \
struct class *(var) = (struct class *) \
(lsh_check_object_subtype(class##_class, (struct lsh_object *) (o))
#else /* !DEBUG_ALLOC */
#if 0
#define MDEBUG(x)
#define MDEBUG_SUBTYPE(x)
#endif
#define CAST(class, var, o) \
struct class *(var) = (struct class *) (o)
#define CAST_SUBTYPE(class, var, o) CAST(class, var, o)
#endif /* !DEBUG_ALLOC */
#define NEW(x) ((x) = lsh_object_alloc(sizeof(*(x))))
#define NEW(class, var) struct (class) * (var) = lsh_object_alloc(class##_class)
#define NEW_SPACE(x) ((x) = lsh_space_alloc(sizeof(*(x))))
#include "gc.h"
#define KILL(x) gc_kill((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