~jlukas79/+junk/mysql-server

« back to all changes in this revision

Viewing changes to mysys/array.c

manual merge 6.0-main --> 6.0-bka-review

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
 
41
41
*/
42
42
 
43
43
my_bool init_dynamic_array2(DYNAMIC_ARRAY *array, uint element_size,
44
 
                            void *init_buffer, uint init_alloc, 
 
44
                            void *init_buffer, uint init_alloc,
45
45
                            uint alloc_increment CALLER_INFO_PROTO)
46
46
{
47
47
  DBUG_ENTER("init_dynamic_array");
63
63
  array->size_of_element=element_size;
64
64
  if ((array->buffer= init_buffer))
65
65
    DBUG_RETURN(FALSE);
66
 
  if (!(array->buffer=(uchar*) my_malloc_ci(element_size*init_alloc,MYF(MY_WME))))
 
66
  if (!(array->buffer=(uchar*) my_malloc_ci(element_size*init_alloc,
 
67
                                            MYF(MY_WME))))
67
68
  {
68
69
    array->max_element=0;
69
70
    DBUG_RETURN(TRUE);
70
71
  }
71
72
  DBUG_RETURN(FALSE);
72
 
 
73
}
73
74
 
74
75
my_bool init_dynamic_array(DYNAMIC_ARRAY *array, uint element_size,
75
 
                           uint init_alloc, 
 
76
                           uint init_alloc,
76
77
                           uint alloc_increment CALLER_INFO_PROTO)
77
78
{
78
79
  /* placeholder to preserve ABI */
79
 
  return my_init_dynamic_array_ci(array, element_size, init_alloc, 
 
80
  return my_init_dynamic_array_ci(array, element_size, init_alloc,
80
81
                                  alloc_increment);
81
82
}
82
83
/*
92
93
    FALSE       Ok
93
94
*/
94
95
 
95
 
my_bool insert_dynamic(DYNAMIC_ARRAY *array, uchar* element)
 
96
my_bool insert_dynamic(DYNAMIC_ARRAY *array, const uchar* element)
96
97
{
97
98
  uchar* buffer;
98
99
  if (array->elements == array->max_element)
111
112
 
112
113
 
113
114
/*
114
 
  Alloc space for next element(s) 
 
115
  Alloc space for next element(s)
115
116
 
116
117
  SYNOPSIS
117
118
    alloc_dynamic()
129
130
 
130
131
uchar *alloc_dynamic(DYNAMIC_ARRAY *array)
131
132
{
 
133
  DBUG_ENTER("alloc_dynamic");
132
134
  if (array->elements == array->max_element)
133
135
  {
134
136
    char *new_ptr;
142
144
                                         array->alloc_increment) *
143
145
                                        array->size_of_element,
144
146
                                        MYF(MY_WME))))
145
 
        return 0;
146
 
      memcpy(new_ptr, array->buffer, 
 
147
        DBUG_RETURN(0);
 
148
      memcpy(new_ptr, array->buffer,
147
149
             array->elements * array->size_of_element);
148
150
    }
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))))
154
 
      return 0;
 
151
    else if (!(new_ptr=(char*)
 
152
               my_realloc(array->buffer,(array->max_element+
 
153
                                         array->alloc_increment)*
 
154
                          array->size_of_element,
 
155
                          MYF(MY_WME | MY_ALLOW_ZERO_PTR))))
 
156
      DBUG_RETURN(0);
155
157
    array->buffer= (uchar*) new_ptr;
156
158
    array->max_element+=array->alloc_increment;
157
159
  }
158
 
  return array->buffer+(array->elements++ * array->size_of_element);
 
160
  DBUG_RETURN(array->buffer+(array->elements++ * array->size_of_element));
159
161
}
160
162
 
161
163
 
165
167
  SYNOPSIS
166
168
    pop_dynamic()
167
169
      array
168
 
  
169
 
  RETURN VALUE    
 
170
 
 
171
  RETURN VALUE
170
172
    pointer     Ok
171
173
    0           Array is empty
172
174
*/
179
181
}
180
182
 
181
183
/*
182
 
  Replace elemnent in array with given element and index
 
184
  Replace element in array with given element and index
183
185
 
184
186
  SYNOPSIS
185
187
    set_dynamic()
188
190
      idx       Index where element is to be inserted
189
191
 
190
192
  DESCRIPTION
191
 
    set_dynamic() replaces element in array. 
192
 
    If idx > max_element insert new element. Allocate memory if needed. 
193
 
 
 
193
    set_dynamic() replaces element in array.
 
194
    If idx > max_element insert new element. Allocate memory if needed.
 
195
 
194
196
  RETURN VALUE
195
197
    TRUE        Idx was out of range and allocation of new memory failed
196
198
    FALSE       Ok
200
202
{
201
203
  if (idx >= array->elements)
202
204
  {
203
 
    if (idx >= array->max_element)
204
 
    {
205
 
      uint size;
206
 
      char *new_ptr;
207
 
      size=(idx+array->alloc_increment)/array->alloc_increment;
208
 
      size*= array->alloc_increment;
209
 
      if (array->buffer == (uchar *)(array + 1))
210
 
      {
211
 
        /*
212
 
          In this senerio, the buffer is statically preallocated,
213
 
          so we have to create an all-new malloc since we overflowed
214
 
        */
215
 
        if (!(new_ptr= (char *) my_malloc(size *
216
 
                                          array->size_of_element,
217
 
                                          MYF(MY_WME))))
218
 
          return 0;
219
 
        memcpy(new_ptr, array->buffer, 
220
 
               array->elements * array->size_of_element);
221
 
      }
222
 
      else
223
 
      if (!(new_ptr=(char*) my_realloc(array->buffer,size*
224
 
                                       array->size_of_element,
225
 
                                       MYF(MY_WME | MY_ALLOW_ZERO_PTR))))
226
 
        return TRUE;
227
 
      array->buffer= (uchar*) new_ptr;
228
 
      array->max_element=size;
229
 
    }
 
205
    if (idx >= array->max_element && allocate_dynamic(array, idx))
 
206
      return TRUE;
230
207
    bzero((uchar*) (array->buffer+array->elements*array->size_of_element),
231
 
          (idx - array->elements)*array->size_of_element);
 
208
          (idx - array->elements)*array->size_of_element);
232
209
    array->elements=idx+1;
233
210
  }
234
211
  memcpy(array->buffer+(idx * array->size_of_element),element,
235
 
         (size_t) array->size_of_element);
 
212
         (size_t) array->size_of_element);
236
213
  return FALSE;
237
214
}
238
215
 
 
216
 
 
217
/*
 
218
  Ensure that dynamic array has enough elements
 
219
 
 
220
  SYNOPSIS
 
221
    allocate_dynamic()
 
222
    array
 
223
    max_elements        Numbers of elements that is needed
 
224
 
 
225
  NOTES
 
226
   Any new allocated element are NOT initialized
 
227
 
 
228
  RETURN VALUE
 
229
    FALSE       Ok
 
230
    TRUE        Allocation of new memory failed
 
231
*/
 
232
 
 
233
my_bool allocate_dynamic(DYNAMIC_ARRAY *array, uint max_elements)
 
234
{
 
235
  DBUG_ENTER("allocate_dynamic");
 
236
 
 
237
  if (max_elements >= array->max_element)
 
238
  {
 
239
    uint size;
 
240
    uchar *new_ptr;
 
241
    size= (max_elements + array->alloc_increment)/array->alloc_increment;
 
242
    size*= array->alloc_increment;
 
243
    if (array->buffer == (uchar *)(array + 1))
 
244
    {
 
245
       /*
 
246
         In this senerio, the buffer is statically preallocated,
 
247
         so we have to create an all-new malloc since we overflowed
 
248
       */
 
249
       if (!(new_ptr= (uchar *) my_malloc(size *
 
250
                                          array->size_of_element,
 
251
                                          MYF(MY_WME))))
 
252
         DBUG_RETURN(0);
 
253
       memcpy(new_ptr, array->buffer,
 
254
              array->elements * array->size_of_element);
 
255
     }
 
256
    else if (!(new_ptr= (uchar*) my_realloc(array->buffer,size*
 
257
                                            array->size_of_element,
 
258
                                            MYF(MY_WME | MY_ALLOW_ZERO_PTR))))
 
259
      DBUG_RETURN(TRUE);
 
260
    array->buffer= new_ptr;
 
261
    array->max_element= size;
 
262
  }
 
263
  DBUG_RETURN(FALSE);
 
264
}
 
265
 
 
266
 
239
267
/*
240
268
  Get an element from array by given index
241
269
 
242
270
  SYNOPSIS
243
271
    get_dynamic()
244
 
      array     
 
272
      array
245
273
      uchar*    Element to be returned. If idx > elements contain zeroes.
246
 
      idx       Index of element wanted. 
 
274
      idx       Index of element wanted.
247
275
*/
248
276
 
249
277
void get_dynamic(DYNAMIC_ARRAY *array, uchar* element, uint idx)
320
348
  */
321
349
  if (array->buffer == (uchar *)(array + 1))
322
350
    return;
323
 
    
 
351
 
324
352
  if (array->buffer && array->max_element != elements)
325
353
  {
326
354
    array->buffer=(uchar*) my_realloc(array->buffer,
337
365
  SYNOPSIS
338
366
    get_index_dynamic()
339
367
     array      Array
340
 
     element Whose element index 
 
368
     element Whose element index
341
369
 
342
370
*/
343
371