~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
2318.4.8 by Olaf van der Spek
Remove malloc NULL check. Just die.
49
void 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))
2318.4.8 by Olaf van der Spek
Remove malloc NULL check. Just die.
70
    return;
71
  array->buffer= (unsigned char*) malloc(element_size*init_alloc);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
72
}
1 by brian
clean slate
73
74
/*
75
  Insert element at the end of array. Allocate memory if needed.
76
77
  SYNOPSIS
78
    insert_dynamic()
79
      array
80
      element
81
82
  RETURN VALUE
163 by Brian Aker
Merge Monty's code.
83
    true	Insert failed
84
    false	Ok
1 by brian
clean slate
85
*/
86
2210.3.6 by Olaf van der Spek
Use push_back
87
static void insert_dynamic(DYNAMIC_ARRAY *array, void* element)
1 by brian
clean slate
88
{
481 by Brian Aker
Remove all of uchar.
89
  unsigned char* buffer;
2221.7.1 by Olaf van der Spek
DYNAMIC_ARRAY::size()
90
  if (array->size() == array->max_element)
2210.3.6 by Olaf van der Spek
Use push_back
91
    buffer= alloc_dynamic(array);
1 by brian
clean slate
92
  else
93
  {
2221.7.1 by Olaf van der Spek
DYNAMIC_ARRAY::size()
94
    buffer= array->buffer+(array->size() * array->size_of_element);
95
    array->set_size(array->size() + 1);
1 by brian
clean slate
96
  }
2210.3.6 by Olaf van der Spek
Use push_back
97
  memcpy(buffer,element, array->size_of_element);
1 by brian
clean slate
98
}
99
2210.3.5 by Olaf van der Spek
Refactor
100
void DYNAMIC_ARRAY::push_back(void* v)
101
{
2210.3.6 by Olaf van der Spek
Use push_back
102
  insert_dynamic(this, v);
2210.3.5 by Olaf van der Spek
Refactor
103
}
104
1 by brian
clean slate
105
106
/*
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
107
  Alloc space for next element(s)
1 by brian
clean slate
108
109
  SYNOPSIS
110
    alloc_dynamic()
111
      array
112
113
  DESCRIPTION
114
    alloc_dynamic() checks if there is empty space for at least
115
    one element if not tries to allocate space for alloc_increment
116
    elements at the end of array.
117
118
  RETURN VALUE
119
    pointer	Pointer to empty space for element
120
    0		Error
121
*/
122
481 by Brian Aker
Remove all of uchar.
123
unsigned char *alloc_dynamic(DYNAMIC_ARRAY *array)
1 by brian
clean slate
124
{
2221.7.1 by Olaf van der Spek
DYNAMIC_ARRAY::size()
125
  if (array->size() == array->max_element)
1 by brian
clean slate
126
  {
127
    char *new_ptr;
481 by Brian Aker
Remove all of uchar.
128
    if (array->buffer == (unsigned char *)(array + 1))
1 by brian
clean slate
129
    {
130
      /*
131
        In this senerio, the buffer is statically preallocated,
132
        so we have to create an all-new malloc since we overflowed
133
      */
2318.4.8 by Olaf van der Spek
Remove malloc NULL check. Just die.
134
      new_ptr= (char*) malloc((array->max_element + array->alloc_increment) * array->size_of_element);
135
      memcpy(new_ptr, array->buffer, array->size() * array->size_of_element);
1 by brian
clean slate
136
    }
2318.4.8 by Olaf van der Spek
Remove malloc NULL check. Just die.
137
    else 
138
      new_ptr= (char*) realloc(array->buffer, (array->max_element + array->alloc_increment) * array->size_of_element);
481 by Brian Aker
Remove all of uchar.
139
    array->buffer= (unsigned char*) new_ptr;
1 by brian
clean slate
140
    array->max_element+=array->alloc_increment;
141
  }
2221.7.1 by Olaf van der Spek
DYNAMIC_ARRAY::size()
142
  array->set_size(array->size() + 1);
143
  return array->buffer + ((array->size() - 1) * array->size_of_element);
1 by brian
clean slate
144
}
145
146
/*
147
  Empty array by freeing all memory
148
149
  SYNOPSIS
150
    delete_dynamic()
151
      array	Array to be deleted
152
*/
153
154
void delete_dynamic(DYNAMIC_ARRAY *array)
155
{
156
  /*
157
    Just mark as empty if we are using a static buffer
158
  */
481 by Brian Aker
Remove all of uchar.
159
  if (array->buffer == (unsigned char *)(array + 1))
2221.7.1 by Olaf van der Spek
DYNAMIC_ARRAY::size()
160
    array->set_size(0);
1 by brian
clean slate
161
  else
162
  if (array->buffer)
163
  {
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.
164
    free(array->buffer);
1 by brian
clean slate
165
    array->buffer=0;
2221.7.1 by Olaf van der Spek
DYNAMIC_ARRAY::size()
166
    array->set_size(array->max_element=0);
1 by brian
clean slate
167
  }
168
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
169
170
} /* namespace drizzled */