Skip to content

Commit

Permalink
Wrote a generic object cleanup function.
Browse files Browse the repository at this point in the history
Instead of having global cleanup functions for each type of object, now
there is a single rs_object_release() function that takes care of
releasing any "extra" resources used by an object (strings, files, etc).

Eventually rs_object_release() will be made static, and the garbage
collector will call it just before it frees an object. For now, the
function is global, and it also takes care of freeing the object itself.
  • Loading branch information
ingramj committed Aug 10, 2011
1 parent 439c999 commit ef5de22
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
18 changes: 16 additions & 2 deletions object.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ static struct rs_hobject *rs_alloc_obj(void);
static void rs_free_obj(struct rs_hobject *obj);


static void rs_symbol_release(rs_symbol *sym);

void rs_object_release(rs_object obj)
{
if (rs_immediate_p(obj)) return;

if (rs_symbol_p(obj)) {
rs_symbol_release(rs_obj_to_symbol(obj));
} else {
rs_fatal("unknown object type");
}
rs_free_obj((struct rs_hobject*)obj);
}


rs_object rs_symbol_create(const char *name)
{
assert(name != NULL);
Expand All @@ -38,14 +53,13 @@ rs_object rs_symbol_create(const char *name)
}


void rs_symbol_release(rs_symbol *sym)
static void rs_symbol_release(rs_symbol *sym)
{
assert(sym != NULL);
assert(sym->val.sym != NULL);
assert(rs_symbol_p((rs_object)sym));

free(sym->val.sym);
rs_free_obj(sym);
}


Expand Down
4 changes: 1 addition & 3 deletions rescheme.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ int main(void)
rs_write(stdout, obj);
printf("\n");

if (rs_symbol_p(obj)) {
rs_symbol_release(rs_obj_to_symbol(obj));
}
rs_object_release(obj);
}
return 0;
}
13 changes: 5 additions & 8 deletions rescheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,18 @@ static inline int rs_null_p(rs_object obj);
static inline int rs_eof_p(rs_object obj);


/* Symbols, strings, lists, etc. are all heap objects. They have two additional
functions:
/* Symbols, strings, lists, etc. are all heap objects. They have an additional
function:
* rs_object rs_X_create(...)
Allocate and initialize an rs_X. The arguments depend on the type: a
const char* for rs_symbol or rs_string, for example.
* void rs_X_release(rs_X *obj)
Perform any cleanup that is required when an rs_X is no longer needed,
such as freeing memory, or closing a file. Eventually there will be a
garbage collector that will call this function whenever it collects an
object. For now, it must be called manually.
*/

struct rs_hobject;

/* Perform any type-specific cleanup required for obj. */
void rs_object_release(rs_object obj);


/** Symbols **/
typedef struct rs_hobject rs_symbol;
Expand All @@ -88,7 +86,6 @@ static inline int rs_symbol_p(rs_object obj);
static inline rs_object rs_symbol_to_obj(rs_symbol *sym);
static inline rs_symbol *rs_obj_to_symbol(rs_object obj);
rs_object rs_symbol_create(const char *name);
void rs_symbol_release(rs_symbol *sym);

/* Get the C string representation of sym. */
static inline const char *rs_symbol_cstr(rs_symbol *sym);
Expand Down

0 comments on commit ef5de22

Please sign in to comment.