1
/*****************************************************************************
3
Copyright (c) 1994, 2009, Innobase Oy. All Rights Reserved.
5
This program is free software; you can redistribute it and/or modify it under
6
the terms of the GNU General Public License as published by the Free Software
7
Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License along with
14
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15
Place, Suite 330, Boston, MA 02111-1307 USA
17
*****************************************************************************/
19
/********************************************************************//**
23
Created 5/11/1994 Heikki Tuuri
24
*************************************************************************/
32
#ifndef UNIV_HOTBACKUP
33
# include "os0thread.h"
39
/** This struct is placed first in every allocated memory block */
40
typedef struct ut_mem_block_struct ut_mem_block_t;
42
/** The total amount of memory currently allocated from the operating
43
system with os_mem_alloc_large() or malloc(). Does not count malloc()
44
if srv_use_sys_malloc is set. Protected by ut_list_mutex. */
45
UNIV_INTERN ulint ut_total_allocated_memory = 0;
47
/** Mutex protecting ut_total_allocated_memory and ut_mem_block_list */
48
UNIV_INTERN os_fast_mutex_t ut_list_mutex;
50
/** Dynamically allocated memory block */
51
struct ut_mem_block_struct{
52
UT_LIST_NODE_T(ut_mem_block_t) mem_block_list;
53
/*!< mem block list node */
54
ulint size; /*!< size of allocated memory */
55
ulint magic_n;/*!< magic number (UT_MEM_MAGIC_N) */
58
/** The value of ut_mem_block_struct::magic_n. Used in detecting
60
#define UT_MEM_MAGIC_N 1601650166
62
/** List of all memory blocks allocated from the operating system
63
with malloc. Protected by ut_list_mutex. */
64
static UT_LIST_BASE_NODE_T(ut_mem_block_t) ut_mem_block_list;
66
/** Flag: has ut_mem_block_list been initialized? */
67
static ibool ut_mem_block_list_inited = FALSE;
69
/** A dummy pointer for generating a null pointer exception in
71
static ulint* ut_mem_null_ptr = NULL;
73
/**********************************************************************//**
74
Initializes the mem block list at database startup. */
80
ut_a(!ut_mem_block_list_inited);
81
os_fast_mutex_init(&ut_list_mutex);
82
UT_LIST_INIT(ut_mem_block_list);
83
ut_mem_block_list_inited = TRUE;
85
#endif /* !UNIV_HOTBACKUP */
87
/**********************************************************************//**
88
Allocates memory. Sets it also to zero if UNIV_SET_MEM_TO_ZERO is
89
defined and set_to_zero is TRUE.
90
@return own: allocated memory */
95
ulint n, /*!< in: number of bytes to allocate */
96
ibool set_to_zero, /*!< in: TRUE if allocated memory should be
97
set to zero if UNIV_SET_MEM_TO_ZERO is
99
ibool assert_on_error)/*!< in: if TRUE, we crash mysqld if the
100
memory cannot be allocated */
102
#ifndef UNIV_HOTBACKUP
106
if (UNIV_LIKELY(srv_use_sys_malloc)) {
108
ut_a(ret || !assert_on_error);
110
#ifdef UNIV_SET_MEM_TO_ZERO
112
memset(ret, '\0', n);
113
UNIV_MEM_ALLOC(ret, n);
119
ut_ad((sizeof(ut_mem_block_t) % 8) == 0); /* check alignment ok */
120
ut_a(ut_mem_block_list_inited);
124
os_fast_mutex_lock(&ut_list_mutex);
126
ret = malloc(n + sizeof(ut_mem_block_t));
128
if (ret == NULL && retry_count < 60) {
129
if (retry_count == 0) {
130
ut_print_timestamp(stderr);
133
" InnoDB: Error: cannot allocate"
135
"InnoDB: memory with malloc!"
136
" Total allocated memory\n"
137
"InnoDB: by InnoDB %lu bytes."
138
" Operating system errno: %lu\n"
139
"InnoDB: Check if you should"
140
" increase the swap file or\n"
141
"InnoDB: ulimits of your operating system.\n"
142
"InnoDB: On FreeBSD check you"
143
" have compiled the OS with\n"
144
"InnoDB: a big enough maximum process size.\n"
145
"InnoDB: Note that in most 32-bit"
146
" computers the process\n"
147
"InnoDB: memory space is limited"
148
" to 2 GB or 4 GB.\n"
149
"InnoDB: We keep retrying"
150
" the allocation for 60 seconds...\n",
151
(ulong) n, (ulong) ut_total_allocated_memory,
153
(ulong) GetLastError()
160
os_fast_mutex_unlock(&ut_list_mutex);
162
/* Sleep for a second and retry the allocation; maybe this is
163
just a temporary shortage of memory */
165
os_thread_sleep(1000000);
173
/* Flush stderr to make more probable that the error
174
message gets in the error file before we generate a seg
179
os_fast_mutex_unlock(&ut_list_mutex);
181
/* Make an intentional seg fault so that we get a stack
183
/* Intentional segfault on NetWare causes an abend. Avoid this
184
by graceful exit handling in ut_a(). */
185
#if (!defined __NETWARE__)
186
if (assert_on_error) {
187
ut_print_timestamp(stderr);
190
" InnoDB: We now intentionally"
191
" generate a seg fault so that\n"
192
"InnoDB: on Linux we get a stack trace.\n");
194
if (*ut_mem_null_ptr) ut_mem_null_ptr = 0;
204
#ifdef UNIV_SET_MEM_TO_ZERO
205
memset(ret, '\0', n + sizeof(ut_mem_block_t));
209
UNIV_MEM_ALLOC(ret, n + sizeof(ut_mem_block_t));
211
((ut_mem_block_t*)ret)->size = n + sizeof(ut_mem_block_t);
212
((ut_mem_block_t*)ret)->magic_n = UT_MEM_MAGIC_N;
214
ut_total_allocated_memory += n + sizeof(ut_mem_block_t);
216
UT_LIST_ADD_FIRST(mem_block_list, ut_mem_block_list,
217
((ut_mem_block_t*)ret));
218
os_fast_mutex_unlock(&ut_list_mutex);
220
return((void*)((byte*)ret + sizeof(ut_mem_block_t)));
221
#else /* !UNIV_HOTBACKUP */
222
void* ret = malloc(n);
223
ut_a(ret || !assert_on_error);
225
# ifdef UNIV_SET_MEM_TO_ZERO
227
memset(ret, '\0', n);
231
#endif /* !UNIV_HOTBACKUP */
234
/**********************************************************************//**
235
Allocates memory. Sets it also to zero if UNIV_SET_MEM_TO_ZERO is
237
@return own: allocated memory */
242
ulint n) /*!< in: number of bytes to allocate */
244
#ifndef UNIV_HOTBACKUP
245
return(ut_malloc_low(n, TRUE, TRUE));
246
#else /* !UNIV_HOTBACKUP */
248
#endif /* !UNIV_HOTBACKUP */
251
#ifndef UNIV_HOTBACKUP
252
/**********************************************************************//**
253
Tests if malloc of n bytes would succeed. ut_malloc() asserts if memory runs
254
out. It cannot be used if we want to return an error message. Prints to
255
stderr a message if fails.
256
@return TRUE if succeeded */
261
ulint n) /*!< in: try to allocate this many bytes */
268
ut_print_timestamp(stderr);
270
" InnoDB: Error: cannot allocate"
271
" %lu bytes of memory for\n"
272
"InnoDB: a BLOB with malloc! Total allocated memory\n"
273
"InnoDB: by InnoDB %lu bytes."
274
" Operating system errno: %d\n"
275
"InnoDB: Check if you should increase"
276
" the swap file or\n"
277
"InnoDB: ulimits of your operating system.\n"
278
"InnoDB: On FreeBSD check you have"
279
" compiled the OS with\n"
280
"InnoDB: a big enough maximum process size.\n",
282
(ulong) ut_total_allocated_memory,
291
#endif /* !UNIV_HOTBACKUP */
293
/**********************************************************************//**
294
Frees a memory block allocated with ut_malloc. */
299
void* ptr) /*!< in, own: memory block */
301
#ifndef UNIV_HOTBACKUP
302
ut_mem_block_t* block;
304
if (UNIV_LIKELY(srv_use_sys_malloc)) {
309
block = (ut_mem_block_t*)((byte*)ptr - sizeof(ut_mem_block_t));
311
os_fast_mutex_lock(&ut_list_mutex);
313
ut_a(block->magic_n == UT_MEM_MAGIC_N);
314
ut_a(ut_total_allocated_memory >= block->size);
316
ut_total_allocated_memory -= block->size;
318
UT_LIST_REMOVE(mem_block_list, ut_mem_block_list, block);
321
os_fast_mutex_unlock(&ut_list_mutex);
322
#else /* !UNIV_HOTBACKUP */
324
#endif /* !UNIV_HOTBACKUP */
327
#ifndef UNIV_HOTBACKUP
328
/**********************************************************************//**
329
Implements realloc. This is needed by /pars/lexyy.c. Otherwise, you should not
330
use this function because the allocation functions in mem0mem.h are the
331
recommended ones in InnoDB.
333
man realloc in Linux, 2004:
335
realloc() changes the size of the memory block pointed to
336
by ptr to size bytes. The contents will be unchanged to
337
the minimum of the old and new sizes; newly allocated mem-
338
ory will be uninitialized. If ptr is NULL, the call is
339
equivalent to malloc(size); if size is equal to zero, the
340
call is equivalent to free(ptr). Unless ptr is NULL, it
341
must have been returned by an earlier call to malloc(),
342
calloc() or realloc().
345
realloc() returns a pointer to the newly allocated memory,
346
which is suitably aligned for any kind of variable and may
347
be different from ptr, or NULL if the request fails. If
348
size was equal to 0, either NULL or a pointer suitable to
349
be passed to free() is returned. If realloc() fails the
350
original block is left untouched - it is not freed or
352
@return own: pointer to new mem block or NULL */
357
void* ptr, /*!< in: pointer to old block or NULL */
358
ulint size) /*!< in: desired size */
360
ut_mem_block_t* block;
365
if (UNIV_LIKELY(srv_use_sys_malloc)) {
366
return(realloc(ptr, size));
371
return(ut_malloc(size));
380
block = (ut_mem_block_t*)((byte*)ptr - sizeof(ut_mem_block_t));
382
ut_a(block->magic_n == UT_MEM_MAGIC_N);
384
old_size = block->size - sizeof(ut_mem_block_t);
386
if (size < old_size) {
392
new_ptr = ut_malloc(size);
394
if (new_ptr == NULL) {
399
/* Copy the old data from ptr */
400
ut_memcpy(new_ptr, ptr, min_size);
407
/**********************************************************************//**
408
Frees in shutdown all allocated memory not freed yet. */
411
ut_free_all_mem(void)
412
/*=================*/
414
ut_mem_block_t* block;
416
ut_a(ut_mem_block_list_inited);
417
ut_mem_block_list_inited = FALSE;
418
os_fast_mutex_free(&ut_list_mutex);
420
while ((block = UT_LIST_GET_FIRST(ut_mem_block_list))) {
422
ut_a(block->magic_n == UT_MEM_MAGIC_N);
423
ut_a(ut_total_allocated_memory >= block->size);
425
ut_total_allocated_memory -= block->size;
427
UT_LIST_REMOVE(mem_block_list, ut_mem_block_list, block);
431
if (ut_total_allocated_memory != 0) {
433
"InnoDB: Warning: after shutdown"
434
" total allocated memory is %lu\n",
435
(ulong) ut_total_allocated_memory);
438
#endif /* !UNIV_HOTBACKUP */
440
/**********************************************************************//**
441
Copies up to size - 1 characters from the NUL-terminated string src to
442
dst, NUL-terminating the result. Returns strlen(src), so truncation
443
occurred if the return value >= size.
444
@return strlen(src) */
449
char* dst, /*!< in: destination buffer */
450
const char* src, /*!< in: source buffer */
451
ulint size) /*!< in: size of destination buffer */
453
ulint src_size = strlen(src);
456
ulint n = ut_min(src_size, size - 1);
465
/**********************************************************************//**
466
Like ut_strlcpy, but if src doesn't fit in dst completely, copies the last
467
(size - 1) bytes of src, not the first.
468
@return strlen(src) */
473
char* dst, /*!< in: destination buffer */
474
const char* src, /*!< in: source buffer */
475
ulint size) /*!< in: size of destination buffer */
477
ulint src_size = strlen(src);
480
ulint n = ut_min(src_size, size - 1);
482
memcpy(dst, src + src_size - n, n + 1);
488
/**********************************************************************//**
489
Make a quoted copy of a NUL-terminated string. Leading and trailing
490
quotes will not be included; only embedded quotes will be escaped.
491
See also ut_strlenq() and ut_memcpyq().
492
@return pointer to end of dest */
497
char* dest, /*!< in: output buffer */
498
char q, /*!< in: the quote character */
499
const char* src) /*!< in: null-terminated string */
502
if ((*dest++ = *src++) == q) {
510
/**********************************************************************//**
511
Make a quoted copy of a fixed-length string. Leading and trailing
512
quotes will not be included; only embedded quotes will be escaped.
513
See also ut_strlenq() and ut_strcpyq().
514
@return pointer to end of dest */
519
char* dest, /*!< in: output buffer */
520
char q, /*!< in: the quote character */
521
const char* src, /*!< in: string to be quoted */
522
ulint len) /*!< in: length of src */
524
const char* srcend = src + len;
526
while (src < srcend) {
527
if ((*dest++ = *src++) == q) {
535
#ifndef UNIV_HOTBACKUP
536
/**********************************************************************//**
537
Return the number of times s2 occurs in s1. Overlapping instances of s2
538
are only counted once.
539
@return the number of times s2 occurs in s1 */
544
const char* s1, /*!< in: string to search in */
545
const char* s2) /*!< in: string to search for */
548
ulint len = strlen(s2);
570
/**********************************************************************//**
571
Replace every occurrence of s1 in str with s2. Overlapping instances of s1
572
are only replaced once.
573
@return own: modified string, must be freed with mem_free() */
578
const char* str, /*!< in: string to operate on */
579
const char* s1, /*!< in: string to replace */
580
const char* s2) /*!< in: string to replace s1 with */
585
ulint str_len = strlen(str);
586
ulint s1_len = strlen(s1);
587
ulint s2_len = strlen(s2);
589
int len_delta = (int)s2_len - (int)s1_len;
591
str_end = str + str_len;
593
if (len_delta <= 0) {
596
count = ut_strcount(str, s1);
599
new_str = mem_alloc(str_len + count * len_delta + 1);
603
const char* next = strstr(str, s1);
609
memcpy(ptr, str, next - str);
612
if (next == str_end) {
617
memcpy(ptr, s2, s2_len);
628
#ifdef UNIV_COMPILE_TEST_FUNCS
631
test_ut_str_sql_format()
636
#define CALL_AND_TEST(str, str_len, buf, buf_size, ret_expected, buf_expected)\
639
memset(buf, 'x', 10);\
641
fprintf(stderr, "TESTING \"%s\", %lu, %lu\n",\
642
str, (ulint) str_len, (ulint) buf_size);\
643
ret = ut_str_sql_format(str, str_len, buf, buf_size);\
644
if (ret != ret_expected) {\
645
fprintf(stderr, "expected ret %lu, got %lu\n",\
646
(ulint) ret_expected, ret);\
649
if (strcmp((char*) buf, buf_expected) != 0) {\
650
fprintf(stderr, "expected buf \"%s\", got \"%s\"\n",\
655
fprintf(stderr, "OK: %lu, \"%s\"\n\n",\
662
CALL_AND_TEST("abcd", 4, buf, 0, 0, "xxxxxxxxxx");
664
CALL_AND_TEST("abcd", 4, buf, 1, 1, "");
666
CALL_AND_TEST("abcd", 4, buf, 2, 1, "");
668
CALL_AND_TEST("abcd", 0, buf, 3, 3, "''");
669
CALL_AND_TEST("abcd", 1, buf, 3, 1, "");
670
CALL_AND_TEST("abcd", 2, buf, 3, 1, "");
671
CALL_AND_TEST("abcd", 3, buf, 3, 1, "");
672
CALL_AND_TEST("abcd", 4, buf, 3, 1, "");
674
CALL_AND_TEST("abcd", 0, buf, 4, 3, "''");
675
CALL_AND_TEST("abcd", 1, buf, 4, 4, "'a'");
676
CALL_AND_TEST("abcd", 2, buf, 4, 4, "'a'");
677
CALL_AND_TEST("abcd", 3, buf, 4, 4, "'a'");
678
CALL_AND_TEST("abcd", 4, buf, 4, 4, "'a'");
679
CALL_AND_TEST("abcde", 5, buf, 4, 4, "'a'");
680
CALL_AND_TEST("'", 1, buf, 4, 3, "''");
681
CALL_AND_TEST("''", 2, buf, 4, 3, "''");
682
CALL_AND_TEST("a'", 2, buf, 4, 4, "'a'");
683
CALL_AND_TEST("'a", 2, buf, 4, 3, "''");
684
CALL_AND_TEST("ab", 2, buf, 4, 4, "'a'");
686
CALL_AND_TEST("abcdef", 0, buf, 5, 3, "''");
687
CALL_AND_TEST("abcdef", 1, buf, 5, 4, "'a'");
688
CALL_AND_TEST("abcdef", 2, buf, 5, 5, "'ab'");
689
CALL_AND_TEST("abcdef", 3, buf, 5, 5, "'ab'");
690
CALL_AND_TEST("abcdef", 4, buf, 5, 5, "'ab'");
691
CALL_AND_TEST("abcdef", 5, buf, 5, 5, "'ab'");
692
CALL_AND_TEST("abcdef", 6, buf, 5, 5, "'ab'");
693
CALL_AND_TEST("'", 1, buf, 5, 5, "''''");
694
CALL_AND_TEST("''", 2, buf, 5, 5, "''''");
695
CALL_AND_TEST("a'", 2, buf, 5, 4, "'a'");
696
CALL_AND_TEST("'a", 2, buf, 5, 5, "''''");
697
CALL_AND_TEST("ab", 2, buf, 5, 5, "'ab'");
698
CALL_AND_TEST("abc", 3, buf, 5, 5, "'ab'");
700
CALL_AND_TEST("ab", 2, buf, 6, 5, "'ab'");
702
CALL_AND_TEST("a'b'c", 5, buf, 32, 10, "'a''b''c'");
703
CALL_AND_TEST("a'b'c'", 6, buf, 32, 12, "'a''b''c'''");
706
#endif /* UNIV_COMPILE_TEST_FUNCS */
707
#endif /* !UNIV_HOTBACKUP */