75 |
75 |
76 // reallocate if possible |
76 // reallocate if possible |
77 if (needrealloc) { |
77 if (needrealloc) { |
78 // a reallocator and a capacity variable must be available |
78 // a reallocator and a capacity variable must be available |
79 if (reallocator == NULL || capacity == NULL) { |
79 if (reallocator == NULL || capacity == NULL) { |
80 return CX_ARRAY_COPY_REALLOC_NOT_SUPPORTED; |
80 return CX_ARRAY_REALLOC_NOT_SUPPORTED; |
81 } |
81 } |
82 |
82 |
83 // check, if we need to repair the src pointer |
83 // check, if we need to repair the src pointer |
84 uintptr_t targetaddr = (uintptr_t) *target; |
84 uintptr_t targetaddr = (uintptr_t) *target; |
85 uintptr_t srcaddr = (uintptr_t) src; |
85 uintptr_t srcaddr = (uintptr_t) src; |
93 // perform reallocation |
93 // perform reallocation |
94 void *newmem = reallocator->realloc( |
94 void *newmem = reallocator->realloc( |
95 *target, cap, elem_size, reallocator |
95 *target, cap, elem_size, reallocator |
96 ); |
96 ); |
97 if (newmem == NULL) { |
97 if (newmem == NULL) { |
98 return CX_ARRAY_COPY_REALLOC_FAILED; |
98 return CX_ARRAY_REALLOC_FAILED; |
99 } |
99 } |
100 |
100 |
101 // repair src pointer, if necessary |
101 // repair src pointer, if necessary |
102 if (repairsrc) { |
102 if (repairsrc) { |
103 src = ((char *) newmem) + (srcaddr - targetaddr); |
103 src = ((char *) newmem) + (srcaddr - targetaddr); |
115 // copy elements and set new size |
115 // copy elements and set new size |
116 memmove(start, src, elem_count * elem_size); |
116 memmove(start, src, elem_count * elem_size); |
117 *size = newsize; |
117 *size = newsize; |
118 |
118 |
119 // return successfully |
119 // return successfully |
120 return CX_ARRAY_COPY_SUCCESS; |
120 return CX_ARRAY_SUCCESS; |
121 } |
121 } |
122 |
122 |
123 #ifndef CX_ARRAY_SWAP_SBO_SIZE |
123 #ifndef CX_ARRAY_SWAP_SBO_SIZE |
124 #define CX_ARRAY_SWAP_SBO_SIZE 128 |
124 #define CX_ARRAY_SWAP_SBO_SIZE 128 |
125 #endif |
125 #endif |
225 char const *first_to_move = (char const *) arl->data; |
225 char const *first_to_move = (char const *) arl->data; |
226 first_to_move += index * list->item_size; |
226 first_to_move += index * list->item_size; |
227 size_t elems_to_move = list->size - index; |
227 size_t elems_to_move = list->size - index; |
228 size_t start_of_moved = index + n; |
228 size_t start_of_moved = index + n; |
229 |
229 |
230 if (CX_ARRAY_COPY_SUCCESS != cx_array_copy( |
230 if (CX_ARRAY_SUCCESS != cx_array_copy( |
231 &arl->data, |
231 &arl->data, |
232 &list->size, |
232 &list->size, |
233 &arl->capacity, |
233 &arl->capacity, |
234 start_of_moved, |
234 start_of_moved, |
235 first_to_move, |
235 first_to_move, |
245 // note that if we had to move the elements, the following operation |
245 // note that if we had to move the elements, the following operation |
246 // is guaranteed to succeed, because we have the memory already allocated |
246 // is guaranteed to succeed, because we have the memory already allocated |
247 // therefore, it is impossible to leave this function with an invalid array |
247 // therefore, it is impossible to leave this function with an invalid array |
248 |
248 |
249 // place the new elements |
249 // place the new elements |
250 if (CX_ARRAY_COPY_SUCCESS == cx_array_copy( |
250 if (CX_ARRAY_SUCCESS == cx_array_copy( |
251 &arl->data, |
251 &arl->data, |
252 &list->size, |
252 &list->size, |
253 &arl->capacity, |
253 &arl->capacity, |
254 index, |
254 index, |
255 array, |
255 array, |