]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - stat.c
Major header reworking, this respects the namespaces properly. Makes object dependenc...
[xonotic/gmqcc.git] / stat.c
diff --git a/stat.c b/stat.c
index 372cc143205ee5ce937fac666735fa715ac6adb2..aecd7ebcd9619e81b3d3f5632e7a5a0eccb0eeb0 100644 (file)
--- a/stat.c
+++ b/stat.c
@@ -1,3 +1,7 @@
+#include <string.h>
+#include <stdlib.h>
+#include <ctype.h>
+
 #include "gmqcc.h"
 
 /*
@@ -17,6 +21,17 @@ typedef struct stat_mem_block_s {
     struct stat_mem_block_s *prev;
 } stat_mem_block_t;
 
+typedef struct {
+    size_t key;
+    size_t value;
+} stat_size_entry_t, **stat_size_table_t;
+
+typedef struct {
+    uint64_t used;
+    uint64_t type;
+    uint64_t size;
+} stat_entry_t;
+
 static uint64_t          stat_mem_allocated         = 0;
 static uint64_t          stat_mem_deallocated       = 0;
 static uint64_t          stat_mem_allocated_total   = 0;
@@ -32,6 +47,39 @@ static stat_size_table_t stat_size_vectors          = NULL;
 static stat_size_table_t stat_size_hashtables       = NULL;
 static stat_mem_block_t *stat_mem_block_root        = NULL;
 
+/*
+ * A tiny size_t key-value hashtbale for tracking vector and hashtable
+ * sizes. We can use it for other things too, if we need to. This is
+ * very TIGHT, and efficent in terms of space though.
+ */
+static stat_size_table_t stat_size_new() {
+    return (stat_size_table_t)memset(
+        mem_a(sizeof(stat_size_entry_t*) * ST_SIZE),
+        0, ST_SIZE * sizeof(stat_size_entry_t*)
+    );
+}
+
+static void stat_size_del(stat_size_table_t table) {
+    size_t i = 0;
+    for (; i < ST_SIZE; i++) if(table[i]) mem_d(table[i]);
+    mem_d(table);
+}
+
+static stat_size_entry_t *stat_size_get(stat_size_table_t table, size_t key) {
+    size_t hash = (key % ST_SIZE);
+    while (table[hash] && table[hash]->key != key)
+        hash = (hash + 1) % ST_SIZE;
+    return table[hash];
+}
+static void stat_size_put(stat_size_table_t table, size_t key, size_t value) {
+    size_t hash = (key % ST_SIZE);
+    while (table[hash] && table[hash]->key != key)
+        hash = (hash + 1) % ST_SIZE;
+    table[hash] = (stat_size_entry_t*)mem_a(sizeof(stat_size_entry_t));
+    table[hash]->key = key;
+    table[hash]->value = value;
+}
+
 /*
  * A basic header of information wrapper allocator. Simply stores
  * information as a header, returns the memory + 1 past it, can be
@@ -284,14 +332,14 @@ hash_table_t *util_htnew(size_t size) {
     if ((find = stat_size_get(stat_size_hashtables, size)))
         find->value++;
     else {
-        stat_used_hashtables++;
+        stat_type_hashtables++;
         stat_size_put(stat_size_hashtables, size, 1);
     }
 
     hashtable->size = size;
     memset(hashtable->table, 0, sizeof(hash_node_t*) * size);
 
-    stat_type_hashtables++;
+    stat_used_hashtables++;
     return hashtable;
 }
 
@@ -431,39 +479,6 @@ void util_htdel(hash_table_t *ht) {
     util_htrem(ht, NULL);
 }
 
-/*
- * A tiny size_t key-value hashtbale for tracking vector and hashtable
- * sizes. We can use it for other things too, if we need to. This is
- * very TIGHT, and efficent in terms of space though.
- */
-stat_size_table_t stat_size_new() {
-    return (stat_size_table_t)memset(
-        mem_a(sizeof(stat_size_entry_t*) * ST_SIZE),
-        0, ST_SIZE * sizeof(stat_size_entry_t*)
-    );
-}
-
-void stat_size_del(stat_size_table_t table) {
-    size_t i = 0;
-    for (; i < ST_SIZE; i++) if(table[i]) mem_d(table[i]);
-    mem_d(table);
-}
-
-stat_size_entry_t *stat_size_get(stat_size_table_t table, size_t key) {
-    size_t hash = (key % ST_SIZE);
-    while (table[hash] && table[hash]->key != key)
-        hash = (hash + 1) % ST_SIZE;
-    return table[hash];
-}
-void stat_size_put(stat_size_table_t table, size_t key, size_t value) {
-    size_t hash = (key % ST_SIZE);
-    while (table[hash] && table[hash]->key != key)
-        hash = (hash + 1) % ST_SIZE;
-    table[hash] = (stat_size_entry_t*)mem_a(sizeof(stat_size_entry_t));
-    table[hash]->key = key;
-    table[hash]->value = value;
-}
-
 /*
  * The following functions below implement printing / dumping of statistical
  * information.
@@ -524,6 +539,9 @@ static void stat_dump_mem_info() {
 static void stat_dump_stats_table(stat_size_table_t table, const char *string, uint64_t *size) {
     size_t i,j;
     
+    if (!table)
+        return;
+    
     for (i = 0, j = 0; i < ST_SIZE; i++) {
         stat_size_entry_t *entry;
 
@@ -548,7 +566,7 @@ void stat_info() {
 
     if (OPTS_OPTION_BOOL(OPTION_MEMCHK) ||
         OPTS_OPTION_BOOL(OPTION_STATISTICS)) {
-        uint64_t mem;
+        uint64_t mem = 0;
         
         con_out("\nAdditional Statistics:\n\
     Total vectors allocated:    %llu\n\