Small memory improvement for hashtable iterators
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 14 May 2020 21:35:55 +0000 (16:35 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 14 May 2020 21:35:55 +0000 (16:35 -0500)
bh.h
onyx.c

diff --git a/bh.h b/bh.h
index e0f20b2b5dd74d7fe88070b2f1df588648de9762..12de3f4eced158eef489dac66d49c9467bbd24eb 100644 (file)
--- a/bh.h
+++ b/bh.h
@@ -6,6 +6,7 @@
 #include <fcntl.h>
 #include <errno.h>
 
+#include <stdio.h> // TODO: Replace with custom implementation of printf
 #include <stdlib.h>
 #include <string.h> // TODO: Replace with needed functions
 #include <assert.h>
@@ -27,6 +28,42 @@ typedef unsigned long isize;
 typedef i32 b32;
 typedef void* ptr;
 
+//-------------------------------------------------------------------------------------
+// Better debug functions
+//-------------------------------------------------------------------------------------
+#ifdef BH_DEBUG
+
+void* bh__debug_malloc(size_t size, const char* file, u64 line);
+void  bh__debug_free(void* ptr, const char* file, u64 line);
+void* bh__debug_realloc(void* ptr, size_t size, const char* file, u64 line);
+
+#ifdef BH_DEFINE
+
+void* bh__debug_malloc(size_t size, const char* file, u64 line) {
+       void* p = malloc(size);
+       printf("[DEBUG] %p = malloc(%ld) at %s:%ld\n", p, size, file, line);
+       return p;
+}
+
+void bh__debug_free(void* ptr, const char* file, u64 line) {
+       printf("[DEBUG] free(%p) at %s:%ld\n", ptr, file, line);
+       free(ptr);
+}
+
+void* bh__debug_realloc(void* ptr, size_t size, const char* file, u64 line) {
+       void* p = realloc(ptr, size);
+       printf("[DEBUG] %p = realloc(%p, %ld) at %s:%ld\n", p, ptr, size, file, line);
+       return p;
+}
+
+#endif
+
+#define malloc(size)           (bh__debug_malloc(size, __FILE__, __LINE__))
+#define free(ptr)                      (bh__debug_free(ptr, __FILE__, __LINE__))
+#define realloc(ptr, size)     (bh__debug_realloc(ptr, size, __FILE__, __LINE__))
+
+#endif
+
 //-------------------------------------------------------------------------------------
 // Better character functions
 //-------------------------------------------------------------------------------------
@@ -262,8 +299,8 @@ u64 bh__hash_function(const char* str, i32 len) {
 #endif
 
 typedef struct bh_hash_iterator {
-       ptr *tab, *endtab, arr;
-       i32 elemsize, arridx;
+       ptr *tab, *endtab;
+       i32 elemsize, arrlen;
        bh__hash_entry* entry;
 } bh_hash_iterator;
 
@@ -898,7 +935,6 @@ bh_hash_iterator bh__hash_iter_setup(ptr *table, i32 elemsize) {
        bh_hash_iterator it = {
                .tab = table,
                .endtab = table + BH__HASH_MODULUS,
-               .arr = NULL,
                .elemsize = elemsize,
                .entry = NULL
        };
@@ -909,8 +945,8 @@ b32 bh_hash_iter_next(bh_hash_iterator* it) {
        if (it->tab == NULL) return 0;
 
        if (it->entry != NULL) {
-               it->arridx++;
-               if (it->arridx >= bh_arr_length(it->arr)) {
+               it->arrlen--;
+               if (it->arrlen <= 0) {
                        it->tab++;
                        goto step_to_next;
                }
@@ -920,16 +956,15 @@ b32 bh_hash_iter_next(bh_hash_iterator* it) {
        }
 
 step_to_next:
-       // Set forward to find next valid
+       // Step forward to find next valid
        while (*it->tab == NULL && it->tab != it->endtab) {
                it->tab++;
        }
 
        if (it->tab == it->endtab) return 0;
 
-       it->arr = *it->tab;
-       it->entry = it->arr;
-       it->arridx = 0;
+       it->entry = *it->tab;
+       it->arrlen = bh_arr_length(it->entry);
        return 1;
 }
 
diff --git a/onyx.c b/onyx.c
index 58a3ca49a5c04445515d41831c64ae8792901c27..5195faf5dfaa699efedb7d784d36508b35e53fbb 100644 (file)
--- a/onyx.c
+++ b/onyx.c
@@ -1,4 +1,5 @@
 #define BH_NO_STRING
+// #define BH_DEBUG
 #define BH_DEFINE
 #include "bh.h"
 
@@ -37,6 +38,7 @@ int main(int argc, char *argv[]) {
 
        bh_file_contents_delete(&fc);
        bh_arr_free(token_arr);
+       bh_hash_free(symbol_count);
 
        return 0;
 }