MySQL 8.3.0
Source Code Documentation
mem0mem.h File Reference

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_tmem_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 bytemem_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
 

Detailed Description

The memory management.

Created 6/9/1994 Heikki Tuuri

Macro Definition Documentation

◆ MEM_BLOCK_HEADER_SIZE

#define MEM_BLOCK_HEADER_SIZE    ut_calc_align(sizeof(mem_block_info_t), UNIV_MEM_ALIGNMENT)

◆ MEM_BLOCK_STANDARD_SIZE

#define MEM_BLOCK_STANDARD_SIZE    (UNIV_PAGE_SIZE >= 16384 ? 8000 : MEM_MAX_ALLOC_IN_BUF)

◆ MEM_MAX_ALLOC_IN_BUF

#define MEM_MAX_ALLOC_IN_BUF
Value:
constexpr int MEM_NO_MANS_LAND
Definition: mem0mem.h:100
#define MEM_BLOCK_HEADER_SIZE
Definition: mem0mem.h:347
#define UNIV_PAGE_SIZE
The universal page size of the database.
Definition: univ.i:293
constexpr uint32_t UNIV_MEM_ALIGNMENT
The following alignment is used in memory allocations in memory heap management to ensure correct ali...
Definition: univ.i:282

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 Documentation

◆ mem_block_t

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.

◆ mem_heap_t

A memory heap is a nonempty linear list of memory blocks.

Function Documentation

◆ mem_block_validate()

static void mem_block_validate ( const mem_heap_t heap)
inlinestatic

Checks that an object is a memory heap (or a block of it)

Parameters
[in]heapMemory heap to check

◆ mem_heap_alloc()

static void * mem_heap_alloc ( mem_heap_t heap,
ulint  n 
)
inlinestatic

Allocates n bytes of memory from a memory heap.

Parameters
[in]heapmemory heap
[in]nnumber of bytes; if the heap is allowed to grow into the buffer pool, this must be <= MEM_MAX_ALLOC_IN_BUF
Returns
allocated storage, NULL if did not succeed (only possible for MEM_HEAP_BTR_SEARCH type heaps)

◆ mem_heap_create()

static mem_heap_t * mem_heap_create ( ulint  size,
ut::Location  loc,
ulint  type = MEM_HEAP_DYNAMIC 
)
inlinestatic

Creates a memory heap.

A single user buffer of 'size' will fit in the block. 0 creates a default size block.

Parameters
[in]sizeDesired start block size.
[in]locLocation where created
[in]typeHeap type
Returns
own: memory heap, NULL if did not succeed (only possible for MEM_HEAP_BTR_SEARCH type heaps)

◆ mem_heap_dup()

void * mem_heap_dup ( mem_heap_t heap,
const void *  data,
ulint  len 
)

Duplicate a block of data, allocated from a memory heap.

Returns
own: a copy of the data in: length of data, in bytes
own: a copy of the data
Parameters
heapin: memory heap where copy is allocated
datain: data to be copied
lenin: length of data, in bytes

◆ mem_heap_dup_replace()

static void * mem_heap_dup_replace ( mem_heap_t heap,
const void *  top,
ulint  top_sz,
const void *  data,
ulint  data_sz 
)
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().

Parameters
[in,out]heapmemory heap
[in]topchunk to discard if possible
[in]top_szsize of top in bytes
[in]datanew data to duplicate
[in]data_szsize of data in bytes
Returns
allocated storage, NULL if did not succeed (only possible for MEM_HEAP_BTR_SEARCH type heaps)

◆ mem_heap_empty()

static void mem_heap_empty ( mem_heap_t heap)
inlinestatic

Empties a memory heap.

The first memory block of the heap is not freed.

Parameters
[in]heapheap to empty

◆ mem_heap_free()

static void mem_heap_free ( mem_heap_t heap)
inlinestatic

Frees the space occupied by a memory heap.

Parameters
[in]heapHeap to be freed

◆ mem_heap_free_heap_top()

static void mem_heap_free_heap_top ( mem_heap_t heap,
byte old_top 
)
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.

Parameters
[in]heapheap from which to free
[in]old_toppointer to old top of heap

◆ mem_heap_free_top()

static void mem_heap_free_top ( mem_heap_t heap,
ulint  n 
)
inlinestatic

Frees the topmost element in a memory heap.

Parameters
[in]heapmemory heap
[in]nsize of the topmost element The size of the element must be given.

◆ mem_heap_get_heap_top()

static byte * mem_heap_get_heap_top ( mem_heap_t heap)
inlinestatic

Returns a pointer to the heap top.

Parameters
[in]heapmemory heap
Returns
pointer to the heap top

◆ mem_heap_get_size()

static size_t mem_heap_get_size ( mem_heap_t heap)
inlinestatic

Returns the space in bytes occupied by a memory heap.

in: heap

◆ mem_heap_get_top()

static void * mem_heap_get_top ( mem_heap_t heap,
ulint  n 
)
inlinestatic

Returns a pointer to the topmost element in a memory heap.

The size of the element must be given.

Parameters
[in]heapmemory heap
[in]nsize of the topmost element
Returns
pointer to the topmost element

◆ mem_heap_is_top()

static bool mem_heap_is_top ( mem_heap_t heap,
const void *  buf,
ulint  buf_sz 
)
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.

Parameters
[in]heapmemory heap
[in]bufpresumed topmost element
[in]buf_szsize of buf in bytes
Returns
true if topmost

◆ mem_heap_printf()

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).

Returns
heap-allocated formatted string
Parameters
heapin: memory heap
formatin: format string

◆ mem_heap_replace()

static void * mem_heap_replace ( mem_heap_t heap,
const void *  top,
ulint  top_sz,
ulint  new_sz 
)
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().

Parameters
[in,out]heapmemory heap
[in]topchunk to discard if possible
[in]top_szsize of top in bytes
[in]new_szdesired size of the new chunk
Returns
allocated storage, NULL if did not succeed (only possible for MEM_HEAP_BTR_SEARCH type heaps)

◆ mem_heap_strcat()

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.

Returns
own: the result in: string 2
own: the result
Parameters
heapin: memory heap where string is allocated
s1in: string 1
s2in: string 2

◆ mem_heap_strdup()

char * mem_heap_strdup ( mem_heap_t heap,
const char *  str 
)

Duplicates a NUL-terminated string, allocated from a memory heap.

Parameters
[in]heapmemory heap where string is allocated
[in]strstring to be copied
Returns
own: a copy of the string

◆ mem_heap_strdup_replace()

static char * mem_heap_strdup_replace ( mem_heap_t heap,
const void *  top,
ulint  top_sz,
const char *  str 
)
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().

Parameters
[in,out]heapmemory heap
[in]topchunk to discard if possible
[in]top_szsize of top in bytes
[in]strnew data to duplicate
Returns
allocated string, NULL if did not succeed (only possible for MEM_HEAP_BTR_SEARCH type heaps)

◆ mem_heap_strdupl()

static char * mem_heap_strdupl ( mem_heap_t heap,
const char *  str,
ulint  len 
)
inlinestatic

Makes a NUL-terminated copy of a nonterminated string, allocated from a memory heap.

Parameters
[in]heapmemory heap where string is allocated
[in]strstring to be copied
[in]lenlength of str, in bytes
Returns
own: a copy of the string

◆ mem_heap_validate()

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.

Parameters
[in]heapMemory heap to validate.

◆ mem_heap_zalloc()

static void * mem_heap_zalloc ( mem_heap_t heap,
ulint  n 
)
inlinestatic

Allocates and zero-fills n bytes of memory from a memory heap.

Parameters
[in]heapmemory heap
[in]nnumber of bytes; if the heap is allowed to grow into the buffer pool, this must be <= MEM_MAX_ALLOC_IN_BUF
Returns
allocated, zero-filled storage

◆ MEM_SPACE_NEEDED()

static uint64_t MEM_SPACE_NEEDED ( uint64_t  N)
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.

◆ mem_strdup()

static char * mem_strdup ( const char *  str)
inlinestatic

Duplicates a NUL-terminated string.

Parameters
[in]strstring to be copied
Returns
own: a copy of the string, must be deallocated with ut_free

◆ mem_strdupl()

static char * mem_strdupl ( const char *  str,
ulint  len 
)
inlinestatic

Makes a NUL-terminated copy of a nonterminated string.

Parameters
[in]strstring to be copied
[in]lenlength of str, in bytes
Returns
own: a copy of the string, must be deallocated with ut_free

◆ operator!=()

template<class T >
bool operator!= ( const mem_heap_allocator< T > &  left,
const mem_heap_allocator< T > &  right 
)

◆ operator==()

template<class T >
bool operator== ( const mem_heap_allocator< T > &  left,
const mem_heap_allocator< T > &  right 
)

Variable Documentation

◆ MEM_BLOCK_MAGIC_N

constexpr uint64_t MEM_BLOCK_MAGIC_N = 0x445566778899AABB
constexpr

◆ MEM_BLOCK_START_SIZE

constexpr uint32_t MEM_BLOCK_START_SIZE = 64
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.

◆ MEM_FREED_BLOCK_MAGIC_N

constexpr uint64_t MEM_FREED_BLOCK_MAGIC_N = 0xBBAA998877665544
constexpr

◆ MEM_HEAP_BTR_SEARCH

constexpr uint32_t MEM_HEAP_BTR_SEARCH = 2
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.

◆ MEM_HEAP_BUFFER

constexpr uint32_t MEM_HEAP_BUFFER = 1
constexpr

◆ MEM_HEAP_DYNAMIC

constexpr uint32_t MEM_HEAP_DYNAMIC = 0
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

◆ MEM_HEAP_FOR_BTR_SEARCH

constexpr uint32_t MEM_HEAP_FOR_BTR_SEARCH
constexpr
Initial value:
=
constexpr uint32_t MEM_HEAP_BTR_SEARCH
this flag can optionally be ORed to MEM_HEAP_BUFFER, in which case heap->free_block is used in some c...
Definition: mem0mem.h:66
constexpr uint32_t MEM_HEAP_BUFFER
Definition: mem0mem.h:62

Different type of heaps in terms of which data structure is using them.

◆ MEM_HEAP_FOR_LOCK_HEAP

constexpr uint32_t MEM_HEAP_FOR_LOCK_HEAP = MEM_HEAP_BUFFER
constexpr

◆ MEM_HEAP_FOR_PAGE_HASH

constexpr uint32_t MEM_HEAP_FOR_PAGE_HASH = MEM_HEAP_DYNAMIC
constexpr

◆ MEM_HEAP_FOR_RECV_SYS

constexpr uint32_t MEM_HEAP_FOR_RECV_SYS = MEM_HEAP_BUFFER
constexpr

◆ MEM_NO_MANS_LAND

constexpr int MEM_NO_MANS_LAND = 16
constexpr

◆ MEM_NO_MANS_LAND_AFTER_BYTE

const byte MEM_NO_MANS_LAND_AFTER_BYTE = 0xDF

◆ MEM_NO_MANS_LAND_BEFORE_BYTE

const byte MEM_NO_MANS_LAND_BEFORE_BYTE = 0xCE