]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - typedef.c
Better codegen
[xonotic/gmqcc.git] / typedef.c
index 0259f350c32d7de88f7e7cd02673fb246b388799..106310a79c1650283a12f8f4d89bb6f1a4fcb73b 100644 (file)
--- a/typedef.c
+++ b/typedef.c
@@ -29,21 +29,8 @@ void typedef_init() {
         typedef_table[i] = NULL;
 }
 
-unsigned int typedef_hash(const char *s) {
-    unsigned int hash = 0;
-    unsigned int size = strlen(s);
-    unsigned int iter;
-    
-    for (iter = 0; iter < size; iter++) {
-        hash += s[iter];
-        hash += (hash << 10);
-        hash ^= (hash >> 6);
-    }
-    hash += (hash << 3);
-    hash ^= (hash >> 11);
-    hash += (hash << 15);
-    
-    return hash % 1024;
+uint32_t typedef_hash(const char *s) {
+    return util_crc32(s, strlen(s), 1024);
 }
 
 typedef_node *typedef_find(const char *s) {
@@ -76,15 +63,21 @@ int typedef_add(struct lex_file *file, const char *from, const char *to) {
         strncmp(from, "entity", sizeof("entity")) == 0 ||
         strncmp(from, "void",   sizeof("void"))   == 0) {
         
-        typedef_table[hash]       = mem_a(sizeof(typedef_node));
-        typedef_table[hash]->name = util_strdup(from);
+        typedef_table[hash] = mem_a(sizeof(typedef_node));
+        if (typedef_table[hash])
+            typedef_table[hash]->name = util_strdup(from);
+        else
+            return error(file, ERROR_PARSE, "ran out of resources for typedef %s\n", to);
         return -100;
     } else {
         /* search the typedefs for it (typedef-a-typedef?) */
         typedef_node *find = typedef_table[typedef_hash(from)];
         if (find) {
-            typedef_table[hash]       = mem_a(sizeof(typedef_node));
-            typedef_table[hash]->name = util_strdup(find->name);
+            typedef_table[hash] = mem_a(sizeof(typedef_node));
+            if (typedef_table[hash])
+                typedef_table[hash]->name = util_strdup(find->name);
+            else
+                return error(file, ERROR_PARSE, "ran out of resources for typedef %s\n", to);
             return -100;
         }
     }