mirror of
https://github.com/MariaDB/server.git
synced 2025-01-18 04:53:01 +01:00
e2513bf07f
Fixes the following bugs: Bug #30706: SQL thread on slave is allowed to block client queries when slave load is high Add (innodb|innobase|srv)_replication_delay MySQL config parameter. Bug #30888: Innodb table + stored procedure + row deletion = server crash While adding code for the low level read of the AUTOINC value from the index, the case for MEDIUM ints which are 3 bytes was missed triggering an assertion. Bug #30907: Regression: "--innodb_autoinc_lock_mode=0" (off) not same as older releases We don't rely on *first_value to be 0 when checking whether get_auto_increment() has been invoked for the first time in a multi-row INSERT. We instead use trx_t::n_autoinc_rows. Initialize trx::n_autoinc_rows inside ha_innobase::start_stmt() too. Bug #31444: "InnoDB: Error: MySQL is freeing a thd" in innodb_mysql.test ha_innobase::external_lock(): Update prebuilt->mysql_has_locked and trx->n_mysql_tables_in_use only after row_lock_table_for_mysql() returns DB_SUCCESS. A timeout on LOCK TABLES would lead to an inconsistent state, which would cause trx_free() to print a warning. Bug #31494: innodb + 5.1 + read committed crash, assertion Set an error code when a deadlock occurs in semi-consistent read. mysql-test/r/innodb.result: Apply snapshot innodb-5.1-ss1989 Also, a test is moved into the new innodb_autoinc_lock_mode_zero test, because it depends on a non-default setting for a read-only variable. Revision r1821: Merge a change from MySQL AB: ChangeSet@1.2536.50.1 2007-08-02 12:45:56-07:00 igor@mysql.com Fixed bug#28404. This patch adds cost estimation for the queries with ORDER BY / GROUP BY and LIMIT. If there was a ref/range access to the table whose rows were required to be ordered in the result set the optimizer always employed this access though a scan by a different index that was compatible with the required order could be cheaper to produce the first L rows of the result set. Now for such queries the optimizer makes a choice between the cheapest ref/range accesses not compatible with the given order and index scans compatible with it. innodb.result: Adjusted results for test cases affected fy the fix for bug #28404. Revision r1781: Fix a test case that was broken after Bug#16979 fix. See r1645 and r1735. The variable used in the tests below was introduced in r1735. Revision r1792: innodb.result: Revert r1655, which should have been reverted as part of r1781. Revision r1843: Add test for Bug# 21409, the actual bug was fixed in r1334. mysql-test/t/innodb.test: Apply snapshot innodb-5.1-ss1989 Also, a test is moved into the new innodb_autoinc_lock_mode_zero test, because it depends on a non-default setting for a read-only variable. Revision r1781: Fix a test case that was broken after Bug#16979 fix. See r1645 and r1735. The variable used in the tests below was introduced in r1735. Revision r1843: Add test for Bug# 21409, the actual bug was fixed in r1334. storage/innobase/buf/buf0lru.c: Apply snapshot innodb-5.1-ss1989 Revision r1819: Merge r1815:1817 from branches/zip: Improve Valgrind instrumentation. UNIV_MEM_ASSERT_RW(): New macro, to check that the contents of a memory area is defined. UNIV_MEM_ASSERT_W(): New macro, to check that a memory area is writable. UNIV_MEM_ASSERT_AND_FREE(): New macro, to check that the memory is writable before declaring it free (unwritable). This replaces UNIV_MEM_FREE() in many places. mem_init_buf(): Check that the memory is writable, and declare it undefined. mem_erase_buf(): Check that the memory is writable, and declare it freed. storage/innobase/dict/dict0dict.c: Apply snapshot innodb-5.1-ss1989 Revision r1894: Add debug lock checks to autoinc functions. Add lock guards around an invocation of dict_table_autoinc_initialize(). storage/innobase/dict/dict0load.c: Apply snapshot innodb-5.1-ss1989 Revision r1974: Prevent loading of tables that have unsupported features most notably FTS indexes. storage/innobase/handler/ha_innodb.cc: Apply snapshot innodb-5.1-ss1989 Revision r1850: Implement this feature request: http://bugs.mysql.com/30706 * Add a function that returns the number of microseconds since epoch - ut_time_us(). * Add (innodb|innobase|srv)_replication_delay MySQL config parameter. * Add UT_WAIT_FOR() macro that waits for a specified condition to occur until a timeout elapses. * Using all of the above, handle the replication thread specially in srv_conc_enter_innodb(). Approved by: Heikki Revision r1887: Merge changes from MySQL AB: ChangeSet@1.2528.115.25 2007-08-27 18:18:14-06:00 tsmith@hindu.god Fix some Windows compiler warnings. dict0mem.c: Fix compiler warning with a cast. ha_innodb.cc: Change type to fix a compiler warning. Revision r1809: ha_innobase::external_lock(): Update prebuilt->mysql_has_locked and trx->n_mysql_tables_in_use only after row_lock_table_for_mysql() returns DB_SUCCESS. A timeout on LOCK TABLES would lead to an inconsistent state, which would cause trx_free() to print a warning. This was later reported as Bug #31444. Revision r1833: Add /*== ... === */ decoration that was missing around some auto-inc functions. Add a missing comment, fix the length of a decoration. Initialize the *value out parameter in ha_innobase::innobase_get_auto_increment(). Revision r1866: Revert r1850 as MySQL did not approve the addition. log for r1850: Implement this feature request: http://bugs.mysql.com/30706 * Add a function that returns the number of microseconds since epoch - ut_time_us(). * Add (innodb|innobase|srv)_replication_delay MySQL config parameter. * Add UT_WAIT_FOR() macro that waits for a specified condition to occur until a timeout elapses. * Using all of the above, handle the replication thread specially in srv_conc_enter_innodb(). Revision r1846: Add config option innodb_use_adaptive_hash_indexes to enable/disable adaptive hash indexes. It is enabled by default (no change in default behavior). Approved by: Marko Revision r1974: Prevent loading of tables that have unsupported features most notably FTS indexes. Revision r1829: Add assertion to enforce check of an implicit invariant and add comment about retry of autoinc read semantics. We always reread the table's autoinc counter after attempting to initialize it i.e., we want to guarantee that a read of autoinc valus that is returned to the caller is always covered by the AUTOINC locking mechanism. Revision r1787: Move the prototype of innobase_print_identifier() from ut0ut.c to ha_prototypes.h. Enclose the definitions in ha_prototypes.h in #ifndef UNIV_HOTBACKUP. Revision r1888: Merge a change from MySQL AB: ChangeSet@1.2528.115.30 2007-08-28 10:17:15-06:00 tsmith@hindu.god Fix another compiler warning on Windows in InnoDB. ha_innodb.cc: Fix compiler warning: ::get_auto_increment takes a ulonglong for nb_desired_values, but InnoDB's trx struct stores it as a ulint (unsigned long). Probably harmless, as a single statement won't be asking for more than 2^32 rows. Revision r1987: Bug fix: The problem was that when write_row() attempted to update the max autoinc value, and if it was rolled back because of a deadlock, the deadlock error (transaction rollback) was not being propagated back to MySQL. Revision r1889: Merge a change from MySQL AB: ChangeSet@1.2560 2007-09-21 10:15:16+02:00 gkodinov@local ha_innodb.cc: fixed type conversion warnings revealed by bug 30639 Revision r1989: Suppress printing of deadlock errors while reading the autoinc value. DB_DEADLOCK errors are part of normal processing and excessive printing of these error messages could be disconcerting for users. Revision r1828: Fix two bugs: Bug# 30907: We don't rely on *first_value to be 0 when checking whether get_auto_increment() has been invoked for the first time in a multi-row INSERT. We instead use trx_t::n_autoinc_rows. Initialize trx::n_autoinc_rows inside ha_innobase::start_stmt() too. Bug# 30888: While adding code for the low level read of the AUTOINC value from the index, the case for MEDIUM ints which are 3 bytes was missed triggering an assertion. storage/innobase/handler/ha_innodb.h: Apply snapshot innodb-5.1-ss1989 Revision r1844: Remove the prototypes of some functions inside #if 0. The function definitions were removed in r1746. storage/innobase/ibuf/ibuf0ibuf.c: Apply snapshot innodb-5.1-ss1989 Revision r1965: ibuf_insert_to_index_page(): Fix typos in diagnostic output. storage/innobase/include/db0err.h: Apply snapshot innodb-5.1-ss1989 Revision r1974: Prevent loading of tables that have unsupported features most notably FTS indexes. storage/innobase/include/ha_prototypes.h: Apply snapshot innodb-5.1-ss1989 Revision r1787: Move the prototype of innobase_print_identifier() from ut0ut.c to ha_prototypes.h. Enclose the definitions in ha_prototypes.h in #ifndef UNIV_HOTBACKUP. storage/innobase/include/mach0data.h: Apply snapshot innodb-5.1-ss1989 Revision r1779: Fix a bug that handles the case where the host specific byte order matches the InnoDB storage byte order, which is big-endian. storage/innobase/include/mach0data.ic: Apply snapshot innodb-5.1-ss1989 Revision r1779: Fix a bug that handles the case where the host specific byte order matches the InnoDB storage byte order, which is big-endian. storage/innobase/include/mem0dbg.h: Apply snapshot innodb-5.1-ss1989 Revision r1830: Improve memory debugging. This is follow-up to r1819. mem_heap_validate(): Compile this function also if UNIV_MEM_DEBUG is defined. Previously, this function was only compiled with UNIV_DEBUG. mem_heap_free_heap_top(): Flag the memory allocated, not freed, for Valgrind. Otherwise, Valgrind would complain on the second call of mem_heap_empty(). UNIV_MEM_ASSERT_RW(), UNIV_MEM_ASSERT_W(): Display additional diagnostics for failed Valgrind checks. storage/innobase/include/mem0mem.ic: Apply snapshot innodb-5.1-ss1989 Revision r1830: Improve memory debugging. This is follow-up to r1819. mem_heap_validate(): Compile this function also if UNIV_MEM_DEBUG is defined. Previously, this function was only compiled with UNIV_DEBUG. mem_heap_free_heap_top(): Flag the memory allocated, not freed, for Valgrind. Otherwise, Valgrind would complain on the second call of mem_heap_empty(). UNIV_MEM_ASSERT_RW(), UNIV_MEM_ASSERT_W(): Display additional diagnostics for failed Valgrind checks. Revision r1937: mem_heap_free_top(): Remove a bogus Valgrind warning. Revision r1819: Merge r1815:1817 from branches/zip: Improve Valgrind instrumentation. UNIV_MEM_ASSERT_RW(): New macro, to check that the contents of a memory area is defined. UNIV_MEM_ASSERT_W(): New macro, to check that a memory area is writable. UNIV_MEM_ASSERT_AND_FREE(): New macro, to check that the memory is writable before declaring it free (unwritable). This replaces UNIV_MEM_FREE() in many places. mem_init_buf(): Check that the memory is writable, and declare it undefined. mem_erase_buf(): Check that the memory is writable, and declare it freed. storage/innobase/include/rem0rec.ic: Apply snapshot innodb-5.1-ss1989 Revision r1918: Improve Valgrind instrumentation. rec_offs_set_n_alloc(): Use UNIV_MEM_ASSERT_AND_ALLOC(). UNIV_MEM_ASSERT_AND_ALLOC(): New directive, similar to UNIV_MEM_ASSERT_AND_FREE(). storage/innobase/include/row0mysql.h: Apply snapshot innodb-5.1-ss1989 Revision r1783: Correct the function comments of row_create_table_for_mysql() and row_drop_table_for_mysql(). storage/innobase/include/sync0rw.h: Apply snapshot innodb-5.1-ss1989 Revision r1757: Enclose rw_lock_validate() in #ifdef UNIV_DEBUG. It is only called by debug assertions. storage/innobase/include/univ.i: Apply snapshot innodb-5.1-ss1989 Revision r1827: Merge r1826 from branches/zip: UNIV_MEM_ASSERT_AND_FREE(): Use UNIV_MEM_ASSERT_W() instead of UNIV_MEM_ASSERT_RW(). The memory area need not be initialized. This mistake was made in r1815. Revision r1918: Improve Valgrind instrumentation. rec_offs_set_n_alloc(): Use UNIV_MEM_ASSERT_AND_ALLOC(). UNIV_MEM_ASSERT_AND_ALLOC(): New directive, similar to UNIV_MEM_ASSERT_AND_FREE(). Revision r1830: Improve memory debugging. This is follow-up to r1819. mem_heap_validate(): Compile this function also if UNIV_MEM_DEBUG is defined. Previously, this function was only compiled with UNIV_DEBUG. mem_heap_free_heap_top(): Flag the memory allocated, not freed, for Valgrind. Otherwise, Valgrind would complain on the second call of mem_heap_empty(). UNIV_MEM_ASSERT_RW(), UNIV_MEM_ASSERT_W(): Display additional diagnostics for failed Valgrind checks. Revision r1819: Merge r1815:1817 from branches/zip: Improve Valgrind instrumentation. UNIV_MEM_ASSERT_RW(): New macro, to check that the contents of a memory area is defined. UNIV_MEM_ASSERT_W(): New macro, to check that a memory area is writable. UNIV_MEM_ASSERT_AND_FREE(): New macro, to check that the memory is writable before declaring it free (unwritable). This replaces UNIV_MEM_FREE() in many places. mem_init_buf(): Check that the memory is writable, and declare it undefined. mem_erase_buf(): Check that the memory is writable, and declare it freed. Revision r1948: UNIV_MEM_ASSERT_RW(), UNIV_MEM_ASSERT_W(): Display also __FILE__ and __LINE__ when these Valgrind checks fail. storage/innobase/include/ut0ut.h: Apply snapshot innodb-5.1-ss1989 Revision r1850: Implement this feature request: http://bugs.mysql.com/30706 * Add a function that returns the number of microseconds since epoch - ut_time_us(). * Add (innodb|innobase|srv)_replication_delay MySQL config parameter. * Add UT_WAIT_FOR() macro that waits for a specified condition to occur until a timeout elapses. * Using all of the above, handle the replication thread specially in srv_conc_enter_innodb(). Approved by: Heikki Revision r1862: Add ut_snprintf() function. On Windows this needs to be implemented using auxiliary functions because there is no snprintf-variant on Windows that behaves exactly as specified in the standard: * Always return the number of characters that would have been printed if the size were unlimited (not including the final `\0'). * Always '\0'-terminate the result * Do not touch the buffer if size=0, only return the number of characters that would have been printed. Can be used to estimate the size needed and to allocate it dynamically. See http://www.freebsd.org/cgi/query-pr.cgi?pr=87260 for the reason why 2 ap variables are used. Approved by: Heikki Revision r1866: Revert r1850 as MySQL did not approve the addition. log for r1850: Implement this feature request: http://bugs.mysql.com/30706 * Add a function that returns the number of microseconds since epoch - ut_time_us(). * Add (innodb|innobase|srv)_replication_delay MySQL config parameter. * Add UT_WAIT_FOR() macro that waits for a specified condition to occur until a timeout elapses. * Using all of the above, handle the replication thread specially in srv_conc_enter_innodb(). storage/innobase/mem/mem0dbg.c: Apply snapshot innodb-5.1-ss1989 Revision r1830: Improve memory debugging. This is follow-up to r1819. mem_heap_validate(): Compile this function also if UNIV_MEM_DEBUG is defined. Previously, this function was only compiled with UNIV_DEBUG. mem_heap_free_heap_top(): Flag the memory allocated, not freed, for Valgrind. Otherwise, Valgrind would complain on the second call of mem_heap_empty(). UNIV_MEM_ASSERT_RW(), UNIV_MEM_ASSERT_W(): Display additional diagnostics for failed Valgrind checks. Revision r1819: Merge r1815:1817 from branches/zip: Improve Valgrind instrumentation. UNIV_MEM_ASSERT_RW(): New macro, to check that the contents of a memory area is defined. UNIV_MEM_ASSERT_W(): New macro, to check that a memory area is writable. UNIV_MEM_ASSERT_AND_FREE(): New macro, to check that the memory is writable before declaring it free (unwritable). This replaces UNIV_MEM_FREE() in many places. mem_init_buf(): Check that the memory is writable, and declare it undefined. mem_erase_buf(): Check that the memory is writable, and declare it freed. storage/innobase/mem/mem0mem.c: Apply snapshot innodb-5.1-ss1989 Revision r1819: Merge r1815:1817 from branches/zip: Improve Valgrind instrumentation. UNIV_MEM_ASSERT_RW(): New macro, to check that the contents of a memory area is defined. UNIV_MEM_ASSERT_W(): New macro, to check that a memory area is writable. UNIV_MEM_ASSERT_AND_FREE(): New macro, to check that the memory is writable before declaring it free (unwritable). This replaces UNIV_MEM_FREE() in many places. mem_init_buf(): Check that the memory is writable, and declare it undefined. mem_erase_buf(): Check that the memory is writable, and declare it freed. storage/innobase/row/row0mysql.c: Apply snapshot innodb-5.1-ss1989 Revision r1786: row_create_table_for_mysql(), row_truncate_table_for_mysql(), row_drop_table_for_mysql(): Do not mention innodb_force_recovery when newraw is set. Revision r1790: row_drop_table_for_mysql(): Before calling dict_table_remove_from_cache(table) and thus freeing the memory allocated for the table, copy the table name. This avoids reading freed memory when name == table->name. Approved by Sunny. Revision r1783: Correct the function comments of row_create_table_for_mysql() and row_drop_table_for_mysql(). Revision r1894: Add debug lock checks to autoinc functions. Add lock guards around an invocation of dict_table_autoinc_initialize(). storage/innobase/row/row0sel.c: Apply snapshot innodb-5.1-ss1989 Revision r1782: Add comment that the variable dest should be word aligned. After discussion on IM with Heikki. Revision r1988: Set an error code when a deadlock occurs in semi-consistent read. (Bug #31494) innodb-semi-consistent: New tests for InnoDB semi-consistent reads. Unfortunately, these will not trigger Bug #31494, because there merely occur lock wait timeouts, not deadlocks. Revision r1820: Use the clustered index and not the one selected by the optimizer in the plan, when building a previous version of the row. This bug is triggered when running queries via InnoDB's internal SQL parser; when InnoDB's optimizer selects a secondary index for the plan. Revision r1828: Fix two bugs: Bug# 30907: We don't rely on *first_value to be 0 when checking whether get_auto_increment() has been invoked for the first time in a multi-row INSERT. We instead use trx_t::n_autoinc_rows. Initialize trx::n_autoinc_rows inside ha_innobase::start_stmt() too. Bug# 30888: While adding code for the low level read of the AUTOINC value from the index, the case for MEDIUM ints which are 3 bytes was missed triggering an assertion. Revision r1779: Fix a bug that handles the case where the host specific byte order matches the InnoDB storage byte order, which is big-endian. storage/innobase/sync/sync0rw.c: Apply snapshot innodb-5.1-ss1989 Revision r1757: Enclose rw_lock_validate() in #ifdef UNIV_DEBUG. It is only called by debug assertions. storage/innobase/ut/ut0ut.c: Apply snapshot innodb-5.1-ss1989 Revision r1850: Implement this feature request: http://bugs.mysql.com/30706 * Add a function that returns the number of microseconds since epoch - ut_time_us(). * Add (innodb|innobase|srv)_replication_delay MySQL config parameter. * Add UT_WAIT_FOR() macro that waits for a specified condition to occur until a timeout elapses. * Using all of the above, handle the replication thread specially in srv_conc_enter_innodb(). Approved by: Heikki Revision r1873: snprintf() should always return non-negative result. According to Microsoft documentation about _vscprintf(): If format is a null pointer, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, the functions return -1 and set errno to EINVAL. The UNIX variant of snprintf() segfaults if format is a NULL pointer (similar to strlen(NULL) for example), so it is better to conform to this behavior and crash our custom Windows version instead of returning -1. Noone would expect -1 to be returned from snprintf(). Cosmetic: Add a space after typecast. Approved by: Marko Revision r1862: Add ut_snprintf() function. On Windows this needs to be implemented using auxiliary functions because there is no snprintf-variant on Windows that behaves exactly as specified in the standard: * Always return the number of characters that would have been printed if the size were unlimited (not including the final `\0'). * Always '\0'-terminate the result * Do not touch the buffer if size=0, only return the number of characters that would have been printed. Can be used to estimate the size needed and to allocate it dynamically. See http://www.freebsd.org/cgi/query-pr.cgi?pr=87260 for the reason why 2 ap variables are used. Approved by: Heikki Revision r1866: Revert r1850 as MySQL did not approve the addition. log for r1850: Implement this feature request: http://bugs.mysql.com/30706 * Add a function that returns the number of microseconds since epoch - ut_time_us(). * Add (innodb|innobase|srv)_replication_delay MySQL config parameter. * Add UT_WAIT_FOR() macro that waits for a specified condition to occur until a timeout elapses. * Using all of the above, handle the replication thread specially in srv_conc_enter_innodb(). Revision r1787: Move the prototype of innobase_print_identifier() from ut0ut.c to ha_prototypes.h. Enclose the definitions in ha_prototypes.h in #ifndef UNIV_HOTBACKUP. Revision r1789: ut_print_namel(): Do not assume that all '/' are separators between database and table names. Approved by Heikki. Revision r1936: ut_print_buf(): Add a Valgrind check that the buffer is wholly defined. mysql-test/r/innodb-semi-consistent.result: Apply snapshot innodb-5.1-ss1989 Revision r1988: Set an error code when a deadlock occurs in semi-consistent read. (Bug #31494) innodb-semi-consistent: New tests for InnoDB semi-consistent reads. Unfortunately, these will not trigger Bug #31494, because there merely occur lock wait timeouts, not deadlocks. mysql-test/r/innodb_autoinc_lock_mode_zero.result: New test, using read-only setting --innodb-autoinc-lock-mode=0 mysql-test/t/innodb-semi-consistent-master.opt: Apply snapshot innodb-5.1-ss1989 Revision r1988: Set an error code when a deadlock occurs in semi-consistent read. (Bug #31494) innodb-semi-consistent: New tests for InnoDB semi-consistent reads. Unfortunately, these will not trigger Bug #31494, because there merely occur lock wait timeouts, not deadlocks. mysql-test/t/innodb-semi-consistent.test: Apply snapshot innodb-5.1-ss1989 Revision r1988: Set an error code when a deadlock occurs in semi-consistent read. (Bug #31494) innodb-semi-consistent: New tests for InnoDB semi-consistent reads. Unfortunately, these will not trigger Bug #31494, because there merely occur lock wait timeouts, not deadlocks. mysql-test/t/innodb_autoinc_lock_mode_zero-master.opt: New test, using read-only setting --innodb-autoinc-lock-mode=0 mysql-test/t/innodb_autoinc_lock_mode_zero.test: New test, using read-only setting --innodb-autoinc-lock-mode=0
577 lines
14 KiB
C
577 lines
14 KiB
C
/************************************************************************
|
|
The memory management
|
|
|
|
(c) 1994, 1995 Innobase Oy
|
|
|
|
Created 6/9/1994 Heikki Tuuri
|
|
*************************************************************************/
|
|
|
|
|
|
#include "mem0mem.h"
|
|
#ifdef UNIV_NONINL
|
|
#include "mem0mem.ic"
|
|
#endif
|
|
|
|
#include "mach0data.h"
|
|
#include "buf0buf.h"
|
|
#include "btr0sea.h"
|
|
#include "srv0srv.h"
|
|
#include "mem0dbg.c"
|
|
#include <stdarg.h>
|
|
|
|
/*
|
|
THE MEMORY MANAGEMENT
|
|
=====================
|
|
|
|
The basic element of the memory management is called a memory
|
|
heap. A memory heap is conceptually a
|
|
stack from which memory can be allocated. The stack may grow infinitely.
|
|
The top element of the stack may be freed, or
|
|
the whole stack can be freed at one time. The advantage of the
|
|
memory heap concept is that we can avoid using the malloc and free
|
|
functions of C which are quite expensive, for example, on the Solaris + GCC
|
|
system (50 MHz Sparc, 1993) the pair takes 3 microseconds,
|
|
on Win NT + 100MHz Pentium, 2.5 microseconds.
|
|
When we use a memory heap,
|
|
we can allocate larger blocks of memory at a time and thus
|
|
reduce overhead. Slightly more efficient the method is when we
|
|
allocate the memory from the index page buffer pool, as we can
|
|
claim a new page fast. This is called buffer allocation.
|
|
When we allocate the memory from the dynamic memory of the
|
|
C environment, that is called dynamic allocation.
|
|
|
|
The default way of operation of the memory heap is the following.
|
|
First, when the heap is created, an initial block of memory is
|
|
allocated. In dynamic allocation this may be about 50 bytes.
|
|
If more space is needed, additional blocks are allocated
|
|
and they are put into a linked list.
|
|
After the initial block, each allocated block is twice the size of the
|
|
previous, until a threshold is attained, after which the sizes
|
|
of the blocks stay the same. An exception is, of course, the case
|
|
where the caller requests a memory buffer whose size is
|
|
bigger than the threshold. In that case a block big enough must
|
|
be allocated.
|
|
|
|
The heap is physically arranged so that if the current block
|
|
becomes full, a new block is allocated and always inserted in the
|
|
chain of blocks as the last block.
|
|
|
|
In the debug version of the memory management, all the allocated
|
|
heaps are kept in a list (which is implemented as a hash table).
|
|
Thus we can notice if the caller tries to free an already freed
|
|
heap. In addition, each buffer given to the caller contains
|
|
start field at the start and a trailer field at the end of the buffer.
|
|
|
|
The start field has the following content:
|
|
A. sizeof(ulint) bytes of field length (in the standard byte order)
|
|
B. sizeof(ulint) bytes of check field (a random number)
|
|
|
|
The trailer field contains:
|
|
A. sizeof(ulint) bytes of check field (the same random number as at the start)
|
|
|
|
Thus we can notice if something has been copied over the
|
|
borders of the buffer, which is illegal.
|
|
The memory in the buffers is initialized to a random byte sequence.
|
|
After freeing, all the blocks in the heap are set to random bytes
|
|
to help us discover errors which result from the use of
|
|
buffers in an already freed heap. */
|
|
|
|
#ifdef MEM_PERIODIC_CHECK
|
|
|
|
ibool mem_block_list_inited;
|
|
/* List of all mem blocks allocated; protected by the mem_comm_pool mutex */
|
|
UT_LIST_BASE_NODE_T(mem_block_t) mem_block_list;
|
|
|
|
#endif
|
|
|
|
/*******************************************************************
|
|
NOTE: Use the corresponding macro instead of this function.
|
|
Allocates a single buffer of memory from the dynamic memory of
|
|
the C compiler. Is like malloc of C. The buffer must be freed
|
|
with mem_free. */
|
|
|
|
void*
|
|
mem_alloc_func_noninline(
|
|
/*=====================*/
|
|
/* out, own: free storage */
|
|
ulint n, /* in: desired number of bytes */
|
|
const char* file_name, /* in: file name where created */
|
|
ulint line) /* in: line where created */
|
|
{
|
|
return(mem_alloc_func(n, file_name, line));
|
|
}
|
|
|
|
/**************************************************************************
|
|
Duplicates a NUL-terminated string, allocated from a memory heap. */
|
|
|
|
char*
|
|
mem_heap_strdup(
|
|
/*============*/
|
|
/* out, own: a copy of the string */
|
|
mem_heap_t* heap, /* in: memory heap where string is allocated */
|
|
const char* str) /* in: string to be copied */
|
|
{
|
|
return(mem_heap_dup(heap, str, strlen(str) + 1));
|
|
}
|
|
|
|
/**************************************************************************
|
|
Duplicate a block of data, allocated from a memory heap. */
|
|
|
|
void*
|
|
mem_heap_dup(
|
|
/*=========*/
|
|
/* out, own: a copy of the data */
|
|
mem_heap_t* heap, /* in: memory heap where copy is allocated */
|
|
const void* data, /* in: data to be copied */
|
|
ulint len) /* in: length of data, in bytes */
|
|
{
|
|
return(memcpy(mem_heap_alloc(heap, len), data, len));
|
|
}
|
|
|
|
/**************************************************************************
|
|
Concatenate two memory blocks and return the result, using a memory heap. */
|
|
|
|
void*
|
|
mem_heap_cat(
|
|
/*=========*/
|
|
/* out, own: the result */
|
|
mem_heap_t* heap, /* in: memory heap where result is allocated */
|
|
const void* b1, /* in: block 1 */
|
|
ulint len1, /* in: length of b1, in bytes */
|
|
const void* b2, /* in: block 2 */
|
|
ulint len2) /* in: length of b2, in bytes */
|
|
{
|
|
void* res = mem_heap_alloc(heap, len1 + len2);
|
|
|
|
memcpy(res, b1, len1);
|
|
memcpy((char*)res + len1, b2, len2);
|
|
|
|
return(res);
|
|
}
|
|
|
|
/**************************************************************************
|
|
Concatenate two strings and return the result, using a memory heap. */
|
|
|
|
char*
|
|
mem_heap_strcat(
|
|
/*============*/
|
|
/* out, own: the result */
|
|
mem_heap_t* heap, /* in: memory heap where string is allocated */
|
|
const char* s1, /* in: string 1 */
|
|
const char* s2) /* in: string 2 */
|
|
{
|
|
char* s;
|
|
ulint s1_len = strlen(s1);
|
|
ulint s2_len = strlen(s2);
|
|
|
|
s = mem_heap_alloc(heap, s1_len + s2_len + 1);
|
|
|
|
memcpy(s, s1, s1_len);
|
|
memcpy(s + s1_len, s2, s2_len);
|
|
|
|
s[s1_len + s2_len] = '\0';
|
|
|
|
return(s);
|
|
}
|
|
|
|
|
|
/********************************************************************
|
|
Helper function for mem_heap_printf. */
|
|
static
|
|
ulint
|
|
mem_heap_printf_low(
|
|
/*================*/
|
|
/* out: length of formatted string,
|
|
including terminating NUL */
|
|
char* buf, /* in/out: buffer to store formatted string
|
|
in, or NULL to just calculate length */
|
|
const char* format, /* in: format string */
|
|
va_list ap) /* in: arguments */
|
|
{
|
|
ulint len = 0;
|
|
|
|
while (*format) {
|
|
|
|
/* Does this format specifier have the 'l' length modifier. */
|
|
ibool is_long = FALSE;
|
|
|
|
/* Length of one parameter. */
|
|
size_t plen;
|
|
|
|
if (*format++ != '%') {
|
|
/* Non-format character. */
|
|
|
|
len++;
|
|
|
|
if (buf) {
|
|
*buf++ = *(format - 1);
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
if (*format == 'l') {
|
|
is_long = TRUE;
|
|
format++;
|
|
}
|
|
|
|
switch (*format++) {
|
|
case 's':
|
|
/* string */
|
|
{
|
|
char* s = va_arg(ap, char*);
|
|
|
|
/* "%ls" is a non-sensical format specifier. */
|
|
ut_a(!is_long);
|
|
|
|
plen = strlen(s);
|
|
len += plen;
|
|
|
|
if (buf) {
|
|
memcpy(buf, s, plen);
|
|
buf += plen;
|
|
}
|
|
}
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
/* unsigned int */
|
|
{
|
|
char tmp[32];
|
|
unsigned long val;
|
|
|
|
/* We only support 'long' values for now. */
|
|
ut_a(is_long);
|
|
|
|
val = va_arg(ap, unsigned long);
|
|
|
|
plen = sprintf(tmp, "%lu", val);
|
|
len += plen;
|
|
|
|
if (buf) {
|
|
memcpy(buf, tmp, plen);
|
|
buf += plen;
|
|
}
|
|
}
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
/* "%l%" is a non-sensical format specifier. */
|
|
ut_a(!is_long);
|
|
|
|
len++;
|
|
|
|
if (buf) {
|
|
*buf++ = '%';
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
ut_error;
|
|
}
|
|
}
|
|
|
|
/* For the NUL character. */
|
|
len++;
|
|
|
|
if (buf) {
|
|
*buf = '\0';
|
|
}
|
|
|
|
return(len);
|
|
}
|
|
|
|
/********************************************************************
|
|
A simple (s)printf replacement that dynamically allocates the space for the
|
|
formatted string from the given heap. This supports a very limited set of
|
|
the printf syntax: types 's' and 'u' and length modifier 'l' (which is
|
|
required for the 'u' type). */
|
|
|
|
char*
|
|
mem_heap_printf(
|
|
/*============*/
|
|
/* out: heap-allocated formatted string */
|
|
mem_heap_t* heap, /* in: memory heap */
|
|
const char* format, /* in: format string */
|
|
...)
|
|
{
|
|
va_list ap;
|
|
char* str;
|
|
ulint len;
|
|
|
|
/* Calculate length of string */
|
|
len = 0;
|
|
va_start(ap, format);
|
|
len = mem_heap_printf_low(NULL, format, ap);
|
|
va_end(ap);
|
|
|
|
/* Now create it for real. */
|
|
str = mem_heap_alloc(heap, len);
|
|
va_start(ap, format);
|
|
mem_heap_printf_low(str, format, ap);
|
|
va_end(ap);
|
|
|
|
return(str);
|
|
}
|
|
|
|
/*******************************************************************
|
|
Creates a memory heap block where data can be allocated. */
|
|
|
|
mem_block_t*
|
|
mem_heap_create_block(
|
|
/*==================*/
|
|
/* out, own: memory heap block, NULL if
|
|
did not succeed (only possible for
|
|
MEM_HEAP_BTR_SEARCH type heaps) */
|
|
mem_heap_t* heap, /* in: memory heap or NULL if first block
|
|
should be created */
|
|
ulint n, /* in: number of bytes needed for user data, or
|
|
if init_block is not NULL, its size in bytes */
|
|
void* init_block, /* in: init block in fast create,
|
|
type must be MEM_HEAP_DYNAMIC */
|
|
ulint type, /* in: type of heap: MEM_HEAP_DYNAMIC or
|
|
MEM_HEAP_BUFFER */
|
|
const char* file_name,/* in: file name where created */
|
|
ulint line) /* in: line where created */
|
|
{
|
|
mem_block_t* block;
|
|
ulint len;
|
|
|
|
ut_ad((type == MEM_HEAP_DYNAMIC) || (type == MEM_HEAP_BUFFER)
|
|
|| (type == MEM_HEAP_BUFFER + MEM_HEAP_BTR_SEARCH));
|
|
|
|
if (heap && heap->magic_n != MEM_BLOCK_MAGIC_N) {
|
|
mem_analyze_corruption(heap);
|
|
}
|
|
|
|
/* In dynamic allocation, calculate the size: block header + data. */
|
|
|
|
if (init_block != NULL) {
|
|
ut_ad(type == MEM_HEAP_DYNAMIC);
|
|
ut_ad(n > MEM_BLOCK_START_SIZE + MEM_BLOCK_HEADER_SIZE);
|
|
len = n;
|
|
block = init_block;
|
|
|
|
} else if (type == MEM_HEAP_DYNAMIC) {
|
|
|
|
len = MEM_BLOCK_HEADER_SIZE + MEM_SPACE_NEEDED(n);
|
|
block = mem_area_alloc(len, mem_comm_pool);
|
|
} else {
|
|
ut_ad(n <= MEM_MAX_ALLOC_IN_BUF);
|
|
|
|
len = MEM_BLOCK_HEADER_SIZE + MEM_SPACE_NEEDED(n);
|
|
|
|
if (len < UNIV_PAGE_SIZE / 2) {
|
|
|
|
block = mem_area_alloc(len, mem_comm_pool);
|
|
} else {
|
|
len = UNIV_PAGE_SIZE;
|
|
|
|
if ((type & MEM_HEAP_BTR_SEARCH) && heap) {
|
|
/* We cannot allocate the block from the
|
|
buffer pool, but must get the free block from
|
|
the heap header free block field */
|
|
|
|
block = (mem_block_t*)heap->free_block;
|
|
heap->free_block = NULL;
|
|
} else {
|
|
block = (mem_block_t*)buf_frame_alloc();
|
|
}
|
|
}
|
|
}
|
|
|
|
if (block == NULL) {
|
|
/* Only MEM_HEAP_BTR_SEARCH allocation should ever fail. */
|
|
ut_a(type & MEM_HEAP_BTR_SEARCH);
|
|
|
|
return(NULL);
|
|
}
|
|
|
|
block->magic_n = MEM_BLOCK_MAGIC_N;
|
|
ut_strlcpy_rev(block->file_name, file_name, sizeof(block->file_name));
|
|
block->line = line;
|
|
|
|
#ifdef MEM_PERIODIC_CHECK
|
|
mem_pool_mutex_enter();
|
|
|
|
if (!mem_block_list_inited) {
|
|
mem_block_list_inited = TRUE;
|
|
UT_LIST_INIT(mem_block_list);
|
|
}
|
|
|
|
UT_LIST_ADD_LAST(mem_block_list, mem_block_list, block);
|
|
|
|
mem_pool_mutex_exit();
|
|
#endif
|
|
mem_block_set_len(block, len);
|
|
mem_block_set_type(block, type);
|
|
mem_block_set_free(block, MEM_BLOCK_HEADER_SIZE);
|
|
mem_block_set_start(block, MEM_BLOCK_HEADER_SIZE);
|
|
|
|
block->free_block = NULL;
|
|
block->init_block = (init_block != NULL);
|
|
|
|
ut_ad((ulint)MEM_BLOCK_HEADER_SIZE < len);
|
|
|
|
return(block);
|
|
}
|
|
|
|
/*******************************************************************
|
|
Adds a new block to a memory heap. */
|
|
|
|
mem_block_t*
|
|
mem_heap_add_block(
|
|
/*===============*/
|
|
/* out: created block, NULL if did not
|
|
succeed (only possible for
|
|
MEM_HEAP_BTR_SEARCH type heaps)*/
|
|
mem_heap_t* heap, /* in: memory heap */
|
|
ulint n) /* in: number of bytes user needs */
|
|
{
|
|
mem_block_t* block;
|
|
mem_block_t* new_block;
|
|
ulint new_size;
|
|
|
|
ut_ad(mem_heap_check(heap));
|
|
|
|
block = UT_LIST_GET_LAST(heap->base);
|
|
|
|
/* We have to allocate a new block. The size is always at least
|
|
doubled until the standard size is reached. After that the size
|
|
stays the same, except in cases where the caller needs more space. */
|
|
|
|
new_size = 2 * mem_block_get_len(block);
|
|
|
|
if (heap->type != MEM_HEAP_DYNAMIC) {
|
|
/* From the buffer pool we allocate buffer frames */
|
|
ut_a(n <= MEM_MAX_ALLOC_IN_BUF);
|
|
|
|
if (new_size > MEM_MAX_ALLOC_IN_BUF) {
|
|
new_size = MEM_MAX_ALLOC_IN_BUF;
|
|
}
|
|
} else if (new_size > MEM_BLOCK_STANDARD_SIZE) {
|
|
|
|
new_size = MEM_BLOCK_STANDARD_SIZE;
|
|
}
|
|
|
|
if (new_size < n) {
|
|
new_size = n;
|
|
}
|
|
|
|
new_block = mem_heap_create_block(heap, new_size, NULL, heap->type,
|
|
heap->file_name, heap->line);
|
|
if (new_block == NULL) {
|
|
|
|
return(NULL);
|
|
}
|
|
|
|
/* Add the new block as the last block */
|
|
|
|
UT_LIST_INSERT_AFTER(list, heap->base, block, new_block);
|
|
|
|
return(new_block);
|
|
}
|
|
|
|
/**********************************************************************
|
|
Frees a block from a memory heap. */
|
|
|
|
void
|
|
mem_heap_block_free(
|
|
/*================*/
|
|
mem_heap_t* heap, /* in: heap */
|
|
mem_block_t* block) /* in: block to free */
|
|
{
|
|
ulint type;
|
|
ulint len;
|
|
ibool init_block;
|
|
|
|
if (block->magic_n != MEM_BLOCK_MAGIC_N) {
|
|
mem_analyze_corruption(block);
|
|
}
|
|
|
|
UT_LIST_REMOVE(list, heap->base, block);
|
|
|
|
#ifdef MEM_PERIODIC_CHECK
|
|
mem_pool_mutex_enter();
|
|
|
|
UT_LIST_REMOVE(mem_block_list, mem_block_list, block);
|
|
|
|
mem_pool_mutex_exit();
|
|
#endif
|
|
type = heap->type;
|
|
len = block->len;
|
|
init_block = block->init_block;
|
|
block->magic_n = MEM_FREED_BLOCK_MAGIC_N;
|
|
|
|
#ifdef UNIV_MEM_DEBUG
|
|
/* In the debug version we set the memory to a random combination
|
|
of hex 0xDE and 0xAD. */
|
|
|
|
mem_erase_buf((byte*)block, len);
|
|
#else /* UNIV_MEM_DEBUG */
|
|
UNIV_MEM_ASSERT_AND_FREE(block, len);
|
|
#endif /* UNIV_MEM_DEBUG */
|
|
|
|
if (init_block) {
|
|
/* Do not have to free: do nothing */
|
|
|
|
} else if (type == MEM_HEAP_DYNAMIC) {
|
|
|
|
mem_area_free(block, mem_comm_pool);
|
|
} else {
|
|
ut_ad(type & MEM_HEAP_BUFFER);
|
|
|
|
if (len >= UNIV_PAGE_SIZE / 2) {
|
|
buf_frame_free((byte*)block);
|
|
} else {
|
|
mem_area_free(block, mem_comm_pool);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**********************************************************************
|
|
Frees the free_block field from a memory heap. */
|
|
|
|
void
|
|
mem_heap_free_block_free(
|
|
/*=====================*/
|
|
mem_heap_t* heap) /* in: heap */
|
|
{
|
|
if (heap->free_block) {
|
|
|
|
buf_frame_free(heap->free_block);
|
|
|
|
heap->free_block = NULL;
|
|
}
|
|
}
|
|
|
|
#ifdef MEM_PERIODIC_CHECK
|
|
/**********************************************************************
|
|
Goes through the list of all allocated mem blocks, checks their magic
|
|
numbers, and reports possible corruption. */
|
|
|
|
void
|
|
mem_validate_all_blocks(void)
|
|
/*=========================*/
|
|
{
|
|
mem_block_t* block;
|
|
|
|
mem_pool_mutex_enter();
|
|
|
|
block = UT_LIST_GET_FIRST(mem_block_list);
|
|
|
|
while (block) {
|
|
if (block->magic_n != MEM_BLOCK_MAGIC_N) {
|
|
mem_analyze_corruption(block);
|
|
}
|
|
|
|
block = UT_LIST_GET_NEXT(mem_block_list, block);
|
|
}
|
|
|
|
mem_pool_mutex_exit();
|
|
}
|
|
#endif
|