summaryrefslogtreecommitdiff
path: root/scope.c
diff options
context:
space:
mode:
authorCarson Fleming <cflems@cflems.net>2026-08-04 22:51:29 -0400
committerCarson Fleming <cflems@cflems.net>2026-08-04 22:51:29 -0400
commitebe7f44385c4be54afd2993d54ce6352f612f9fa (patch)
tree008ee208ada0d7eb468ba6e74753872fc56d7d33 /scope.c
parentb1ff7e8bf24b40cf64127a6194d45661db3a4baf (diff)
downloadccc-ebe7f44385c4be54afd2993d54ce6352f612f9fa.tar.gz
add string literals via the data segment
Diffstat (limited to 'scope.c')
-rw-r--r--scope.c36
1 files changed, 11 insertions, 25 deletions
diff --git a/scope.c b/scope.c
index 3f3dc44..7077748 100644
--- a/scope.c
+++ b/scope.c
@@ -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;
}