diff options
Diffstat (limited to 'scope.c')
| -rw-r--r-- | scope.c | 36 |
1 files changed, 11 insertions, 25 deletions
@@ -64,12 +64,9 @@ static struct type_alias** type_cell( static void rehash_types(struct scope* scope) { struct type_alias** old_types = scope->types; integral_t old_cap = scope->type_cap; - scope->type_cap *= 2; - scope->types = calloc(scope->type_cap, sizeof(struct type_def*)); - if (scope->types == NULL) { - fprintf(stderr, "ccc: out of memory\n"); - exit(1); - } + + scope->type_cap = (scope->type_cap + 1) << 1; + scope->types = ccc_alloc(scope->type_cap * sizeof(struct type_def*)); for (integral_t i = 0; i < old_cap; i++) { if (old_types[i] == NULL) continue; @@ -81,6 +78,7 @@ static void rehash_types(struct scope* scope) { } *cell = old_types[i]; } + free(old_types); } @@ -102,12 +100,9 @@ static struct var_def** var_cell( static void rehash_vars(struct scope* scope) { struct var_def** old_vars = scope->vars; integral_t old_cap = scope->var_cap; - scope->var_cap *= 2; - scope->vars = calloc(scope->var_cap, sizeof(struct var_def*)); - if (scope->vars == NULL) { - fprintf(stderr, "ccc: out of memory\n"); - exit(1); - } + + scope->var_cap = (scope->var_cap + 1) << 1; + scope->vars = ccc_alloc(scope->var_cap * sizeof(struct var_def*)); for (integral_t i = 0; i < old_cap; i++) { if (old_vars[i] == NULL) continue; @@ -119,11 +114,12 @@ static void rehash_vars(struct scope* scope) { } *cell = old_vars[i]; } + free(old_vars); } void scope_push(struct scope** p_scope) { - struct scope* inner_scope = calloc(1, sizeof(struct scope)); + struct scope* inner_scope = ccc_alloc(sizeof(struct scope)); scope_init(inner_scope); inner_scope->next_out = *p_scope; *p_scope = inner_scope; @@ -160,11 +156,7 @@ const struct type_alias* scope_define_type( /* redefinition leaks memory, so refuse */ if (*cell != NULL) return NULL; - *cell = calloc(1, sizeof(struct type_alias)); - if (*cell == NULL) { - fprintf(stderr, "ccc: out of memory\n"); - exit(1); - } + *cell = ccc_alloc(sizeof(struct type_alias)); **cell = type; return *cell; } @@ -193,13 +185,7 @@ struct var_def* scope_define_var(struct scope* scope, struct var_def var) { /* redefinition leaks memory, so refuse */ if (*cell != NULL) return NULL; - if (*cell == NULL) { - *cell = calloc(1, sizeof(struct var_def)); - if (*cell == NULL) { - fprintf(stderr, "ccc: out of memory\n"); - exit(1); - } - } + if (*cell == NULL) *cell = ccc_alloc(sizeof(struct var_def)); **cell = var; return *cell; } |
