From: erickson Date: Fri, 24 Mar 2006 22:37:00 +0000 (+0000) Subject: taking advantage of macros X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=eda9af4d6ab86c8f548bcc496d371b62ebf6ead3;p=opensrf%2Fbjwebb.git taking advantage of macros git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@669 9efc2488-bf62-4759-914b-345cdb29e865 --- diff --git a/src/libstack/osrf_app_session.c b/src/libstack/osrf_app_session.c index 5338a36..d1ee5cb 100644 --- a/src/libstack/osrf_app_session.c +++ b/src/libstack/osrf_app_session.c @@ -64,7 +64,7 @@ void osrf_app_session_request_finish( osrf_app_session* session, int req_id ){ if(session == NULL) return; - osrf_app_request* req = osrfListGetIndex( session->request_queue, req_id ); + osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, req_id ); if(req == NULL) return; osrfListRemove( req->session->request_queue, req->request_id ); } @@ -73,7 +73,7 @@ void osrf_app_session_request_finish( void osrf_app_session_request_reset_timeout( osrf_app_session* session, int req_id ) { if(session == NULL) return; osrfLogDebug( OSRF_LOG_MARK, "Resetting request timeout %d", req_id ); - osrf_app_request* req = osrfListGetIndex( session->request_queue, req_id ); + osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, req_id ); if(req == NULL) return; req->reset_timeout = 1; } @@ -337,14 +337,14 @@ void osrf_app_session_set_complete( osrf_app_session* session, int request_id ) if(session == NULL) return; - osrf_app_request* req = osrfListGetIndex( session->request_queue, request_id ); + osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, request_id ); if(req) req->complete = 1; } int osrf_app_session_request_complete( osrf_app_session* session, int request_id ) { if(session == NULL) return 0; - osrf_app_request* req = osrfListGetIndex( session->request_queue, request_id ); + osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, request_id ); if(req) return req->complete; return 0; @@ -377,7 +377,7 @@ int osrf_app_session_push_queue( osrf_app_session* session, osrf_message* msg ){ if(session == NULL || msg == NULL) return 0; - osrf_app_request* req = osrfListGetIndex( session->request_queue, msg->thread_trace ); + osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, msg->thread_trace ); if(req == NULL) return 0; _osrf_app_request_push_queue( req, msg ); @@ -452,7 +452,7 @@ int osrf_app_session_disconnect( osrf_app_session* session){ } int osrf_app_session_request_resend( osrf_app_session* session, int req_id ) { - osrf_app_request* req = osrfListGetIndex( session->request_queue, req_id ); + osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, req_id ); return _osrf_app_request_resend( req ); } @@ -561,7 +561,7 @@ osrf_message* osrf_app_session_request_recv( osrf_app_session* session, int req_id, int timeout ) { if(req_id < 0 || session == NULL) return NULL; - osrf_app_request* req = osrfListGetIndex( session->request_queue, req_id ); + osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, req_id ); return _osrf_app_request_recv( req, timeout ); } diff --git a/src/libstack/osrf_hash.c b/src/libstack/osrf_hash.c index 968e9b9..69de9e7 100644 --- a/src/libstack/osrf_hash.c +++ b/src/libstack/osrf_hash.c @@ -1,10 +1,12 @@ #include "osrf_hash.h" osrfHash* osrfNewHash() { - osrfHash* hash = safe_malloc(sizeof(osrfHash)); + osrfHash* hash; + OSRF_MALLOC(hash, sizeof(osrfHash)); hash->hash = osrfNewList(); hash->freeItem = NULL; hash->size = 0; + hash->keys = osrfNewStringArray(64); return hash; } @@ -15,28 +17,73 @@ static unsigned int osrfHashMakeKey(char* str) { if(!str) return 0; unsigned int len = strlen(str); unsigned int h = len; - unsigned int i = 0; + unsigned int i = 0; for(i = 0; i < len; str++, i++) h = ((h << 5) ^ (h >> 27)) ^ (*str); - return (h & 0x7FF); + return (h & (OSRF_HASH_LIST_SIZE-1)); } +/* +#define OSRF_HASH_MAKE_KEY(str,num) \ + do {\ + unsigned int len = strlen(str); \ + unsigned int h = len;\ + unsigned int i = 0;\ + for(i = 0; i < len; str++, i++)\ + h = ((h << 5) ^ (h >> 27)) ^ (*str);\ + *num = (h & (OSRF_HASH_LIST_SIZE-1));\ + } while(0) + */ + +/* +#define OSRF_HASH_MAKE_KEY(str,num) \ + unsigned int ___len = strlen(str);\ + unsigned int ___h = ___len;\ + unsigned int ___i = 0;\ + for(___i = 0; ___i < ___len; str++, ___i++)\ + ___h = ((___h << 5) ^ (___h >> 27)) ^ (*str);\ + num = (___h & (OSRF_HASH_LIST_SIZE-1));\ + */ + +/* +#define OSRF_HASH_MAKE_KEY(str,num,len) \ + unsigned int __i;\ + num = len;\ + for(__i = 0; __i < len; __i++, str++)\ + num = ((num << 5) ^ (num >> 27)) ^ (*str);\ + num = (num & (OSRF_HASH_LIST_SIZE-1));\ + */ + +/* +#define OSRF_HASH_MAKE_KEY(str,num, len) \ + num = osrfHashMakeKey(str);\ + fprintf(stderr, "made num: %d\n", num); + */ + + + /* returns the index of the item and points l to the sublist the item * lives in if the item and points n to the hashnode the item * lives in if the item is found. Otherwise -1 is returned */ static unsigned int osrfHashFindItem( osrfHash* hash, char* key, osrfList** l, osrfHashNode** n ) { if(!(hash && key)) return -1; + int i = osrfHashMakeKey(key); - osrfList* list = osrfListGetIndex( hash->hash, i ); - if( !list ) { return -1; } + /* + unsigned int i; + OSRF_HASH_MAKE_KEY(key, &i); + */ + + osrfList* list = OSRF_LIST_GET_INDEX( hash->hash, i ); + if( !list ) { return -1; } int k; osrfHashNode* node = NULL; for( k = 0; k < list->size; k++ ) { - node = osrfListGetIndex(list, k); + node = OSRF_LIST_GET_INDEX(list, k); if( node && node->key && !strcmp(node->key, key) ) break; node = NULL; @@ -51,16 +98,22 @@ static unsigned int osrfHashFindItem( osrfHash* hash, char* key, osrfList** l, o osrfHashNode* osrfNewHashNode(char* key, void* item) { if(!(key && item)) return NULL; - osrfHashNode* n = safe_malloc(sizeof(osrfHashNode)); + osrfHashNode* n; + OSRF_MALLOC(n, sizeof(osrfHashNode)); n->key = strdup(key); n->item = item; return n; } -void osrfHashNodeFree(osrfHashNode* node) { - if(!node) return; +void* osrfHashNodeFree(osrfHash* hash, osrfHashNode* node) { + if(!(node && hash)) return NULL; + void* item = NULL; + if( hash->freeItem ) + hash->freeItem( node->key, node->item ); + else item = node->item; free(node->key); free(node); + return item; } void* osrfHashSet( osrfHash* hash, void* item, const char* key, ... ) { @@ -68,10 +121,16 @@ void* osrfHashSet( osrfHash* hash, void* item, const char* key, ... ) { VA_LIST_TO_STRING(key); void* olditem = osrfHashRemove( hash, VA_BUF ); + int bucketkey = osrfHashMakeKey(VA_BUF); + /* + unsigned int bucketkey; + OSRF_HASH_MAKE_KEY(VA_BUF, &bucketkey); + */ + osrfList* bucket; - if( !(bucket = osrfListGetIndex(hash->hash, bucketkey)) ) { + if( !(bucket = OSRF_LIST_GET_INDEX(hash->hash, bucketkey)) ) { bucket = osrfNewList(); osrfListSet( hash->hash, bucket, bucketkey ); } @@ -79,6 +138,9 @@ void* osrfHashSet( osrfHash* hash, void* item, const char* key, ... ) { osrfHashNode* node = osrfNewHashNode(VA_BUF, item); osrfListPushFirst( bucket, node ); + if(!osrfStringArrayContains(hash->keys, VA_BUF)) + osrfStringArrayAdd( hash->keys, VA_BUF ); + hash->size++; return olditem; } @@ -96,12 +158,8 @@ void* osrfHashRemove( osrfHash* hash, const char* key, ... ) { osrfListRemove( list, index ); hash->size--; - void* item = NULL; - if(hash->freeItem) - hash->freeItem((char*) VA_BUF, node->item); - else item = node->item; - - osrfHashNodeFree(node); + void* item = osrfHashNodeFree(hash, node); + osrfStringArrayRemove(hash->keys, VA_BUF); return item; } @@ -117,19 +175,24 @@ void* osrfHashGet( osrfHash* hash, const char* key, ... ) { } -osrfStringArray* osrfHashKeys( osrfHash* hash ) { +osrfStringArray* osrfHashKeysInc( osrfHash* hash ) { if(!hash) return NULL; + return hash->keys; +} +osrfStringArray* osrfHashKeys( osrfHash* hash ) { + if(!hash) return NULL; + int i, k; osrfList* list; osrfHashNode* node; osrfStringArray* strings = osrfNewStringArray(8); for( i = 0; i != hash->hash->size; i++ ) { - list = osrfListGetIndex( hash->hash, i ); + list = OSRF_LIST_GET_INDEX( hash->hash, i ); if(list) { for( k = 0; k != list->size; k++ ) { - node = osrfListGetIndex( list, k ); + node = OSRF_LIST_GET_INDEX( list, k ); if( node ) osrfStringArrayAdd( strings, node->key ); } } @@ -152,12 +215,10 @@ void osrfHashFree( osrfHash* hash ) { osrfHashNode* node; for( i = 0; i != hash->hash->size; i++ ) { - if( ( list = osrfListGetIndex( hash->hash, i )) ) { + if( ( list = OSRF_LIST_GET_INDEX( hash->hash, i )) ) { for( j = 0; j != list->size; j++ ) { - if( (node = osrfListGetIndex( list, j )) ) { - if( hash->freeItem ) - hash->freeItem( node->key, node->item ); - osrfHashNodeFree(node); + if( (node = OSRF_LIST_GET_INDEX( list, j )) ) { + OSRF_HASH_NODE_FREE(hash, node); } } osrfListFree(list); @@ -165,6 +226,7 @@ void osrfHashFree( osrfHash* hash ) { } osrfListFree(hash->hash); + osrfStringArrayFree(hash->keys); free(hash); } @@ -172,10 +234,12 @@ void osrfHashFree( osrfHash* hash ) { osrfHashIterator* osrfNewHashIterator( osrfHash* hash ) { if(!hash) return NULL; - osrfHashIterator* itr = safe_malloc(sizeof(osrfHashIterator)); + //osrfHashIterator* itr = safe_malloc(sizeof(osrfHashIterator)); + osrfHashIterator* itr; + OSRF_MALLOC(itr, sizeof(osrfHashIterator)); itr->hash = hash; itr->current = NULL; - itr->keys = osrfHashKeys(hash); + itr->keys = osrfHashKeysInc(hash); return itr; } @@ -192,15 +256,15 @@ void* osrfHashIteratorNext( osrfHashIterator* itr ) { void osrfHashIteratorFree( osrfHashIterator* itr ) { if(!itr) return; free(itr->current); - osrfStringArrayFree(itr->keys); + //osrfStringArrayFree(itr->keys); free(itr); } void osrfHashIteratorReset( osrfHashIterator* itr ) { if(!itr) return; free(itr->current); - osrfStringArrayFree(itr->keys); - itr->keys = osrfHashKeys(itr->hash); + //osrfStringArrayFree(itr->keys); + itr->keys = osrfHashKeysInc(itr->hash); itr->currentIdx = 0; itr->current = NULL; } diff --git a/src/libstack/osrf_hash.h b/src/libstack/osrf_hash.h index e176b51..7146e13 100644 --- a/src/libstack/osrf_hash.h +++ b/src/libstack/osrf_hash.h @@ -5,16 +5,22 @@ #include "opensrf/string_array.h" #include "osrf_list.h" -#define OSRF_HASH_MAXKEY 256 +/* 0x100 is a good size for small hashes */ +#define OSRF_HASH_LIST_SIZE 0x100 /* size of the main hash list */ -#define OSRF_HASH_KEY_MASK 0x7FF /* hash keys are a maximun of 2047 in size */ -#define OSRF_HASH_KEY_SIZE 2048 /* size of the container list for the keys */ +/* used internally */ +#define OSRF_HASH_NODE_FREE(h, n) \ + if(h && n) { \ + if(h->freeItem) h->freeItem(n->key, n->item);\ + free(n->key); free(n); \ + } struct __osrfHashStruct { osrfList* hash; /* this hash */ void (*freeItem) (char* key, void* item); /* callback for freeing stored items */ unsigned int size; + osrfStringArray* keys; }; typedef struct __osrfHashStruct osrfHash; @@ -34,7 +40,7 @@ struct __osrfHashIteratorStruct { typedef struct __osrfHashIteratorStruct osrfHashIterator; osrfHashNode* osrfNewHashNode(char* key, void* item); -void osrfHashNodeFree(osrfHashNode*); +void* osrfHashNodeFree(osrfHash*, osrfHashNode*); /** Allocates a new hash object @@ -69,6 +75,8 @@ void* osrfHashGet( osrfHash* hash, const char* key, ... ); */ osrfStringArray* osrfHashKeys( osrfHash* hash ); +osrfStringArray* osrfHashKeysInc( osrfHash* hash ); + /** Frees a hash */ diff --git a/src/libstack/osrf_list.c b/src/libstack/osrf_list.c index 9cfd89f..f4f75e4 100644 --- a/src/libstack/osrf_list.c +++ b/src/libstack/osrf_list.c @@ -1,20 +1,26 @@ #include "osrf_list.h" osrfList* osrfNewList() { - osrfList* list = safe_malloc(sizeof(osrfList)); + //osrfList* list = safe_malloc(sizeof(osrfList)); + osrfList* list; + OSRF_MALLOC(list, sizeof(osrfList)); list->size = 0; list->freeItem = NULL; list->arrsize = OSRF_LIST_DEFAULT_SIZE; - list->arrlist = safe_malloc( list->arrsize * sizeof(void*) ); + //list->arrlist = safe_malloc( list->arrsize * sizeof(void*) ); + OSRF_MALLOC(list->arrlist, list->arrsize * sizeof(void*)); return list; } osrfList* osrfNewListSize( unsigned int size ) { - osrfList* list = safe_malloc(sizeof(osrfList)); + //osrfList* list = safe_malloc(sizeof(osrfList)); + osrfList* list; + OSRF_MALLOC(list, sizeof(osrfList)); list->size = 0; list->freeItem = NULL; list->arrsize = size; - list->arrlist = safe_malloc( list->arrsize * sizeof(void*) ); + //list->arrlist = safe_malloc( list->arrsize * sizeof(void*) ); + OSRF_MALLOC( list->arrlist, list->arrsize * sizeof(void*) ); return list; } @@ -45,7 +51,7 @@ void* osrfListSet( osrfList* list, void* item, unsigned int position ) { newsize += OSRF_LIST_INC_SIZE; if( newsize > list->arrsize ) { /* expand the list if necessary */ - newarr = safe_malloc( newsize * sizeof(void*) ); + OSRF_MALLOC(newarr, newsize * sizeof(void*)); for( i = 0; i < list->arrsize; i++ ) newarr[i] = list->arrlist[i]; free(list->arrlist); @@ -121,7 +127,9 @@ void* osrfListPop( osrfList* list ) { osrfListIterator* osrfNewListIterator( osrfList* list ) { if(!list) return NULL; - osrfListIterator* itr = safe_malloc(sizeof(osrfListIterator)); + //osrfListIterator* itr = safe_malloc(sizeof(osrfListIterator)); + osrfListIterator* itr; + OSRF_MALLOC(itr, sizeof(osrfListIterator)); itr->list = list; itr->current = 0; return itr; diff --git a/src/libstack/osrf_list.h b/src/libstack/osrf_list.h index 99ede9f..cc1a2b7 100644 --- a/src/libstack/osrf_list.h +++ b/src/libstack/osrf_list.h @@ -7,6 +7,9 @@ #define OSRF_LIST_INC_SIZE 256 #define OSRF_LIST_MAX_SIZE 10240 + +#define OSRF_LIST_GET_INDEX(l, i) (!l || i >= l->size) ? NULL: l->arrlist[i] + /** Items are stored as void*'s so it's up to the user to manage the data wisely. Also, if the 'freeItem' callback is defined for the list, diff --git a/src/utils/string_array.c b/src/utils/string_array.c index fcc2567..ee8c542 100644 --- a/src/utils/string_array.c +++ b/src/utils/string_array.c @@ -8,9 +8,16 @@ string_array* init_string_array(int size) { if(size > STRING_ARRAY_MAX_SIZE) osrfLogError( OSRF_LOG_MARK, "init_string_array size is too large"); + /* string_array* arr = (string_array*) safe_malloc(sizeof(string_array)); - arr->array = (char**) safe_malloc(size * sizeof(char*)); + */ + string_array* arr; + OSRF_MALLOC( arr, sizeof(string_array)); + + //arr->array = (char**) safe_malloc(size * sizeof(char*)); + OSRF_MALLOC(arr->array, size * sizeof(char*)); + arr->size = 0; arr->arr_size = size; return arr; @@ -33,7 +40,9 @@ void string_array_add(string_array* arr, char* str) { /* if necessary, double capacity */ if(arr->size >= arr->arr_size) { arr->arr_size *= 2; - char** tmp = (char**) safe_malloc(arr->arr_size * sizeof(char*)); + //char** tmp = (char**) safe_malloc(arr->arr_size * sizeof(char*)); + char** tmp; + OSRF_MALLOC( tmp, arr->arr_size * sizeof(char*)); int i; /* copy the string pointers over */ @@ -85,3 +94,23 @@ int osrfStringArrayContains( osrfStringArray* arr, char* string ) { return 0; } +void osrfStringArrayRemove( osrfStringArray* arr, char* tstr) { + if(!(arr && tstr)) return; + int i; + for( i = 0; i != arr->size; i++ ) { + char* str = osrfStringArrayGetString(arr, i); + if(str) { + if(!strcmp(str, tstr)) { + free(arr->array[i]); + arr->array[i] = NULL; + break; + } + } + } + for( ; i != arr->size; i++ ) + arr->array[i] = arr->array[i+1]; + + arr->size--; +} + + diff --git a/src/utils/string_array.h b/src/utils/string_array.h index 0bee6e5..c235b93 100644 --- a/src/utils/string_array.h +++ b/src/utils/string_array.h @@ -36,5 +36,6 @@ void osrfStringArrayFree(osrfStringArray*); /* total size of all included strings */ int string_array_get_total_size(osrfStringArray* arr); +void osrfStringArrayRemove( osrfStringArray* arr, char* str); #endif