src/cx/array_list.h

changeset 1318
12fa1d37fe48
parent 1239
b4b1f15d1866
child 1319
aa1f580f8f59
equal deleted inserted replaced
1317:eeb2fc3850e2 1318:12fa1d37fe48
121 * cx_array_initialize(arr1, 500); // error: maximum for uint8_t is 255 121 * cx_array_initialize(arr1, 500); // error: maximum for uint8_t is 255
122 * cx_array_initialize(arr2, 500); // OK 122 * cx_array_initialize(arr2, 500); // OK
123 * @endcode 123 * @endcode
124 * 124 *
125 * 125 *
126 * The memory for the array is allocated with stdlib malloc(). 126 * The memory for the array is allocated with the cxDefaultAllocator.
127 *
127 * @param array the name of the array 128 * @param array the name of the array
128 * @param capacity the initial capacity 129 * @param capacity the initial capacity
129 * @see cx_array_initialize_a() 130 * @see cx_array_initialize_a()
130 * @see CX_ARRAY_DECLARE_SIZED() 131 * @see CX_ARRAY_DECLARE_SIZED()
131 * @see CX_ARRAY_DECLARE() 132 * @see CX_ARRAY_DECLARE()
132 */ 133 */
133 #define cx_array_initialize(array, capacity) \ 134 #define cx_array_initialize(array, capacity) \
134 array##_capacity = capacity; \ 135 array##_capacity = capacity; \
135 array##_size = 0; \ 136 array##_size = 0; \
136 array = malloc(sizeof(array[0]) * capacity) 137 array = cxMalloc(cxDefaultAllocator, sizeof(array[0]) * capacity)
137 138
138 /** 139 /**
139 * Initializes an array with the given capacity using the specified allocator. 140 * Initializes an array with the given capacity using the specified allocator.
140 * 141 *
141 * @par Example 142 * @par Example
147 * cx_array_initialize_a(al, myarray, 128); 148 * cx_array_initialize_a(al, myarray, 128);
148 * // ... 149 * // ...
149 * cxFree(al, myarray); // don't forget to free with same allocator 150 * cxFree(al, myarray); // don't forget to free with same allocator
150 * @endcode 151 * @endcode
151 * 152 *
152 * The memory for the array is allocated with stdlib malloc().
153 * @param allocator (@c CxAllocator*) the allocator 153 * @param allocator (@c CxAllocator*) the allocator
154 * @param array the name of the array 154 * @param array the name of the array
155 * @param capacity the initial capacity 155 * @param capacity the initial capacity
156 * @see cx_array_initialize() 156 * @see cx_array_initialize()
157 * @see CX_ARRAY_DECLARE_SIZED() 157 * @see CX_ARRAY_DECLARE_SIZED()
215 * Typedef for the array reallocator struct. 215 * Typedef for the array reallocator struct.
216 */ 216 */
217 typedef struct cx_array_reallocator_s CxArrayReallocator; 217 typedef struct cx_array_reallocator_s CxArrayReallocator;
218 218
219 /** 219 /**
220 * A default stdlib-based array reallocator. 220 * A default array reallocator that is based on the cxDefaultAllocator.
221 */ 221 */
222 cx_attr_export 222 cx_attr_export
223 extern CxArrayReallocator *cx_array_default_reallocator; 223 extern CxArrayReallocator *cx_array_default_reallocator;
224 224
225 /** 225 /**
226 * Creates a new array reallocator. 226 * Creates a new array reallocator.
227 * 227 *
228 * When @p allocator is @c NULL, the stdlib default allocator will be used. 228 * When @p allocator is @c NULL, the cxDefaultAllocator will be used.
229 * 229 *
230 * When @p stackmem is not @c NULL, the reallocator is supposed to be used 230 * When @p stackmem is not @c NULL, the reallocator is supposed to be used
231 * @em only for the specific array that is initially located at @p stackmem. 231 * @em only for the specific array that is initially located at @p stackmem.
232 * When reallocation is needed, the reallocator checks, if the array is 232 * When reallocation is needed, the reallocator checks, if the array is
233 * still located at @p stackmem and copies the contents to the heap. 233 * still located at @p stackmem and copies the contents to the heap.
697 * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of 697 * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of
698 * copies of the added elements and the compare function will be automatically set 698 * copies of the added elements and the compare function will be automatically set
699 * to cx_cmp_ptr(), if none is given. 699 * to cx_cmp_ptr(), if none is given.
700 * 700 *
701 * @param allocator the allocator for allocating the list memory 701 * @param allocator the allocator for allocating the list memory
702 * (if @c NULL, a default stdlib allocator will be used) 702 * (if @c NULL, the cxDefaultAllocator will be used)
703 * @param comparator the comparator for the elements 703 * @param comparator the comparator for the elements
704 * (if @c NULL, and the list is not storing pointers, sort and find 704 * (if @c NULL, and the list is not storing pointers, sort and find
705 * functions will not work) 705 * functions will not work)
706 * @param elem_size the size of each element in bytes 706 * @param elem_size the size of each element in bytes
707 * @param initial_capacity the initial number of elements the array can store 707 * @param initial_capacity the initial number of elements the array can store

mercurial