MySQL 9.1.0
Source Code Documentation
|
The memory management. More...
#include "mach0data.h"
#include "ut0byte.h"
#include "ut0mem.h"
#include "ut0rnd.h"
#include <limits.h>
#include <functional>
#include <memory>
#include <type_traits>
#include "mem0mem.ic"
Go to the source code of this file.
Classes | |
struct | mem_block_info_t |
The info structure stored at the beginning of a heap block. More... | |
class | mem_heap_allocator< T > |
A C++ wrapper class to the mem_heap_t routines, so that it can be used as an STL allocator. More... | |
struct | mem_heap_allocator< T >::rebind< U > |
Allocators are required to supply the below template class member which enables the possibility of obtaining a related allocator, parametrized in terms of a different type. More... | |
struct | Scoped_heap |
Heap wrapper that destroys the heap instance when it goes out of scope. More... | |
struct | Scoped_heap::mem_heap_free_functor |
A functor with no state to be used for mem_heap destruction. More... | |
Macros | |
#define | MEM_BLOCK_STANDARD_SIZE (UNIV_PAGE_SIZE >= 16384 ? 8000 : MEM_MAX_ALLOC_IN_BUF) |
#define | MEM_MAX_ALLOC_IN_BUF |
If a memory heap is allowed to grow into the buffer pool, the following is the maximum size for a single allocated buffer (from UNIV_PAGE_SIZE we subtract MEM_BLOCK_HEADER_SIZE and 2*MEM_NO_MANS_LAND since it's something we always need to put. More... | |
#define | MEM_BLOCK_HEADER_SIZE ut_calc_align(sizeof(mem_block_info_t), UNIV_MEM_ALIGNMENT) |
Typedefs | |
typedef struct mem_block_info_t | mem_block_t |
A block of a memory heap consists of the info structure followed by an area of memory. More... | |
typedef mem_block_t | mem_heap_t |
A memory heap is a nonempty linear list of memory blocks. More... | |
Functions | |
static uint64_t | MEM_SPACE_NEEDED (uint64_t N) |
Space needed when allocating for a user a field of length N. More... | |
static mem_heap_t * | mem_heap_create (ulint size, ut::Location loc, ulint type=MEM_HEAP_DYNAMIC) |
Creates a memory heap. More... | |
static void | mem_heap_free (mem_heap_t *heap) |
Frees the space occupied by a memory heap. More... | |
static void * | mem_heap_zalloc (mem_heap_t *heap, ulint n) |
Allocates and zero-fills n bytes of memory from a memory heap. More... | |
static void * | mem_heap_alloc (mem_heap_t *heap, ulint n) |
Allocates n bytes of memory from a memory heap. More... | |
static byte * | mem_heap_get_heap_top (mem_heap_t *heap) |
Returns a pointer to the heap top. More... | |
static void | mem_heap_free_heap_top (mem_heap_t *heap, byte *old_top) |
Frees the space in a memory heap exceeding the pointer given. More... | |
static void | mem_heap_empty (mem_heap_t *heap) |
Empties a memory heap. More... | |
static void * | mem_heap_get_top (mem_heap_t *heap, ulint n) |
Returns a pointer to the topmost element in a memory heap. More... | |
static bool | mem_heap_is_top (mem_heap_t *heap, const void *buf, ulint buf_sz) |
Checks if a given chunk of memory is the topmost element stored in the heap. More... | |
static void * | mem_heap_replace (mem_heap_t *heap, const void *top, ulint top_sz, ulint new_sz) |
Allocate a new chunk of memory from a memory heap, possibly discarding the topmost element. More... | |
static void * | mem_heap_dup_replace (mem_heap_t *heap, const void *top, ulint top_sz, const void *data, ulint data_sz) |
Allocate a new chunk of memory from a memory heap, possibly discarding the topmost element and then copy the specified data to it. More... | |
static char * | mem_heap_strdup_replace (mem_heap_t *heap, const void *top, ulint top_sz, const char *str) |
Allocate a new chunk of memory from a memory heap, possibly discarding the topmost element and then copy the specified string to it. More... | |
static void | mem_heap_free_top (mem_heap_t *heap, ulint n) |
Frees the topmost element in a memory heap. More... | |
static size_t | mem_heap_get_size (mem_heap_t *heap) |
Returns the space in bytes occupied by a memory heap. More... | |
static char * | mem_strdup (const char *str) |
Duplicates a NUL-terminated string. More... | |
static char * | mem_strdupl (const char *str, ulint len) |
Makes a NUL-terminated copy of a nonterminated string. More... | |
char * | mem_heap_strdup (mem_heap_t *heap, const char *str) |
Duplicates a NUL-terminated string, allocated from a memory heap. More... | |
static char * | mem_heap_strdupl (mem_heap_t *heap, const char *str, ulint len) |
Makes a NUL-terminated copy of a nonterminated string, allocated from a memory heap. More... | |
char * | mem_heap_strcat (mem_heap_t *heap, const char *s1, const char *s2) |
Concatenate two strings and return the result, using a memory heap. More... | |
void * | mem_heap_dup (mem_heap_t *heap, const void *data, ulint len) |
Duplicate a block of data, allocated from a memory heap. More... | |
char * | mem_heap_printf (mem_heap_t *heap, const char *format,...) |
A simple sprintf replacement that dynamically allocates the space for the formatted string from the given heap. More... | |
static void | mem_block_validate (const mem_heap_t *heap) |
Checks that an object is a memory heap (or a block of it) More... | |
void | mem_heap_validate (const mem_heap_t *heap) |
Validates the contents of a memory heap. More... | |
template<class T > | |
bool | operator== (const mem_heap_allocator< T > &left, const mem_heap_allocator< T > &right) |
template<class T > | |
bool | operator!= (const mem_heap_allocator< T > &left, const mem_heap_allocator< T > &right) |
Variables | |
constexpr uint32_t | MEM_HEAP_DYNAMIC = 0 |
Types of allocation for memory heaps: DYNAMIC means allocation from the dynamic memory pool of the C compiler, BUFFER means allocation from the buffer pool; the latter method is used for very big heaps. More... | |
constexpr uint32_t | MEM_HEAP_BUFFER = 1 |
constexpr uint32_t | MEM_HEAP_BTR_SEARCH = 2 |
this flag can optionally be ORed to MEM_HEAP_BUFFER, in which case heap->free_block is used in some cases for memory allocations, and if it's NULL, the memory allocation functions can return NULL. More... | |
constexpr uint32_t | MEM_HEAP_FOR_BTR_SEARCH |
Different type of heaps in terms of which data structure is using them. More... | |
constexpr uint32_t | MEM_HEAP_FOR_PAGE_HASH = MEM_HEAP_DYNAMIC |
constexpr uint32_t | MEM_HEAP_FOR_RECV_SYS = MEM_HEAP_BUFFER |
constexpr uint32_t | MEM_HEAP_FOR_LOCK_HEAP = MEM_HEAP_BUFFER |
constexpr uint32_t | MEM_BLOCK_START_SIZE = 64 |
The following start size is used for the first block in the memory heap if the size is not specified, i.e., 0 is given as the parameter in the call of create. More... | |
constexpr int | MEM_NO_MANS_LAND = 16 |
const byte | MEM_NO_MANS_LAND_BEFORE_BYTE = 0xCE |
const byte | MEM_NO_MANS_LAND_AFTER_BYTE = 0xDF |
constexpr uint64_t | MEM_BLOCK_MAGIC_N = 0x445566778899AABB |
constexpr uint64_t | MEM_FREED_BLOCK_MAGIC_N = 0xBBAA998877665544 |
The memory management.
Created 6/9/1994 Heikki Tuuri
#define MEM_BLOCK_HEADER_SIZE ut_calc_align(sizeof(mem_block_info_t), UNIV_MEM_ALIGNMENT) |
#define MEM_BLOCK_STANDARD_SIZE (UNIV_PAGE_SIZE >= 16384 ? 8000 : MEM_MAX_ALLOC_IN_BUF) |
#define MEM_MAX_ALLOC_IN_BUF |
If a memory heap is allowed to grow into the buffer pool, the following is the maximum size for a single allocated buffer (from UNIV_PAGE_SIZE we subtract MEM_BLOCK_HEADER_SIZE and 2*MEM_NO_MANS_LAND since it's something we always need to put.
Since in MEM_SPACE_NEEDED we round n to the next multiple of UNIV_MEM_ALINGMENT, we need to cut from the rest the part that cannot be divided by UNIV_MEM_ALINGMENT):
typedef struct mem_block_info_t mem_block_t |
A block of a memory heap consists of the info structure followed by an area of memory.
typedef mem_block_t mem_heap_t |
A memory heap is a nonempty linear list of memory blocks.
|
inlinestatic |
Checks that an object is a memory heap (or a block of it)
[in] | heap | Memory heap to check |
|
inlinestatic |
Allocates n bytes of memory from a memory heap.
[in] | heap | memory heap |
[in] | n | number of bytes; if the heap is allowed to grow into the buffer pool, this must be <= MEM_MAX_ALLOC_IN_BUF |
|
inlinestatic |
Creates a memory heap.
A single user buffer of 'size' will fit in the block. 0 creates a default size block.
[in] | size | Desired start block size. |
[in] | loc | Location where created |
[in] | type | Heap type |
void * mem_heap_dup | ( | mem_heap_t * | heap, |
const void * | data, | ||
ulint | len | ||
) |
Duplicate a block of data, allocated from a memory heap.
heap | in: memory heap where copy is allocated |
data | in: data to be copied |
len | in: length of data, in bytes |
|
inlinestatic |
Allocate a new chunk of memory from a memory heap, possibly discarding the topmost element and then copy the specified data to it.
If the memory chunk specified with (top, top_sz) is the topmost element, then it will be discarded, otherwise it will be left untouched and this function will be equivalent to mem_heap_dup().
[in,out] | heap | memory heap |
[in] | top | chunk to discard if possible |
[in] | top_sz | size of top in bytes |
[in] | data | new data to duplicate |
[in] | data_sz | size of data in bytes |
|
inlinestatic |
Empties a memory heap.
The first memory block of the heap is not freed.
[in] | heap | heap to empty |
|
inlinestatic |
Frees the space occupied by a memory heap.
[in] | heap | Heap to be freed |
|
inlinestatic |
Frees the space in a memory heap exceeding the pointer given.
The pointer must have been acquired from mem_heap_get_heap_top. The first memory block of the heap is not freed.
[in] | heap | heap from which to free |
[in] | old_top | pointer to old top of heap |
|
inlinestatic |
Frees the topmost element in a memory heap.
[in] | heap | memory heap |
[in] | n | size of the topmost element The size of the element must be given. |
|
inlinestatic |
Returns a pointer to the heap top.
[in] | heap | memory heap |
|
inlinestatic |
Returns the space in bytes occupied by a memory heap.
in: heap
|
inlinestatic |
Returns a pointer to the topmost element in a memory heap.
The size of the element must be given.
[in] | heap | memory heap |
[in] | n | size of the topmost element |
|
inlinestatic |
Checks if a given chunk of memory is the topmost element stored in the heap.
If this is the case, then calling mem_heap_free_top() would free that element from the heap.
[in] | heap | memory heap |
[in] | buf | presumed topmost element |
[in] | buf_sz | size of buf in bytes |
char * mem_heap_printf | ( | mem_heap_t * | heap, |
const char * | format, | ||
... | |||
) |
A simple sprintf 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).
heap | in: memory heap |
format | in: format string |
|
inlinestatic |
Allocate a new chunk of memory from a memory heap, possibly discarding the topmost element.
If the memory chunk specified with (top, top_sz) is the topmost element, then it will be discarded, otherwise it will be left untouched and this function will be equivallent to mem_heap_alloc().
[in,out] | heap | memory heap |
[in] | top | chunk to discard if possible |
[in] | top_sz | size of top in bytes |
[in] | new_sz | desired size of the new chunk |
char * mem_heap_strcat | ( | mem_heap_t * | heap, |
const char * | s1, | ||
const char * | s2 | ||
) |
Concatenate two strings and return the result, using a memory heap.
heap | in: memory heap where string is allocated |
s1 | in: string 1 |
s2 | in: string 2 |
char * mem_heap_strdup | ( | mem_heap_t * | heap, |
const char * | str | ||
) |
Duplicates a NUL-terminated string, allocated from a memory heap.
[in] | heap | memory heap where string is allocated |
[in] | str | string to be copied |
|
inlinestatic |
Allocate a new chunk of memory from a memory heap, possibly discarding the topmost element and then copy the specified string to it.
If the memory chunk specified with (top, top_sz) is the topmost element, then it will be discarded, otherwise it will be left untouched and this function will be equivalent to mem_heap_strdup().
[in,out] | heap | memory heap |
[in] | top | chunk to discard if possible |
[in] | top_sz | size of top in bytes |
[in] | str | new data to duplicate |
|
inlinestatic |
Makes a NUL-terminated copy of a nonterminated string, allocated from a memory heap.
[in] | heap | memory heap where string is allocated |
[in] | str | string to be copied |
[in] | len | length of str, in bytes |
void mem_heap_validate | ( | const mem_heap_t * | heap | ) |
Validates the contents of a memory heap.
Checks a memory heap for consistency, prints the contents if any error is detected. A fatal error is logged if an error is detected.
[in] | heap | Memory heap to validate. |
|
inlinestatic |
Allocates and zero-fills n bytes of memory from a memory heap.
[in] | heap | memory heap |
[in] | n | number of bytes; if the heap is allowed to grow into the buffer pool, this must be <= MEM_MAX_ALLOC_IN_BUF |
|
inlinestatic |
Space needed when allocating for a user a field of length N.
The space is allocated only in multiples of UNIV_MEM_ALIGNMENT. In debug mode contains two areas of no mans lands before and after the buffer requested.
|
inlinestatic |
Duplicates a NUL-terminated string.
[in] | str | string to be copied |
|
inlinestatic |
Makes a NUL-terminated copy of a nonterminated string.
[in] | str | string to be copied |
[in] | len | length of str, in bytes |
bool operator!= | ( | const mem_heap_allocator< T > & | left, |
const mem_heap_allocator< T > & | right | ||
) |
bool operator== | ( | const mem_heap_allocator< T > & | left, |
const mem_heap_allocator< T > & | right | ||
) |
|
constexpr |
|
constexpr |
The following start size is used for the first block in the memory heap if the size is not specified, i.e., 0 is given as the parameter in the call of create.
The standard size is the maximum (payload) size of the blocks used for allocations of small buffers.
|
constexpr |
|
constexpr |
this flag can optionally be ORed to MEM_HEAP_BUFFER, in which case heap->free_block is used in some cases for memory allocations, and if it's NULL, the memory allocation functions can return NULL.
|
constexpr |
|
constexpr |
Types of allocation for memory heaps: DYNAMIC means allocation from the dynamic memory pool of the C compiler, BUFFER means allocation from the buffer pool; the latter method is used for very big heaps.
the most common type
|
constexpr |
Different type of heaps in terms of which data structure is using them.
|
constexpr |
|
constexpr |
|
constexpr |
|
constexpr |
const byte MEM_NO_MANS_LAND_AFTER_BYTE = 0xDF |
const byte MEM_NO_MANS_LAND_BEFORE_BYTE = 0xCE |