25 #include <drizzled/internal/my_sys.h>
26 #include <drizzled/internal/m_string.h>
27 #include <drizzled/sql_string.h>
36 static const unsigned int MAX_BLOCK_TO_DROP= 4096;
37 static const unsigned int MAX_BLOCK_USAGE_BEFORE_DROP= 10;
57 void Root::init(
size_t block_size_arg)
59 free= used= pre_alloc= 0;
61 block_size= block_size_arg - ROOT_MIN_BLOCK_SIZE;
81 void Root::reset_defaults(
size_t block_size_arg,
size_t pre_alloc_size)
83 block_size= block_size_arg - ROOT_MIN_BLOCK_SIZE;
87 if (not pre_alloc || pre_alloc->size != size)
97 if (mem->size == size)
115 mem->left= pre_alloc_size;
117 *prev= pre_alloc= mem;
140 unsigned char* Root::alloc(
size_t length)
143 assert(alloc_root_inited());
145 length= ALIGN_SIZE(length);
149 if ((*prev)->left < length &&
150 this->first_block_usage++ >= MAX_BLOCK_USAGE_BEFORE_DROP &&
151 (*prev)->left < MAX_BLOCK_TO_DROP)
155 next->next= this->used;
157 this->first_block_usage= 0;
159 for (next= *prev; next && next->left < length; next= next->next)
164 size_t tmp_block_size= this->block_size * (this->block_num >> 2);
166 get_size= max(get_size, tmp_block_size);
171 next->size= get_size;
176 unsigned char* point= (
unsigned char*) ((
char*) next+ (next->size-next->left));
178 if ((next->left-= length) < this->min_malloc)
181 next->next= this->used;
183 this->first_block_usage= 0;
212 void* Root::multi_alloc(
int unused, ...)
215 char* *ptr, *start, *res;
216 size_t tot_length, length;
219 va_start(args, unused);
221 while ((ptr= va_arg(args,
char* *)))
223 length= va_arg(args, uint);
224 tot_length+= ALIGN_SIZE(length);
228 start= (
char*) this->alloc(tot_length);
230 va_start(args, unused);
232 while ((ptr= va_arg(args,
char* *)))
235 length= va_arg(args, uint);
236 res+= ALIGN_SIZE(length);
239 return((
void*) start);
246 void Root::mark_blocks_free()
253 for (next= free; next; next= *(last= &next->next))
262 for (; next; next= next->next)
269 first_block_usage= 0;
288 void Root::free_root(myf MyFlags)
290 if (MyFlags & MARK_BLOCKS_FREE)
292 this->mark_blocks_free();
295 if (!(MyFlags & KEEP_PREALLOC))
302 if (old != this->pre_alloc)
309 if (old != this->pre_alloc)
312 this->used=this->free=0;
315 this->free=this->pre_alloc;
320 this->first_block_usage= 0;
328 char* Root::strdup(
const char* str)
330 return strdup(str, strlen(str));
344 char* Root::strdup(
const char* str,
size_t len)
346 char* pos= (
char*)alloc(len + 1);
347 memcpy(pos, str, len);
354 return strdup(v.data(), v.size());
366 void* Root::memdup(
const void* str,
size_t len)
368 void* pos= alloc(len);
369 memcpy(pos, str, len);
TODO: Rename this file - func.h is stupid.
Memory root declarations.