18 #include "mysys_priv.h"
43 my_bool init_dynamic_array2(
DYNAMIC_ARRAY *array, uint element_size,
44 void *init_buffer, uint init_alloc,
47 DBUG_ENTER(
"init_dynamic_array");
50 alloc_increment=MY_MAX((8192-MALLOC_OVERHEAD)/element_size,16);
51 if (init_alloc > 8 && alloc_increment > init_alloc * 2)
52 alloc_increment=init_alloc*2;
57 init_alloc=alloc_increment;
61 array->max_element=init_alloc;
62 array->alloc_increment=alloc_increment;
63 array->size_of_element=element_size;
64 if ((array->buffer= init_buffer))
70 if (!(array->buffer= (uchar*) my_malloc(element_size*init_alloc, MYF(0))))
75 my_bool init_dynamic_array(
DYNAMIC_ARRAY *array, uint element_size,
76 uint init_alloc, uint alloc_increment)
79 return my_init_dynamic_array_ci(array, element_size, init_alloc,
95 my_bool insert_dynamic(
DYNAMIC_ARRAY *array,
const void *element)
98 if (array->elements == array->max_element)
100 if (!(buffer=alloc_dynamic(array)))
105 buffer=array->buffer+(array->elements * array->size_of_element);
108 memcpy(buffer,element,(
size_t) array->size_of_element);
132 if (array->elements == array->max_element)
135 if (array->buffer == (uchar *)(array + 1))
141 if (!(new_ptr= (
char *) my_malloc((array->max_element+
142 array->alloc_increment) *
143 array->size_of_element,
146 memcpy(new_ptr, array->buffer,
147 array->elements * array->size_of_element);
150 if (!(new_ptr=(
char*) my_realloc(array->buffer,(array->max_element+
151 array->alloc_increment)*
152 array->size_of_element,
153 MYF(MY_WME | MY_ALLOW_ZERO_PTR))))
155 array->buffer= (uchar*) new_ptr;
156 array->max_element+=array->alloc_increment;
158 return array->buffer+(array->elements++ * array->size_of_element);
177 return array->buffer+(--array->elements * array->size_of_element);
199 my_bool set_dynamic(
DYNAMIC_ARRAY *array,
const void *element, uint idx)
201 if (idx >= array->elements)
203 if (idx >= array->max_element && allocate_dynamic(array, idx))
205 memset((array->buffer+array->elements*array->size_of_element), 0,
206 (idx - array->elements)*array->size_of_element);
207 array->elements=idx+1;
209 memcpy(array->buffer+(idx * array->size_of_element),element,
210 (
size_t) array->size_of_element);
231 my_bool allocate_dynamic(
DYNAMIC_ARRAY *array, uint max_elements)
233 if (max_elements >= array->max_element)
237 size= (max_elements + array->alloc_increment)/array->alloc_increment;
238 size*= array->alloc_increment;
239 if (array->buffer == (uchar *)(array + 1))
245 if (!(new_ptr= (uchar *) my_malloc(size *
246 array->size_of_element,
249 memcpy(new_ptr, array->buffer,
250 array->elements * array->size_of_element);
255 if (!(new_ptr= (uchar*) my_realloc(array->buffer,size*
256 array->size_of_element,
257 MYF(MY_WME | MY_ALLOW_ZERO_PTR))))
259 array->buffer= new_ptr;
260 array->max_element=
size;
276 void get_dynamic(
DYNAMIC_ARRAY *array,
void *element, uint idx)
278 if (idx >= array->elements)
280 DBUG_PRINT(
"warning",(
"To big array idx: %d, array size is %d",
281 idx,array->elements));
282 memset(element, 0, array->size_of_element);
285 memcpy(element,array->buffer+idx*array->size_of_element,
286 (
size_t) array->size_of_element);
303 if (array->buffer == (uchar *)(array + 1))
308 my_free(array->buffer);
310 array->elements=array->max_element=0;
325 char *ptr= (
char*) array->buffer+array->size_of_element*idx;
327 memmove(ptr,ptr+array->size_of_element,
328 (array->elements-idx)*array->size_of_element);
343 uint elements=MY_MAX(array->elements,1);
348 if (array->buffer == (uchar *)(array + 1))
351 if (array->buffer && array->max_element != elements)
353 array->buffer=(uchar*) my_realloc(array->buffer,
354 elements*array->size_of_element,
356 array->max_element=elements;