]> de.git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - correct.c
Remove an illegal mem_d
[xonotic/gmqcc.git] / correct.c
index 04b0d3864985d091a154603e51e299c6d13da035..34f32b4df7742df39a14b9ad2130d4ba6dc32668 100644 (file)
--- a/correct.c
+++ b/correct.c
  */
 #include "gmqcc.h"
 
+/*
+ * A forward allcator for the corrector.  This corrector requires a lot
+ * of allocations.  This forward allocator combats all those allocations
+ * and speeds us up a little.  It also saves us space in a way since each
+ * allocation isn't wasting a little header space for when NOTRACK isn't
+ * defined.
+ */    
+#define CORRECT_POOLSIZE (128*1024*1024)
+
+static unsigned char **correct_pool_data = NULL;
+static unsigned char  *correct_pool_this = NULL;
+static size_t          correct_pool_addr = 0;
+
+static GMQCC_INLINE void correct_pool_new(void) {
+    correct_pool_addr = 0;
+    correct_pool_this = (unsigned char *)mem_a(CORRECT_POOLSIZE);
+
+    vec_push(correct_pool_data, correct_pool_this);
+}
+
+static GMQCC_INLINE void *correct_pool_alloc(size_t bytes) {
+    void *data;
+    if (correct_pool_addr + bytes >= CORRECT_POOLSIZE)
+        correct_pool_new();
+
+    data               = correct_pool_this;
+    correct_pool_this += bytes;
+    correct_pool_addr += bytes;
+
+    return data;
+}
+
+static GMQCC_INLINE void correct_pool_delete(void) {
+    size_t i;
+    for (i = 0; i < vec_size(correct_pool_data); ++i)
+        mem_d(correct_pool_data[i]);
+
+    correct_pool_data = NULL;
+    correct_pool_this = NULL;
+    correct_pool_addr = 0;
+}
+
+
+static GMQCC_INLINE char *correct_outstr(const char *s) {
+    char *o = util_strdup(s);
+    correct_pool_delete();
+    return o;
+}
+
+correct_trie_t* correct_trie_new()
+{
+    correct_trie_t *t = (correct_trie_t*)mem_a(sizeof(correct_trie_t));
+    t->value   = NULL;
+    t->entries = NULL;
+    return t;
+}
+
+void correct_trie_del_sub(correct_trie_t *t)
+{
+    size_t i;
+    for (i = 0; i < vec_size(t->entries); ++i)
+        correct_trie_del_sub(&t->entries[i]);
+    vec_free(t->entries);
+}
+
+void correct_trie_del(correct_trie_t *t)
+{
+    size_t i;
+    for (i = 0; i < vec_size(t->entries); ++i)
+        correct_trie_del_sub(&t->entries[i]);
+    vec_free(t->entries);
+    mem_d(t);
+}
+
+void* correct_trie_get(const correct_trie_t *t, const char *key)
+{
+    const unsigned char *data = (const unsigned char*)key;
+    while (*data) {
+        unsigned char ch = *data;
+        const size_t  vs = vec_size(t->entries);
+        size_t        i;
+        const correct_trie_t *entries = t->entries;
+        for (i = 0; i < vs; ++i) {
+            if (entries[i].ch == ch) {
+                t = &entries[i];
+                ++data;
+                break;
+            }
+        }
+        if (i == vs)
+            return NULL;
+    }
+    return t->value;
+}
+
+void correct_trie_set(correct_trie_t *t, const char *key, void * const value)
+{
+    const unsigned char *data = (const unsigned char*)key;
+    while (*data) {
+        unsigned char ch = *data;
+        correct_trie_t       *entries = t->entries;
+        const size_t  vs = vec_size(t->entries);
+        size_t        i;
+        for (i = 0; i < vs; ++i) {
+            if (entries[i].ch == ch) {
+                t = &entries[i];
+                break;
+            }
+        }
+        if (i == vs) {
+            correct_trie_t *elem  = (correct_trie_t*)vec_add(t->entries, 1);
+            elem->ch      = ch;
+            elem->value   = NULL;
+            elem->entries = NULL;
+            t = elem;
+        }
+        ++data;
+    }
+    t->value = value;
+}
+
 /*
  * This is a very clever method for correcting mistakes in QuakeC code
  * most notably when invalid identifiers are used or inproper assignments;
  */
 
 /* some hashtable management for dictonaries */
-static size_t *correct_find(ht table, const char *word) {
-    return (size_t*)util_htget(table, word);
+static size_t *correct_find(correct_trie_t *table, const char *word) {
+    return (size_t*)correct_trie_get(table, word);
 }
 
-static int correct_update(ht *table, const char *word) {
+static int correct_update(correct_trie_t* *table, const char *word) {
     size_t *data = correct_find(*table, word);
     if (!data)
         return 0;
@@ -86,6 +207,38 @@ static int correct_update(ht *table, const char *word) {
     return 1;
 }
 
+void correct_add(correct_trie_t* table, size_t ***size, const char *ident) {
+    size_t     *data = NULL;
+    const char *add  = ident;
+    
+    if (!correct_update(&table, add)) {
+        data  = (size_t*)mem_a(sizeof(size_t));
+        *data = 1;
+
+        vec_push((*size), data);
+        correct_trie_set(table, add, data);
+    }
+}
+
+void correct_del(correct_trie_t* dictonary, size_t **data) {
+    size_t i;
+    const size_t vs = vec_size(data);
+    for (i = 0; i < vs; i++)
+        mem_d(data[i]);
+
+    vec_free(data);
+    correct_trie_del(dictonary);
+}
+#if 1
+#undef mem_a
+#undef mem_r
+#undef mem_d
+#define mem_a(x)   correct_alloc((x))
+#define mem_r(a,b) correct_realloc((a),(b))
+/* doing this in order to avoid 'unused variable' warnings */
+#define mem_d(x)   ((void)(0 && (x)))
+#endif
+
 
 /*
  * _ is valid in identifiers. I've yet to implement numerics however
@@ -94,37 +247,6 @@ static int correct_update(ht *table, const char *word) {
  */
 static const char correct_alpha[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
 
-static char *correct_strndup(const char *src, size_t n) {
-    char   *ret;
-    size_t  len = strlen(src);
-
-    if (n < len)
-        len = n;
-
-    if (!(ret = (char*)mem_a(len + 1)))
-        return NULL;
-
-    ret[len] = '\0';
-    return (char*)memcpy(ret, src, len);
-}
-
-static char *correct_concat(char *str1, char *str2, bool next) {
-    char *ret = NULL;
-
-    if (!str1) {
-         str1 = mem_a(1);
-        *str1 = '\0';
-    }
-
-    str1 = mem_r (str1, strlen(str1) + strlen(str2) + 1);
-    ret  = strcat(str1, str2);
-
-    if (str2 && next)
-        mem_d(str2);
-
-    return ret;
-}
-
 /*
  * correcting logic for the following forms of transformations:
  *  1) deletion
@@ -137,11 +259,10 @@ static size_t correct_deletion(const char *ident, char **array, size_t index) {
     size_t len = strlen(ident);
 
     for (itr = 0; itr < len; itr++) {
-        array[index + itr] = correct_concat (
-            correct_strndup (ident,       itr),
-            correct_strndup (ident+itr+1, len-(itr+1)),
-            true
-        );
+        char *a = (char*)correct_pool_alloc(len+1);
+        memcpy(a, ident, itr);
+        memcpy(a + itr, ident + itr + 1, len - itr);
+        array[index + itr] = a;
     }
 
     return itr;
@@ -152,19 +273,13 @@ static size_t correct_transposition(const char *ident, char **array, size_t inde
     size_t len = strlen(ident);
 
     for (itr = 0; itr < len - 1; itr++) {
-        array[index + itr] = correct_concat (
-            correct_concat (
-                correct_strndup(ident,     itr),
-                correct_strndup(ident+itr+1, 1),
-                true
-            ),
-            correct_concat (
-                correct_strndup(ident+itr,   1),
-                correct_strndup(ident+itr+2, len-(itr+2)),
-                true
-            ),
-            true
-        );
+        char  tmp;
+        char *a = (char*)correct_pool_alloc(len+1);
+        memcpy(a, ident, len+1);
+        tmp      = a[itr];
+        a[itr  ] = a[itr+1];
+        a[itr+1] = tmp;
+        array[index + itr] = a;
     }
 
     return itr;
@@ -175,23 +290,13 @@ static size_t correct_alteration(const char *ident, char **array, size_t index)
     size_t jtr;
     size_t ktr;
     size_t len    = strlen(ident);
-    char   cct[2] = { 0, 0 }; /* char code table, for concatenation */
 
     for (itr = 0, ktr = 0; itr < len; itr++) {
-        for (jtr = 0; jtr < sizeof(correct_alpha); jtr++, ktr++) {
-            *cct = correct_alpha[jtr];
-            array[index + ktr] = correct_concat (
-                correct_concat (
-                    correct_strndup(ident, itr),
-                    (char *) &cct,
-                    false
-                ),
-                correct_strndup (
-                    ident + (itr+1),
-                    len   - (itr+1)
-                ),
-                true
-            );
+        for (jtr = 0; jtr < sizeof(correct_alpha)-1; jtr++, ktr++) {
+            char *a = (char*)correct_pool_alloc(len+1);
+            memcpy(a, ident, len+1);
+            a[itr] = correct_alpha[jtr];
+            array[index + ktr] = a;
         }
     }
 
@@ -202,24 +307,15 @@ static size_t correct_insertion(const char *ident, char **array, size_t index) {
     size_t itr;
     size_t jtr;
     size_t ktr;
-    size_t len    = strlen(ident);
-    char   cct[2] = { 0, 0 }; /* char code table, for concatenation */
+    const size_t len    = strlen(ident);
 
     for (itr = 0, ktr = 0; itr <= len; itr++) {
-        for (jtr = 0; jtr < sizeof(correct_alpha); jtr++, ktr++) {
-            *cct = correct_alpha[jtr];
-            array[index + ktr] = correct_concat (
-                correct_concat (
-                    correct_strndup (ident, itr),
-                    (char *) &cct,
-                    false
-                ),
-                correct_strndup (
-                    ident+itr,
-                    len - itr
-                ),
-                true
-            );
+        for (jtr = 0; jtr < sizeof(correct_alpha)-1; jtr++, ktr++) {
+            char *a = (char*)correct_pool_alloc(len+2);
+            memcpy(a, ident, itr);
+            memcpy(a + itr + 1, ident + itr, len - itr + 1);
+            a[itr] = correct_alpha[jtr];
+            array[index + ktr] = a;
         }
     }
 
@@ -235,12 +331,12 @@ static GMQCC_INLINE size_t correct_size(const char *ident) {
      */   
 
     register size_t len = strlen(ident);
-    return (len) + (len - 1) + (len * sizeof(correct_alpha)) + ((len + 1) * sizeof(correct_alpha));
+    return (len) + (len - 1) + (len * (sizeof(correct_alpha)-1)) + ((len + 1) * (sizeof(correct_alpha)-1));
 }
 
 static char **correct_edit(const char *ident) {
     size_t next;
-    char **find = (char**)mem_a(correct_size(ident) * sizeof(char*));
+    char **find = (char**)correct_pool_alloc(correct_size(ident) * sizeof(char*));
 
     if (!find)
         return NULL;
@@ -267,13 +363,26 @@ static int correct_exist(char **array, size_t rows, char *ident) {
     return 0;
 }
 
-static char **correct_known(ht table, char **array, size_t rows, size_t *next) {
+static GMQCC_INLINE char **correct_known_resize(char **res, size_t *allocated, size_t size) {
+    size_t oldallocated = *allocated;
+    char **out;
+    if (size+1 < *allocated)
+        return res;
+
+    *allocated += 32;
+    out = correct_pool_alloc(sizeof(*res) * *allocated);
+    memcpy(out, res, sizeof(*res) * oldallocated);
+    return out;
+}
+
+static char **correct_known(correct_trie_t* table, char **array, size_t rows, size_t *next) {
     size_t itr;
     size_t jtr;
     size_t len;
     size_t row;
-    char **res = NULL;
-    char **end;
+    size_t nxt = 8;
+    char **res = correct_pool_alloc(sizeof(char *) * nxt);
+    char **end = NULL;
 
     for (itr = 0, len = 0; itr < rows; itr++) {
         end = correct_edit(array[itr]);
@@ -281,19 +390,17 @@ static char **correct_known(ht table, char **array, size_t rows, size_t *next) {
 
         for (jtr = 0; jtr < row; jtr++) {
             if (correct_find(table, end[jtr]) && !correct_exist(res, len, end[jtr])) {
-                res        = mem_r(res, sizeof(char*) * (len + 1));
+                res        = correct_known_resize(res, &nxt, len+1);
                 res[len++] = end[jtr];
             }
         }
-
-        mem_d(end);
     }
 
     *next = len;
     return res;
 }
 
-static char *correct_maximum(ht table, char **array, size_t rows) {
+static char *correct_maximum(correct_trie_t* table, char **array, size_t rows) {
     char   *str  = NULL;
     size_t *itm  = NULL;
     size_t  itr;
@@ -309,12 +416,6 @@ static char *correct_maximum(ht table, char **array, size_t rows) {
     return str;
 }
 
-static void correct_cleanup(char **array, size_t rows) {
-    size_t itr;
-    for (itr = 0; itr < rows; itr++)
-        mem_d(array[itr]);
-}
-
 /*
  * This is the exposed interface:
  * takes a table for the dictonary a vector of sizes (used for internal
@@ -323,20 +424,8 @@ static void correct_cleanup(char **array, size_t rows) {
  * the add function works the same.  Except the identifier is used to
  * add to the dictonary.  
  */   
-void correct_add(ht table, size_t ***size, const char *ident) {
-    size_t     *data = NULL;
-    const char *add  = ident;
-    
-    if (!correct_update(&table, add)) {
-        data  = (size_t*)mem_a(sizeof(size_t));
-        *data = 1;
 
-        vec_push((*size), data);
-        util_htset(table, add, data);
-    }
-}
-
-char *correct_correct(ht table, const char *ident) {
+char *correct_str(correct_trie_t* table, const char *ident) {
     char **e1;
     char **e2;
     char  *e1ident;
@@ -346,74 +435,25 @@ char *correct_correct(ht table, const char *ident) {
     size_t e1rows = 0;
     size_t e2rows = 0;
 
+    correct_pool_new();
+
     /* needs to be allocated for free later */
     if (correct_find(table, ident))
-        return found;
+        return correct_outstr(found);
 
-    mem_d(found);
     if ((e1rows = correct_size(ident))) {
         e1      = correct_edit(ident);
 
         if ((e1ident = correct_maximum(table, e1, e1rows))) {
             found = util_strdup(e1ident);
-            correct_cleanup(e1, e1rows);
-            mem_d(e1);
-            return found;
+            return correct_outstr(found);
         }
     }
 
     e2 = correct_known(table, e1, e1rows, &e2rows);
-    if (e2rows && ((e2ident = correct_maximum(table, e2, e2rows))))
+    if (e2rows && ((e2ident = correct_maximum(table, e2, e2rows)))) {
         found = util_strdup(e2ident);
-    
-    correct_cleanup(e1, e1rows);
-    correct_cleanup(e2, e2rows);
-
-    mem_d(e1);
-    mem_d(e2);
-    
-    return found;
-}
-
-void correct_del(ht dictonary, size_t **data) {
-    size_t i;
-    for (i = 0; i < vec_size(data); i++)
-        mem_d(data[i]);
-
-    vec_free(data);
-    util_htdel(dictonary);
-}
-
-int main() {
-    opts.debug = true;
-    opts.memchk = true;
-    con_init();
-
-    ht       t = util_htnew(1024);
-    size_t **d = NULL;
-
-    correct_add(t, &d, "hellobain");
-    correct_add(t, &d, "ellor");
-    correct_add(t, &d, "world");
-
-    printf("found identifiers: (2)\n");
-    printf(" 1: hellobain\n");
-    printf(" 2: ellor\n");
-    printf(" 3: world\n");
-
-    char *b = correct_correct(t, "rld");
-    char *a = correct_correct(t, "ello");
-    char *c = correct_correct(t, "helbain");
-
-    printf("invalid identifier: `%s` (did you mean: `%s`?)\n", "ello", a);
-    printf("invalid identifier: `%s` (did you mean: `%s`?)\n", "rld", b);
-    printf("invalid identifier: `%s` (did you mean: `%s`?)\n", "helbain", c);
-
-    correct_del(t, d);
-    mem_d(b);
-    mem_d(a);
-    mem_d(c);
-
-    /*util_meminfo();*/
+    }
 
+    return correct_outstr(found);
 }