From ec1c10f56141c6e1fd02f13f41fb3c5191bb418a Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Mon, 18 Apr 2011 22:36:07 -0700 Subject: [PATCH] glib-ized --- env.c | 4 +--- int.c | 3 ++- lake.c | 11 +++++------ list.c | 15 ++++++++------- parse.c | 15 ++++++++------- string.c | 14 +++++++------- sym.c | 8 ++++---- 7 files changed, 35 insertions(+), 35 deletions(-) diff --git a/env.c b/env.c index 614f0fd..1ef9492 100644 --- a/env.c +++ b/env.c @@ -31,9 +31,7 @@ Env *shared_env(void) Env *env_make(Env *parent) { - Env *env; - env = malloc(sizeof(Env)); - if (!env) oom(); + Env *env = g_malloc(sizeof(Env)); env->parent = parent; env->bindings = g_hash_table_new(g_str_hash, g_str_equal); env->symbols = g_hash_table_new(g_str_hash, g_str_equal); diff --git a/int.c b/int.c index 3583871..6a8ebc0 100644 --- a/int.c +++ b/int.c @@ -7,6 +7,7 @@ * */ +#include #include #include #include "bool.h" @@ -14,7 +15,7 @@ static LakeInt *int_alloc(void) { - LakeInt *i = malloc(sizeof(LakeInt)); + LakeInt *i = g_malloc(sizeof(LakeInt)); i->base.type = TYPE_INT; i->base.size = sizeof(LakeInt); return i; diff --git a/lake.c b/lake.c index a800acc..cec6194 100644 --- a/lake.c +++ b/lake.c @@ -10,6 +10,7 @@ * */ +#include #include #include #include @@ -103,7 +104,7 @@ char *repr(LakeVal *expr) switch (expr->type) { case TYPE_NIL: - s = strdup("nil"); + s = g_strdup("nil"); break; case TYPE_SYM: @@ -112,7 +113,7 @@ char *repr(LakeVal *expr) case TYPE_BOOL: printf("%s:%d TODO: repr for bools", __FILE__, __LINE__); - s = strdup("[bool]"); + s = g_strdup("[bool]"); break; case TYPE_INT: @@ -122,9 +123,7 @@ char *repr(LakeVal *expr) break; case TYPE_STR: - str = STR(expr); - s = malloc(STR_N(str) + 3); - sprintf(s, "\"%s\"", STR_S(str)); + s = g_strdup_printf("\"%s\"", STR_S(STR(expr))); break; case TYPE_LIST: @@ -133,7 +132,7 @@ char *repr(LakeVal *expr) default: printf("unrecognized value, type %d, size %Zu bytes", expr->type, expr->size); - s = strdup("unrecognized value"); + s = g_strdup("unrecognized value"); } return s; diff --git a/list.c b/list.c index c26292d..c1e44fd 100644 --- a/list.c +++ b/list.c @@ -7,6 +7,7 @@ * */ +#include #include #include #include "int.h" @@ -18,7 +19,7 @@ static LakeList *list_alloc(void) { - LakeList *list = malloc(sizeof(LakeList)); + LakeList *list = g_malloc(sizeof(LakeList)); list->base.type = TYPE_LIST; list->base.size = sizeof(LakeList); return list; @@ -39,7 +40,7 @@ LakeList *list_make_with_cap(size_t cap) LakeList *list = list_alloc(); list->cap = cap; list->n = 0; - list->vals = malloc(cap * sizeof(LakeVal *)); + list->vals = g_malloc(cap * sizeof(LakeVal *)); return list; } @@ -62,12 +63,12 @@ NILP list_grow(LakeList *list) { list->cap *= 2; LakeVal **new_vals; - new_vals = malloc(list->cap * sizeof(LakeVal *)); + new_vals = g_malloc(list->cap * sizeof(LakeVal *)); int i; for (i = 0; i < list->n; ++i) { new_vals[i] = list->vals[i]; } - free(list->vals); + g_free(list->vals); list->vals = new_vals; return NIL; } @@ -116,7 +117,7 @@ LakeStr *list_to_str(LakeList *list) { char *s = list_repr(list); LakeStr *str = str_from_c(s); - free(s); + g_free(s); return str; } @@ -133,10 +134,10 @@ char *list_repr(LakeList *list) len = strlen(s2); memcpy(s + n, s2, len); n += len; - free(s2); + g_free(s2); if (i != LIST_N(list) - 1) s[n++] = ' '; } s[n++] = ')'; s[n] = '\0'; - return strdup(s); + return g_strdup(s); } diff --git a/parse.c b/parse.c index 03758e9..2cfc928 100644 --- a/parse.c +++ b/parse.c @@ -7,6 +7,7 @@ * */ +#include #include #include #include @@ -68,7 +69,7 @@ static char ch(Ctx *ctx, char expected) consume1(ctx); return c; } - char *msg = malloc(64); + char *msg = g_malloc(64); sprintf(msg, "parse error, expected '%c' got '%c'", expected, c); die(msg); return 0; /* placate the compiler */ @@ -113,7 +114,7 @@ static char *parse_while(Ctx *ctx, int (*is_valid)(char)) { size_t n = 8; size_t i = 0; - char *s = malloc(n); + char *s = g_malloc(n); char c; while ((c = peek(ctx)) != PARSE_EOF && is_valid(c)) { s[i++] = c; @@ -121,7 +122,7 @@ static char *parse_while(Ctx *ctx, int (*is_valid)(char)) /* grow if necessary */ if (i >= n) { n *= 2; - char *t = realloc(s, n); + char *t = g_realloc(s, n); if (!t) oom(); s = t; } @@ -194,7 +195,7 @@ static LakeVal *parse_str(Ctx *ctx) { size_t n = 8; size_t i = 0; - char *s = malloc(n); + char *s = g_malloc(n); char c; ch(ctx, '"'); while ((c = peek(ctx)) != PARSE_EOF && c != '"') { @@ -209,7 +210,7 @@ static LakeVal *parse_str(Ctx *ctx) /* grow if necessary */ if (i >= n) { n *= 2; - char *t = realloc(s, n); + char *t = g_realloc(s, n); if (!t) oom(); s = t; } @@ -217,7 +218,7 @@ static LakeVal *parse_str(Ctx *ctx) s[i] = '\0'; ch(ctx, '"'); LakeStr *str = str_from_c(s); - free(s); + g_free(s); return VAL(str); } @@ -244,7 +245,7 @@ static int is_not_newline(char c) static void parse_comment(Ctx *ctx) { - free(parse_while(ctx, is_not_newline)); + g_free(parse_while(ctx, is_not_newline)); } static LakeVal *_parse_expr(Ctx *ctx) diff --git a/string.c b/string.c index 847d772..3826ed6 100644 --- a/string.c +++ b/string.c @@ -16,7 +16,7 @@ static LakeStr *str_alloc(void) { - LakeStr *str = malloc(sizeof(LakeStr)); + LakeStr *str = g_malloc(sizeof(LakeStr)); str->base.type = TYPE_STR; str->base.size = sizeof(LakeStr); return str; @@ -24,14 +24,14 @@ static LakeStr *str_alloc(void) void str_free(LakeStr *str) { - free(str->s); - free(str); + g_free(str->s); + g_free(str); } NILP str_set(LakeStr *str, char *s) { str->n = strlen(s); - str->s = strdup(s); + str->s = g_strdup(s); return NIL; } @@ -59,17 +59,17 @@ LakeStr *str_copy(LakeStr *str) char *str_val(LakeStr *str) { - return strdup(str->s); + return g_strdup(str->s); } LakeInt *str_cmp(LakeStr *a, LakeStr *b) { - return int_from_c(strncmp(a->s, b->s, MIN(a->n, b->n))); + return int_from_c(g_strcmp0(a->s, b->s)); } LakeSym *str_eq(LakeStr *a, LakeStr *b) { - return bool_from_int(strncmp(a->s, b->s, MIN(a->n, b->n)) == 0); + return bool_from_int(g_strcmp0(a->s, b->s) == 0); } LakeStr *str_to_str(LakeStr *str) diff --git a/sym.c b/sym.c index 992ee55..65ce10f 100644 --- a/sym.c +++ b/sym.c @@ -20,7 +20,7 @@ static LakeSym *sym_alloc(void) { - LakeSym *sym = malloc(sizeof(LakeSym)); + LakeSym *sym = g_malloc(sizeof(LakeSym)); sym->base.type = TYPE_SYM; sym->base.size = sizeof(LakeSym); return sym; @@ -33,7 +33,7 @@ LakeSym *sym_intern(char *s) if (!sym) { sym = sym_alloc(); sym->n = strlen(s); - sym->s = strdup(s); + sym->s = g_strdup(s); sym->hash = g_str_hash(s); g_hash_table_insert(symbols, sym->s, sym); } @@ -52,7 +52,7 @@ LakeSym *sym_from_str(LakeStr *str) char *sym_repr(LakeSym *sym) { - return strdup(sym->s); + return g_strdup(sym->s); } unsigned long sym_val(LakeSym *sym) @@ -62,5 +62,5 @@ unsigned long sym_val(LakeSym *sym) LakeSym *sym_eq(LakeSym *a, LakeSym *b) { - return bool_from_int(strncmp(a->s, b->s, MIN(a->n, b->n)) == 0); + return bool_from_int(g_strcmp0(a->s, b->s) == 0); }