* Fix some bugs in ht.c and ht64.c

* Initial working version of RMixed
  - Needs some memleaking fixes and impl missing methods
  - Simple test case works
This commit is contained in:
pancake 2011-03-18 09:53:50 +01:00
parent a74b312765
commit c0ab6cedd4
4 changed files with 44 additions and 35 deletions

View File

@ -3,7 +3,7 @@ include ../config.mk
NAME=r_util
OBJ=mem.o pool.o num.o str.o re.o hex.o file.o alloca.o range.o log.o
OBJ+=prof.o cache.o sys.o btree.o buf.o list.o flist.o w32-sys.o base64.o
OBJ+=ht.o ht64.o
OBJ+=ht.o ht64.o mixed.o
ifeq (${HAVE_LIB_GMP},1)
OBJ+=big-gmp.o
else
@ -23,4 +23,8 @@ ht64:
#cat ht.c ht64.c | sed -e 's,) {,);,'
m:
gcc mixed.c -I ../include/ -DTEST=1 -lr_util -g
./a.out
#valgrind ./a.out
h:
gcc ht.c -I ../include/ -DTEST=1 -lr_util -g
#valgrind ./a.out

View File

@ -82,7 +82,7 @@ static const struct {
{ 2147483648ul, 2362232233ul, 2362232231ul}
};
#define entry_is_free(x) (!x->data)
#define entry_is_free(x) (!x || !x->data)
#define entry_is_deleted(x) (x->data==&deleted_data)
#define entry_is_present(x) (x->data && x->data != &deleted_data)
@ -114,7 +114,7 @@ static void r_hashtable_rehash(RHashTable *ht, int new_size_index) {
if (new_size_index >= ARRAY_SIZE (hash_sizes))
return;
// XXX: This code is redupped! fuck't
ht->table = malloc (hash_sizes[new_size_index].size * sizeof (*ht->table));
ht->table = calloc (hash_sizes[new_size_index].size, sizeof (*ht->table));
if (!ht->table)
return;
ht->size_index = new_size_index;
@ -131,11 +131,11 @@ static void r_hashtable_rehash(RHashTable *ht, int new_size_index) {
}
R_API RHashTable* r_hashtable_new(void) {
RHashTable *ht = malloc (sizeof (*ht));
if (!ht)
return NULL;
RHashTable *ht = R_NEW (RHashTable);
if (!ht) return NULL;
// TODO: use slices here
ht->table = malloc (ht->size * sizeof (*ht->table));
ht->size = hash_sizes[0].size;
ht->table = calloc (ht->size, sizeof (*ht->table));
if (!ht->table) {
free (ht);
return NULL;
@ -143,7 +143,6 @@ R_API RHashTable* r_hashtable_new(void) {
ht->size_index = 0;
ht->entries = 0;
ht->deleted_entries = 0;
ht->size = hash_sizes[ht->size_index].size;
ht->rehash = hash_sizes[ht->size_index].rehash;
ht->max_entries = hash_sizes[ht->size_index].max_entries;
return ht;

View File

@ -114,7 +114,7 @@ static void r_hashtable64_rehash(RHashTable64 *ht, int new_size_index) {
if (new_size_index >= ARRAY_SIZE (hash_sizes))
return;
// XXX: This code is redupped! fuck't
ht->table = malloc (hash_sizes[new_size_index].size * sizeof (*ht->table));
ht->table = calloc (hash_sizes[new_size_index].size, sizeof (*ht->table));
if (!ht->table)
return;
ht->size_index = new_size_index;
@ -131,11 +131,11 @@ static void r_hashtable64_rehash(RHashTable64 *ht, int new_size_index) {
}
R_API RHashTable64* r_hashtable64_new(void) {
RHashTable64 *ht = malloc (sizeof (*ht));
if (!ht)
return NULL;
RHashTable64 *ht = R_NEW (RHashTable64);
if (!ht) return NULL;
// TODO: use slices here
ht->table = malloc (ht->size * sizeof (*ht->table));
ht->size = hash_sizes[0].size;
ht->table = calloc (ht->size, sizeof (*ht->table));
if (!ht->table) {
free (ht);
return NULL;
@ -143,7 +143,6 @@ R_API RHashTable64* r_hashtable64_new(void) {
ht->size_index = 0;
ht->entries = 0;
ht->deleted_entries = 0;
ht->size = hash_sizes[ht->size_index].size;
ht->rehash = hash_sizes[ht->size_index].rehash;
ht->max_entries = hash_sizes[ht->size_index].max_entries;
return ht;
@ -212,22 +211,23 @@ int main () {
const char *str;
int ret;
RHashTable64 *ht = r_hashtable64_new ();
#define HASH 268453705
ret = r_hashtable64_insert (ht, 33, "patata");
ret = r_hashtable64_insert (ht, HASH, "patata");
if (!ret)
printf ("Cannot reinsert !!1\n");
str = r_hashtable64_lookup (ht, 33);
str = r_hashtable64_lookup (ht, HASH);
if (str) printf ("String is (%s)\n", str);
else printf ("Cannot find string\n");
r_hashtable64_remove (ht, 33);
r_hashtable64_remove (ht, HASH);
str = r_hashtable64_lookup (ht, 33);
str = r_hashtable64_lookup (ht, HASH);
if (str) printf ("String is (%s)\n", str);
else printf("Cannot find string\n");
else printf("Cannot find string which is ok :)\n");
r_hashtable64_search (ht, 33);
r_hashtable64_search (ht, HASH);
r_hashtable64_free (ht);
}

View File

@ -69,10 +69,8 @@ R_API int r_mixed_key(RMixed *m, int key, int size) {
switch (size) {
case 1: case 2: case 4:
m->keys[key]->hash.ht = r_hashtable_new ();
eprintf ("new hash %d = %p\n", key, m->keys[key]->hash.ht);
return R_TRUE;
case 8: m->keys[key]->hash.ht64 = r_hashtable64_new ();
eprintf ("---new hash %d\n", key);
return R_TRUE;
}
}
@ -96,7 +94,6 @@ R_API RList *r_mixed_get (RMixed *m, int key, ut64 value) {
if (m->keys[key])
switch (m->keys[key]->size) {
case 1: case 2: case 4:
eprintf ("sz = %d ht=%p\n", m->keys[key]->size, m->keys[key]->hash.ht);
return r_hashtable_lookup (m->keys[key]->hash.ht, (ut32)value);
case 8: return r_hashtable64_lookup (m->keys[key]->hash.ht64, value);
}
@ -105,11 +102,6 @@ eprintf ("sz = %d ht=%p\n", m->keys[key]->size, m->keys[key]->hash.ht);
R_API void *r_mixed_get0 (RMixed *m, int key, ut64 value) {
RList *list = r_mixed_get (m, key, value);
printf ("LURKITAPP %d = %p\n", key, r_hashtable_lookup (m->keys[key]->hash.ht, (ut32)value));
eprintf ("lookup for %llx\n", value);
eprintf ("LIST = %p\n", list);
//eprintf ("HEAD = %p\n", r_list_head (list));
//eprintf ("DATA = %p\n", r_list_head (list)->data);
if (list && !r_list_empty (list))
return r_list_head (list)->data;
return NULL;
@ -120,7 +112,7 @@ R_API int r_mixed_add (RMixed *m, void *p) {
RHashTable64 *ht64;
RList *list = NULL;
ut64 value;
int i, size;
int i, size, ret = R_FALSE;;
r_list_append (m->list, p);
for (i=0; i<RMIXED_MAXKEYS; i++) {
if (!m->keys[i])
@ -134,10 +126,9 @@ R_API int r_mixed_add (RMixed *m, void *p) {
if (!list) {
list = r_list_new ();
r_hashtable_insert (ht, (ut32)value, list);
printf ("INSERTING NEWTABLE AT %d PWND = %p\n", i, r_hashtable_lookup (ht, (ut32)value));
}
eprintf ("key=%d newlist=%p value=%llx p=%p\n", i, list, value, p);
r_list_append (list, p);
ret = R_TRUE;
break;
case 8:
ht64 = m->keys[i]->hash.ht64;
@ -146,21 +137,26 @@ eprintf ("key=%d newlist=%p value=%llx p=%p\n", i, list, value, p);
list = r_list_new ();
r_hashtable64_insert (ht64, value, list);
}
eprintf ("key64=%d newlist=%p value=%llx p=%p\n", i, list, value, p);
r_list_append (list, p);
ret = R_TRUE;
break;
}
}
return ret;
}
R_API int r_mixed_del (RMixed *m, const void *p) {
R_API int r_mixed_del (RMixed *m, void *p) {
int i;
r_list_delete_data (m->list, p);
// TODO delete indexed hashtables
for (i=0; i<RMIXED_MAXKEYS; i++) {
if (!m->keys[i]) continue;
// TODO: remove that key ptr from everywhere
}
return R_FALSE;
}
#if TEST
typedef struct {
char *name;
ut32 hashname;
@ -174,7 +170,6 @@ TestStruct *test_struct_new(const char *name, int length, ut64 offset) {
ts->hashname = r_str_hash (name);
ts->length = length;
ts->offset = offset;
eprintf ("name : %s\n", name);
return ts;
}
@ -184,6 +179,8 @@ void test_struct_free(TestStruct *ts) {
}
int main () {
RList *list;
RListIter *iter;
TestStruct *ts;
RMixed *mx = r_mixed_new ();
R_MIXED_KEY (mx, TestStruct, ts, hashname);
@ -203,5 +200,14 @@ int main () {
printf ("OFF: %llx\n", ts->offset);
} else eprintf ("oops. cannot find 'food'\n");
eprintf ("--\n");
list = r_mixed_get (mx, r_offsetof (TestStruct, offset), 0x224944);
r_list_foreach (list, iter, ts) {
printf ("NAM: %s\n", ts->name);
printf ("LEN: %d\n", ts->length);
printf ("OFF: %llx\n", ts->offset);
}
r_mixed_free (mx);
}
#endif