12
12
You should have received a copy of the GNU General Public License
13
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
16
/* Handling of arrays that can grow dynamicly. */
19
#include "drizzled/internal/my_sys.h"
20
#include "drizzled/internal/m_string.h"
29
static bool allocate_dynamic(DYNAMIC_ARRAY *array, uint32_t max_elements);
18
#include "mysys_priv.h"
19
#include <mystrings/m_string.h>
32
22
Initiate dynamic array
40
30
alloc_increment Increment for adding new elements
43
init_dynamic_array() initiates array and allocate space for
33
init_dynamic_array() initiates array and allocate space for
45
35
Array is usable even if space allocation failed.
46
36
Static buffers must begin immediately after the array structure.
39
true my_malloc_ci() failed
53
bool init_dynamic_array2(DYNAMIC_ARRAY *array, uint32_t element_size,
54
void *init_buffer, uint32_t init_alloc,
55
uint32_t alloc_increment)
43
bool init_dynamic_array2(DYNAMIC_ARRAY *array, uint element_size,
44
void *init_buffer, uint init_alloc,
45
uint alloc_increment CALLER_INFO_PROTO)
57
47
if (!alloc_increment)
59
alloc_increment=max((8192-MALLOC_OVERHEAD)/element_size,16U);
49
alloc_increment=max((8192-MALLOC_OVERHEAD)/element_size,16);
60
50
if (init_alloc > 8 && alloc_increment > init_alloc * 2)
61
51
alloc_increment=init_alloc*2;
70
60
array->max_element=init_alloc;
71
61
array->alloc_increment=alloc_increment;
72
62
array->size_of_element=element_size;
73
if ((array->buffer= (unsigned char*) init_buffer))
63
if ((array->buffer= init_buffer))
75
if (!(array->buffer=(unsigned char*) malloc(element_size*init_alloc)))
65
if (!(array->buffer=(uchar*) my_malloc_ci(element_size*init_alloc,
77
68
array->max_element=0;
74
bool init_dynamic_array(DYNAMIC_ARRAY *array, uint element_size,
76
uint alloc_increment CALLER_INFO_PROTO)
78
/* placeholder to preserve ABI */
79
return my_init_dynamic_array_ci(array, element_size, init_alloc,
84
83
Insert element at the end of array. Allocate memory if needed.
96
bool insert_dynamic(DYNAMIC_ARRAY *array, unsigned char* element)
95
bool insert_dynamic(DYNAMIC_ARRAY *array, uchar* element)
98
unsigned char* buffer;
99
98
if (array->elements == array->max_element)
100
99
{ /* Call only when nessesary */
101
100
if (!(buffer=alloc_dynamic(array)))
131
unsigned char *alloc_dynamic(DYNAMIC_ARRAY *array)
130
uchar *alloc_dynamic(DYNAMIC_ARRAY *array)
133
132
if (array->elements == array->max_element)
136
if (array->buffer == (unsigned char *)(array + 1))
135
if (array->buffer == (uchar *)(array + 1))
139
138
In this senerio, the buffer is statically preallocated,
140
139
so we have to create an all-new malloc since we overflowed
142
if (!(new_ptr= (char *) malloc((array->max_element+
143
array->alloc_increment) *
144
array->size_of_element)))
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,
146
memcpy(new_ptr, array->buffer,
147
147
array->elements * array->size_of_element);
149
else if (!(new_ptr= (char*) realloc(array->buffer,
151
array->alloc_increment)*
152
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))))
154
array->buffer= (unsigned char*) new_ptr;
155
array->buffer= (uchar*) new_ptr;
155
156
array->max_element+=array->alloc_increment;
157
158
return array->buffer+(array->elements++ * array->size_of_element);
187
188
idx Index where element is to be inserted
190
set_dynamic() replaces element in array.
191
If idx > max_element insert new element. Allocate memory if needed.
191
set_dynamic() replaces element in array.
192
If idx > max_element insert new element. Allocate memory if needed.
194
195
true Idx was out of range and allocation of new memory failed
198
bool set_dynamic(DYNAMIC_ARRAY *array, unsigned char* element, uint32_t idx)
199
bool set_dynamic(DYNAMIC_ARRAY *array, uchar* element, uint idx)
200
201
if (idx >= array->elements)
227
228
true Allocation of new memory failed
230
static bool allocate_dynamic(DYNAMIC_ARRAY *array, uint32_t max_elements)
231
bool allocate_dynamic(DYNAMIC_ARRAY *array, uint max_elements)
232
233
if (max_elements >= array->max_element)
235
unsigned char *new_ptr;
236
237
size= (max_elements + array->alloc_increment)/array->alloc_increment;
237
238
size*= array->alloc_increment;
238
if (array->buffer == (unsigned char *)(array + 1))
239
if (array->buffer == (uchar *)(array + 1))
241
242
In this senerio, the buffer is statically preallocated,
242
243
so we have to create an all-new malloc since we overflowed
244
if (!(new_ptr= (unsigned char *) malloc(size *
245
array->size_of_element)))
245
if (!(new_ptr= (uchar *) my_malloc(size *
246
array->size_of_element,
247
memcpy(new_ptr, array->buffer,
249
memcpy(new_ptr, array->buffer,
248
250
array->elements * array->size_of_element);
253
if (!(new_ptr=(unsigned char*) realloc(array->buffer,
254
size* 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))))
256
259
array->buffer= new_ptr;
257
260
array->max_element= size;
269
unsigned char* Element to be returned. If idx > elements contain zeroes.
270
idx Index of element wanted.
272
uchar* Element to be returned. If idx > elements contain zeroes.
273
idx Index of element wanted.
273
void get_dynamic(DYNAMIC_ARRAY *array, unsigned char* element, uint32_t idx)
276
void get_dynamic(DYNAMIC_ARRAY *array, uchar* element, uint idx)
275
278
if (idx >= array->elements)
296
299
Just mark as empty if we are using a static buffer
298
if (array->buffer == (unsigned char *)(array + 1))
301
if (array->buffer == (uchar *)(array + 1))
299
302
array->elements= 0;
301
304
if (array->buffer)
306
my_free(array->buffer,MYF(MY_WME));
305
308
array->elements=array->max_element=0;
309
} /* namespace drizzled */
313
Delete element by given index
316
delete_dynamic_element()
318
idx Index of element to be deleted
321
void delete_dynamic_element(DYNAMIC_ARRAY *array, uint idx)
323
char *ptr= (char*) array->buffer+array->size_of_element*idx;
325
memmove(ptr,ptr+array->size_of_element,
326
(array->elements-idx)*array->size_of_element);
335
array Array to be freed
339
void freeze_size(DYNAMIC_ARRAY *array)
341
uint elements=max(array->elements,1);
344
Do nothing if we are using a static buffer
346
if (array->buffer == (uchar *)(array + 1))
349
if (array->buffer && array->max_element != elements)
351
array->buffer=(uchar*) my_realloc(array->buffer,
352
elements*array->size_of_element,
354
array->max_element=elements;
360
Get the index of a dynamic element
365
element Whose element index
369
int get_index_dynamic(DYNAMIC_ARRAY *array, uchar* element)
372
if (array->buffer > element)
375
ret= (element - array->buffer) / array->size_of_element;
376
if (ret > array->elements)