Implemented arena allocator, except for libraries and eaxhla...

This commit is contained in:
xolatile
2024-11-18 08:53:03 -05:00
parent a5057f18d0
commit 5d7d59143d
6 changed files with 22 additions and 87 deletions

@ -1,69 +1,3 @@
/*
xolatile@bunsenlabs:~/Git/eaxcc$ grep -rn malloc source/*
source/eaxhla.c:276: r = malloc(2 + scl + 1 + nml + 1);
source/eaxhla.l:268: yylval.blobval.data = malloc(sdslen(string_literal_buffer));
xolatile@bunsenlabs:~/Git/eaxcc$ grep -rn calloc source/*
source/arena.h:44: arena->array[arena->count - 1] = calloc(1, sizeof(*arena));
source/arena.h:46: arena->array[arena->count - 1]->buffer = calloc(ARENA_CHUNK, 1);
source/arena.h:57: arena = calloc(1, sizeof(*arena));
source/assembler.c:778: empty_array = calloc(1024, sizeof(*empty_array));
source/assembler.c:779: empty_imbue = calloc(1024, sizeof(*empty_imbue));
source/assembler.c:780: empty_store = calloc(1024, sizeof(*empty_store));
source/compile.c:25: token_array = calloc(1440UL, sizeof(*token_array));
source/compile.c:117: text_sector_byte = calloc(4096UL, sizeof(*text_sector_byte));
source/eaxhla.c:149: undeclared_symbol = (symbol_t *)calloc(sizeof(symbol_t), 1);
source/eaxhla.c:159: r = (symbol_t *)calloc(sizeof(symbol_t), 1);
source/eaxhla.c:384: symbol_t * literal = (symbol_t *)calloc(sizeof(symbol_t), 1);
xolatile@bunsenlabs:~/Git/eaxcc$ grep -rn realloc source/*
source/arena.h:42: arena->array = realloc(arena->array, arena->count * sizeof(*arena->array));
xolatile@bunsenlabs:~/Git/eaxcc$ grep -rn free source/*
source/arena.h:31: free(arena->array[index]->buffer);
source/arena.h:32: free(arena->array[index]);
source/arena.h:35: free(arena->array);
source/arena.h:36: free(arena);
source/assembler.c:810: free(empty_array);
source/assembler.c:811: free(empty_imbue);
source/assembler.c:812: free(empty_store);
source/compile.c:30: free(token_array);
source/compile.c:130: free(text_sector_byte);
source/compile.h:19: * A variadic argument interface would be ideal except theres not free
source/eaxhla.c:143: free(scope);
source/eaxhla.c:165:void free_symbol(void * data) {
source/eaxhla.c:168: free(variable->name);
source/eaxhla.c:172: free(variable->array_value);
source/eaxhla.c:175: free(variable);
source/eaxhla.c:181: free(undeclared_symbol);
source/eaxhla.c:182: tommy_hashtable_foreach(&symbol_table, free_symbol);
source/eaxhla.c:262: free(scope);
source/eaxhla.c:296: free(alternative_name);
source/eaxhla.c:403: free(full_name);
source/eaxhla.c:499: free(varname);
source/eaxhla.c:563: free(msg);
source/eaxhla.c:583: free(msg);
source/eaxhla.h:41:void free_symbol(void * name);
source/eaxhla.l:358:void yyfree_leftovers(void) {
source/eaxhla.l:365: sdsfree(string_literal_buffer);
source/eaxhla.y:21: extern void yyfree_leftovers(void);
source/eaxhla.y:120: free($3);
source/eaxhla.y:142: free($3);
source/eaxhla.y:158: free($3);
source/eaxhla.y:162: free($3);
source/eaxhla.y:166: free($6);
source/eaxhla.y:170: free($6);
source/eaxhla.y:174: free($5);
source/eaxhla.y:200: free($1);
source/eaxhla.y:216: free($2);
source/eaxhla.y:224: free($1);
source/eaxhla.y:233: free($1);
source/eaxhla.y:255: free($1);
source/eaxhla.y:331: | machine_code IDENTIFIER { free($2); }
source/eaxhla.y:336: free($2);
source/eaxhla.y:341: | IDENTIFIER arguments { free($1); }
source/main.c:18: extern void yyfree_leftovers(void);
source/main.c:20: yyfree_leftovers();
xolatile@bunsenlabs:~/Git/eaxcc$
*/
#ifndef ARENA_H
#define ARENA_H
@ -119,7 +53,6 @@ void * aalloc(size_t size) {
if (arena == NULL) {
atexit(arena_deinit);
open_arena = 1;
arena = calloc(1, sizeof(*arena));
arena_reinit();
}

@ -1,5 +1,6 @@
#include "assembler.h"
#include "debug.h"
#include "arena.h"
#include <stdlib.h>
@ -775,9 +776,9 @@ int assemble ( int count,
return EXIT_FAILURE;
}
empty_array = calloc(1024, sizeof(*empty_array));
empty_imbue = calloc(1024, sizeof(*empty_imbue));
empty_store = calloc(1024, sizeof(*empty_store));
empty_array = aalloc(1024ul * sizeof(*empty_array));
empty_imbue = aalloc(1024ul * sizeof(*empty_imbue));
empty_store = aalloc(1024ul * sizeof(*empty_store));
for (int index = 0; index < count; ++index) {
const int size = text_sector_size;
@ -807,9 +808,5 @@ int assemble ( int count,
replace(&text_sector_byte[get], (char*)&set, (int)sizeof(get));
}
free(empty_array);
free(empty_imbue);
free(empty_store);
return EXIT_SUCCESS;
}

@ -13,6 +13,7 @@
#include "unix.h"
#include "safety.h"
#include "debug.h"
#include "arena.h"
#include "printf2.h"
int * token_array = NULL;
@ -22,12 +23,14 @@ char * output_file_name = "a.out";
int compile_init(void) {
// Funny hacky solution, we can use arenas here.
token_array = calloc(1440UL, sizeof(*token_array));
// And a day later we did use them...
token_array = aalloc(1440ul * sizeof(*token_array));
return 0;
}
// Function not needful anymore sir!
// Arena handles the memory, no need to free!
int compile_deinit(void) {
free(token_array);
return 0;
}
@ -114,7 +117,7 @@ int compile(void) {
dump_variables_to_assembler();
text_sector_byte = calloc(4096UL, sizeof(*text_sector_byte));
text_sector_byte = aalloc(4096ul * sizeof(*text_sector_byte));
if (assemble(token_count, token_array)) {
issue_internal_error();
@ -127,7 +130,5 @@ int compile(void) {
make_executable(output_file_name);
free(text_sector_byte);
return 0;
}

@ -13,6 +13,7 @@
#include "eaxhla.tab.h"
#include "arena.h"
#include "debug.h"
#include "assembler.h"
#include "compile.h"
@ -144,7 +145,6 @@ void empty_out_scope(void) {
scope = NULL;
}
int eaxhla_init(void) {
undeclared_symbol = (symbol_t *)calloc(sizeof(symbol_t), 1);
tommy_hashtable_init(&symbol_table, 256);
@ -156,7 +156,7 @@ int eaxhla_init(void) {
symbol_t * new_symbol(char * name) {
symbol_t * r;
r = (symbol_t *)calloc(sizeof(symbol_t), 1);
r = (symbol_t *)calloc(sizeof(symbol_t), 1ul);
r->name = name;
return r;
@ -273,7 +273,7 @@ char * make_scoped_name(const char * const scope, const char * const name) {
char * r;
const long scl = strlen(scope);
const long nml = strlen(name);
r = malloc(2 + scl + 1 + nml + 1);
r = calloc((size_t) (2 + scl + 1 + nml + 1), 1ul);
r[0] = '_';
r[1] = '_';
memcpy(r + 2, scope, scl);
@ -337,7 +337,6 @@ void _add_variable(unsigned type, const char * const name, size_t size, void * v
variable->symbol_type = VARIABLE_SYMBOL;
symbol_insert(variable);
}
void add_variable(unsigned type, const char * const name) {
@ -381,7 +380,7 @@ void add_literal(void * data, size_t size) {
int ignore = asprintf(&name, "_anon_%lu", anon_variable_counter++);
(void)ignore;
symbol_t * literal = (symbol_t *)calloc(sizeof(symbol_t), 1);
symbol_t * literal = (symbol_t *)calloc(sizeof(symbol_t), 1ul);
literal->name = name;
literal->elements = size;
literal->array_value = data;
@ -497,6 +496,7 @@ symbol_t * get_variable(const char * const name) {
}
free(varname);
return r;
}

@ -1,5 +1,6 @@
%{
#include <stdlib.h>
#include "sds/sds.h"
#include "eaxhla.h"
#include "eaxhla.tab.h"
@ -265,7 +266,7 @@ library { BEGIN INITIAL; return END_LIBRARY; }
/* XXX: the first WORD_SIZE_IN_BYTES bytes should be 0'd */
\" {
BEGIN INITIAL;
yylval.blobval.data = malloc(sdslen(string_literal_buffer));
yylval.blobval.data = calloc(sdslen(string_literal_buffer), 1ul);
memcpy(yylval.blobval.data, string_literal_buffer, sdslen(string_literal_buffer));
yylval.blobval.len = sdslen(string_literal_buffer);

@ -8,6 +8,9 @@
#include "assembler.h"
#include "debug.h"
#define ARENA_IMPLEMENTATION
#include "arena.h"
int init(void) {
eaxhla_init();
compile_init();
@ -19,8 +22,8 @@ void deinit(void) {
yyfree_leftovers();
eaxhla_deinit();
compile_deinit();
eaxhla_deinit(); // We need this one because of 1000 dependencies.
compile_deinit(); // We don't need this one anymore...
}
signed main(int argc, char * argv[]) {