46
46
void init_alloc_root(MEM_ROOT *mem_root, size_t block_size,
47
size_t pre_alloc_size __attribute__((unused)))
49
DBUG_ENTER("init_alloc_root");
50
DBUG_PRINT("enter",("root: 0x%lx", (long) mem_root));
49
52
mem_root->free= mem_root->used= mem_root->pre_alloc= 0;
50
53
mem_root->min_malloc= 32;
51
54
mem_root->block_size= block_size - ALLOC_ROOT_MIN_BLOCK_SIZE;
53
56
mem_root->block_num= 4; /* We shift this with >>2 */
54
57
mem_root->first_block_usage= 0;
59
#if !(defined(HAVE_purify) && defined(EXTRA_DEBUG))
62
if ((mem_root->free= mem_root->pre_alloc=
63
(USED_MEM*) my_malloc(pre_alloc_size+ ALIGN_SIZE(sizeof(USED_MEM)),
66
mem_root->free->size= pre_alloc_size+ALIGN_SIZE(sizeof(USED_MEM));
67
mem_root->free->left= pre_alloc_size;
68
mem_root->free->next= 0;
77
93
void reset_root_defaults(MEM_ROOT *mem_root, size_t block_size,
78
size_t pre_alloc_size)
94
size_t pre_alloc_size __attribute__((unused)))
80
assert(alloc_root_inited(mem_root));
96
DBUG_ASSERT(alloc_root_inited(mem_root));
82
98
mem_root->block_size= block_size - ALLOC_ROOT_MIN_BLOCK_SIZE;
99
#if !(defined(HAVE_purify) && defined(EXTRA_DEBUG))
83
100
if (pre_alloc_size)
85
102
size_t size= pre_alloc_size + ALIGN_SIZE(sizeof(USED_MEM));
104
121
/* remove block from the list and free it */
105
122
*prev= mem->next;
123
my_free(mem, MYF(0));
109
126
prev= &mem->next;
111
128
/* Allocate new prealloc block and add it to the end of free list */
112
if ((mem= (USED_MEM *) malloc(size)))
129
if ((mem= (USED_MEM *) my_malloc(size, MYF(0))))
115
132
mem->left= pre_alloc_size;
116
133
mem->next= *prev;
117
*prev= mem_root->pre_alloc= mem;
134
*prev= mem_root->pre_alloc= mem;
127
144
mem_root->pre_alloc= 0;
132
148
void *alloc_root(MEM_ROOT *mem_root, size_t length)
150
#if defined(HAVE_purify) && defined(EXTRA_DEBUG)
151
register USED_MEM *next;
152
DBUG_ENTER("alloc_root");
153
DBUG_PRINT("enter",("root: 0x%lx", (long) mem_root));
155
DBUG_ASSERT(alloc_root_inited(mem_root));
157
length+=ALIGN_SIZE(sizeof(USED_MEM));
158
if (!(next = (USED_MEM*) my_malloc(length,MYF(MY_WME | ME_FATALERROR))))
160
if (mem_root->error_handler)
161
(*mem_root->error_handler)();
162
DBUG_RETURN((uchar*) 0); /* purecov: inspected */
164
next->next= mem_root->used;
166
mem_root->used= next;
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))));
134
171
size_t get_size, block_size;
135
unsigned char* point;
136
173
register USED_MEM *next= 0;
137
174
register USED_MEM **prev;
138
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));
140
179
length= ALIGN_SIZE(length);
141
180
if ((*(prev= &mem_root->free)) != NULL)
159
198
get_size= length+ALIGN_SIZE(sizeof(USED_MEM));
160
199
get_size= max(get_size, block_size);
162
if (!(next = (USED_MEM*) malloc(get_size)))
201
if (!(next = (USED_MEM*) my_malloc(get_size,MYF(MY_WME | ME_FATALERROR))))
164
203
if (mem_root->error_handler)
165
204
(*mem_root->error_handler)();
166
return((void*) 0); /* purecov: inspected */
205
DBUG_RETURN((void*) 0); /* purecov: inspected */
168
207
mem_root->block_num++;
169
208
next->next= *prev;
175
point= (unsigned char*) ((char*) next+ (next->size-next->left));
214
point= (uchar*) ((char*) next+ (next->size-next->left));
176
215
/*TODO: next part may be unneded due to mem_root->first_block_usage counter*/
177
216
if ((next->left-= length) < mem_root->min_malloc)
178
217
{ /* Full block */
284
326
One can call this function either with root block initialised with
285
init_alloc_root() or with a zero:ed block.
327
init_alloc_root() or with a bzero()-ed block.
286
328
It's also safe to call this multiple times with the same mem_root.
289
331
void free_root(MEM_ROOT *root, myf MyFlags)
291
333
register USED_MEM *next,*old;
334
DBUG_ENTER("free_root");
335
DBUG_PRINT("enter",("root: 0x%lx flags: %u", (long) root, (uint) MyFlags));
293
337
if (MyFlags & MY_MARK_BLOCKS_FREE)
295
339
mark_blocks_free(root);
298
342
if (!(MyFlags & MY_KEEP_PREALLOC))
299
343
root->pre_alloc=0;
303
347
old=next; next= next->next ;
304
348
if (old != root->pre_alloc)
307
351
for (next=root->free ; next ;)
309
353
old=next; next= next->next;
310
354
if (old != root->pre_alloc)
313
357
root->used=root->free=0;
314
358
if (root->pre_alloc)
358
402
char *strmake_root(MEM_ROOT *root, const char *str, size_t len)
361
if ((pos=(char *)alloc_root(root,len+1)))
405
if ((pos=alloc_root(root,len+1)))
363
407
memcpy(pos,str,len);