Skip to content

Commit cfcc553

Browse files
authored
Merge pull request #176 from ChAoSUnItY/refactor/reduce-blocks-alloc
Replace fixed size blocks with linked list
2 parents 95a55e7 + e22f595 commit cfcc553

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

src/defs.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#define MAX_FIELDS 64
2222
#define MAX_FUNCS 512
2323
#define MAX_FUNC_TRIES 2160
24-
#define MAX_BLOCKS 2048
2524
#define MAX_TYPES 64
2625
#define MAX_IR_INSTR 50000
2726
#define MAX_BB_PRED 128
@@ -227,11 +226,16 @@ struct block {
227226
func_t *func;
228227
macro_t *macro;
229228
int locals_size;
230-
int index;
229+
struct block *next;
231230
};
232231

233232
typedef struct block block_t;
234233

234+
typedef struct {
235+
block_t *head;
236+
block_t *tail;
237+
} block_list_t;
238+
235239
/* phase-1 IR definition */
236240
typedef struct {
237241
opcode_t op;

src/globals.c

+19-7
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
/* Global objects */
99

10-
block_t *BLOCKS;
11-
int blocks_idx = 0;
10+
block_list_t BLOCKS;
1211

1312
macro_t *MACROS;
1413
int macros_idx = 0;
@@ -226,8 +225,16 @@ int find_label_offset(char name[])
226225

227226
block_t *add_block(block_t *parent, func_t *func, macro_t *macro)
228227
{
229-
block_t *blk = &BLOCKS[blocks_idx];
230-
blk->index = blocks_idx++;
228+
block_t *blk = malloc(sizeof(block_t));
229+
230+
if (!BLOCKS.head) {
231+
BLOCKS.head = blk;
232+
BLOCKS.tail = BLOCKS.head;
233+
} else {
234+
BLOCKS.tail->next = blk;
235+
BLOCKS.tail = blk;
236+
}
237+
231238
blk->parent = parent;
232239
blk->func = func;
233240
blk->macro = macro;
@@ -394,7 +401,7 @@ var_t *find_local_var(char *token, block_t *block)
394401

395402
var_t *find_global_var(char *token)
396403
{
397-
block_t *block = &BLOCKS[0];
404+
block_t *block = BLOCKS.head;
398405

399406
for (int i = 0; i < block->next_local; i++) {
400407
if (!strcmp(block->locals[i].var_name, token))
@@ -587,7 +594,8 @@ void global_init()
587594
{
588595
elf_code_start = ELF_START + elf_header_len;
589596

590-
BLOCKS = malloc(MAX_BLOCKS * sizeof(block_t));
597+
BLOCKS.head = NULL;
598+
BLOCKS.tail = NULL;
591599
MACROS = malloc(MAX_ALIASES * sizeof(macro_t));
592600
FUNCS = malloc(MAX_FUNCS * sizeof(func_t));
593601
FUNC_TRIES = malloc(MAX_FUNC_TRIES * sizeof(trie_t));
@@ -613,7 +621,11 @@ void global_init()
613621

614622
void global_release()
615623
{
616-
free(BLOCKS);
624+
while (BLOCKS.head) {
625+
block_t *next = BLOCKS.head->next;
626+
free(BLOCKS.head);
627+
BLOCKS.head = next;
628+
}
617629
free(MACROS);
618630
free(FUNCS);
619631
free(FUNC_TRIES);

src/parser.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -2153,7 +2153,7 @@ bool read_global_assignment(char *token)
21532153
{
21542154
ph1_ir_t *ph1_ir;
21552155
var_t *vd;
2156-
block_t *parent = &BLOCKS[0];
2156+
block_t *parent = BLOCKS.head;
21572157

21582158
/* global initialization must be constant */
21592159
var_t *var = find_global_var(token);
@@ -3182,7 +3182,7 @@ void read_global_decl(block_t *block)
31823182
void read_global_statement()
31833183
{
31843184
char token[MAX_ID_LEN];
3185-
block_t *block = &BLOCKS[0]; /* global block */
3185+
block_t *block = BLOCKS.head; /* global block */
31863186

31873187
if (lex_accept(T_struct)) {
31883188
int i = 0, size = 0;

0 commit comments

Comments
 (0)