ucx/cx/array_list.h

branch
dav-2
changeset 886
da79af4baec8
parent 854
1c8401ece69e
child 889
42cdbf9bbd49
equal deleted inserted replaced
885:591377a27fa3 886:da79af4baec8
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 = cxMallocDefault(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()
176 * and copying the array contents. The information in the custom fields of 176 * and copying the array contents. The information in the custom fields of
177 * the referenced allocator can be used to track the state of the memory 177 * the referenced allocator can be used to track the state of the memory
178 * or to transport other additional data. 178 * or to transport other additional data.
179 * 179 *
180 * @param array the array to reallocate 180 * @param array the array to reallocate
181 * @param capacity the new capacity (number of elements) 181 * @param old_capacity the old number of elements
182 * @param new_capacity the new number of elements
182 * @param elem_size the size of each element 183 * @param elem_size the size of each element
183 * @param alloc a reference to this allocator 184 * @param alloc a reference to this allocator
184 * @return a pointer to the reallocated memory or @c NULL on failure 185 * @return a pointer to the reallocated memory or @c NULL on failure
185 */ 186 */
186 cx_attr_nodiscard 187 cx_attr_nodiscard
187 cx_attr_nonnull_arg(4) 188 cx_attr_nonnull_arg(5)
188 cx_attr_allocsize(2, 3) 189 cx_attr_allocsize(3, 4)
189 void *(*realloc)( 190 void *(*realloc)(
190 void *array, 191 void *array,
191 size_t capacity, 192 size_t old_capacity,
193 size_t new_capacity,
192 size_t elem_size, 194 size_t elem_size,
193 struct cx_array_reallocator_s *alloc 195 struct cx_array_reallocator_s *alloc
194 ); 196 );
195 197
196 /** 198 /**
215 * Typedef for the array reallocator struct. 217 * Typedef for the array reallocator struct.
216 */ 218 */
217 typedef struct cx_array_reallocator_s CxArrayReallocator; 219 typedef struct cx_array_reallocator_s CxArrayReallocator;
218 220
219 /** 221 /**
220 * A default stdlib-based array reallocator. 222 * A default array reallocator that is based on the cxDefaultAllocator.
221 */ 223 */
222 cx_attr_export 224 cx_attr_export
223 extern CxArrayReallocator *cx_array_default_reallocator; 225 extern CxArrayReallocator *cx_array_default_reallocator;
224 226
225 /** 227 /**
226 * Creates a new array reallocator. 228 * Creates a new array reallocator.
227 * 229 *
228 * When @p allocator is @c NULL, the stdlib default allocator will be used. 230 * When @p allocator is @c NULL, the cxDefaultAllocator will be used.
229 * 231 *
230 * When @p stackmem is not @c NULL, the reallocator is supposed to be used 232 * 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. 233 * @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 234 * When reallocation is needed, the reallocator checks, if the array is
233 * still located at @p stackmem and copies the contents to the heap. 235 * 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 699 * 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 700 * copies of the added elements and the compare function will be automatically set
699 * to cx_cmp_ptr(), if none is given. 701 * to cx_cmp_ptr(), if none is given.
700 * 702 *
701 * @param allocator the allocator for allocating the list memory 703 * @param allocator the allocator for allocating the list memory
702 * (if @c NULL, a default stdlib allocator will be used) 704 * (if @c NULL, the cxDefaultAllocator will be used)
703 * @param comparator the comparator for the elements 705 * @param comparator the comparator for the elements
704 * (if @c NULL, and the list is not storing pointers, sort and find 706 * (if @c NULL, and the list is not storing pointers, sort and find
705 * functions will not work) 707 * functions will not work)
706 * @param elem_size the size of each element in bytes 708 * @param elem_size the size of each element in bytes
707 * @param initial_capacity the initial number of elements the array can store 709 * @param initial_capacity the initial number of elements the array can store
725 * If you want to call functions that need a compare function, you have to 727 * If you want to call functions that need a compare function, you have to
726 * set it immediately after creation or use cxArrayListCreate(). 728 * set it immediately after creation or use cxArrayListCreate().
727 * 729 *
728 * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of 730 * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of
729 * copies of the added elements and the compare function will be automatically set 731 * copies of the added elements and the compare function will be automatically set
730 * to cx_cmp_ptr(), if none is given. 732 * to cx_cmp_ptr().
731 * 733 *
732 * @param elem_size (@c size_t) the size of each element in bytes 734 * @param elem_size (@c size_t) the size of each element in bytes
733 * @param initial_capacity (@c size_t) the initial number of elements the array can store 735 * @param initial_capacity (@c size_t) the initial number of elements the array can store
734 * @return the created list 736 * @return the created list
735 */ 737 */

mercurial