Skip to content

Commit

Permalink
Refactor allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoNex committed Feb 10, 2024
1 parent 7d34193 commit aef5583
Show file tree
Hide file tree
Showing 20 changed files with 86 additions and 94 deletions.
2 changes: 1 addition & 1 deletion internal/obj/boolean.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ struct object parse_bool(uint32_t b) {
}

char *boolean_str(struct object o) {
return o.data.i ? strdup("true") : strdup("false");
return o.data.i ? GC_STRDUP("true") : GC_STRDUP("false");
}
18 changes: 9 additions & 9 deletions internal/obj/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static struct object input_b(struct object *args, size_t len) {

do {
tmp = getchar();
char *reinput = realloc(input, ilen + 1);
char *reinput = GC_REALLOC(input, ilen + 1);
if (reinput == NULL) {
return errorf("input: error allocating memory");
}
Expand Down Expand Up @@ -107,7 +107,7 @@ static struct object error_b(struct object *args, size_t len) {
} else if (args[0].type != obj_string) {
return errorf("error: argument must be a string, got %s", otype_str(args[0].type));
}
return new_error_obj(strdup(args[0].data.str->str), args[0].data.str->len);
return new_error_obj(GC_STRDUP(args[0].data.str->str), args[0].data.str->len);
}

static struct object type_b(struct object *args, size_t len) {
Expand All @@ -116,7 +116,7 @@ static struct object type_b(struct object *args, size_t len) {
}

char *s = otype_str(args[0].type);
return new_string_obj(strdup(s), strlen(s));
return new_string_obj(GC_STRDUP(s), strlen(s));
}

static struct object int_b(struct object *args, size_t len) {
Expand Down Expand Up @@ -267,7 +267,7 @@ static struct object append_b(struct object *args, size_t len) {
// and we copy all the old objects to the new list.
size_t llen = 0;
size_t cap = pow(2, ceil(log2(old->cap + (len - 1))));
struct object *l = malloc(sizeof(struct object) * cap);
struct object *l = GC_MALLOC(sizeof(struct object) * cap);

// Copy the objects in the old list to the new one.
for (size_t i = 0; i < old->len; i++) {
Expand Down Expand Up @@ -384,7 +384,7 @@ static struct object hex_b(struct object *args, size_t len) {
return errorf("hex: first argument must be int, got %s instead", otype_str(args[0].type));
}

char *s = calloc(30, sizeof(char));
char *s = GC_CALLOC(30, sizeof(char));
#ifdef __unix__
sprintf(s, "0x%lx", args[0].data.i);
#else
Expand All @@ -402,7 +402,7 @@ static struct object oct_b(struct object *args, size_t len) {
return errorf("oct: first argument must be int, got %s instead", otype_str(args[0].type));
}

char *s = calloc(30, sizeof(char));
char *s = GC_CALLOC(30, sizeof(char));
#ifdef __unix__
sprintf(s, "0o%lo", args[0].data.i);
#else
Expand All @@ -420,7 +420,7 @@ static struct object bin_b(struct object *args, size_t len) {
return errorf("bin: first argument must be int, got %s instead", otype_str(args[0].type));
}

char *s = calloc(67, sizeof(char));
char *s = GC_CALLOC(67, sizeof(char));
s[0] = '0';
s[1] = 'b';
int idx = 2;
Expand Down Expand Up @@ -464,7 +464,7 @@ static struct object slice_b(struct object *args, size_t len) {
if (end > args[0].data.str->len) {
return errorf("slice: string bounds out of range %d with capacity %lu", end, args[0].data.list->len);
} else if (start == end) {
return new_string_obj(strdup(""), 0);
return new_string_obj(GC_STRDUP(""), 0);
}
return new_string_obj(&args[0].data.str->str[start], end-start);
}
Expand Down Expand Up @@ -523,7 +523,7 @@ static struct object bytes_b(struct object *args, size_t len) {
case obj_list: {
size_t len = arg.data.list->len;
struct object *list = arg.data.list->list;
uint8_t *b = malloc(sizeof(uint8_t) * len);
uint8_t *b = GC_MALLOC(sizeof(uint8_t) * len);

for (uint32_t i = 0; i < len; i++) {
if (list[i].type != obj_integer) {
Expand Down
6 changes: 3 additions & 3 deletions internal/obj/bytes.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

char *bytes_str(struct object o) {
size_t slen = o.data.bytes->len * 5 + 3;
char *s = calloc(slen, sizeof(char));
char *s = GC_CALLOC(slen, sizeof(char));
s[0] = '[';

char tmp[4] = {'\0'};
Expand All @@ -21,7 +21,7 @@ char *bytes_str(struct object o) {
}

struct object new_bytes_obj(uint8_t *bytes, size_t len) {
struct bytes *b = malloc(sizeof(struct bytes));
struct bytes *b = GC_MALLOC(sizeof(struct bytes));
b->bytes = bytes;
b->len = len;

Expand All @@ -32,7 +32,7 @@ struct object new_bytes_obj(uint8_t *bytes, size_t len) {
}

struct object new_bytes_slice(uint8_t *bytes, size_t len) {
struct bytes *b = malloc(sizeof(struct bytes));
struct bytes *b = GC_MALLOC(sizeof(struct bytes));
b->bytes = bytes;
b->len = len;

Expand Down
4 changes: 2 additions & 2 deletions internal/obj/closure.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
#include "../vm/gc.h"

char *closure_str(struct object o) {
char *str = calloc(35, sizeof(char));
char *str = GC_CALLOC(35, sizeof(char));
sprintf(str, "closure[%p]", o.data.cl->fn);

return str;
}

struct object new_closure_obj(struct function *fn, struct object *free, size_t num_free) {
struct closure *cl = malloc(sizeof(struct closure));
struct closure *cl = GC_MALLOC(sizeof(struct closure));
cl->fn = fn;
cl->free = free;
cl->num_free = num_free;
Expand Down
6 changes: 3 additions & 3 deletions internal/obj/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
#include "../vm/gc.h"

char *error_str(struct object o) {
return strdup(o.data.str->str);
return GC_STRDUP(o.data.str->str);
}

struct object new_error_obj(char *str, size_t len) {
struct string *s = malloc(sizeof(struct string));
struct string *s = GC_MALLOC(sizeof(struct string));
s->str = str;
s->len = len;

Expand All @@ -20,7 +20,7 @@ struct object new_error_obj(char *str, size_t len) {
}

inline struct object errorf(char *fmt, ...) {
char *msg = malloc(sizeof(char) * 256);
char *msg = GC_MALLOC(sizeof(char) * 256);
msg[255] = '\0';

va_list ap;
Expand Down
2 changes: 1 addition & 1 deletion internal/obj/float.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "../vm/gc.h"

char *float_str(struct object o) {
char *str = calloc(35, sizeof(char));
char *str = GC_CALLOC(35, sizeof(char));
sprintf(str, "%f", o.data.f);
return str;
}
Expand Down
4 changes: 2 additions & 2 deletions internal/obj/function.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
#include "../vm/gc.h"

char *function_str(struct object o) {
char *str = calloc(35, sizeof(char));
char *str = GC_CALLOC(35, sizeof(char));
sprintf(str, "closure[%p]", o.data.fn);

return str;
}

inline struct function *new_function(uint8_t *insts, size_t len, uint32_t num_locals, uint32_t num_params, struct bookmark *bmarks, uint32_t bklen) {
struct function *fn = malloc(sizeof(struct function));
struct function *fn = GC_MALLOC(sizeof(struct function));
fn->instructions = insts;
fn->len = len;
fn->num_locals = num_locals;
Expand Down
2 changes: 1 addition & 1 deletion internal/obj/integer.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "../vm/gc.h"

char *integer_str(struct object o) {
char *str = calloc(64, sizeof(char));
char *str = GC_CALLOC(64, sizeof(char));
sprintf(str, "%" PRId64, o.data.i);

return str;
Expand Down
18 changes: 9 additions & 9 deletions internal/obj/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ char *list_str(struct object o) {
string_len += i < len-1 ? strlen(s) + 2 : strlen(s);
}

char *str = calloc(string_len, sizeof(char));
char *str = GC_CALLOC(string_len, sizeof(char));
str[0] = '[';

for (int i = 0; i < len; i++) {
Expand All @@ -27,7 +27,7 @@ char *list_str(struct object o) {
}

inline struct object new_list_obj(struct object *list, size_t len) {
struct list *l = malloc(sizeof(struct list));
struct list *l = GC_MALLOC(sizeof(struct list));
l->list = list;
l->len = len;
l->cap = len;
Expand All @@ -39,7 +39,7 @@ inline struct object new_list_obj(struct object *list, size_t len) {
}

struct object new_list_obj_data(struct object *list, size_t len, size_t cap) {
struct list *l = malloc(sizeof(struct list));
struct list *l = GC_MALLOC(sizeof(struct list));
l->list = list;
l->len = len;
l->cap = cap;
Expand All @@ -51,7 +51,7 @@ struct object new_list_obj_data(struct object *list, size_t len, size_t cap) {
}

struct object new_list_slice(struct object *list, size_t len, uint32_t *m_parent) {
struct list *l = malloc(sizeof(struct list));
struct list *l = GC_MALLOC(sizeof(struct list));
l->list = list;
l->len = len;
l->cap = len;
Expand All @@ -64,7 +64,7 @@ struct object new_list_slice(struct object *list, size_t len, uint32_t *m_parent

inline struct list list_copy(struct list l) {
struct list ret = {
.list = malloc(sizeof(struct object) * l.cap),
.list = GC_MALLOC(sizeof(struct object) * l.cap),
.cap = l.cap,
.len = l.len
};
Expand All @@ -75,7 +75,7 @@ inline struct list list_copy(struct list l) {

inline struct list new_list(size_t cap) {
return (struct list) {
.list = malloc(sizeof(struct object) * cap),
.list = GC_MALLOC(sizeof(struct object) * cap),
.cap = cap,
.len = 0
};
Expand All @@ -85,15 +85,15 @@ inline void list_insert(struct list *l, struct object o, size_t idx) {
if (idx >= l->cap) {
if (l->cap == 0) l->cap = 1;
while (l->cap <= idx) l->cap *= 2;
l->list = realloc(l->list, sizeof(struct object) * l->cap);
l->list = GC_REALLOC(l->list, sizeof(struct object) * l->cap);
}
l->list[idx] = o;
l->len++;
}

struct object make_list(size_t cap) {
struct list *l = malloc(sizeof(struct list));
l->list = calloc(cap, sizeof(struct object));
struct list *l = GC_MALLOC(sizeof(struct list));
l->list = GC_CALLOC(cap, sizeof(struct object));
l->len = 0;
l->cap = cap;

Expand Down
8 changes: 3 additions & 5 deletions internal/obj/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static inline struct map_pair _map_get(struct map_node * restrict n, struct key_

static inline void _map_set(struct map_node **n, struct key_hash k, struct map_pair v) {
if (*n == NULL) {
struct map_node *tmp = malloc(sizeof(struct map_node));
struct map_node *tmp = GC_MALLOC(sizeof(struct map_node));
tmp->key = k;
tmp->val = v;
tmp->l = NULL;
Expand Down Expand Up @@ -101,7 +101,6 @@ static inline void map_set_node(struct map_node **root, struct map_node **cur, s
struct map_node *l = (*cur)->l;
struct map_node *r = (*cur)->r;

free(*cur);
*cur = n;
if (l != NULL) map_set_node(root, root, l);
if (r != NULL) map_set_node(root, root, r);
Expand All @@ -121,7 +120,6 @@ static inline void _map_delete(struct map_node **root, struct map_node **n, stru
*n = NULL;
if (node->l) map_set_node(root, root, node->l);
if (node->r) map_set_node(root, root, node->r);
free(node);
} else if (cmp < 0) {
_map_delete(root, &(*n)->l, k);
} else {
Expand Down Expand Up @@ -164,7 +162,7 @@ void map_delete(struct object map, struct object key) {

// TODO: actually return map content as string.
char *map_str(struct object map) {
char *str = malloc(sizeof(char) * 64);
char *str = GC_MALLOC(sizeof(char) * 64);
str[63] = '\0';
sprintf(str, "map[%p]", map.data.map->root);

Expand All @@ -173,7 +171,7 @@ char *map_str(struct object map) {

struct object new_map() {
return (struct object) {
.data.map = calloc(1, sizeof(struct map_node)),
.data.map = GC_CALLOC(1, sizeof(struct map_node)),
.type = obj_map,
};
}
8 changes: 4 additions & 4 deletions internal/obj/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ static void _object_to_module(struct object mod, struct object_node * restrict n

static inline void _object_set(struct object_node **n, uint64_t key, char *name, struct object val) {
if (*n == NULL) {
*n = malloc(sizeof(struct object_node));
(*n)->name = strdup(name);
*n = GC_MALLOC(sizeof(struct object_node));
(*n)->name = GC_STRDUP(name);
(*n)->key = key;
(*n)->val = val;
(*n)->l = NULL;
Expand Down Expand Up @@ -67,7 +67,7 @@ struct object object_set(struct object obj, char *name, struct object val) {

// TODO: actually return object content as string.
char *object_obj_str(struct object obj) {
char *str = malloc(sizeof(char) * 64);
char *str = GC_MALLOC(sizeof(char) * 64);
str[63] = '\0';
sprintf(str, "object[%p]", *obj.data.obj);

Expand All @@ -76,7 +76,7 @@ char *object_obj_str(struct object obj) {

struct object new_object() {
return (struct object) {
.data.obj = calloc(1, sizeof(struct object_node)),
.data.obj = GC_CALLOC(1, sizeof(struct object_node)),
.type = obj_object,
};
}
Expand Down
4 changes: 3 additions & 1 deletion internal/obj/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ func (o Object) TypeString() string {
}

func (o Object) String() string {
return C.GoString(C.object_str(o))
cs := C.go_object_str(o)
defer C.free(unsafe.Pointer(cs))
return C.GoString(cs)
}

func (o Object) Int() int64 {
Expand Down
1 change: 1 addition & 0 deletions internal/obj/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ struct object new_builtin_obj(struct object (*builtin)(struct object *args, size
// Util functions.
char *otype_str(enum obj_type t);
char *object_str(struct object o);
char *go_object_str(struct object o);
void print_obj(struct object o);
struct object errorf(char *fmt, ...);
uint64_t fnv64a(char *s);
10 changes: 5 additions & 5 deletions internal/obj/pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int pipe_send(struct object pipe, struct object o) {
} else {
if (p->len == p->cap) {
p->cap *= 2;
p->buf = realloc(p->buf, p->cap * sizeof(struct object));
p->buf = GC_REALLOC(p->buf, p->cap * sizeof(struct object));
}
}
p->buf[p->tail] = o;
Expand Down Expand Up @@ -69,8 +69,8 @@ struct object pipe_recv(struct object pipe) {
}

struct object new_pipe() {
struct pipe *pipe = malloc(sizeof(struct pipe));
pipe->buf = calloc(1, sizeof(struct object));
struct pipe *pipe = GC_MALLOC(sizeof(struct pipe));
pipe->buf = GC_CALLOC(1, sizeof(struct object));
pipe->cap = 1;
pipe->len = 0;
pipe->head = 0;
Expand All @@ -87,8 +87,8 @@ struct object new_pipe() {
}

struct object new_buffered_pipe(size_t size) {
struct pipe *pipe = malloc(sizeof(struct pipe));
pipe->buf = calloc(size, sizeof(struct object));
struct pipe *pipe = GC_MALLOC(sizeof(struct pipe));
pipe->buf = GC_CALLOC(size, sizeof(struct object));
pipe->cap = size;
pipe->len = 0;
pipe->head = 0;
Expand Down
Loading

0 comments on commit aef5583

Please sign in to comment.