Hashtables now allow you to specify allocation and free functions; the mcg code
authorDavid Given <dg@cowlark.com>
Fri, 13 Jan 2017 21:27:59 +0000 (22:27 +0100)
committerDavid Given <dg@cowlark.com>
Fri, 13 Jan 2017 21:27:59 +0000 (22:27 +0100)
generator uses this to put all temporary data onto the procedure heap.

mach/proto/mcg/hop.h
mach/proto/mcg/ir.h
mach/proto/mcg/procedure.c
mach/proto/mcg/procedure.h
modules/src/data/hashtable.c
modules/src/data/hashtable.h
modules/src/data/heap.c

index 920bd77..e70d6d2 100644 (file)
@@ -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;
 };
index dd85edf..e533c0c 100644 (file)
@@ -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
index be59958..b8af086 100644 (file)
@@ -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 : */
index fba0339..328a521 100644 (file)
@@ -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
 
index c28ff12..e396908 100644 (file)
@@ -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; i<old_num_buckets; i++)
        {
@@ -92,7 +99,7 @@ void hashtable_rebucket(struct hashtable* ht, unsigned int num_buckets)
                }
        }
 
-       free(old_buckets);
+       ht->freefunction(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;
         }
index bdcf600..20fb00b 100644 (file)
@@ -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;
index 522a023..d02f11b 100644 (file)
@@ -29,4 +29,4 @@ void heap_free(struct heap* heap)
         heap->next = old->next;
         free(old);
     }
-}
\ No newline at end of file
+}