170 struct cx_array_reallocator_s { |
170 struct cx_array_reallocator_s { |
171 /** |
171 /** |
172 * Reallocates space for the given array. |
172 * Reallocates space for the given array. |
173 * |
173 * |
174 * Implementations are not required to free the original array. |
174 * Implementations are not required to free the original array. |
175 * This allows reallocation of static memory by allocating heap memory |
175 * This allows reallocation of static or stack memory by allocating heap memory |
176 * and copying the array contents. The information in the custom fields of |
176 * and copying the array contents; namely when @c stack_ptr in this struct |
177 * the referenced allocator can be used to track the state of the memory |
177 * is not @c NULL and @p array equals @c stack_ptr. |
178 * or to transport other additional data. |
|
179 * |
178 * |
180 * @param array the array to reallocate |
179 * @param array the array to reallocate |
181 * @param old_capacity the old number of elements |
180 * @param old_capacity the old number of elements |
182 * @param new_capacity the new number of elements |
181 * @param new_capacity the new number of elements |
183 * @param elem_size the size of each element |
182 * @param elem_size the size of each element |
194 size_t elem_size, |
193 size_t elem_size, |
195 struct cx_array_reallocator_s *alloc |
194 struct cx_array_reallocator_s *alloc |
196 ); |
195 ); |
197 |
196 |
198 /** |
197 /** |
199 * Custom data pointer. |
198 * The allocator that shall be used for the reallocations. |
200 */ |
199 */ |
201 void *ptr1; |
200 const CxAllocator *allocator; |
202 /** |
201 /** |
203 * Custom data pointer. |
202 * Optional pointer to stack memory |
|
203 * if the array is originally located on the stack. |
204 */ |
204 */ |
205 void *ptr2; |
205 const void *stack_ptr; |
206 /** |
|
207 * Custom data integer. |
|
208 */ |
|
209 size_t int1; |
|
210 /** |
|
211 * Custom data integer. |
|
212 */ |
|
213 size_t int2; |
|
214 }; |
206 }; |
215 |
207 |
216 /** |
208 /** |
217 * Typedef for the array reallocator struct. |
209 * Typedef for the array reallocator struct. |
218 */ |
210 */ |
227 /** |
219 /** |
228 * Creates a new array reallocator. |
220 * Creates a new array reallocator. |
229 * |
221 * |
230 * When @p allocator is @c NULL, the cxDefaultAllocator will be used. |
222 * When @p allocator is @c NULL, the cxDefaultAllocator will be used. |
231 * |
223 * |
232 * When @p stackmem is not @c NULL, the reallocator is supposed to be used |
224 * When @p stack_ptr is not @c NULL, the reallocator is supposed to be used |
233 * @em only for the specific array initially located at @p stackmem. |
225 * @em only for the specific array initially located at @p stack_ptr. |
234 * When reallocation is needed, the reallocator checks if the array is |
226 * When reallocation is needed, the reallocator checks if the array is |
235 * still located at @p stackmem and copies the contents to the heap. |
227 * still located at @p stack_ptr and copies the contents to the heap. |
236 * |
228 * |
237 * @note Invoking this function with both arguments being @c NULL will return a |
229 * @note Invoking this function with both arguments being @c NULL will return a |
238 * reallocator that behaves like #cx_array_default_reallocator. |
230 * reallocator that behaves like #cx_array_default_reallocator. |
239 * |
231 * |
240 * @param allocator the allocator this reallocator shall be based on |
232 * @param allocator the allocator this reallocator shall be based on |
241 * @param stackmem the address of the array when the array is initially located |
233 * @param stack_ptr the address of the array when the array is initially located |
242 * on the stack or shall not reallocate in place |
234 * on the stack or shall not reallocate in place |
243 * @return an array reallocator |
235 * @return an array reallocator |
244 */ |
236 */ |
245 cx_attr_export |
237 cx_attr_export |
246 CxArrayReallocator cx_array_reallocator( |
238 CxArrayReallocator cx_array_reallocator( |
247 const struct cx_allocator_s *allocator, |
239 const struct cx_allocator_s *allocator, |
248 const void *stackmem |
240 const void *stack_ptr |
249 ); |
241 ); |
250 |
242 |
251 /** |
243 /** |
252 * Reserves memory for additional elements. |
244 * Reserves memory for additional elements. |
253 * |
245 * |