~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/array.cc

  • Committer: Monty Taylor
  • Date: 2008-12-06 07:22:02 UTC
  • mto: (656.1.7 devel) (660.1.5 codestyle)
  • mto: This revision was merged to the branch mainline in revision 665.
  • Revision ID: monty@inaugust.com-20081206072202-2g25o9doqr1l8euu
OOOh doggie. Got rid of my_alloca.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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        malloc() failed
 
39
    true        my_malloc_ci() 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,
 
44
                            void *init_buffer, uint32_t init_alloc, 
45
45
                            uint32_t alloc_increment)
46
46
{
47
47
  if (!alloc_increment)
62
62
  array->size_of_element=element_size;
63
63
  if ((array->buffer= (unsigned char*) init_buffer))
64
64
    return(false);
65
 
  if (!(array->buffer=(unsigned char*) malloc(element_size*init_alloc)))
 
65
  if (!(array->buffer=(unsigned char*) my_malloc_ci(element_size*init_alloc,
 
66
                                            MYF(MY_WME))))
66
67
  {
67
68
    array->max_element=0;
68
69
    return(true);
69
70
  }
70
71
  return(false);
71
 
}
 
72
72
73
 
73
74
bool init_dynamic_array(DYNAMIC_ARRAY *array, uint32_t element_size,
74
 
                           uint32_t init_alloc,
 
75
                           uint32_t init_alloc, 
75
76
                           uint32_t alloc_increment)
76
77
{
77
78
  /* placeholder to preserve ABI */
78
 
  return my_init_dynamic_array_ci(array, element_size, init_alloc,
 
79
  return my_init_dynamic_array_ci(array, element_size, init_alloc, 
79
80
                                  alloc_increment);
80
81
}
81
82
/*
110
111
 
111
112
 
112
113
/*
113
 
  Alloc space for next element(s)
 
114
  Alloc space for next element(s) 
114
115
 
115
116
  SYNOPSIS
116
117
    alloc_dynamic()
137
138
        In this senerio, the buffer is statically preallocated,
138
139
        so we have to create an all-new malloc since we overflowed
139
140
      */
140
 
      if (!(new_ptr= (char *) malloc((array->max_element+
141
 
                                     array->alloc_increment) *
142
 
                                     array->size_of_element)))
 
141
      if (!(new_ptr= (char *) my_malloc((array->max_element+
 
142
                                         array->alloc_increment) *
 
143
                                        array->size_of_element,
 
144
                                        MYF(MY_WME))))
143
145
        return 0;
144
 
      memcpy(new_ptr, array->buffer,
 
146
      memcpy(new_ptr, array->buffer, 
145
147
             array->elements * array->size_of_element);
146
148
    }
147
 
    else if (!(new_ptr= (char*) realloc(array->buffer,
148
 
                                        (array->max_element+
149
 
                                         array->alloc_increment)*
150
 
                                        array->size_of_element)))
 
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))))
151
154
      return 0;
152
155
    array->buffer= (unsigned char*) new_ptr;
153
156
    array->max_element+=array->alloc_increment;
162
165
  SYNOPSIS
163
166
    pop_dynamic()
164
167
      array
165
 
 
166
 
  RETURN VALUE
 
168
  
 
169
  RETURN VALUE    
167
170
    pointer     Ok
168
171
    0           Array is empty
169
172
*/
185
188
      idx       Index where element is to be inserted
186
189
 
187
190
  DESCRIPTION
188
 
    set_dynamic() replaces element in array.
189
 
    If idx > max_element insert new element. Allocate memory if needed.
190
 
 
 
191
    set_dynamic() replaces element in array. 
 
192
    If idx > max_element insert new element. Allocate memory if needed. 
 
193
 
191
194
  RETURN VALUE
192
195
    true        Idx was out of range and allocation of new memory failed
193
196
    false       Ok
239
242
         In this senerio, the buffer is statically preallocated,
240
243
         so we have to create an all-new malloc since we overflowed
241
244
       */
242
 
       if (!(new_ptr= (unsigned char *) malloc(size *
243
 
                                               array->size_of_element)))
 
245
       if (!(new_ptr= (unsigned char *) my_malloc(size *
 
246
                                         array->size_of_element,
 
247
                                         MYF(MY_WME))))
244
248
         return 0;
245
 
       memcpy(new_ptr, array->buffer,
 
249
       memcpy(new_ptr, array->buffer, 
246
250
              array->elements * array->size_of_element);
247
251
     }
248
252
     else
249
253
 
250
254
 
251
 
    if (!(new_ptr=(unsigned char*) realloc(array->buffer,
252
 
                                        size* array->size_of_element)))
 
255
    if (!(new_ptr= (unsigned char*) my_realloc(array->buffer,size*
 
256
                                       array->size_of_element,
 
257
                                       MYF(MY_WME | MY_ALLOW_ZERO_PTR))))
253
258
      return true;
254
259
    array->buffer= new_ptr;
255
260
    array->max_element= size;
263
268
 
264
269
  SYNOPSIS
265
270
    get_dynamic()
266
 
      array
 
271
      array     
267
272
      unsigned char*    Element to be returned. If idx > elements contain zeroes.
268
 
      idx       Index of element wanted.
 
273
      idx       Index of element wanted. 
269
274
*/
270
275
 
271
276
void get_dynamic(DYNAMIC_ARRAY *array, unsigned char* element, uint32_t idx)
340
345
  */
341
346
  if (array->buffer == (unsigned char *)(array + 1))
342
347
    return;
343
 
 
 
348
    
344
349
  if (array->buffer && array->max_element != elements)
345
350
  {
346
 
    array->buffer=(unsigned char*) realloc(array->buffer,
347
 
                                     elements*array->size_of_element);
 
351
    array->buffer=(unsigned char*) my_realloc(array->buffer,
 
352
                                     elements*array->size_of_element,
 
353
                                     MYF(MY_WME));
348
354
    array->max_element=elements;
349
355
  }
350
356
}
356
362
  SYNOPSIS
357
363
    get_index_dynamic()
358
364
     array      Array
359
 
     element Whose element index
 
365
     element Whose element index 
360
366
 
361
367
*/
362
368