45
46
void init_alloc_root(MEM_ROOT *mem_root, size_t block_size,
46
47
size_t pre_alloc_size __attribute__((unused)))
49
DBUG_ENTER("init_alloc_root");
50
DBUG_PRINT("enter",("root: 0x%lx", (long) mem_root));
48
52
mem_root->free= mem_root->used= mem_root->pre_alloc= 0;
49
53
mem_root->min_malloc= 32;
50
54
mem_root->block_size= block_size - ALLOC_ROOT_MIN_BLOCK_SIZE;
89
93
void reset_root_defaults(MEM_ROOT *mem_root, size_t block_size,
90
94
size_t pre_alloc_size __attribute__((unused)))
92
assert(alloc_root_inited(mem_root));
96
DBUG_ASSERT(alloc_root_inited(mem_root));
94
98
mem_root->block_size= block_size - ALLOC_ROOT_MIN_BLOCK_SIZE;
95
99
#if !(defined(HAVE_purify) && defined(EXTRA_DEBUG))
146
150
#if defined(HAVE_purify) && defined(EXTRA_DEBUG)
147
151
register USED_MEM *next;
152
DBUG_ENTER("alloc_root");
153
DBUG_PRINT("enter",("root: 0x%lx", (long) mem_root));
149
assert(alloc_root_inited(mem_root));
155
DBUG_ASSERT(alloc_root_inited(mem_root));
151
157
length+=ALIGN_SIZE(sizeof(USED_MEM));
152
158
if (!(next = (USED_MEM*) my_malloc(length,MYF(MY_WME | ME_FATALERROR))))
154
160
if (mem_root->error_handler)
155
161
(*mem_root->error_handler)();
156
return((unsigned char*) 0); /* purecov: inspected */
162
DBUG_RETURN((uchar*) 0); /* purecov: inspected */
158
164
next->next= mem_root->used;
159
165
next->size= length;
160
166
mem_root->used= next;
161
return((unsigned char*) (((char*) next)+ALIGN_SIZE(sizeof(USED_MEM))));
167
DBUG_PRINT("exit",("ptr: 0x%lx", (long) (((char*) next)+
168
ALIGN_SIZE(sizeof(USED_MEM)))));
169
DBUG_RETURN((uchar*) (((char*) next)+ALIGN_SIZE(sizeof(USED_MEM))));
163
171
size_t get_size, block_size;
164
unsigned char* point;
165
173
register USED_MEM *next= 0;
166
174
register USED_MEM **prev;
167
assert(alloc_root_inited(mem_root));
175
DBUG_ENTER("alloc_root");
176
DBUG_PRINT("enter",("root: 0x%lx", (long) mem_root));
177
DBUG_ASSERT(alloc_root_inited(mem_root));
169
179
length= ALIGN_SIZE(length);
170
180
if ((*(prev= &mem_root->free)) != NULL)
186
196
{ /* Time to alloc new block */
187
197
block_size= mem_root->block_size * (mem_root->block_num >> 2);
188
198
get_size= length+ALIGN_SIZE(sizeof(USED_MEM));
189
get_size= cmax(get_size, block_size);
199
get_size= max(get_size, block_size);
191
201
if (!(next = (USED_MEM*) my_malloc(get_size,MYF(MY_WME | ME_FATALERROR))))
193
203
if (mem_root->error_handler)
194
204
(*mem_root->error_handler)();
195
return((void*) 0); /* purecov: inspected */
205
DBUG_RETURN((void*) 0); /* purecov: inspected */
197
207
mem_root->block_num++;
198
208
next->next= *prev;
204
point= (unsigned char*) ((char*) next+ (next->size-next->left));
214
point= (uchar*) ((char*) next+ (next->size-next->left));
205
215
/*TODO: next part may be unneded due to mem_root->first_block_usage counter*/
206
216
if ((next->left-= length) < mem_root->min_malloc)
207
217
{ /* Full block */
314
326
One can call this function either with root block initialised with
315
init_alloc_root() or with a zero:ed block.
327
init_alloc_root() or with a bzero()-ed block.
316
328
It's also safe to call this multiple times with the same mem_root.
319
331
void free_root(MEM_ROOT *root, myf MyFlags)
321
333
register USED_MEM *next,*old;
334
DBUG_ENTER("free_root");
335
DBUG_PRINT("enter",("root: 0x%lx flags: %u", (long) root, (uint) MyFlags));
323
337
if (MyFlags & MY_MARK_BLOCKS_FREE)
325
339
mark_blocks_free(root);
328
342
if (!(MyFlags & MY_KEEP_PREALLOC))
329
343
root->pre_alloc=0;
333
347
old=next; next= next->next ;
334
348
if (old != root->pre_alloc)
337
351
for (next=root->free ; next ;)
339
353
old=next; next= next->next;
340
354
if (old != root->pre_alloc)
343
357
root->used=root->free=0;
344
358
if (root->pre_alloc)