Use heaps to reduce memory usage a bit.
authorDavid Given <dg@cowlark.com>
Sat, 26 Nov 2016 22:07:41 +0000 (23:07 +0100)
committerDavid Given <dg@cowlark.com>
Sat, 26 Nov 2016 22:07:41 +0000 (23:07 +0100)
mach/proto/mcg/hop.c
mach/proto/mcg/ir.c
mach/proto/mcg/mcg.h
mach/proto/mcg/pass_instructionselection.c
mach/proto/mcg/pass_phigroups.c
mach/proto/mcg/procedure.c
mach/proto/mcg/procedure.h
mach/proto/mcg/reg.c
modules/src/data/heap.c [new file with mode: 0644]
modules/src/data/heap.h [new file with mode: 0644]

index 77c6dc2..72464a8 100644 (file)
@@ -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;
 }
index e30e82a..550f97a 100644 (file)
@@ -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;
index 540f579..8607042 100644 (file)
@@ -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"
index 860dad0..c683255 100644 (file)
@@ -83,7 +83,7 @@ static struct constraint* get_constraint(struct vreg* vreg)
     struct constraint* c = pmap_findleft(&current_hop->constraints, vreg);
     if (!c)
     {
-        c = calloc(1, sizeof(*c));
+        c = heap_alloc(&proc_heap, 1, sizeof(*c));
         pmap_put(&current_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)
index dfff73f..68d8c7d 100644 (file)
@@ -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);
index f819c56..4d5eb71 100644 (file)
@@ -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 : */
index 226db80..55ed710 100644 (file)
@@ -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
 
index d40deb1..5c57790 100644 (file)
@@ -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 (file)
index 0000000..522a023
--- /dev/null
@@ -0,0 +1,32 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#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 (file)
index 0000000..f197c78
--- /dev/null
@@ -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