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 );
}
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;
}
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;
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 );
}
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 );
}
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 );
}
#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;
}
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;
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, ... ) {
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 );
}
osrfHashNode* node = osrfNewHashNode(VA_BUF, item);
osrfListPushFirst( bucket, node );
+ if(!osrfStringArrayContains(hash->keys, VA_BUF))
+ osrfStringArrayAdd( hash->keys, VA_BUF );
+
hash->size++;
return olditem;
}
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;
}
}
-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 );
}
}
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);
}
osrfListFree(hash->hash);
+ osrfStringArrayFree(hash->keys);
free(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;
}
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;
}
#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;
}
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);
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;