From d8e6899053d00b51f69c27560cf1b1983ebb5595 Mon Sep 17 00:00:00 2001 From: nbysani2 Date: Fri, 13 Mar 2009 04:04:11 +0000 Subject: [PATCH] hashmap bugs.. git-svn-id: https://subversion.cs.uiuc.edu/svn/bang/eoh2009@172 69d76c3e-0761-0410-948c-9895a8bb34fc --- src/base/bang-module-api.c | 14 +++++++--- src/base/bang-module.c | 7 +++++ src/base/bang-routing.c | 56 ++++++++++++++++++++++++++++++-------- src/base/bang-utils.c | 12 ++++---- src/base/bang-utils.h | 2 +- src/modules/test-module.c | 1 + 6 files changed, 70 insertions(+), 22 deletions(-) diff --git a/src/base/bang-module-api.c b/src/base/bang-module-api.c index a221d59..edff3b6 100644 --- a/src/base/bang-module-api.c +++ b/src/base/bang-module-api.c @@ -30,16 +30,22 @@ static uuid_t* get_valid_routes(BANG_module_info *info) { BANG_read_lock(info->lck); int i, size = 1; - uuid_t *valid_routes = calloc(size + 1,sizeof(uuid_t)); + uuid_t *valid_routes = calloc(size,sizeof(uuid_t)); + +#ifdef BDEBUG_1 + fprintf(stderr,"Peer info for %s is %d.\n",info->module_name, info->peers_info->peer_number); +#endif for (i = 0; i < info->peers_info->peer_number; ++i) { if (info->peers_info->validity[i]) { - valid_routes = realloc(valid_routes,(size++ + 1) * sizeof(uuid_t)); - uuid_copy(valid_routes[i],info->peers_info->uuids[i]); + size++; + valid_routes = realloc(valid_routes, size * sizeof(uuid_t)); + uuid_copy(valid_routes[size - 2],info->peers_info->uuids[i]); } } - uuid_clear(valid_routes[size]); + uuid_clear(valid_routes[size - 1]); + #ifdef BDEBUG_1 fprintf(stderr,"Number of valid routes for %s is %d.\n",info->module_name, size); #endif diff --git a/src/base/bang-module.c b/src/base/bang-module.c index 51632bb..c1ec242 100644 --- a/src/base/bang-module.c +++ b/src/base/bang-module.c @@ -116,6 +116,7 @@ BANG_module_info* new_BANG_module_info(char* module_name, unsigned char* module_ info->peers_info = calloc(1,sizeof(peers_information)); /* We aren't even a peer yet since we haven't been run/registered */ info->peers_info->peer_number = 0; + info->module_name_length = strlen(module_name); info->module_name = malloc(info->module_name_length + 1); strcpy(info->module_name, module_name); @@ -300,6 +301,7 @@ void* BANG_get_symbol(BANG_module *module, char *symbol) { static int BANG_module_info_peer_add(BANG_module_info *info, uuid_t new_peer) { int i; BANG_write_lock(info->lck); + for (i = 0; i < info->peers_info->peer_number; ++i) { if (info->peers_info->validity[i] == 0) { uuid_copy(info->peers_info->uuids[i],new_peer); @@ -310,6 +312,11 @@ static int BANG_module_info_peer_add(BANG_module_info *info, uuid_t new_peer) { } } + + info->peers_info->validity = realloc(info->peers_info->validity, info->peers_info->peer_number++); + info->peers_info->uuids = realloc(info->peers_info->uuids, info->peers_info->peer_number * sizeof(uuid_t)); + uuid_copy(info->peers_info->uuids[info->peers_info->peer_number - 1], new_peer); + BANG_write_unlock(info->lck); return -1; diff --git a/src/base/bang-routing.c b/src/base/bang-routing.c index de3af95..003ead9 100644 --- a/src/base/bang-routing.c +++ b/src/base/bang-routing.c @@ -148,6 +148,7 @@ static int remove_uuid_from_peer(const void *p, void *r) { BANG_write_lock(ptu->lck); + while ((cur_route = BANG_linked_list_pop(ptu->routes)) != NULL) { if (uuid_compare(*cur_route,route) != 0) { BANG_linked_list_push(new, cur_route); @@ -191,10 +192,24 @@ static unsigned int uuid_hashcode(const void *uuid) { unsigned int hc = (*((uuid_t*)uuid)[0] << 8) | (*((uuid_t*)uuid)[15]); hc |= (*((uuid_t*)uuid)[3] << 16) | (*((uuid_t*)uuid)[7]) << 24; +#ifdef BDEBUG_1 + char unparsed[37]; + uuid_unparse(*((uuid_t*)uuid),unparsed); + + fprintf(stderr, "ROUTING:\thashing %s to %d\n", unparsed, hc); +#endif + return hc; } static int uuid_ptr_compare(const void *uuid1, const void *uuid2) { +#ifdef BDEBUG_1 + char parsed[37], parsed2[37]; + uuid_unparse(*((uuid_t*)uuid1),parsed); + uuid_unparse(*((uuid_t*)uuid2),parsed2); + + fprintf(stderr, "ROUTING:\tgiven %s comparing to %s\n", parsed2, parsed); +#endif return uuid_compare(*((uuid_t*)uuid1),*((uuid_t*)uuid2)); } @@ -327,7 +342,13 @@ void BANG_route_request_job(uuid_t peer, uuid_t authority) { } void BANG_route_assertion_of_authority(uuid_t authority, uuid_t peer) { - /* assert(routes != NULL); */ +#ifdef BDEBUG_1 + char unparsed[37], parsed2[37]; + uuid_unparse(authority,unparsed); + uuid_unparse(peer,parsed2); + + fprintf(stderr,"ROUTING:\tAsserting authority to %s from %s.\n", parsed2, unparsed); +#endif assert(!uuid_is_null(authority)); assert(!uuid_is_null(peer)); @@ -335,6 +356,10 @@ void BANG_route_assertion_of_authority(uuid_t authority, uuid_t peer) { peer_or_module *route = BANG_hashmap_get(routes,&peer); BANG_read_unlock(routes_lock); +#ifdef BDEBUG_1 + fprintf(stderr,"ROUTING:\tFound route at %p for assertation of auth %s.\n",route, unparsed); +#endif + if (route == NULL) return; if (route->remote == LOCAL) { @@ -450,9 +475,9 @@ void BANG_route_new_peer(uuid_t peer, uuid_t new_peer) { fprintf(stderr,"Someone is trying to add a to a remote peer?"); #endif /* - BANG_request *request = create_request_with_message(BANG_DEBUG_REQUEST,message); - BANG_request_peer_id(route->pr->peer_id,request); - */ + BANG_request *request = create_request_with_message(BANG_DEBUG_REQUEST,message); + BANG_request_peer_id(route->pr->peer_id,request); + */ } } @@ -472,26 +497,35 @@ void BANG_route_remove_peer(uuid_t peer, uuid_t old_peer) { } else { #ifdef BDEBUG_1 - fprintf(stderr,"Someone is trying to remove a remote peer?"); + fprintf(stderr,"ROUTING:\tSomeone is trying to remove a remote peer?"); #endif /* - BANG_request *request = create_request_with_message(BANG_DEBUG_REQUEST,message); - BANG_request_peer_id(route->pr->peer_id,request); - */ + BANG_request *request = create_request_with_message(BANG_DEBUG_REQUEST,message); + BANG_request_peer_id(route->pr->peer_id,request); + */ } } void BANG_register_module_route(BANG_module *module) { assert(module != NULL); - uuid_t new_uuid; - uuid_generate(new_uuid); + module->info->peers_info->peer_number = 1; + module->info->peers_info->uuids = calloc(1,sizeof(uuid_t)); + module->info->peers_info->validity = malloc(1); + module->info->peers_info->validity[0] = 1; + uuid_generate(module->info->peers_info->uuids[module->info->my_id]); + +#ifdef BDEBUG_1 + char route[37]; + uuid_unparse(module->info->peers_info->uuids[module->info->my_id],route); + fprintf(stderr,"ROUTING:\tRegistering a module route %s.\n", route); +#endif peer_or_module *pom = new_pom_module_route(module); BANG_write_lock(routes_lock); - BANG_hashmap_set(routes,&new_uuid,pom); + BANG_hashmap_set(routes,&module->info->peers_info->uuids[0],pom); BANG_write_unlock(routes_lock); } diff --git a/src/base/bang-utils.c b/src/base/bang-utils.c index 0577cb6..74d3279 100644 --- a/src/base/bang-utils.c +++ b/src/base/bang-utils.c @@ -386,7 +386,7 @@ BANG_hashmap* new_BANG_hashmap(BANG_hashcode hash_func, BANG_compare comp_func) #ifdef BDEBUG_1 fprintf(stderr,"Creating a new hashmap.\n"); #endif - int i; + unsigned int i; new->data = calloc(HASHMAP_DEFAULT_SIZE,sizeof(BANG_linked_list*)); new->data_size = HASHMAP_DEFAULT_SIZE; @@ -402,7 +402,7 @@ BANG_hashmap* new_BANG_hashmap(BANG_hashcode hash_func, BANG_compare comp_func) } void free_BANG_hashmap(BANG_hashmap *hashmap) { - int i = 0; + unsigned int i = 0; for (; i < hashmap->data_size; ++i) { free_BANG_linked_list(hashmap->data[i],NULL); @@ -457,7 +457,7 @@ void* BANG_hashmap_get(BANG_hashmap *hashmap, void *key) { fi.comp_func = hashmap->compare_func; fi.pair = new_BANG_hashmap_pair(key,NULL); - int pos = key_hash % hashmap->data_size; + unsigned int pos = key_hash % hashmap->data_size; #ifdef BDEBUG_1 fprintf(stderr,"Hashmap get found pos %d which is pointer %p.\n", pos, hashmap->data[pos]); @@ -469,7 +469,7 @@ void* BANG_hashmap_get(BANG_hashmap *hashmap, void *key) { } void BANG_hashmap_set(BANG_hashmap *hashmap, void *key, void *item){ - int pos = hashmap->hash_func(key) % hashmap->data_size; + unsigned int pos = hashmap->hash_func(key) % hashmap->data_size; #ifdef BDEBUG_1 fprintf(stderr,"Found pos %d for item %p.\n", pos, item); #endif @@ -480,9 +480,9 @@ void BANG_hashmap_set(BANG_hashmap *hashmap, void *key, void *item){ if (BANG_linked_list_conditional_iterate(hashmap->data[pos],&find_item_set,&fi)) { #ifdef BDEBUG_1 - fprintf(stderr,"Hashmap setting an item.\n"); + fprintf(stderr,"Hashmap setting an item at %d.\n", pos); #endif - BANG_linked_list_push(hashmap->data[pos],fi.pair); + BANG_linked_list_push(hashmap->data[pos], fi.pair); } } diff --git a/src/base/bang-utils.h b/src/base/bang-utils.h index c3187bd..64c6517 100644 --- a/src/base/bang-utils.h +++ b/src/base/bang-utils.h @@ -62,7 +62,7 @@ typedef struct { typedef struct { BANG_linked_list **data; - int data_size; + unsigned int data_size; BANG_hashcode hash_func; BANG_compare compare_func; } BANG_hashmap; diff --git a/src/modules/test-module.c b/src/modules/test-module.c index 3989a50..2a01e43 100644 --- a/src/modules/test-module.c +++ b/src/modules/test-module.c @@ -89,4 +89,5 @@ void BANG_module_run(BANG_module_info *info) { BANG_module_version[1], BANG_module_version[2]); api->BANG_debug_on_all_peers(info,"TESTING ON ALL PEERS!\n"); + api->BANG_assert_authority(info, api->BANG_get_my_id(info)); }