drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
root.cc
Go to the documentation of this file.
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
14 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
20
21#include <config.h>
22
25#include <drizzled/internal/my_sys.h>
26#include <drizzled/internal/m_string.h>
27#include <drizzled/sql_string.h>
28
29#include <algorithm>
30
31using namespace std;
32
33namespace drizzled {
34namespace memory {
35
36static const unsigned int MAX_BLOCK_TO_DROP= 4096;
37static const unsigned int MAX_BLOCK_USAGE_BEFORE_DROP= 10;
38
57void Root::init(size_t block_size_arg)
58{
59 free= used= pre_alloc= 0;
60 min_malloc= 32;
61 block_size= block_size_arg - ROOT_MIN_BLOCK_SIZE;
62 block_num= 4; /* We shift this with >>2 */
64}
65
81void Root::reset_defaults(size_t block_size_arg, size_t pre_alloc_size)
82{
83 block_size= block_size_arg - ROOT_MIN_BLOCK_SIZE;
84 if (pre_alloc_size)
85 {
86 size_t size= pre_alloc_size + ALIGN_SIZE(sizeof(internal::UsedMemory));
87 if (not pre_alloc || pre_alloc->size != size)
88 {
89 internal::UsedMemory *mem, **prev= &this->free;
90 /*
91 Free unused blocks, so that consequent calls
92 to reset_root_defaults won't eat away memory.
93 */
94 while (*prev)
95 {
96 mem= *prev;
97 if (mem->size == size)
98 {
99 /* We found a suitable block, no need to do anything else */
100 pre_alloc= mem;
101 return;
102 }
103 if (mem->left + ALIGN_SIZE(sizeof(internal::UsedMemory)) == mem->size)
104 {
105 /* remove block from the list and free it */
106 *prev= mem->next;
107 std::free(mem);
108 }
109 else
110 prev= &mem->next;
111 }
112 /* Allocate new prealloc block and add it to the end of free list */
113 mem= static_cast<internal::UsedMemory *>(malloc(size));
114 mem->size= size;
115 mem->left= pre_alloc_size;
116 mem->next= *prev;
117 *prev= pre_alloc= mem;
118 }
119 }
120 else
121 {
122 pre_alloc= 0;
123 }
124}
125
140unsigned char* Root::alloc(size_t length)
141{
142 internal::UsedMemory *next= NULL;
143 assert(alloc_root_inited());
144
145 length= ALIGN_SIZE(length);
146 internal::UsedMemory **prev= &this->free;
147 if (*prev)
148 {
149 if ((*prev)->left < length &&
150 this->first_block_usage++ >= MAX_BLOCK_USAGE_BEFORE_DROP &&
151 (*prev)->left < MAX_BLOCK_TO_DROP)
152 {
153 next= *prev;
154 *prev= next->next; /* Remove block from list */
155 next->next= this->used;
156 this->used= next;
157 this->first_block_usage= 0;
158 }
159 for (next= *prev; next && next->left < length; next= next->next)
160 prev= &next->next;
161 }
162 if (! next)
163 { /* Time to alloc new block */
164 size_t tmp_block_size= this->block_size * (this->block_num >> 2);
165 size_t get_size= length+ALIGN_SIZE(sizeof(internal::UsedMemory));
166 get_size= max(get_size, tmp_block_size);
167
168 next = static_cast<internal::UsedMemory *>(malloc(get_size));
169 this->block_num++;
170 next->next= *prev;
171 next->size= get_size;
172 next->left= get_size-ALIGN_SIZE(sizeof(internal::UsedMemory));
173 *prev=next;
174 }
175
176 unsigned char* point= (unsigned char*) ((char*) next+ (next->size-next->left));
178 if ((next->left-= length) < this->min_malloc)
179 { /* Full block */
180 *prev= next->next; /* Remove block from list */
181 next->next= this->used;
182 this->used= next;
183 this->first_block_usage= 0;
184 }
185
186 return point;
187}
188
189
212void* Root::multi_alloc(int unused, ...)
213{
214 va_list args;
215 char* *ptr, *start, *res;
216 size_t tot_length, length;
217
218 va_start(args, unused);
219 tot_length= 0;
220 while ((ptr= va_arg(args, char* *)))
221 {
222 length= va_arg(args, uint);
223 tot_length+= ALIGN_SIZE(length);
224 }
225 va_end(args);
226
227 start= (char*) this->alloc(tot_length);
228
229 va_start(args, unused);
230 res= start;
231 while ((ptr= va_arg(args, char* *)))
232 {
233 *ptr= res;
234 length= va_arg(args, uint);
235 res+= ALIGN_SIZE(length);
236 }
237 va_end(args);
238 return((void*) start);
239}
240
246{
249
250 /* iterate through (partially) free blocks, mark them free */
251 last= &free;
252 for (next= free; next; next= *(last= &next->next))
253 {
254 next->left= next->size - ALIGN_SIZE(sizeof(internal::UsedMemory));
255 }
256
257 /* Combine the free and the used list */
258 *last= next= used;
259
260 /* now go through the used blocks and mark them free */
261 for (; next; next= next->next)
262 {
263 next->left= next->size - ALIGN_SIZE(sizeof(internal::UsedMemory));
264 }
265
266 /* Now everything is set; Indicate that nothing is used anymore */
267 used= 0;
269}
270
287void Root::free_root(myf MyFlags)
288{
289 if (MyFlags & MARK_BLOCKS_FREE)
290 {
291 this->mark_blocks_free();
292 return;
293 }
294 if (!(MyFlags & KEEP_PREALLOC))
295 this->pre_alloc=0;
296
297 for (internal::UsedMemory* next= this->used; next;)
298 {
299 internal::UsedMemory* old =next;
300 next= next->next;
301 if (old != this->pre_alloc)
302 std::free(old);
303 }
304 for (internal::UsedMemory* next=this->free; next;)
305 {
306 internal::UsedMemory* old= next;
307 next= next->next;
308 if (old != this->pre_alloc)
309 std::free(old);
310 }
311 this->used=this->free=0;
312 if (this->pre_alloc)
313 {
314 this->free=this->pre_alloc;
315 this->free->left=this->pre_alloc->size-ALIGN_SIZE(sizeof(internal::UsedMemory));
316 this->free->next=0;
317 }
318 this->block_num= 4;
319 this->first_block_usage= 0;
320}
321
327char* Root::strdup(const char* str)
328{
329 return strdup(str, strlen(str));
330}
331
343char* Root::strdup(const char* str, size_t len)
344{
345 char* pos= (char*)alloc(len + 1);
346 memcpy(pos, str, len);
347 pos[len]= 0;
348 return pos;
349}
350
351char* Root::strdup(str_ref v)
352{
353 return strdup(v.data(), v.size());
354}
355
365void* Root::memdup(const void* str, size_t len)
366{
367 void* pos= alloc(len);
368 memcpy(pos, str, len);
369 return pos;
370}
371
372}
373} /* namespace drizzled */
unsigned char * alloc(size_t Size)
Allocate a chunk of memory from the Root structure provided, obtaining more memory from the heap if n...
Definition root.cc:140
void free_root(myf MyFLAGS)
Deallocate everything used by alloc_root or just move used blocks to free list if called with MY_USED...
Definition root.cc:287
void * memdup(const void *, size_t)
Duplicate the provided block into memory allocated from within the specified Root.
Definition root.cc:365
unsigned int block_num
allocated blocks counter
Definition root.h:100
unsigned int first_block_usage
Definition root.h:106
void * multi_alloc(int unused,...)
Allocate many pointers at the same time.
Definition root.cc:212
void mark_blocks_free()
Mark all data in blocks free for reusage.
Definition root.cc:245
size_t block_size
initial block size
Definition root.h:99
char * strdup(const char *)
Duplicate a null-terminated string into memory allocated from within the specified Root.
Definition root.cc:327
internal::UsedMemory * free
Definition root.h:82
void reset_defaults(size_t block_size, size_t prealloc_size)
Definition root.cc:81
internal::UsedMemory * used
Definition root.h:87
internal::UsedMemory * pre_alloc
Definition root.h:92
void init(size_t block_size=ROOT_MIN_BLOCK_SIZE)
Initialize memory root.
Definition root.cc:57
TODO: Rename this file - func.h is stupid.
STL namespace.
Memory root declarations.