1
/* Copyright (C) 2000 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
/* Handling of arrays that can grow dynamicly. */
18
#include "mysys_priv.h"
22
Initiate dynamic array
26
array Pointer to an array
27
element_size Size of element
28
init_buffer Initial buffer pointer
29
init_alloc Number of initial elements
30
alloc_increment Increment for adding new elements
33
init_dynamic_array() initiates array and allocate space for
35
Array is usable even if space allocation failed.
36
Static buffers must begin immediately after the array structure.
39
TRUE my_malloc_ci() failed
43
my_bool init_dynamic_array2(DYNAMIC_ARRAY *array, uint element_size,
44
void *init_buffer, uint init_alloc,
45
uint alloc_increment CALLER_INFO_PROTO)
47
DBUG_ENTER("init_dynamic_array");
50
alloc_increment=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))
66
if (!(array->buffer=(uchar*) my_malloc_ci(element_size*init_alloc,
75
my_bool init_dynamic_array(DYNAMIC_ARRAY *array, uint element_size,
77
uint alloc_increment CALLER_INFO_PROTO)
79
/* placeholder to preserve ABI */
80
return my_init_dynamic_array_ci(array, element_size, init_alloc,
84
Insert element at the end of array. Allocate memory if needed.
96
my_bool insert_dynamic(DYNAMIC_ARRAY *array, uchar* element)
99
if (array->elements == array->max_element)
100
{ /* Call only when nessesary */
101
if (!(buffer=alloc_dynamic(array)))
106
buffer=array->buffer+(array->elements * array->size_of_element);
109
memcpy(buffer,element,(size_t) array->size_of_element);
115
Alloc space for next element(s)
122
alloc_dynamic() checks if there is empty space for at least
123
one element if not tries to allocate space for alloc_increment
124
elements at the end of array.
127
pointer Pointer to empty space for element
131
uchar *alloc_dynamic(DYNAMIC_ARRAY *array)
133
if (array->elements == array->max_element)
136
if (array->buffer == (uchar *)(array + 1))
139
In this senerio, the buffer is statically preallocated,
140
so we have to create an all-new malloc since we overflowed
142
if (!(new_ptr= (char *) my_malloc((array->max_element+
143
array->alloc_increment) *
144
array->size_of_element,
147
memcpy(new_ptr, array->buffer,
148
array->elements * array->size_of_element);
151
if (!(new_ptr=(char*) my_realloc(array->buffer,(array->max_element+
152
array->alloc_increment)*
153
array->size_of_element,
154
MYF(MY_WME | MY_ALLOW_ZERO_PTR))))
156
array->buffer= (uchar*) new_ptr;
157
array->max_element+=array->alloc_increment;
159
return array->buffer+(array->elements++ * array->size_of_element);
164
Pop last element from array.
175
uchar *pop_dynamic(DYNAMIC_ARRAY *array)
178
return array->buffer+(--array->elements * array->size_of_element);
183
Replace element in array with given element and index
188
element Element to be inserted
189
idx Index where element is to be inserted
192
set_dynamic() replaces element in array.
193
If idx > max_element insert new element. Allocate memory if needed.
196
TRUE Idx was out of range and allocation of new memory failed
200
my_bool set_dynamic(DYNAMIC_ARRAY *array, uchar* element, uint idx)
202
if (idx >= array->elements)
204
if (idx >= array->max_element && allocate_dynamic(array, idx))
206
bzero((uchar*) (array->buffer+array->elements*array->size_of_element),
207
(idx - array->elements)*array->size_of_element);
208
array->elements=idx+1;
210
memcpy(array->buffer+(idx * array->size_of_element),element,
211
(size_t) array->size_of_element);
217
Ensure that dynamic array has enough elements
222
max_elements Numbers of elements that is needed
225
Any new allocated element are NOT initialized
229
TRUE Allocation of new memory failed
232
my_bool allocate_dynamic(DYNAMIC_ARRAY *array, uint max_elements)
234
if (max_elements >= array->max_element)
238
size= (max_elements + array->alloc_increment)/array->alloc_increment;
239
size*= array->alloc_increment;
240
if (array->buffer == (uchar *)(array + 1))
243
In this senerio, the buffer is statically preallocated,
244
so we have to create an all-new malloc since we overflowed
246
if (!(new_ptr= (uchar *) my_malloc(size *
247
array->size_of_element,
250
memcpy(new_ptr, array->buffer,
251
array->elements * array->size_of_element);
256
if (!(new_ptr= (uchar*) my_realloc(array->buffer,size*
257
array->size_of_element,
258
MYF(MY_WME | MY_ALLOW_ZERO_PTR))))
260
array->buffer= new_ptr;
261
array->max_element= size;
268
Get an element from array by given index
273
uchar* Element to be returned. If idx > elements contain zeroes.
274
idx Index of element wanted.
277
void get_dynamic(DYNAMIC_ARRAY *array, uchar* element, uint idx)
279
if (idx >= array->elements)
281
DBUG_PRINT("warning",("To big array idx: %d, array size is %d",
282
idx,array->elements));
283
bzero(element,array->size_of_element);
286
memcpy(element,array->buffer+idx*array->size_of_element,
287
(size_t) array->size_of_element);
292
Empty array by freeing all memory
296
array Array to be deleted
299
void delete_dynamic(DYNAMIC_ARRAY *array)
302
Just mark as empty if we are using a static buffer
304
if (array->buffer == (uchar *)(array + 1))
309
my_free(array->buffer,MYF(MY_WME));
311
array->elements=array->max_element=0;
316
Delete element by given index
319
delete_dynamic_element()
321
idx Index of element to be deleted
324
void delete_dynamic_element(DYNAMIC_ARRAY *array, uint idx)
326
char *ptr= (char*) array->buffer+array->size_of_element*idx;
328
memmove(ptr,ptr+array->size_of_element,
329
(array->elements-idx)*array->size_of_element);
338
array Array to be freed
342
void freeze_size(DYNAMIC_ARRAY *array)
344
uint elements=max(array->elements,1);
347
Do nothing if we are using a static buffer
349
if (array->buffer == (uchar *)(array + 1))
352
if (array->buffer && array->max_element != elements)
354
array->buffer=(uchar*) my_realloc(array->buffer,
355
elements*array->size_of_element,
357
array->max_element=elements;
363
Get the index of a dynamic element
368
element Whose element index
372
int get_index_dynamic(DYNAMIC_ARRAY *array, uchar* element)
375
if (array->buffer > element)
378
ret= (element - array->buffer) / array->size_of_element;
379
if (ret > array->elements)