Bug fix: after reallocating the hash table, we have to re-insert each value
authorVikram S. Adve <vadve@cs.uiuc.edu>
Tue, 29 Jul 2003 20:01:01 +0000 (20:01 +0000)
committerVikram S. Adve <vadve@cs.uiuc.edu>
Tue, 29 Jul 2003 20:01:01 +0000 (20:01 +0000)
instead of copying table entries!

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@7396 91177308-0d34-0410-b5e6-96231b3b80d8

runtime/libtrace/tracelib.c

index 73190244bfd3269eaa42bc6082d5d08653691a58..ba20ff3ce3ee19e6c6ea8a8557a7ba6f690c2451 100644 (file)
@@ -1,26 +1,24 @@
-/*===-- tracelib.c - Runtime routines for tracing ---------------*- C++ -*-===*\
-//
-// Runtime routines for supporting tracing of execution for code generated by
-// LLVM.
-//
-//===----------------------------------------------------------------------===*/
+/*===-- tracelib.c - Runtime routines for tracing ---------------*- C++ -*-===*
+ *
+ * Runtime routines for supporting tracing of execution for code generated by
+ * LLVM.
+ *
+ *===----------------------------------------------------------------------===*/
 
 #include "tracelib.h"
 #include <assert.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#ifndef sun
-#include <stdint.h>
-#endif
+#include "Support/DataTypes.h"
 
 /*===---------------------------------------------------------------------=====
  * HASH FUNCTIONS
  *===---------------------------------------------------------------------===*/
 
 /* use #defines until we have inlining */
-typedef uint32_t Index;                 /* type of index/size for hash table */
-typedef uint32_t Generic;               /* type of values stored in table */ 
+typedef uintptr_t Index;                 /* type of keys, size for hash table */
+typedef uint32_t  Generic;               /* type of values stored in table */ 
 
 /* Index IntegerHashFunc(const Generic value, const Index size) */
 #define IntegerHashFunc(value, size) \
@@ -38,7 +36,6 @@ typedef uint32_t Generic;               /* type of values stored in table */
 #define PointerRehashFunc(value, size) \
   IntegerRehashFunc((Index) value, size)
 
-
 /*===---------------------------------------------------------------------=====
  * POINTER-TO-GENERIC HASH TABLE.
  * These should be moved to a separate location: HashTable.[ch]
@@ -71,6 +68,10 @@ extern void Insert(PtrValueHashTable* ptrTable, void* ptr, Generic value);
 
 extern void Delete(PtrValueHashTable* ptrTable, void* ptr);
 
+/* Returns NULL if the item is not found. */
+/* void* LookupPtr(PtrValueHashTable* ptrTable, void* ptr) */
+#define LookupPtr(ptrTable, ptr) \
+  LookupOrInsertPtr(ptrTable, ptr, FIND)
 
 void
 InitializeTable(PtrValueHashTable* ptrTable, Index newSize)
@@ -98,11 +99,9 @@ ReallocTable(PtrValueHashTable* ptrTable, Index newSize)
     return;
 
 #ifndef NDEBUG
-  printf("\n***\n*** WARNING: REALLOCATING SPACE FOR POINTER HASH TABLE.\n");
-  printf("*** oldSize = %d, oldCapacity = %d\n***\n\n",
-         ptrTable->size, ptrTable->capacity); 
-  printf("*** NEW SEQUENCE NUMBER FOR A POINTER WILL PROBABLY NOT MATCH ");
-  printf(" THE OLD ONE!\n***\n\n");
+  printf("\n***\n*** REALLOCATING SPACE FOR POINTER HASH TABLE.\n");
+  printf("*** oldSize = %ld, oldCapacity = %ld\n***\n\n",
+         (long) ptrTable->size, (long) ptrTable->capacity); 
 #endif
 
   unsigned int i;
@@ -113,20 +112,18 @@ ReallocTable(PtrValueHashTable* ptrTable, Index newSize)
   
   /* allocate the new storage and flags and re-insert the old entries */
   InitializeTable(ptrTable, newSize);
-  memcpy(ptrTable->table, oldTable,
-         oldCapacity * sizeof(PtrValueHashEntry));
-  memcpy(ptrTable->fullEmptyFlags, oldFlags,
-         oldCapacity * sizeof(FULLEMPTY));
-  ptrTable->size = oldSize;
+  for (i=0; i < oldCapacity; ++i)
+    if (oldFlags[i] == FULL)
+      Insert(ptrTable, oldTable[i].key, oldTable[i].value);
+
+  assert(ptrTable->size == oldSize && "Incorrect number of entries copied?");
 
 #ifndef NDEBUG
-  for (i=0; i < oldCapacity; ++i) {
-    assert(ptrTable->fullEmptyFlags[i] == oldFlags[i]);
-    assert(ptrTable->table[i].key == oldTable[i].key);
-    assert(ptrTable->table[i].value == oldTable[i].value);
-  }
+  for (i=0; i < oldCapacity; ++i)
+    if (! oldFlags[i])
+      assert(LookupPtr(ptrTable, oldTable[i].key) == oldTable[i].value);
 #endif
-      
+  
   free(oldTable);
   free(oldFlags);
 }
@@ -216,11 +213,6 @@ LookupOrInsertPtr(PtrValueHashTable* ptrTable, void* ptr, ACTION action)
   return (Generic) NULL;
 }
 
-/* Returns NULL if the item is not found. */
-/* void* LookupPtr(PtrValueHashTable* ptrTable, void* ptr) */
-#define LookupPtr(ptrTable, ptr) \
-  LookupOrInsertPtr(ptrTable, ptr, FIND)
-
 void
 Insert(PtrValueHashTable* ptrTable, void* ptr, Generic value)
 {