~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
1802.10.2 by Monty Taylor
Update all of the copyright headers to include the correct address.
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
1 by brian
clean slate
15
16
/* Handling of arrays that can grow dynamicly. */
17
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
18
#include <config.h>
2221.7.4 by Olaf van der Spek
Refactor
19
#include <algorithm>
20
#include <drizzled/dynamic_array.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
21
#include <drizzled/internal/my_sys.h>
1 by brian
clean slate
22
1067.4.10 by Nathan Williams
Converted all cmin/cmax usages in the mysys directory to std::min/max.
23
using namespace std;
24
2221.7.1 by Olaf van der Spek
DYNAMIC_ARRAY::size()
25
namespace drizzled {
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
26
1 by brian
clean slate
27
/*
28
  Initiate dynamic array
29
30
  SYNOPSIS
31
    init_dynamic_array2()
32
      array		Pointer to an array
33
      element_size	Size of element
34
      init_buffer       Initial buffer pointer
35
      init_alloc	Number of initial elements
36
      alloc_increment	Increment for adding new elements
37
38
  DESCRIPTION
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
39
    init_dynamic_array() initiates array and allocate space for
40
    init_alloc eilements.
1 by brian
clean slate
41
    Array is usable even if space allocation failed.
42
    Static buffers must begin immediately after the array structure.
43
44
  RETURN VALUE
656.3.2 by Monty Taylor
Removed my_nosys and my_quick. /me shudders.
45
    true	malloc() failed
163 by Brian Aker
Merge Monty's code.
46
    false	Ok
1 by brian
clean slate
47
*/
48
482 by Brian Aker
Remove uint.
49
bool init_dynamic_array2(DYNAMIC_ARRAY *array, uint32_t element_size,
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
50
                            void *init_buffer, uint32_t init_alloc,
575.1.6 by Monty Taylor
Cleaned up some headers for PCH.
51
                            uint32_t alloc_increment)
1 by brian
clean slate
52
{
53
  if (!alloc_increment)
54
  {
1067.4.10 by Nathan Williams
Converted all cmin/cmax usages in the mysys directory to std::min/max.
55
    alloc_increment=max((8192-MALLOC_OVERHEAD)/element_size,16U);
1 by brian
clean slate
56
    if (init_alloc > 8 && alloc_increment > init_alloc * 2)
57
      alloc_increment=init_alloc*2;
58
  }
59
60
  if (!init_alloc)
61
  {
62
    init_alloc=alloc_increment;
63
    init_buffer= 0;
64
  }
2221.7.1 by Olaf van der Spek
DYNAMIC_ARRAY::size()
65
  array->set_size(0);
1 by brian
clean slate
66
  array->max_element=init_alloc;
67
  array->alloc_increment=alloc_increment;
68
  array->size_of_element=element_size;
575.3.1 by Monty Taylor
Made mysys and mystrings c++. Fixed the resulting bugs the compiler found.
69
  if ((array->buffer= (unsigned char*) init_buffer))
51.3.22 by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile
70
    return(false);
656.3.2 by Monty Taylor
Removed my_nosys and my_quick. /me shudders.
71
  if (!(array->buffer=(unsigned char*) malloc(element_size*init_alloc)))
1 by brian
clean slate
72
  {
73
    array->max_element=0;
51.3.22 by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile
74
    return(true);
1 by brian
clean slate
75
  }
51.3.22 by Jay Pipes
Final round of removal of DBUG in mysys/, including Makefile
76
  return(false);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
77
}
1 by brian
clean slate
78
79
/*
80
  Insert element at the end of array. Allocate memory if needed.
81
82
  SYNOPSIS
83
    insert_dynamic()
84
      array
85
      element
86
87
  RETURN VALUE
163 by Brian Aker
Merge Monty's code.
88
    true	Insert failed
89
    false	Ok
1 by brian
clean slate
90
*/
91
2210.3.6 by Olaf van der Spek
Use push_back
92
static void insert_dynamic(DYNAMIC_ARRAY *array, void* element)
1 by brian
clean slate
93
{
481 by Brian Aker
Remove all of uchar.
94
  unsigned char* buffer;
2221.7.1 by Olaf van der Spek
DYNAMIC_ARRAY::size()
95
  if (array->size() == array->max_element)
2210.3.6 by Olaf van der Spek
Use push_back
96
    buffer= alloc_dynamic(array);
1 by brian
clean slate
97
  else
98
  {
2221.7.1 by Olaf van der Spek
DYNAMIC_ARRAY::size()
99
    buffer= array->buffer+(array->size() * array->size_of_element);
100
    array->set_size(array->size() + 1);
1 by brian
clean slate
101
  }
2210.3.6 by Olaf van der Spek
Use push_back
102
  memcpy(buffer,element, array->size_of_element);
1 by brian
clean slate
103
}
104
2210.3.5 by Olaf van der Spek
Refactor
105
void DYNAMIC_ARRAY::push_back(void* v)
106
{
2210.3.6 by Olaf van der Spek
Use push_back
107
  insert_dynamic(this, v);
2210.3.5 by Olaf van der Spek
Refactor
108
}
109
1 by brian
clean slate
110
111
/*
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
112
  Alloc space for next element(s)
1 by brian
clean slate
113
114
  SYNOPSIS
115
    alloc_dynamic()
116
      array
117
118
  DESCRIPTION
119
    alloc_dynamic() checks if there is empty space for at least
120
    one element if not tries to allocate space for alloc_increment
121
    elements at the end of array.
122
123
  RETURN VALUE
124
    pointer	Pointer to empty space for element
125
    0		Error
126
*/
127
481 by Brian Aker
Remove all of uchar.
128
unsigned char *alloc_dynamic(DYNAMIC_ARRAY *array)
1 by brian
clean slate
129
{
2221.7.1 by Olaf van der Spek
DYNAMIC_ARRAY::size()
130
  if (array->size() == array->max_element)
1 by brian
clean slate
131
  {
132
    char *new_ptr;
481 by Brian Aker
Remove all of uchar.
133
    if (array->buffer == (unsigned char *)(array + 1))
1 by brian
clean slate
134
    {
135
      /*
136
        In this senerio, the buffer is statically preallocated,
137
        so we have to create an all-new malloc since we overflowed
138
      */
656.1.26 by Monty Taylor
Finally removed all of the my_malloc stuff.
139
      if (!(new_ptr= (char *) malloc((array->max_element+
140
                                     array->alloc_increment) *
141
                                     array->size_of_element)))
1 by brian
clean slate
142
        return 0;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
143
      memcpy(new_ptr, array->buffer,
2221.7.1 by Olaf van der Spek
DYNAMIC_ARRAY::size()
144
             array->size() * array->size_of_element);
1 by brian
clean slate
145
    }
656.1.47 by Monty Taylor
More malloc return check fixes.
146
    else if (!(new_ptr= (char*) realloc(array->buffer,
147
                                        (array->max_element+
148
                                         array->alloc_increment)*
149
                                        array->size_of_element)))
1 by brian
clean slate
150
      return 0;
481 by Brian Aker
Remove all of uchar.
151
    array->buffer= (unsigned char*) new_ptr;
1 by brian
clean slate
152
    array->max_element+=array->alloc_increment;
153
  }
2221.7.1 by Olaf van der Spek
DYNAMIC_ARRAY::size()
154
  array->set_size(array->size() + 1);
155
  return array->buffer + ((array->size() - 1) * array->size_of_element);
1 by brian
clean slate
156
}
157
158
159
/*
160
  Pop last element from array.
161
162
  SYNOPSIS
163
    pop_dynamic()
164
      array
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
165
166
  RETURN VALUE
1 by brian
clean slate
167
    pointer	Ok
168
    0		Array is empty
169
*/
170
481 by Brian Aker
Remove all of uchar.
171
unsigned char *pop_dynamic(DYNAMIC_ARRAY *array)
1 by brian
clean slate
172
{
2221.7.1 by Olaf van der Spek
DYNAMIC_ARRAY::size()
173
  if (!array->size())
174
    return 0;
175
  array->set_size(array->size() - 1);
176
  return array->buffer+(array->size() * array->size_of_element);
1 by brian
clean slate
177
}
178
179
/*
180
  Empty array by freeing all memory
181
182
  SYNOPSIS
183
    delete_dynamic()
184
      array	Array to be deleted
185
*/
186
187
void delete_dynamic(DYNAMIC_ARRAY *array)
188
{
189
  /*
190
    Just mark as empty if we are using a static buffer
191
  */
481 by Brian Aker
Remove all of uchar.
192
  if (array->buffer == (unsigned char *)(array + 1))
2221.7.1 by Olaf van der Spek
DYNAMIC_ARRAY::size()
193
    array->set_size(0);
1 by brian
clean slate
194
  else
195
  if (array->buffer)
196
  {
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
197
    free(array->buffer);
1 by brian
clean slate
198
    array->buffer=0;
2221.7.1 by Olaf van der Spek
DYNAMIC_ARRAY::size()
199
    array->set_size(array->max_element=0);
1 by brian
clean slate
200
  }
201
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
202
203
} /* namespace drizzled */