4 #include "opensrf/utils.h"
6 #define OSRF_LIST_DEFAULT_SIZE 48 /* most opensrf lists are small... */
7 #define OSRF_LIST_INC_SIZE 256
8 #define OSRF_LIST_MAX_SIZE 10240
11 Items are stored as void*'s so it's up to the user to
12 manage the data wisely. Also, if the 'freeItem' callback is defined for the list,
13 then, it will be used on any item that needs to be freed, so don't mix data
14 types in the list if you want magic freeing */
16 struct __osrfListStruct {
17 unsigned int size; /* how many items in the list including NULL items between non-NULL items */
18 void (*freeItem) (void* item); /* callback for freeing stored items */
20 int arrsize; /* how big is the currently allocated array */
22 typedef struct __osrfListStruct osrfList;
25 struct __osrfListIteratorStruct {
29 typedef struct __osrfListIteratorStruct osrfListIterator;
31 osrfList* osrfNewListSize( unsigned int size );
35 Creates a new list iterator with the given list
37 osrfListIterator* osrfNewListIterator( osrfList* list );
40 Returns the next non-NULL item in the list, return NULL when
41 the end of the list has been reached
43 void* osrfListIteratorNext( osrfListIterator* itr );
46 Deallocates the given list
48 void osrfListIteratorFree( osrfListIterator* itr );
50 void osrfListIteratorReset( osrfListIterator* itr );
55 @param compress If true, the list will compress empty slots on delete. If item positionality
56 is not important, then using this feature is reccomended to keep the list from growing indefinitely.
57 if item positionality is not important.
58 @return The allocated list
60 osrfList* osrfNewList();
63 Pushes an item onto the end of the list. This always finds the highest index
64 in the list and pushes the new item into the list after it.
66 @param item The item to push
67 @return 0 on success, -1 on failure
69 int osrfListPush( osrfList* list, void* item );
73 * Removes the last item in the list
74 * See osrfListRemove for details on how the removed item is handled
75 * @return The item, unless 'freeItem' exists, then returns NULL
77 void* osrfListPop( osrfList* list );
80 Puts the given item into the list at the specified position. If there
81 is already an item at the given position and the list has it's
82 "freeItem" function defined, then it will be used to free said item.
83 If no 'freeItem' callback is defined, then the displaced item will
86 @param item The item to put into the list
87 @param position The position to place the item in
88 @return NULL in successfully inserting the new item and freeing
89 any displaced items. Returns the displaced item if no "freeItem"
92 void* osrfListSet( osrfList* list, void* item, unsigned int position );
95 Returns the item at the given position
97 @param postiont the position
99 void* osrfListGetIndex( osrfList* list, unsigned int position );
102 Frees the list and all list items (if the list has a "freeItem" function defined )
105 void osrfListFree( osrfList* list );
108 Removes the list item at the given index
110 @param position The position of the item to remove
111 @return A pointer to the item removed if "freeItem" is not defined
112 for this list, returns NULL if it is.
114 void* osrfListRemove( osrfList* list, int position );
117 Finds the list item whose void* is the same as the one passed in
119 @param addr The pointer connected to the list item we're to find
120 @return the index of the item, or -1 if the item was not found
122 int osrfListFind( osrfList* list, void* addr );
125 void __osrfListSetSize( osrfList* list );
129 @return The number of non-null items in the list
131 unsigned int osrfListGetCount( osrfList* list );
134 * May be used as a default memory freeing call
135 * Just calls free() on list items
137 void osrfListVanillaFree( void* item );
140 * Tells the list to just call 'free()' on each item when
141 * an item or the whole list is destroyed
143 void osrfListSetDefaultFree( osrfList* list );