diff --git a/fishd.cpp b/fishd.cpp index 7f297cd27..cb63e6aa1 100644 --- a/fishd.cpp +++ b/fishd.cpp @@ -322,7 +322,7 @@ static void broadcast( int type, const wchar_t *key, const wchar_t *val ) for( c = conn; c; c=c->next ) { msg->count++; - q_put( &c->unsent, msg ); + c->unsent->push(msg); } for( c = conn; c; c=c->next ) @@ -738,9 +738,10 @@ int main( int argc, char ** argv ) { debug( 4, L"Close connection %d", c->fd ); - while( !q_empty( &c->unsent ) ) + while( ! c->unsent->empty()) ) { - message_t *msg = (message_t *)q_get( &c->unsent ); + message_t *msg = c->unsent->front(); + c->unsent->pop(); msg->count--; if( !msg->count ) free( msg ); diff --git a/util.cpp b/util.cpp index bf3573b02..8c7e36597 100644 --- a/util.cpp +++ b/util.cpp @@ -101,88 +101,6 @@ int maxi( int a, } -/* Queue functions */ - - -void q_init( dyn_queue_t *q ) -{ - q->start = (void **)malloc( sizeof(void*)*1 ); - if( !q->start ) - { - oom_handler( q ); - return; - } - q->stop = &q->start[1]; - q->put_pos = q->get_pos = q->start; -} - -void q_destroy( dyn_queue_t *q ) -{ - free( q->start ); -} - -/** - Reallocate the queue_t -*/ -static int q_realloc( dyn_queue_t *q ) -{ - void **old_start = q->start; - void **old_stop = q->stop; - ptrdiff_t diff; - int new_size; - - new_size = 2*(q->stop-q->start); - - q->start=(void**)realloc( q->start, sizeof(void*)*new_size ); - if( !q->start ) - { - q->start = old_start; - oom_handler( q ); - return 0; - } - - diff = q->start - old_start; - q->get_pos += diff; - q->stop = &q->start[new_size]; - memcpy( old_stop + diff, q->start, sizeof(void*)*(q->get_pos-q->start)); - q->put_pos = old_stop + diff + (q->get_pos-q->start); - - return 1; -} - -int q_put( dyn_queue_t *q, void *e ) -{ - *q->put_pos = e; - -// fprintf( stderr, "Put element %d to queue %d\n", e, q ); - - if( ++q->put_pos == q->stop ) - q->put_pos = q->start; - if( q->put_pos == q->get_pos ) - return q_realloc( q ); - return 1; -} - -void *q_get( dyn_queue_t *q) -{ - void *e = *q->get_pos; - if( ++q->get_pos == q->stop ) - q->get_pos = q->start; - return e; -} - -void *q_peek( dyn_queue_t *q ) -{ - return *q->get_pos; -} - -int q_empty( dyn_queue_t *q ) -{ -// fprintf( stderr, "Queue %d is %s\n", q, (q->put_pos == q->get_pos)?"empty":"non-empty" ); - return q->put_pos == q->get_pos; -} - - /* Hash table functions */ void hash_init2( hash_table_t *h, @@ -664,113 +582,6 @@ int hash_ptr_cmp( void *a, return a == b; } -void pq_init( priority_queue_t *q, - int (*compare)(void *e1, void *e2) ) -{ - q->arr=0; - q->size=0; - q->count=0; - q->compare = compare; -} - - -int pq_put( priority_queue_t *q, - void *e ) -{ - int i; - - if( q->size == q->count ) - { - void **old_arr = q->arr; - int old_size = q->size; - q->size = maxi( 4, 2*q->size ); - q->arr = (void **)realloc( q->arr, sizeof(void*)*q->size ); - if( q->arr == 0 ) - { - oom_handler( q ); - q->arr = old_arr; - q->size = old_size; - return 0; - } - } - - i = q->count; - while( (i>0) && (q->compare( q->arr[(i-1)/2], e )<0 ) ) - { - q->arr[i] = q->arr[(i-1)/2]; - i = (i-1)/2; - } - q->arr[i]=e; - - q->count++; - - return 1; - -} - -/** - Make a valid head -*/ -static void pq_heapify( priority_queue_t *q, int i ) -{ - int l, r, largest; - l = 2*(i)+1; - r = 2*(i)+2; - if( (l < q->count) && (q->compare(q->arr[l],q->arr[i])>0) ) - { - largest = l; - } - else - { - largest = i; - } - if( (r < q->count) && (q->compare( q->arr[r],q->arr[largest])>0) ) - { - largest = r; - } - - if( largest != i ) - { - void *tmp = q->arr[largest]; - q->arr[largest]=q->arr[i]; - q->arr[i]=tmp; - pq_heapify( q, largest ); - } -} - -void *pq_get( priority_queue_t *q ) -{ - void *result = q->arr[0]; - q->arr[0] = q->arr[--q->count]; - pq_heapify( q, 0 ); - -/* pq_check(q, 0 ); */ -/* pq_print( q ); */ - - return result; -} - -void *pq_peek( priority_queue_t *q ) -{ - return q->arr[0]; -} - -int pq_empty( priority_queue_t *q ) -{ - return q->count == 0; -} - -int pq_get_count( priority_queue_t *q ) -{ - return q->count; -} - -void pq_destroy( priority_queue_t *q ) -{ - free( q->arr ); -} - - array_list_t *al_new() { array_list_t *res = (array_list_t *)malloc( sizeof( array_list_t ) ); diff --git a/util.h b/util.h index 948cccdb4..ad90bb681 100644 --- a/util.h +++ b/util.h @@ -42,23 +42,6 @@ typedef union } anything_t; - -/** - Data structure for an automatically resizing dynamically allocated queue, -*/ -typedef struct dyn_queue -{ - /** Start of the array */ - void **start; - /** End of the array*/ - void **stop; - /** Where to insert elements */ - void **put_pos; - /** Where to remove elements */ - void **get_pos; -} - dyn_queue_t; - /** Internal struct used by hash_table_t. */ @@ -210,38 +193,6 @@ int mini( int a, int b ); to never be less than 50% full. */ -/** - Initialize the queue. A queue is a FIFO buffer, i.e. the first - element to be inserted into the buffer is the first element to be - returned. -*/ -void q_init( dyn_queue_t *q ); - -/** - Destroy the queue -*/ -void q_destroy( dyn_queue_t *q ); - -/** - Insert element into queue -*/ -int q_put( dyn_queue_t *q, void *e ); - -/** - Remove and return next element from queue -*/ -void *q_get( dyn_queue_t *q); - -/** - Return next element from queue without removing it -*/ -void *q_peek( dyn_queue_t *q); - -/** - Returns 1 if the queue is empty, 0 otherwise -*/ -int q_empty( dyn_queue_t *q ); - /** Initialize a hash table. The hash function must never return the value 0. */ @@ -364,49 +315,6 @@ int hash_ptr_cmp( void *a, void *b ); - -/** - Initialize the priority queue - - \param q the queue to initialize - \param compare a comparison function that can compare two entries in the queue -*/ -void pq_init( priority_queue_t *q, - int (*compare)(void *e1, void *e2) ); -/** - Add element to the queue - - \param q the queue - \param e the new element - -*/ -int pq_put( priority_queue_t *q, - void *e ); -/** - Removes and returns the last entry in the priority queue -*/ -void *pq_get( priority_queue_t *q ); - -/** - Returns the last entry in the priority queue witout removing it. -*/ -void *pq_peek( priority_queue_t *q ); - -/** - Returns 1 if the priority queue is empty, 0 otherwise. -*/ -int pq_empty( priority_queue_t *q ); - -/** - Returns the number of elements in the priority queue. -*/ -int pq_get_count( priority_queue_t *q ); - -/** - Destroy the priority queue and free memory used by it. -*/ -void pq_destroy( priority_queue_t *q ); - /** Allocate heap memory for creating a new list and initialize it. Equivalent to calling malloc and al_init.