drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
mem0mem.cc
1/*****************************************************************************
2
3Copyright (C) 1994, 2010, Innobase Oy. All Rights Reserved.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License as published by the Free Software
7Foundation; version 2 of the License.
8
9This program is distributed in the hope that it will be useful, but WITHOUT
10ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13You should have received a copy of the GNU General Public License along with
14this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
15St, Fifth Floor, Boston, MA 02110-1301 USA
16
17*****************************************************************************/
18
19/********************************************************************/
25
26#include "mem0mem.h"
27
28#ifdef UNIV_NONINL
29#include "mem0mem.ic"
30#endif
31
32#include "buf0buf.h"
33#include "srv0srv.h"
34#include "mem0dbg.cc"
35#include <stdarg.h>
36
37/*
38 THE MEMORY MANAGEMENT
39 =====================
40
41The basic element of the memory management is called a memory
42heap. A memory heap is conceptually a
43stack from which memory can be allocated. The stack may grow infinitely.
44The top element of the stack may be freed, or
45the whole stack can be freed at one time. The advantage of the
46memory heap concept is that we can avoid using the malloc and free
47functions of C which can be quite expensive. When we use a memory heap,
48we can allocate larger blocks of memory at a time and thus
49reduce overhead. Slightly more efficient the method is when we
50allocate the memory from the index page buffer pool, as we can
51claim a new page fast. This is called buffer allocation.
52When we allocate the memory from the dynamic memory of the
53C environment, that is called dynamic allocation.
54
55The default way of operation of the memory heap is the following.
56First, when the heap is created, an initial block of memory is
57allocated. In dynamic allocation this may be about 50 bytes.
58If more space is needed, additional blocks are allocated
59and they are put into a linked list.
60After the initial block, each allocated block is twice the size of the
61previous, until a threshold is attained, after which the sizes
62of the blocks stay the same. An exception is, of course, the case
63where the caller requests a memory buffer whose size is
64bigger than the threshold. In that case a block big enough must
65be allocated.
66
67The heap is physically arranged so that if the current block
68becomes full, a new block is allocated and always inserted in the
69chain of blocks as the last block.
70
71In the debug version of the memory management, all the allocated
72heaps are kept in a list (which is implemented as a hash table).
73Thus we can notice if the caller tries to free an already freed
74heap. In addition, each buffer given to the caller contains
75start field at the start and a trailer field at the end of the buffer.
76
77The start field has the following content:
78A. sizeof(ulint) bytes of field length (in the standard byte order)
79B. sizeof(ulint) bytes of check field (a random number)
80
81The trailer field contains:
82A. sizeof(ulint) bytes of check field (the same random number as at the start)
83
84Thus we can notice if something has been copied over the
85borders of the buffer, which is illegal.
86The memory in the buffers is initialized to a random byte sequence.
87After freeing, all the blocks in the heap are set to random bytes
88to help us discover errors which result from the use of
89buffers in an already freed heap. */
90
91#ifdef MEM_PERIODIC_CHECK
92
93ibool mem_block_list_inited;
94/* List of all mem blocks allocated; protected by the mem_comm_pool mutex */
95UT_LIST_BASE_NODE_T(mem_block_t) mem_block_list;
96
97#endif
98
99/**********************************************************************/
102UNIV_INTERN
103char*
105/*============*/
106 mem_heap_t* heap,
107 const char* str)
108{
109 return(static_cast<char *>(mem_heap_dup(heap, str, strlen(str) + 1)));
110}
111
112/**********************************************************************/
115UNIV_INTERN
116void*
118/*=========*/
119 mem_heap_t* heap,
120 const void* data,
121 ulint len)
122{
123 return(memcpy(mem_heap_alloc(heap, len), data, len));
124}
125
126/**********************************************************************/
129UNIV_INTERN
130char*
132/*============*/
133 mem_heap_t* heap,
134 const char* s1,
135 const char* s2)
136{
137 char* s;
138 ulint s1_len = strlen(s1);
139 ulint s2_len = strlen(s2);
140
141 s = static_cast<char *>(mem_heap_alloc(heap, s1_len + s2_len + 1));
142
143 memcpy(s, s1, s1_len);
144 memcpy(s + s1_len, s2, s2_len);
145
146 s[s1_len + s2_len] = '\0';
147
148 return(s);
149}
150
151
152/****************************************************************/
155static
156ulint
157mem_heap_printf_low(
158/*================*/
159 char* buf,
161 const char* format,
162 va_list ap)
163{
164 ulint len = 0;
165
166 while (*format) {
167
168 /* Does this format specifier have the 'l' length modifier. */
169 ibool is_long = FALSE;
170
171 /* Length of one parameter. */
172 size_t plen;
173
174 if (*format++ != '%') {
175 /* Non-format character. */
176
177 len++;
178
179 if (buf) {
180 *buf++ = *(format - 1);
181 }
182
183 continue;
184 }
185
186 if (*format == 'l') {
187 is_long = TRUE;
188 format++;
189 }
190
191 switch (*format++) {
192 case 's':
193 /* string */
194 {
195 char* s = va_arg(ap, char*);
196
197 /* "%ls" is a non-sensical format specifier. */
198 ut_a(!is_long);
199
200 plen = strlen(s);
201 len += plen;
202
203 if (buf) {
204 memcpy(buf, s, plen);
205 buf += plen;
206 }
207 }
208
209 break;
210
211 case 'u':
212 /* unsigned int */
213 {
214 char tmp[32];
215 unsigned long val;
216
217 /* We only support 'long' values for now. */
218 ut_a(is_long);
219
220 val = va_arg(ap, unsigned long);
221
222 plen = sprintf(tmp, "%lu", val);
223 len += plen;
224
225 if (buf) {
226 memcpy(buf, tmp, plen);
227 buf += plen;
228 }
229 }
230
231 break;
232
233 case '%':
234
235 /* "%l%" is a non-sensical format specifier. */
236 ut_a(!is_long);
237
238 len++;
239
240 if (buf) {
241 *buf++ = '%';
242 }
243
244 break;
245
246 default:
247 ut_error;
248 }
249 }
250
251 /* For the NUL character. */
252 len++;
253
254 if (buf) {
255 *buf = '\0';
256 }
257
258 return(len);
259}
260
261/****************************************************************/
267UNIV_INTERN
268char*
270/*============*/
271 mem_heap_t* heap,
272 const char* format,
273 ...)
274{
275 va_list ap;
276 char* str;
277 ulint len;
278
279 /* Calculate length of string */
280 len = 0;
281 va_start(ap, format);
282 len = mem_heap_printf_low(NULL, format, ap);
283 va_end(ap);
284
285 /* Now create it for real. */
286 str = static_cast<char *>(mem_heap_alloc(heap, len));
287 va_start(ap, format);
288 mem_heap_printf_low(str, format, ap);
289 va_end(ap);
290
291 return(str);
292}
293
294/***************************************************************/
298UNIV_INTERN
299mem_block_t*
300mem_heap_create_block(
301/*==================*/
302 mem_heap_t* heap,
304 ulint n,
305 ulint type,
307 const char* file_name,
308 ulint line)
309{
310#ifndef UNIV_HOTBACKUP
311 buf_block_t* buf_block = NULL;
312#endif /* !UNIV_HOTBACKUP */
313 mem_block_t* block;
314 ulint len;
315
316 ut_ad((type == MEM_HEAP_DYNAMIC) || (type == MEM_HEAP_BUFFER)
317 || (type == MEM_HEAP_BUFFER + MEM_HEAP_BTR_SEARCH));
318
319 if (heap && heap->magic_n != MEM_BLOCK_MAGIC_N) {
321 }
322
323 /* In dynamic allocation, calculate the size: block header + data. */
324 len = MEM_BLOCK_HEADER_SIZE + MEM_SPACE_NEEDED(n);
325
326#ifndef UNIV_HOTBACKUP
327 if (type == MEM_HEAP_DYNAMIC || len < UNIV_PAGE_SIZE / 2) {
328
329 ut_ad(type == MEM_HEAP_DYNAMIC || n <= MEM_MAX_ALLOC_IN_BUF);
330
331 block = static_cast<mem_block_info_t *>(mem_area_alloc(&len, mem_comm_pool));
332 } else {
333 len = UNIV_PAGE_SIZE;
334
335 if ((type & MEM_HEAP_BTR_SEARCH) && heap) {
336 /* We cannot allocate the block from the
337 buffer pool, but must get the free block from
338 the heap header free block field */
339
340 buf_block = static_cast<buf_block_t *>(heap->free_block);
341 heap->free_block = NULL;
342
343 if (UNIV_UNLIKELY(!buf_block)) {
344
345 return(NULL);
346 }
347 } else {
348 buf_block = buf_block_alloc(NULL);
349 }
350
351 block = (mem_block_t*) buf_block->frame;
352 }
353
354 ut_ad(block);
355 block->buf_block = buf_block;
356 block->free_block = NULL;
357#else /* !UNIV_HOTBACKUP */
358 len = MEM_BLOCK_HEADER_SIZE + MEM_SPACE_NEEDED(n);
359 block = ut_malloc(len);
360 ut_ad(block);
361#endif /* !UNIV_HOTBACKUP */
362
363 block->magic_n = MEM_BLOCK_MAGIC_N;
364 ut_strlcpy_rev(block->file_name, file_name, sizeof(block->file_name));
365 block->line = line;
366
367#ifdef MEM_PERIODIC_CHECK
368 mutex_enter(&(mem_comm_pool->mutex));
369
370 if (!mem_block_list_inited) {
371 mem_block_list_inited = TRUE;
372 UT_LIST_INIT(mem_block_list);
373 }
374
375 UT_LIST_ADD_LAST(mem_block_list, mem_block_list, block);
376
377 mutex_exit(&(mem_comm_pool->mutex));
378#endif
379 mem_block_set_len(block, len);
380 mem_block_set_type(block, type);
381 mem_block_set_free(block, MEM_BLOCK_HEADER_SIZE);
382 mem_block_set_start(block, MEM_BLOCK_HEADER_SIZE);
383
384 if (UNIV_UNLIKELY(heap == NULL)) {
385 /* This is the first block of the heap. The field
386 total_size should be initialized here */
387 block->total_size = len;
388 } else {
389 /* Not the first allocation for the heap. This block's
390 total_length field should be set to undefined. */
391 ut_d(block->total_size = ULINT_UNDEFINED);
392 UNIV_MEM_INVALID(&block->total_size,
393 sizeof block->total_size);
394
395 heap->total_size += len;
396 }
397
398 ut_ad((ulint)MEM_BLOCK_HEADER_SIZE < len);
399
400 return(block);
401}
402
403/***************************************************************/
407UNIV_INTERN
408mem_block_t*
409mem_heap_add_block(
410/*===============*/
411 mem_heap_t* heap,
412 ulint n)
413{
414 mem_block_t* block;
415 mem_block_t* new_block;
416 ulint new_size;
417
418 ut_ad(mem_heap_check(heap));
419
420 block = UT_LIST_GET_LAST(heap->base);
421
422 /* We have to allocate a new block. The size is always at least
423 doubled until the standard size is reached. After that the size
424 stays the same, except in cases where the caller needs more space. */
425
426 new_size = 2 * mem_block_get_len(block);
427
428 if (heap->type != MEM_HEAP_DYNAMIC) {
429 /* From the buffer pool we allocate buffer frames */
430 ut_a(n <= MEM_MAX_ALLOC_IN_BUF);
431
432 if (new_size > MEM_MAX_ALLOC_IN_BUF) {
433 new_size = MEM_MAX_ALLOC_IN_BUF;
434 }
435 } else if (new_size > MEM_BLOCK_STANDARD_SIZE) {
436
437 new_size = MEM_BLOCK_STANDARD_SIZE;
438 }
439
440 if (new_size < n) {
441 new_size = n;
442 }
443
444 new_block = mem_heap_create_block(heap, new_size, heap->type,
445 heap->file_name, heap->line);
446 if (new_block == NULL) {
447
448 return(NULL);
449 }
450
451 /* Add the new block as the last block */
452
453 UT_LIST_INSERT_AFTER(list, heap->base, block, new_block);
454
455 return(new_block);
456}
457
458/******************************************************************/
460UNIV_INTERN
461void
462mem_heap_block_free(
463/*================*/
464 mem_heap_t* heap,
465 mem_block_t* block)
466{
467 ulint type;
468 ulint len;
469#ifndef UNIV_HOTBACKUP
470 buf_block_t* buf_block = static_cast<buf_block_t *>(block->buf_block);
471#endif /* !UNIV_HOTBACKUP */
472
473 if (block->magic_n != MEM_BLOCK_MAGIC_N) {
475 }
476
477 UT_LIST_REMOVE(list, heap->base, block);
478
479#ifdef MEM_PERIODIC_CHECK
480 mutex_enter(&(mem_comm_pool->mutex));
481
482 UT_LIST_REMOVE(mem_block_list, mem_block_list, block);
483
484 mutex_exit(&(mem_comm_pool->mutex));
485#endif
486
487 ut_ad(heap->total_size >= block->len);
488 heap->total_size -= block->len;
489
490 type = heap->type;
491 len = block->len;
492 block->magic_n = MEM_FREED_BLOCK_MAGIC_N;
493
494#ifndef UNIV_HOTBACKUP
495#ifdef UNIV_MEM_DEBUG
496 /* In the debug version we set the memory to a random combination
497 of hex 0xDE and 0xAD. */
498
499 mem_erase_buf((byte*)block, len);
500#else /* UNIV_MEM_DEBUG */
501 UNIV_MEM_ASSERT_AND_FREE(block, len);
502#endif /* UNIV_MEM_DEBUG */
503
504 if (type == MEM_HEAP_DYNAMIC || len < UNIV_PAGE_SIZE / 2) {
505
506 ut_ad(!buf_block);
508 } else {
509 ut_ad(type & MEM_HEAP_BUFFER);
510
511 buf_block_free(buf_block);
512 }
513#else /* !UNIV_HOTBACKUP */
514#ifdef UNIV_MEM_DEBUG
515 /* In the debug version we set the memory to a random
516 combination of hex 0xDE and 0xAD. */
517
518 mem_erase_buf((byte*)block, len);
519#else /* UNIV_MEM_DEBUG */
520 UNIV_MEM_ASSERT_AND_FREE(block, len);
521#endif /* UNIV_MEM_DEBUG */
522 ut_free(block);
523#endif /* !UNIV_HOTBACKUP */
524}
525
526#ifndef UNIV_HOTBACKUP
527/******************************************************************/
529UNIV_INTERN
530void
531mem_heap_free_block_free(
532/*=====================*/
533 mem_heap_t* heap)
534{
535 if (UNIV_LIKELY_NULL(heap->free_block)) {
536
537 buf_block_free(static_cast<buf_block_t *>(heap->free_block));
538
539 heap->free_block = NULL;
540 }
541}
542#endif /* !UNIV_HOTBACKUP */
543
544#ifdef MEM_PERIODIC_CHECK
545/******************************************************************/
548UNIV_INTERN
549void
550mem_validate_all_blocks(void)
551/*=========================*/
552{
553 mem_block_t* block;
554
555 mutex_enter(&(mem_comm_pool->mutex));
556
557 block = UT_LIST_GET_FIRST(mem_block_list);
558
559 while (block) {
560 if (block->magic_n != MEM_BLOCK_MAGIC_N) {
562 }
563
564 block = UT_LIST_GET_NEXT(mem_block_list, block);
565 }
566
567 mutex_exit(&(mem_comm_pool->mutex));
568}
569#endif
UNIV_INLINE void buf_block_free(buf_block_t *block)
struct buf_block_struct buf_block_t
Definition buf0types.h:33
UNIV_INTERN void mem_analyze_corruption(void *ptr)
Definition mem0dbg.cc:822
UNIV_INTERN char * mem_heap_strdup(mem_heap_t *heap, const char *str)
Definition mem0mem.cc:104
UNIV_INTERN void * mem_heap_dup(mem_heap_t *heap, const void *data, ulint len)
Definition mem0mem.cc:117
UNIV_INTERN char * mem_heap_strcat(mem_heap_t *heap, const char *s1, const char *s2)
Definition mem0mem.cc:131
UNIV_INLINE void * mem_heap_alloc(mem_heap_t *heap, ulint n)
UNIV_INTERN char * mem_heap_printf(mem_heap_t *heap, const char *format,...) __attribute__((format(printf
UNIV_INTERN void mem_area_free(void *ptr, mem_pool_t *pool)
Definition mem0pool.cc:510
UNIV_INTERN void * mem_area_alloc(ulint *psize, mem_pool_t *pool)
Definition mem0pool.cc:368
mem_pool_t * mem_comm_pool
Definition mem0pool.cc:116
#define ut_error
Definition ut0dbg.h:115
#define ut_ad(EXPR)
Definition ut0dbg.h:127
#define ut_d(EXPR)
Definition ut0dbg.h:129
#define ut_a(EXPR)
Definition ut0dbg.h:105
#define UT_LIST_REMOVE(NAME, BASE, N)
Definition ut0lst.h:177
#define UT_LIST_GET_FIRST(BASE)
Definition ut0lst.h:223
#define UT_LIST_GET_LAST(BASE)
Definition ut0lst.h:234
#define UT_LIST_ADD_LAST(NAME, BASE, N)
Definition ut0lst.h:118
#define UT_LIST_GET_NEXT(NAME, N)
Definition ut0lst.h:200
#define UT_LIST_INIT(BASE)
Definition ut0lst.h:83
#define UT_LIST_INSERT_AFTER(NAME, BASE, NODE1, NODE2)
Definition ut0lst.h:141
#define UT_LIST_BASE_NODE_T(TYPE)
Definition ut0lst.h:54
UNIV_INTERN ulint ut_strlcpy_rev(char *dst, const char *src, ulint size)
Definition ut0mem.cc:473
UNIV_INTERN void ut_free(void *ptr)
Definition ut0mem.cc:294
UNIV_INTERN void * ut_malloc(ulint n)
Definition ut0mem.cc:235