From 3af30201282fe08dc50a72f624b4572f238249c6 Mon Sep 17 00:00:00 2001 From: David Given Date: Fri, 13 Jan 2017 22:27:59 +0100 Subject: [PATCH] Hashtables now allow you to specify allocation and free functions; the mcg code generator uses this to put all temporary data onto the procedure heap. --- mach/proto/mcg/hop.h | 2 -- mach/proto/mcg/ir.h | 3 ++- mach/proto/mcg/procedure.c | 12 +++++++++++- mach/proto/mcg/procedure.h | 5 ++++- modules/src/data/hashtable.c | 21 ++++++++++++++------- modules/src/data/hashtable.h | 4 ++++ modules/src/data/heap.c | 2 +- 7 files changed, 36 insertions(+), 13 deletions(-) diff --git a/mach/proto/mcg/hop.h b/mach/proto/mcg/hop.h index 920bd77ea..e70d6d21a 100644 --- a/mach/proto/mcg/hop.h +++ b/mach/proto/mcg/hop.h @@ -30,7 +30,6 @@ struct insel struct valueusage { - struct vreg* vreg; bool input : 1; bool output : 1; bool through : 1; @@ -50,7 +49,6 @@ struct hop PMAPOF(struct value, struct value) equals_constraint; struct hashtable* valueusage; - struct hashtable* vregmapping; /* value -> vreg */ PMAPOF(struct vreg, struct vreg) copies; }; diff --git a/mach/proto/mcg/ir.h b/mach/proto/mcg/ir.h index dd85edfc3..e533c0ce2 100644 --- a/mach/proto/mcg/ir.h +++ b/mach/proto/mcg/ir.h @@ -13,7 +13,8 @@ struct value extern uint32_t value_hash_function(void* key); extern bool value_comparison_function(void* key1, void* key2); #define HASHTABLE_OF_VALUES \ - { value_hash_function, value_comparison_function } + { value_hash_function, value_comparison_function, \ + proc_alloc, proc_free } extern const struct hashtable empty_hashtable_of_values; struct ir diff --git a/mach/proto/mcg/procedure.c b/mach/proto/mcg/procedure.c index be59958c3..b8af0863b 100644 --- a/mach/proto/mcg/procedure.c +++ b/mach/proto/mcg/procedure.c @@ -3,6 +3,16 @@ struct procedure* current_proc; struct heap proc_heap; +void* proc_alloc(size_t nmemb, size_t size) +{ + return heap_alloc(&proc_heap, nmemb, size); +} + +void proc_free(void* ptr) +{ + /* do nothing; objects will leak until the heap is freed */ +} + static void print_blocks(char k) { int i; @@ -197,8 +207,8 @@ void procedure_compile(struct procedure* proc) emit_procedure(proc); - heap_free(&proc_heap); #endif + 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 fba03390c..328a5219c 100644 --- a/mach/proto/mcg/procedure.h +++ b/mach/proto/mcg/procedure.h @@ -25,11 +25,14 @@ struct procedure ARRAYOF(struct hreg) usedregs; }; +extern struct heap proc_heap; +extern void* proc_alloc(size_t nmemb, size_t size); +extern void proc_free(void* ptr); + 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/modules/src/data/hashtable.c b/modules/src/data/hashtable.c index c28ff124c..e39690870 100644 --- a/modules/src/data/hashtable.c +++ b/modules/src/data/hashtable.c @@ -53,8 +53,14 @@ static void lazy_init(struct hashtable* ht) if (!ht->cmpfunction) ht->cmpfunction = standard_pointer_comparison_function; + if (!ht->allocfunction) + ht->allocfunction = calloc; + + if (!ht->freefunction) + ht->freefunction = free; + if (!ht->buckets) - ht->buckets = calloc(ht->num_buckets, sizeof(struct hashnode*)); + ht->buckets = ht->allocfunction(ht->num_buckets, sizeof(struct hashnode*)); } void hashtable_empty(struct hashtable* ht) @@ -65,8 +71,9 @@ void hashtable_empty(struct hashtable* ht) void hashtable_reset(struct hashtable* ht) { + lazy_init(ht); hashtable_empty(ht); - free(ht->buckets); + ht->freefunction(ht->buckets); ht->buckets = NULL; } @@ -77,7 +84,7 @@ void hashtable_rebucket(struct hashtable* ht, unsigned int num_buckets) int i; ht->num_buckets = num_buckets; - ht->buckets = calloc(num_buckets, sizeof(struct hashnode*)); + ht->buckets = ht->allocfunction(num_buckets, sizeof(struct hashnode*)); for (i=0; ifreefunction(old_buckets); } static struct hashnode** findnodep(struct hashtable* ht, void* key) @@ -132,7 +139,7 @@ void* hashtable_put(struct hashtable* ht, void* key, void* value) return hashtable_put(ht, key, value); } - *hnp = calloc(1, sizeof(struct hashnode)); + *hnp = ht->allocfunction(1, sizeof(struct hashnode)); ht->size++; } @@ -161,7 +168,7 @@ void* hashtable_remove(struct hashtable* ht, void* key) struct hashnode* hn = *hnp; void* value = hn->value; *hnp = hn->next; - free(hn); + ht->freefunction(hn); ht->size--; return value; } @@ -185,7 +192,7 @@ void* hashtable_pop(struct hashtable* ht) struct hashnode* hn = *hnp; void* value = hn->value; *hnp = hn->next; - free(hn); + ht->freefunction(hn); ht->size--; return value; } diff --git a/modules/src/data/hashtable.h b/modules/src/data/hashtable.h index bdcf600ef..20fb00b15 100644 --- a/modules/src/data/hashtable.h +++ b/modules/src/data/hashtable.h @@ -5,6 +5,8 @@ typedef uint32_t hashfunction_t(void* key); typedef bool cmpfunction_t(void* key1, void* key2); +typedef void* allocfunction_t(size_t nmemb, size_t size); +typedef void freefunction_t(void* ptr); extern uint32_t standard_pointer_hash_function(void* key); extern bool standard_pointer_comparison_function(void* key1, void* key2); @@ -16,6 +18,8 @@ struct hashtable { hashfunction_t* hashfunction; cmpfunction_t* cmpfunction; + allocfunction_t* allocfunction; + freefunction_t* freefunction; unsigned int num_buckets; /* power of 2 */ struct hashnode** buckets; int size; diff --git a/modules/src/data/heap.c b/modules/src/data/heap.c index 522a023a1..d02f11b5b 100644 --- a/modules/src/data/heap.c +++ b/modules/src/data/heap.c @@ -29,4 +29,4 @@ void heap_free(struct heap* heap) heap->next = old->next; free(old); } -} \ No newline at end of file +} -- 2.34.1