3 osrfList* osrfNewList() {
4 osrfList* list = safe_malloc(sizeof(osrfList));
7 list->arrsize = OSRF_LIST_DEFAULT_SIZE;
8 list->arrlist = safe_malloc( list->arrsize * sizeof(void*) );
12 osrfList* osrfNewListSize( unsigned int size ) {
13 osrfList* list = safe_malloc(sizeof(osrfList));
15 list->freeItem = NULL;
17 list->arrlist = safe_malloc( list->arrsize * sizeof(void*) );
22 int osrfListPush( osrfList* list, void* item ) {
23 if(!(list)) return -1;
24 osrfListSet( list, item, list->size );
28 void* osrfListSet( osrfList* list, void* item, unsigned int position ) {
29 if(!list || position < 0) return NULL;
32 int newsize = list->arrsize;
35 while( position >= newsize )
36 newsize += OSRF_LIST_INC_SIZE;
38 if( newsize > list->arrsize ) { /* expand the list if necessary */
39 newarr = safe_malloc( newsize * sizeof(void*) );
40 for( i = 0; i < list->arrsize; i++ )
41 newarr[i] = list->arrlist[i];
43 list->arrlist = newarr;
44 list->arrsize = newsize;
47 void* olditem = osrfListRemove( list, position );
48 list->arrlist[position] = item;
49 if( list->size == 0 || list->size <= position )
50 list->size = position + 1;
55 void* osrfListGetIndex( osrfList* list, unsigned int position ) {
56 if(!list || position >= list->size) return NULL;
57 return list->arrlist[position];
60 void osrfListFree( osrfList* list ) {
63 if( list->freeItem ) {
65 for( i = 0; i < list->size; i++ ) {
66 if( (val = list->arrlist[i]) )
75 void* osrfListRemove( osrfList* list, int position ) {
76 if(!list || position >= list->size) return NULL;
78 void* olditem = list->arrlist[position];
79 list->arrlist[position] = NULL;
80 if( list->freeItem ) {
81 list->freeItem(olditem);
85 if( position == list->size - 1 ) list->size--;
90 int osrfListFind( osrfList* list, void* addr ) {
91 if(!(list && addr)) return -1;
93 for( index = 0; index < list->size; index++ ) {
94 if( list->arrlist[index] == addr )
101 unsigned int osrfListGetCount( osrfList* list ) {
107 void* osrfListPop( osrfList* list ) {
108 if(!list) return NULL;
109 return osrfListRemove( list, list->size - 1 );
113 osrfListIterator* osrfNewListIterator( osrfList* list ) {
114 if(!list) return NULL;
115 osrfListIterator* itr = safe_malloc(sizeof(osrfListIterator));
121 void* osrfListIteratorNext( osrfListIterator* itr ) {
122 if(!(itr && itr->list)) return NULL;
123 if(itr->current >= itr->list->size) return NULL;
124 return itr->list->arrlist[itr->current++];
127 void osrfListIteratorFree( osrfListIterator* itr ) {
133 void osrfListIteratorReset( osrfListIterator* itr ) {
139 void osrfListVanillaFree( void* item ) {
143 void osrfListSetDefaultFree( osrfList* list ) {
145 list->freeItem = osrfListVanillaFree;