~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/array.c

mergeĀ mainline

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
/* Handling of arrays that can grow dynamicly. */
17
17
 
18
18
#include "mysys_priv.h"
19
 
#include <mystrings/m_string.h>
 
19
#include "m_string.h"
20
20
 
21
21
/*
22
22
  Initiate dynamic array
36
36
    Static buffers must begin immediately after the array structure.
37
37
 
38
38
  RETURN VALUE
39
 
    true        my_malloc_ci() failed
40
 
    false       Ok
 
39
    TRUE        my_malloc_ci() failed
 
40
    FALSE       Ok
41
41
*/
42
42
 
43
 
bool init_dynamic_array2(DYNAMIC_ARRAY *array, uint32_t element_size,
44
 
                            void *init_buffer, uint32_t init_alloc, 
45
 
                            uint32_t alloc_increment CALLER_INFO_PROTO)
 
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)
46
46
{
 
47
  DBUG_ENTER("init_dynamic_array");
47
48
  if (!alloc_increment)
48
49
  {
49
 
    alloc_increment=cmax((8192-MALLOC_OVERHEAD)/element_size,16);
 
50
    alloc_increment=max((8192-MALLOC_OVERHEAD)/element_size,16);
50
51
    if (init_alloc > 8 && alloc_increment > init_alloc * 2)
51
52
      alloc_increment=init_alloc*2;
52
53
  }
61
62
  array->alloc_increment=alloc_increment;
62
63
  array->size_of_element=element_size;
63
64
  if ((array->buffer= init_buffer))
64
 
    return(false);
65
 
  if (!(array->buffer=(unsigned char*) my_malloc_ci(element_size*init_alloc,
 
65
    DBUG_RETURN(FALSE);
 
66
  if (!(array->buffer=(uchar*) my_malloc_ci(element_size*init_alloc,
66
67
                                            MYF(MY_WME))))
67
68
  {
68
69
    array->max_element=0;
69
 
    return(true);
 
70
    DBUG_RETURN(TRUE);
70
71
  }
71
 
  return(false);
 
72
  DBUG_RETURN(FALSE);
72
73
73
74
 
74
 
bool init_dynamic_array(DYNAMIC_ARRAY *array, uint32_t element_size,
75
 
                           uint32_t init_alloc, 
76
 
                           uint32_t alloc_increment CALLER_INFO_PROTO)
 
75
my_bool init_dynamic_array(DYNAMIC_ARRAY *array, uint element_size,
 
76
                           uint init_alloc, 
 
77
                           uint alloc_increment CALLER_INFO_PROTO)
77
78
{
78
79
  /* placeholder to preserve ABI */
79
80
  return my_init_dynamic_array_ci(array, element_size, init_alloc, 
88
89
      element
89
90
 
90
91
  RETURN VALUE
91
 
    true        Insert failed
92
 
    false       Ok
 
92
    TRUE        Insert failed
 
93
    FALSE       Ok
93
94
*/
94
95
 
95
 
bool insert_dynamic(DYNAMIC_ARRAY *array, unsigned char* element)
 
96
my_bool insert_dynamic(DYNAMIC_ARRAY *array, uchar* element)
96
97
{
97
 
  unsigned char* buffer;
 
98
  uchar* buffer;
98
99
  if (array->elements == array->max_element)
99
100
  {                                             /* Call only when nessesary */
100
101
    if (!(buffer=alloc_dynamic(array)))
101
 
      return true;
 
102
      return TRUE;
102
103
  }
103
104
  else
104
105
  {
106
107
    array->elements++;
107
108
  }
108
109
  memcpy(buffer,element,(size_t) array->size_of_element);
109
 
  return false;
 
110
  return FALSE;
110
111
}
111
112
 
112
113
 
127
128
    0           Error
128
129
*/
129
130
 
130
 
unsigned char *alloc_dynamic(DYNAMIC_ARRAY *array)
 
131
uchar *alloc_dynamic(DYNAMIC_ARRAY *array)
131
132
{
132
133
  if (array->elements == array->max_element)
133
134
  {
134
135
    char *new_ptr;
135
 
    if (array->buffer == (unsigned char *)(array + 1))
 
136
    if (array->buffer == (uchar *)(array + 1))
136
137
    {
137
138
      /*
138
139
        In this senerio, the buffer is statically preallocated,
152
153
                                     array->size_of_element,
153
154
                                     MYF(MY_WME | MY_ALLOW_ZERO_PTR))))
154
155
      return 0;
155
 
    array->buffer= (unsigned char*) new_ptr;
 
156
    array->buffer= (uchar*) new_ptr;
156
157
    array->max_element+=array->alloc_increment;
157
158
  }
158
159
  return array->buffer+(array->elements++ * array->size_of_element);
171
172
    0           Array is empty
172
173
*/
173
174
 
174
 
unsigned char *pop_dynamic(DYNAMIC_ARRAY *array)
 
175
uchar *pop_dynamic(DYNAMIC_ARRAY *array)
175
176
{
176
177
  if (array->elements)
177
178
    return array->buffer+(--array->elements * array->size_of_element);
192
193
    If idx > max_element insert new element. Allocate memory if needed. 
193
194
 
194
195
  RETURN VALUE
195
 
    true        Idx was out of range and allocation of new memory failed
196
 
    false       Ok
 
196
    TRUE        Idx was out of range and allocation of new memory failed
 
197
    FALSE       Ok
197
198
*/
198
199
 
199
 
bool set_dynamic(DYNAMIC_ARRAY *array, unsigned char* element, uint32_t idx)
 
200
my_bool set_dynamic(DYNAMIC_ARRAY *array, uchar* element, uint idx)
200
201
{
201
202
  if (idx >= array->elements)
202
203
  {
203
204
    if (idx >= array->max_element && allocate_dynamic(array, idx))
204
 
      return true;
205
 
    memset(array->buffer+array->elements*array->size_of_element, 0,
206
 
           (idx - array->elements)*array->size_of_element);
 
205
      return TRUE;
 
206
    bzero((uchar*) (array->buffer+array->elements*array->size_of_element),
 
207
          (idx - array->elements)*array->size_of_element);
207
208
    array->elements=idx+1;
208
209
  }
209
210
  memcpy(array->buffer+(idx * array->size_of_element),element,
210
211
         (size_t) array->size_of_element);
211
 
  return false;
 
212
  return FALSE;
212
213
}
213
214
 
214
215
 
224
225
   Any new allocated element are NOT initialized
225
226
 
226
227
  RETURN VALUE
227
 
    false       Ok
228
 
    true        Allocation of new memory failed
 
228
    FALSE       Ok
 
229
    TRUE        Allocation of new memory failed
229
230
*/
230
231
 
231
 
bool allocate_dynamic(DYNAMIC_ARRAY *array, uint32_t max_elements)
 
232
my_bool allocate_dynamic(DYNAMIC_ARRAY *array, uint max_elements)
232
233
{
233
234
  if (max_elements >= array->max_element)
234
235
  {
235
 
    uint32_t size;
236
 
    unsigned char *new_ptr;
 
236
    uint size;
 
237
    uchar *new_ptr;
237
238
    size= (max_elements + array->alloc_increment)/array->alloc_increment;
238
239
    size*= array->alloc_increment;
239
 
    if (array->buffer == (unsigned char *)(array + 1))
 
240
    if (array->buffer == (uchar *)(array + 1))
240
241
    {
241
242
       /*
242
243
         In this senerio, the buffer is statically preallocated,
243
244
         so we have to create an all-new malloc since we overflowed
244
245
       */
245
 
       if (!(new_ptr= (unsigned char *) my_malloc(size *
 
246
       if (!(new_ptr= (uchar *) my_malloc(size *
246
247
                                         array->size_of_element,
247
248
                                         MYF(MY_WME))))
248
249
         return 0;
252
253
     else
253
254
 
254
255
 
255
 
    if (!(new_ptr= (unsigned char*) my_realloc(array->buffer,size*
 
256
    if (!(new_ptr= (uchar*) my_realloc(array->buffer,size*
256
257
                                       array->size_of_element,
257
258
                                       MYF(MY_WME | MY_ALLOW_ZERO_PTR))))
258
 
      return true;
 
259
      return TRUE;
259
260
    array->buffer= new_ptr;
260
261
    array->max_element= size;
261
262
  }
262
 
  return false;
 
263
  return FALSE;
263
264
}
264
265
 
265
266
 
269
270
  SYNOPSIS
270
271
    get_dynamic()
271
272
      array     
272
 
      unsigned char*    Element to be returned. If idx > elements contain zeroes.
 
273
      uchar*    Element to be returned. If idx > elements contain zeroes.
273
274
      idx       Index of element wanted. 
274
275
*/
275
276
 
276
 
void get_dynamic(DYNAMIC_ARRAY *array, unsigned char* element, uint32_t idx)
 
277
void get_dynamic(DYNAMIC_ARRAY *array, uchar* element, uint idx)
277
278
{
278
279
  if (idx >= array->elements)
279
280
  {
280
 
    memset(element, 0, array->size_of_element);
 
281
    DBUG_PRINT("warning",("To big array idx: %d, array size is %d",
 
282
                          idx,array->elements));
 
283
    bzero(element,array->size_of_element);
281
284
    return;
282
285
  }
283
286
  memcpy(element,array->buffer+idx*array->size_of_element,
298
301
  /*
299
302
    Just mark as empty if we are using a static buffer
300
303
  */
301
 
  if (array->buffer == (unsigned char *)(array + 1))
 
304
  if (array->buffer == (uchar *)(array + 1))
302
305
    array->elements= 0;
303
306
  else
304
307
  if (array->buffer)
305
308
  {
306
 
    free(array->buffer);
 
309
    my_free(array->buffer,MYF(MY_WME));
307
310
    array->buffer=0;
308
311
    array->elements=array->max_element=0;
309
312
  }
318
321
      idx        Index of element to be deleted
319
322
*/
320
323
 
321
 
void delete_dynamic_element(DYNAMIC_ARRAY *array, uint32_t idx)
 
324
void delete_dynamic_element(DYNAMIC_ARRAY *array, uint idx)
322
325
{
323
326
  char *ptr= (char*) array->buffer+array->size_of_element*idx;
324
327
  array->elements--;
338
341
 
339
342
void freeze_size(DYNAMIC_ARRAY *array)
340
343
{
341
 
  uint32_t elements=cmax(array->elements,1);
 
344
  uint elements=max(array->elements,1);
342
345
 
343
346
  /*
344
347
    Do nothing if we are using a static buffer
345
348
  */
346
 
  if (array->buffer == (unsigned char *)(array + 1))
 
349
  if (array->buffer == (uchar *)(array + 1))
347
350
    return;
348
351
    
349
352
  if (array->buffer && array->max_element != elements)
350
353
  {
351
 
    array->buffer=(unsigned char*) my_realloc(array->buffer,
 
354
    array->buffer=(uchar*) my_realloc(array->buffer,
352
355
                                     elements*array->size_of_element,
353
356
                                     MYF(MY_WME));
354
357
    array->max_element=elements;
366
369
 
367
370
*/
368
371
 
369
 
int get_index_dynamic(DYNAMIC_ARRAY *array, unsigned char* element)
 
372
int get_index_dynamic(DYNAMIC_ARRAY *array, uchar* element)
370
373
{
371
 
  uint32_t ret;
 
374
  uint ret;
372
375
  if (array->buffer > element)
373
376
    return -1;
374
377