~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/array.cc

  • Committer: Monty Taylor
  • Date: 2009-06-08 13:39:05 UTC
  • mto: This revision was merged to the branch mainline in revision 1060.
  • Revision ID: mordred@inaugust.com-20090608133905-3hogrrafmfg6e5hs
Removed CHARSET_INFO stuff from protocol plugin interface - it makes no sense.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
/* Handling of arrays that can grow dynamicly. */
17
17
 
18
 
#include "mysys_priv.h"
 
18
#include "mysys/mysys_priv.h"
19
19
#include <mystrings/m_string.h>
20
20
 
21
21
/*
30
30
      alloc_increment   Increment for adding new elements
31
31
 
32
32
  DESCRIPTION
33
 
    init_dynamic_array() initiates array and allocate space for 
34
 
    init_alloc eilements. 
 
33
    init_dynamic_array() initiates array and allocate space for
 
34
    init_alloc eilements.
35
35
    Array is usable even if space allocation failed.
36
36
    Static buffers must begin immediately after the array structure.
37
37
 
38
38
  RETURN VALUE
39
 
    true        my_malloc_ci() failed
 
39
    true        malloc() failed
40
40
    false       Ok
41
41
*/
42
42
 
43
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)
 
44
                            void *init_buffer, uint32_t init_alloc,
 
45
                            uint32_t alloc_increment)
46
46
{
47
47
  if (!alloc_increment)
48
48
  {
60
60
  array->max_element=init_alloc;
61
61
  array->alloc_increment=alloc_increment;
62
62
  array->size_of_element=element_size;
63
 
  if ((array->buffer= init_buffer))
 
63
  if ((array->buffer= (unsigned char*) init_buffer))
64
64
    return(false);
65
 
  if (!(array->buffer=(unsigned char*) my_malloc_ci(element_size*init_alloc,
66
 
                                            MYF(MY_WME))))
 
65
  if (!(array->buffer=(unsigned char*) malloc(element_size*init_alloc)))
67
66
  {
68
67
    array->max_element=0;
69
68
    return(true);
70
69
  }
71
70
  return(false);
72
 
 
71
}
73
72
 
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)
77
 
{
78
 
  /* placeholder to preserve ABI */
79
 
  return my_init_dynamic_array_ci(array, element_size, init_alloc, 
80
 
                                  alloc_increment);
81
 
}
82
73
/*
83
74
  Insert element at the end of array. Allocate memory if needed.
84
75
 
111
102
 
112
103
 
113
104
/*
114
 
  Alloc space for next element(s) 
 
105
  Alloc space for next element(s)
115
106
 
116
107
  SYNOPSIS
117
108
    alloc_dynamic()
138
129
        In this senerio, the buffer is statically preallocated,
139
130
        so we have to create an all-new malloc since we overflowed
140
131
      */
141
 
      if (!(new_ptr= (char *) my_malloc((array->max_element+
142
 
                                         array->alloc_increment) *
143
 
                                        array->size_of_element,
144
 
                                        MYF(MY_WME))))
 
132
      if (!(new_ptr= (char *) malloc((array->max_element+
 
133
                                     array->alloc_increment) *
 
134
                                     array->size_of_element)))
145
135
        return 0;
146
 
      memcpy(new_ptr, array->buffer, 
 
136
      memcpy(new_ptr, array->buffer,
147
137
             array->elements * array->size_of_element);
148
138
    }
149
 
    else
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))))
 
139
    else if (!(new_ptr= (char*) realloc(array->buffer,
 
140
                                        (array->max_element+
 
141
                                         array->alloc_increment)*
 
142
                                        array->size_of_element)))
154
143
      return 0;
155
144
    array->buffer= (unsigned char*) new_ptr;
156
145
    array->max_element+=array->alloc_increment;
165
154
  SYNOPSIS
166
155
    pop_dynamic()
167
156
      array
168
 
  
169
 
  RETURN VALUE    
 
157
 
 
158
  RETURN VALUE
170
159
    pointer     Ok
171
160
    0           Array is empty
172
161
*/
188
177
      idx       Index where element is to be inserted
189
178
 
190
179
  DESCRIPTION
191
 
    set_dynamic() replaces element in array. 
192
 
    If idx > max_element insert new element. Allocate memory if needed. 
193
 
 
 
180
    set_dynamic() replaces element in array.
 
181
    If idx > max_element insert new element. Allocate memory if needed.
 
182
 
194
183
  RETURN VALUE
195
184
    true        Idx was out of range and allocation of new memory failed
196
185
    false       Ok
242
231
         In this senerio, the buffer is statically preallocated,
243
232
         so we have to create an all-new malloc since we overflowed
244
233
       */
245
 
       if (!(new_ptr= (unsigned char *) my_malloc(size *
246
 
                                         array->size_of_element,
247
 
                                         MYF(MY_WME))))
 
234
       if (!(new_ptr= (unsigned char *) malloc(size *
 
235
                                               array->size_of_element)))
248
236
         return 0;
249
 
       memcpy(new_ptr, array->buffer, 
 
237
       memcpy(new_ptr, array->buffer,
250
238
              array->elements * array->size_of_element);
251
239
     }
252
240
     else
253
241
 
254
242
 
255
 
    if (!(new_ptr= (unsigned char*) my_realloc(array->buffer,size*
256
 
                                       array->size_of_element,
257
 
                                       MYF(MY_WME | MY_ALLOW_ZERO_PTR))))
 
243
    if (!(new_ptr=(unsigned char*) realloc(array->buffer,
 
244
                                        size* array->size_of_element)))
258
245
      return true;
259
246
    array->buffer= new_ptr;
260
247
    array->max_element= size;
268
255
 
269
256
  SYNOPSIS
270
257
    get_dynamic()
271
 
      array     
 
258
      array
272
259
      unsigned char*    Element to be returned. If idx > elements contain zeroes.
273
 
      idx       Index of element wanted. 
 
260
      idx       Index of element wanted.
274
261
*/
275
262
 
276
263
void get_dynamic(DYNAMIC_ARRAY *array, unsigned char* element, uint32_t idx)
308
295
    array->elements=array->max_element=0;
309
296
  }
310
297
}
311
 
 
312
 
/*
313
 
  Delete element by given index
314
 
 
315
 
  SYNOPSIS
316
 
    delete_dynamic_element()
317
 
      array
318
 
      idx        Index of element to be deleted
319
 
*/
320
 
 
321
 
void delete_dynamic_element(DYNAMIC_ARRAY *array, uint32_t idx)
322
 
{
323
 
  char *ptr= (char*) array->buffer+array->size_of_element*idx;
324
 
  array->elements--;
325
 
  memmove(ptr,ptr+array->size_of_element,
326
 
          (array->elements-idx)*array->size_of_element);
327
 
}
328
 
 
329
 
 
330
 
/*
331
 
  Free unused memory
332
 
 
333
 
  SYNOPSIS
334
 
    freeze_size()
335
 
      array     Array to be freed
336
 
 
337
 
*/
338
 
 
339
 
void freeze_size(DYNAMIC_ARRAY *array)
340
 
{
341
 
  uint32_t elements=cmax(array->elements,1);
342
 
 
343
 
  /*
344
 
    Do nothing if we are using a static buffer
345
 
  */
346
 
  if (array->buffer == (unsigned char *)(array + 1))
347
 
    return;
348
 
    
349
 
  if (array->buffer && array->max_element != elements)
350
 
  {
351
 
    array->buffer=(unsigned char*) my_realloc(array->buffer,
352
 
                                     elements*array->size_of_element,
353
 
                                     MYF(MY_WME));
354
 
    array->max_element=elements;
355
 
  }
356
 
}
357
 
 
358
 
 
359
 
/*
360
 
  Get the index of a dynamic element
361
 
 
362
 
  SYNOPSIS
363
 
    get_index_dynamic()
364
 
     array      Array
365
 
     element Whose element index 
366
 
 
367
 
*/
368
 
 
369
 
int get_index_dynamic(DYNAMIC_ARRAY *array, unsigned char* element)
370
 
{
371
 
  uint32_t ret;
372
 
  if (array->buffer > element)
373
 
    return -1;
374
 
 
375
 
  ret= (element - array->buffer) /  array->size_of_element;
376
 
  if (ret > array->elements)
377
 
    return -1;
378
 
 
379
 
  return ret;
380
 
 
381
 
}