Commit fb859880 authored by Niels Möller's avatar Niels Möller

Started on gc.

Rev: src/io.c:1.29
Rev: src/lsh_types.h:1.18
Rev: src/service.h:1.8
Rev: src/xalloc.c:1.10
Rev: src/xalloc.h:1.10
parent 95541e01
...@@ -61,10 +61,10 @@ int io_iter(struct io_backend *b) ...@@ -61,10 +61,10 @@ int io_iter(struct io_backend *b)
* and unlinks any fd:s that should be closed, and also counts how * and unlinks any fd:s that should be closed, and also counts how
* many fd:s there are. */ * many fd:s there are. */
struct lsh_fd **_fd; struct lsh_fd **fd_p;
struct lsh_fd *fd; struct lsh_fd *fd;
for(_fd = &b->files; (fd = *_fd); ) for(fd_p = &b->files; (fd = *fd_p); )
{ {
if (!fd->close_now && fd->prepare) if (!fd->close_now && fd->prepare)
PREPARE_FD(fd); PREPARE_FD(fd);
...@@ -91,11 +91,11 @@ int io_iter(struct io_backend *b) ...@@ -91,11 +91,11 @@ int io_iter(struct io_backend *b)
close(fd->fd); close(fd->fd);
} }
/* Unlink this fd */ /* Unlink this fd */
*_fd = fd->next; *fd_p = fd->next;
continue; continue;
} }
nfds++; nfds++;
_fd = &fd->next; fd_p = &fd->next;
} }
} }
......
...@@ -66,14 +66,41 @@ do { \ ...@@ -66,14 +66,41 @@ do { \
#define LSH_ALLOC_STATIC 1 #define LSH_ALLOC_STATIC 1
#define LSH_ALLOC_STACK 2 #define LSH_ALLOC_STACK 2
struct lsh_class;
struct lsh_object
{
/* Objects are chained together, for the sweep phase of the gc. */
struct lsh_object *next;
struct lsh_class *isa;
char alloc_method;
char marked;
};
struct lsh_class
{
struct lsh_object *super;
struct lsh_class *super_class;
size_t size;
struct lsh_object *(*mark_instance)(struct lsh_object *instance,
void (*mark)(struct lsh_object *o));
void (*free_instance)(struct lsh_object *instance);
/* Particular classes may add their own methods here */
};
#ifdef DEBUG_ALLOC #ifdef DEBUG_ALLOC
#if 0
struct lsh_object struct lsh_object
{ {
int size; /* Zero for objects that are not allocated on the heap. */ int size; /* Zero for objects that are not allocated on the heap. */
char alloc_method; char alloc_method;
char marked; char marked;
}; };
#endif
struct lsh_string_header struct lsh_string_header
{ {
......
...@@ -31,12 +31,23 @@ ...@@ -31,12 +31,23 @@
* in a SSH_MSG_SERVICE_REQUEST or SSH_MSG_USERAUTH_REQUEST) and for * in a SSH_MSG_SERVICE_REQUEST or SSH_MSG_USERAUTH_REQUEST) and for
* any other stuff that needs initialization at some later time. */ * any other stuff that needs initialization at some later time. */
#include "service.h.x"
#if 0
struct ssh_service struct ssh_service
{ {
struct lsh_object header; struct lsh_object header;
int (*init)(struct ssh_service *self, struct ssh_connection *c); int (*init)(struct ssh_service *self, struct ssh_connection *c);
}; };
#endif
/* CLASS:
((name ssh_service)
(vars
(method
int "(*init)(struct ssh_service *self, struct ssh_connection *c)")))
*/
#define SERVICE_INIT(s, c) ((s)->init((s), (c))) #define SERVICE_INIT(s, c) ((s)->init((s), (c)))
......
...@@ -116,6 +116,30 @@ void lsh_string_free(struct lsh_string *s) ...@@ -116,6 +116,30 @@ void lsh_string_free(struct lsh_string *s)
lsh_free(s); lsh_free(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;
gc_register(o);
return o;
}
/* Should be called *only* by the gc */
void lsh_object_free(struct lsh_object *o)
{
if (o->alloc_method != LSH_ALLOC_HEAP)
fatal("lsh_object_free: Object not allocated on the heap!\n");
if (o->isa->free_instance)
o->isa->free_instance(o);
lsh_free(o);
};
#if 0
void *lsh_object_alloc(size_t size) void *lsh_object_alloc(size_t size)
{ {
struct lsh_object *self = xalloc(size); struct lsh_object *self = xalloc(size);
...@@ -133,43 +157,38 @@ void lsh_object_free(void *p) ...@@ -133,43 +157,38 @@ void lsh_object_free(void *p)
fatal("lsh_object_free: Object not allocated on the heap!\n"); fatal("lsh_object_free: Object not allocated on the heap!\n");
lsh_free(p); lsh_free(p);
}; };
#endif
#ifdef DEBUG_ALLOC #ifdef DEBUG_ALLOC
void lsh_object_check(void *p, size_t size) void lsh_object_check(struct lsh_object *instance, struct lsh_class *class)
{ {
struct lsh_object *self = (struct lsh_object *) p; if (instance->marked)
fatal("lsh_object_check: Unexpected marked object!\n");
switch(self->alloc_method) if (instance->dead)
{ fatal("lsh_object_check: Reference to dead object!\n");
case LSH_ALLOC_HEAP:
if (self->size != size) if (instance->class != class)
fatal("Type error!\n"); fatal("lsh_object_check: 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) void lsh_object_check_subtype(struct lsh_object *instance,
struct lsh_class *class)
{ {
struct lsh_object *self = (struct lsh_object *) p; struct lsh_class *type;
switch(self->alloc_method)
{ if (instance->marked)
case LSH_ALLOC_HEAP: fatal("lsh_object_check: Unexpected marked object!\n");
if (self->size < size)
fatal("Type error!\n"); if (instance->dead)
break; fatal("lsh_object_check: Reference to dead object!\n");
case LSH_ALLOC_STATIC:
case LSH_ALLOC_STACK:
break;
default:
fatal("Type error!\n");
}
}
for (type = instance->class; type; type=type->super_class)
if (type == class)
return;
fatal("lsh_object_check_subtype: Unexpected marked object!\n");
}
#endif /* DEBUG_ALLOC */ #endif /* DEBUG_ALLOC */
#ifdef DEBUG_ALLOC #ifdef DEBUG_ALLOC
......
...@@ -29,10 +29,6 @@ ...@@ -29,10 +29,6 @@
#include "lsh_types.h" #include "lsh_types.h"
#include <stdlib.h> #include <stdlib.h>
#if 0
void *xalloc(size_t size);
#endif
/* Allocation */ /* Allocation */
/* The memory allocation model (for strings) is as follows: /* The memory allocation model (for strings) is as follows:
......
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