~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/array.cc

  • Committer: Monty Taylor
  • Date: 2009-01-29 19:04:39 UTC
  • mto: (779.3.29 devel)
  • mto: This revision was merged to the branch mainline in revision 823.
  • Revision ID: mordred@inaugust.com-20090129190439-vfr95s6gaudjacm7
Add timegm which is missing on Solaris.

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        my_malloc_ci() failed
 
39
    true        malloc() failed
40
40
    false       Ok
41
41
*/
42
42
 
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)
 
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)
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=(uchar*) 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, uint element_size,
75
 
                           uint init_alloc, 
76
 
                           uint alloc_increment CALLER_INFO_PROTO)
 
73
bool init_dynamic_array(DYNAMIC_ARRAY *array, uint32_t element_size,
 
74
                           uint32_t init_alloc,
 
75
                           uint32_t alloc_increment)
77
76
{
78
77
  /* placeholder to preserve ABI */
79
 
  return my_init_dynamic_array_ci(array, element_size, init_alloc, 
 
78
  return my_init_dynamic_array_ci(array, element_size, init_alloc,
80
79
                                  alloc_increment);
81
80
}
82
81
/*
92
91
    false       Ok
93
92
*/
94
93
 
95
 
bool insert_dynamic(DYNAMIC_ARRAY *array, uchar* element)
 
94
bool insert_dynamic(DYNAMIC_ARRAY *array, unsigned char* element)
96
95
{
97
 
  uchar* buffer;
 
96
  unsigned char* buffer;
98
97
  if (array->elements == array->max_element)
99
98
  {                                             /* Call only when nessesary */
100
99
    if (!(buffer=alloc_dynamic(array)))
111
110
 
112
111
 
113
112
/*
114
 
  Alloc space for next element(s) 
 
113
  Alloc space for next element(s)
115
114
 
116
115
  SYNOPSIS
117
116
    alloc_dynamic()
127
126
    0           Error
128
127
*/
129
128
 
130
 
uchar *alloc_dynamic(DYNAMIC_ARRAY *array)
 
129
unsigned char *alloc_dynamic(DYNAMIC_ARRAY *array)
131
130
{
132
131
  if (array->elements == array->max_element)
133
132
  {
134
133
    char *new_ptr;
135
 
    if (array->buffer == (uchar *)(array + 1))
 
134
    if (array->buffer == (unsigned char *)(array + 1))
136
135
    {
137
136
      /*
138
137
        In this senerio, the buffer is statically preallocated,
139
138
        so we have to create an all-new malloc since we overflowed
140
139
      */
141
 
      if (!(new_ptr= (char *) my_malloc((array->max_element+
142
 
                                         array->alloc_increment) *
143
 
                                        array->size_of_element,
144
 
                                        MYF(MY_WME))))
 
140
      if (!(new_ptr= (char *) malloc((array->max_element+
 
141
                                     array->alloc_increment) *
 
142
                                     array->size_of_element)))
145
143
        return 0;
146
 
      memcpy(new_ptr, array->buffer, 
 
144
      memcpy(new_ptr, array->buffer,
147
145
             array->elements * array->size_of_element);
148
146
    }
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))))
 
147
    else if (!(new_ptr= (char*) realloc(array->buffer,
 
148
                                        (array->max_element+
 
149
                                         array->alloc_increment)*
 
150
                                        array->size_of_element)))
154
151
      return 0;
155
 
    array->buffer= (uchar*) new_ptr;
 
152
    array->buffer= (unsigned char*) new_ptr;
156
153
    array->max_element+=array->alloc_increment;
157
154
  }
158
155
  return array->buffer+(array->elements++ * array->size_of_element);
165
162
  SYNOPSIS
166
163
    pop_dynamic()
167
164
      array
168
 
  
169
 
  RETURN VALUE    
 
165
 
 
166
  RETURN VALUE
170
167
    pointer     Ok
171
168
    0           Array is empty
172
169
*/
173
170
 
174
 
uchar *pop_dynamic(DYNAMIC_ARRAY *array)
 
171
unsigned char *pop_dynamic(DYNAMIC_ARRAY *array)
175
172
{
176
173
  if (array->elements)
177
174
    return array->buffer+(--array->elements * array->size_of_element);
188
185
      idx       Index where element is to be inserted
189
186
 
190
187
  DESCRIPTION
191
 
    set_dynamic() replaces element in array. 
192
 
    If idx > max_element insert new element. Allocate memory if needed. 
193
 
 
 
188
    set_dynamic() replaces element in array.
 
189
    If idx > max_element insert new element. Allocate memory if needed.
 
190
 
194
191
  RETURN VALUE
195
192
    true        Idx was out of range and allocation of new memory failed
196
193
    false       Ok
197
194
*/
198
195
 
199
 
bool set_dynamic(DYNAMIC_ARRAY *array, uchar* element, uint idx)
 
196
bool set_dynamic(DYNAMIC_ARRAY *array, unsigned char* element, uint32_t idx)
200
197
{
201
198
  if (idx >= array->elements)
202
199
  {
228
225
    true        Allocation of new memory failed
229
226
*/
230
227
 
231
 
bool allocate_dynamic(DYNAMIC_ARRAY *array, uint max_elements)
 
228
bool allocate_dynamic(DYNAMIC_ARRAY *array, uint32_t max_elements)
232
229
{
233
230
  if (max_elements >= array->max_element)
234
231
  {
235
 
    uint size;
236
 
    uchar *new_ptr;
 
232
    uint32_t size;
 
233
    unsigned char *new_ptr;
237
234
    size= (max_elements + array->alloc_increment)/array->alloc_increment;
238
235
    size*= array->alloc_increment;
239
 
    if (array->buffer == (uchar *)(array + 1))
 
236
    if (array->buffer == (unsigned char *)(array + 1))
240
237
    {
241
238
       /*
242
239
         In this senerio, the buffer is statically preallocated,
243
240
         so we have to create an all-new malloc since we overflowed
244
241
       */
245
 
       if (!(new_ptr= (uchar *) my_malloc(size *
246
 
                                         array->size_of_element,
247
 
                                         MYF(MY_WME))))
 
242
       if (!(new_ptr= (unsigned char *) malloc(size *
 
243
                                               array->size_of_element)))
248
244
         return 0;
249
 
       memcpy(new_ptr, array->buffer, 
 
245
       memcpy(new_ptr, array->buffer,
250
246
              array->elements * array->size_of_element);
251
247
     }
252
248
     else
253
249
 
254
250
 
255
 
    if (!(new_ptr= (uchar*) my_realloc(array->buffer,size*
256
 
                                       array->size_of_element,
257
 
                                       MYF(MY_WME | MY_ALLOW_ZERO_PTR))))
 
251
    if (!(new_ptr=(unsigned char*) realloc(array->buffer,
 
252
                                        size* array->size_of_element)))
258
253
      return true;
259
254
    array->buffer= new_ptr;
260
255
    array->max_element= size;
268
263
 
269
264
  SYNOPSIS
270
265
    get_dynamic()
271
 
      array     
272
 
      uchar*    Element to be returned. If idx > elements contain zeroes.
273
 
      idx       Index of element wanted. 
 
266
      array
 
267
      unsigned char*    Element to be returned. If idx > elements contain zeroes.
 
268
      idx       Index of element wanted.
274
269
*/
275
270
 
276
 
void get_dynamic(DYNAMIC_ARRAY *array, uchar* element, uint idx)
 
271
void get_dynamic(DYNAMIC_ARRAY *array, unsigned char* element, uint32_t idx)
277
272
{
278
273
  if (idx >= array->elements)
279
274
  {
298
293
  /*
299
294
    Just mark as empty if we are using a static buffer
300
295
  */
301
 
  if (array->buffer == (uchar *)(array + 1))
 
296
  if (array->buffer == (unsigned char *)(array + 1))
302
297
    array->elements= 0;
303
298
  else
304
299
  if (array->buffer)
305
300
  {
306
 
    my_free(array->buffer,MYF(MY_WME));
 
301
    free(array->buffer);
307
302
    array->buffer=0;
308
303
    array->elements=array->max_element=0;
309
304
  }
318
313
      idx        Index of element to be deleted
319
314
*/
320
315
 
321
 
void delete_dynamic_element(DYNAMIC_ARRAY *array, uint idx)
 
316
void delete_dynamic_element(DYNAMIC_ARRAY *array, uint32_t idx)
322
317
{
323
318
  char *ptr= (char*) array->buffer+array->size_of_element*idx;
324
319
  array->elements--;
338
333
 
339
334
void freeze_size(DYNAMIC_ARRAY *array)
340
335
{
341
 
  uint elements=cmax(array->elements,1);
 
336
  uint32_t elements=cmax(array->elements,1);
342
337
 
343
338
  /*
344
339
    Do nothing if we are using a static buffer
345
340
  */
346
 
  if (array->buffer == (uchar *)(array + 1))
 
341
  if (array->buffer == (unsigned char *)(array + 1))
347
342
    return;
348
 
    
 
343
 
349
344
  if (array->buffer && array->max_element != elements)
350
345
  {
351
 
    array->buffer=(uchar*) my_realloc(array->buffer,
352
 
                                     elements*array->size_of_element,
353
 
                                     MYF(MY_WME));
 
346
    array->buffer=(unsigned char*) realloc(array->buffer,
 
347
                                     elements*array->size_of_element);
354
348
    array->max_element=elements;
355
349
  }
356
350
}
362
356
  SYNOPSIS
363
357
    get_index_dynamic()
364
358
     array      Array
365
 
     element Whose element index 
 
359
     element Whose element index
366
360
 
367
361
*/
368
362
 
369
 
int get_index_dynamic(DYNAMIC_ARRAY *array, uchar* element)
 
363
int get_index_dynamic(DYNAMIC_ARRAY *array, unsigned char* element)
370
364
{
371
 
  uint ret;
 
365
  uint32_t ret;
372
366
  if (array->buffer > element)
373
367
    return -1;
374
368