Skip to content

Commit

Permalink
defines new allocator for custom c-level objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremytregunna committed May 15, 2012
1 parent 9fd5d00 commit 98d55bd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion wee/message.c
Expand Up @@ -28,7 +28,7 @@

msg_t* msg_new(const char* name, list_t* arguments, msg_t* next)
{
msg_t* msg = malloc(sizeof(*msg));
msg_t* msg = (msg_t*)obj_new_with_size(sizeof(*msg));
memcpy(msg->name, name, strlen(name) + 1);
msg->arguments = arguments;
msg->next = next;
Expand Down
11 changes: 9 additions & 2 deletions wee/object.c
Expand Up @@ -30,10 +30,17 @@
#include "hash.h"
#include "object.h"

#define DEFAULT_SLOTTABLE_SIZE 8

obj_t* obj_new(void)
{
obj_t* obj = malloc(sizeof(*obj));
obj->slots = hash_new(8);
return obj_new_with_size(sizeof(obj_t));
}

obj_t* obj_new_with_size(size_t size)
{
obj_t* obj = malloc(size);
obj->slots = hash_new(DEFAULT_SLOTTABLE_SIZE);
return obj;
}

Expand Down
4 changes: 4 additions & 0 deletions wee/object.h
Expand Up @@ -36,6 +36,10 @@ typedef struct
// Create a new empty object
extern obj_t* obj_new(void);

// Create a new empty object and allocate a given size. Use this when allocating
// objects from custom C-level objects, like msg_t.
extern obj_t* obj_new_with_size(size_t);

// Destroy an object
extern void obj_destroy(obj_t*);

Expand Down

0 comments on commit 98d55bd

Please sign in to comment.