From: David Given Date: Sat, 26 Nov 2016 22:07:41 +0000 (+0100) Subject: Use heaps to reduce memory usage a bit. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=257f6f69f3efbcf0b55aede4e5f2db2c52f65f59;p=ack.git Use heaps to reduce memory usage a bit. --- diff --git a/mach/proto/mcg/hop.c b/mach/proto/mcg/hop.c index 77c6dc260..72464a82a 100644 --- a/mach/proto/mcg/hop.c +++ b/mach/proto/mcg/hop.c @@ -10,7 +10,7 @@ static const struct burm_emitter_data emitter_data; struct hop* new_hop(struct basicblock* bb, struct ir* ir) { - struct hop* hop = calloc(1, sizeof *hop); + struct hop* hop = heap_alloc(&proc_heap, 1, sizeof(*hop)); hop->id = hop_count++; hop->bb = bb; hop->ir = ir; @@ -19,7 +19,7 @@ struct hop* new_hop(struct basicblock* bb, struct ir* ir) static struct insel* new_insel(enum insel_type type) { - struct insel* insel = calloc(1, sizeof(*insel)); + struct insel* insel = heap_alloc(&proc_heap, 1, sizeof(*insel)); insel->type = type; return insel; } diff --git a/mach/proto/mcg/ir.c b/mach/proto/mcg/ir.c index e30e82a2b..550f97aad 100644 --- a/mach/proto/mcg/ir.c +++ b/mach/proto/mcg/ir.c @@ -4,7 +4,7 @@ static int next_id = 0; struct ir* new_ir0(int opcode, int size) { - struct ir* ir = calloc(sizeof(struct ir), 1); + struct ir* ir = calloc(1, sizeof(struct ir)); ir->id = next_id++; ir->opcode = opcode; ir->size = size; diff --git a/mach/proto/mcg/mcg.h b/mach/proto/mcg/mcg.h index 540f5799c..860704285 100644 --- a/mach/proto/mcg/mcg.h +++ b/mach/proto/mcg/mcg.h @@ -20,6 +20,7 @@ #include "array.h" #include "imap.h" #include "pmap.h" +#include "heap.h" #include "diagnostics.h" #include "astring.h" #include "ir.h" diff --git a/mach/proto/mcg/pass_instructionselection.c b/mach/proto/mcg/pass_instructionselection.c index 860dad02f..c68325529 100644 --- a/mach/proto/mcg/pass_instructionselection.c +++ b/mach/proto/mcg/pass_instructionselection.c @@ -83,7 +83,7 @@ static struct constraint* get_constraint(struct vreg* vreg) struct constraint* c = pmap_findleft(¤t_hop->constraints, vreg); if (!c) { - c = calloc(1, sizeof(*c)); + c = heap_alloc(&proc_heap, 1, sizeof(*c)); pmap_put(¤t_hop->constraints, vreg, c); } return c; @@ -185,7 +185,7 @@ static void emit(struct insn* insn) static struct insn* walk_instructions(struct burm_node* node, int goal) { - struct insn* insn = calloc(1, sizeof(*insn)); + struct insn* insn = heap_alloc(&proc_heap, 1, sizeof(*insn)); int i; insn->ir = node->ir; @@ -257,7 +257,7 @@ static struct insn* walk_instructions(struct burm_node* node, int goal) static struct burm_node* build_shadow_tree(struct ir* root, struct ir* ir) { - struct burm_node* node = calloc(1, sizeof(*node)); + struct burm_node* node = heap_alloc(&proc_heap, 1, sizeof(*node)); node->ir = ir; if (ir->root == root) diff --git a/mach/proto/mcg/pass_phigroups.c b/mach/proto/mcg/pass_phigroups.c index dfff73faa..68d8c7d34 100644 --- a/mach/proto/mcg/pass_phigroups.c +++ b/mach/proto/mcg/pass_phigroups.c @@ -84,7 +84,7 @@ static void associate_groups(void) while (phimap.count > 0) { - struct phicongruence* c = calloc(1, sizeof(*c)); + struct phicongruence* c = heap_alloc(&proc_heap, 1, sizeof(*c)); c->id = number++; recursively_associate_group(c, phimap.item[0].left); update_vreg_types(c); diff --git a/mach/proto/mcg/procedure.c b/mach/proto/mcg/procedure.c index f819c56e3..4d5eb71bd 100644 --- a/mach/proto/mcg/procedure.c +++ b/mach/proto/mcg/procedure.c @@ -1,6 +1,7 @@ #include "mcg.h" struct procedure* current_proc; +struct heap proc_heap; static void print_blocks(char k) { @@ -203,6 +204,8 @@ void procedure_compile(struct procedure* proc) write_cfg_graph(proc->name); if (dominance_dot_file) write_dominance_graph(proc->name); + + heap_free(&proc_heap); } /* vim: set sw=4 ts=4 expandtab : */ diff --git a/mach/proto/mcg/procedure.h b/mach/proto/mcg/procedure.h index 226db804c..55ed71022 100644 --- a/mach/proto/mcg/procedure.h +++ b/mach/proto/mcg/procedure.h @@ -29,6 +29,7 @@ extern void procedure_compile(struct procedure* proc); extern void procedure_update_bb_graph(struct procedure* proc); extern struct procedure* current_proc; +extern struct heap proc_heap; #endif diff --git a/mach/proto/mcg/reg.c b/mach/proto/mcg/reg.c index d40deb171..5c57790ef 100644 --- a/mach/proto/mcg/reg.c +++ b/mach/proto/mcg/reg.c @@ -4,14 +4,14 @@ static int vreg_count = 1; struct vreg* new_vreg(void) { - struct vreg* vreg = calloc(1, sizeof *vreg); + struct vreg* vreg = heap_alloc(&proc_heap, 1, sizeof *vreg); vreg->id = vreg_count++; return vreg; } struct hreg* new_hreg(const struct burm_register_data* brd) { - struct hreg* hreg = calloc(1, sizeof *hreg); + struct hreg* hreg = heap_alloc(&proc_heap, 1, sizeof *hreg); hreg->id = brd->id; hreg->brd = brd; hreg->attrs = brd->attrs; @@ -23,7 +23,7 @@ struct hreg* new_hreg(const struct burm_register_data* brd) struct hreg* new_stacked_hreg(uint32_t attrs) { static int hreg_count = 1; - struct hreg* hreg = calloc(1, sizeof *hreg); + struct hreg* hreg = heap_alloc(&proc_heap, 1, sizeof *hreg); hreg->id = aprintf("stacked_%d_id_%d", attrs, hreg_count++); hreg->attrs = attrs; hreg->is_stacked = true; diff --git a/modules/src/data/heap.c b/modules/src/data/heap.c new file mode 100644 index 000000000..522a023a1 --- /dev/null +++ b/modules/src/data/heap.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include "heap.h" + +struct heap_node +{ + struct heap_node* next; + char data[1]; +}; + +void* heap_alloc(struct heap* heap, size_t nmemb, size_t size) +{ + size_t bytes = nmemb * size; + struct heap_node* node = calloc(1, sizeof(*node) + bytes - 1); + if (!node) + return NULL; + + node->next = heap->next; + heap->next = node; + return node->data; +} + +void heap_free(struct heap* heap) +{ + while (heap->next) + { + struct heap_node* old = heap->next; + heap->next = old->next; + free(old); + } +} \ No newline at end of file diff --git a/modules/src/data/heap.h b/modules/src/data/heap.h new file mode 100644 index 000000000..f197c7860 --- /dev/null +++ b/modules/src/data/heap.h @@ -0,0 +1,12 @@ +#ifndef HEAP_H +#define HEAP_H + +struct heap +{ + struct heap_node* next; +}; + +extern void* heap_alloc(struct heap* heap, size_t nmemb, size_t size); +extern void heap_free(struct heap* heap); + +#endif