Bug fix: after reallocating the hash table, we have to re-insert each value

instead of copying table entries!

llvm-svn: 7396
This commit is contained in:
Vikram S. Adve 2003-07-29 20:01:01 +00:00
parent e895c2e241
commit f8049f93c2
1 changed files with 25 additions and 33 deletions

View File

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