Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,24 @@ int compact_arenas_selective(int phase_mask)
return total_saved;
}

static void free_src_file_map_values(void)
{
if (!SRC_FILE_MAP)
return;

for (int i = 0; i < SRC_FILE_MAP->cap; i++) {
if (!SRC_FILE_MAP->table[i].occupied)
continue;

strbuf_t *src = SRC_FILE_MAP->table[i].val;
if (!src || src == LIBC_SRC)
continue;

strbuf_free(src);
SRC_FILE_MAP->table[i].val = NULL;
}
}

void global_release(void)
{
/* Cleanup lexer hashmaps */
Expand All @@ -1430,6 +1448,7 @@ void global_release(void)
arena_free(TOKEN_ARENA);
arena_free(GENERAL_ARENA); /* free TYPES and PH2_IR_FLATTEN */
hashmap_free(TOKEN_CACHE);
free_src_file_map_values();
hashmap_free(SRC_FILE_MAP);
Comment on lines +1451 to 1452
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about moving hashmap_free() to the new function? That is:

static void free_src_file_map(void)
{
    if (!SRC_FILE_MAP)
        return;

    for (int i = 0; i < SRC_FILE_MAP->cap; i++) {
        if (!SRC_FILE_MAP->table[i].occupied)
            continue;

        strbuf_t *src = SRC_FILE_MAP->table[i].val;
        if (!src || src == LIBC_SRC)
            continue;

        strbuf_free(src);
        SRC_FILE_MAP->table[i].val = NULL;
    }

    hashmap_free(SRC_FILE_MAP);
}

Make this new function release table[i]->val entries and SRC_FILE_MAP simultaneously.

hashmap_free(FUNC_MAP);
hashmap_free(INCLUSION_MAP);
Expand Down
4 changes: 2 additions & 2 deletions src/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ token_stream_t *gen_file_token_stream(char *filename)
error_at("Internal error, expected eof at the end of file",
&cur->location);

tks = malloc(sizeof(token_stream_t));
tks = arena_calloc(TOKEN_ARENA, 1, sizeof(token_stream_t));
tks->head = head.next;
tks->tail = cur;
hashmap_put(TOKEN_CACHE, filename, tks);
Expand Down Expand Up @@ -1014,7 +1014,7 @@ token_stream_t *gen_libc_token_stream()
error_at("Internal error, expected eof at the end of file",
&cur->location);

tks = malloc(sizeof(token_stream_t));
tks = arena_calloc(TOKEN_ARENA, 1, sizeof(token_stream_t));
tks->head = head.next;
tks->tail = cur;
hashmap_put(TOKEN_CACHE, filename, tks);
Expand Down
8 changes: 4 additions & 4 deletions src/preprocessor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1049,25 +1049,25 @@ token_t *preprocess(token_t *tk)
synth_built_in_loc.line = 1;
synth_built_in_loc.filename = "<built-in>";

macro_t *macro = calloc(1, sizeof(macro_t));
macro_t *macro = arena_calloc(TOKEN_ARENA, 1, sizeof(macro_t));
macro->name = "__FILE__";
macro->handler = file_macro_handler;
hashmap_put(MACROS, "__FILE__", macro);

macro = calloc(1, sizeof(macro_t));
macro = arena_calloc(TOKEN_ARENA, 1, sizeof(macro_t));
macro->name = "__LINE__";
macro->handler = line_macro_handler;
hashmap_put(MACROS, "__LINE__", macro);

/* architecture defines */
macro = calloc(1, sizeof(macro_t));
macro = arena_calloc(TOKEN_ARENA, 1, sizeof(macro_t));
macro->name = ARCH_PREDEFINED;
macro->replacement = new_token(T_numeric, &synth_built_in_loc, 1);
macro->replacement->literal = "1";
hashmap_put(MACROS, ARCH_PREDEFINED, macro);

/* shecc run-time defines */
macro = calloc(1, sizeof(macro_t));
macro = arena_calloc(TOKEN_ARENA, 1, sizeof(macro_t));
macro->name = "__SHECC__";
macro->replacement = new_token(T_numeric, &synth_built_in_loc, 1);
macro->replacement->literal = "1";
Expand Down