Skip to content

Commit

Permalink
hashmap bugs..
Browse files Browse the repository at this point in the history
git-svn-id: https://subversion.cs.uiuc.edu/svn/bang/eoh2009@172 69d76c3e-0761-0410-948c-9895a8bb34fc
  • Loading branch information
nbysani2 committed Mar 13, 2009
1 parent e9f759e commit d8e6899
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 22 deletions.
14 changes: 10 additions & 4 deletions src/base/bang-module-api.c
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/base/bang-module.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
56 changes: 45 additions & 11 deletions src/base/bang-routing.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -327,14 +342,24 @@ 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));

BANG_read_lock(routes_lock);
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) {
Expand Down Expand Up @@ -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);
*/
}
}

Expand All @@ -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);
}
Expand Down
12 changes: 6 additions & 6 deletions src/base/bang-utils.c
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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]);
Expand All @@ -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
Expand All @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/base/bang-utils.h
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/modules/test-module.c
Expand Up @@ -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));
}

0 comments on commit d8e6899

Please sign in to comment.