MySQL  8.0.19
Source Code Documentation
mem0mem.h File Reference
#include "mach0data.h"
#include "univ.i"
#include "ut0byte.h"
#include "ut0mem.h"
#include "ut0rnd.h"
#include <limits.h>
#include <memory>
#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...
 

Macros

#define MEM_HEAP_DYNAMIC   0 /* the most common type */
 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...
 
#define MEM_HEAP_BUFFER   1
 
#define MEM_HEAP_BTR_SEARCH
 
#define MEM_HEAP_FOR_BTR_SEARCH   (MEM_HEAP_BTR_SEARCH | MEM_HEAP_BUFFER)
 Different type of heaps in terms of which data structure is using them. More...
 
#define MEM_HEAP_FOR_PAGE_HASH   (MEM_HEAP_DYNAMIC)
 
#define MEM_HEAP_FOR_RECV_SYS   (MEM_HEAP_BUFFER)
 
#define MEM_HEAP_FOR_LOCK_HEAP   (MEM_HEAP_BUFFER)
 
#define 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...
 
#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_SPACE_NEEDED(N)   ut_calc_align(N + 2 * MEM_NO_MANS_LAND, UNIV_MEM_ALIGNMENT)
 Space needed when allocating for a user a field of length N. More...
 
#define mem_heap_create(size)   mem_heap_create_func((size), __FILE__, __LINE__, MEM_HEAP_DYNAMIC)
 Macro for memory heap creation. More...
 
#define mem_heap_create_typed(size, type)   mem_heap_create_func((size), __FILE__, __LINE__, (type))
 Macro for memory heap creation. More...
 
#define MEM_BLOCK_MAGIC_N   0x445566778899AABB
 
#define MEM_FREED_BLOCK_MAGIC_N   0xBBAA998877665544
 
#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

UNIV_INLINE mem_heap_tmem_heap_create_func (ulint size, const char *file_name, ulint line, ulint type)
 Creates a memory heap. More...
 
UNIV_INLINE void mem_heap_free (mem_heap_t *heap)
 Frees the space occupied by a memory heap. More...
 
UNIV_INLINE void * mem_heap_zalloc (mem_heap_t *heap, ulint n)
 Allocates and zero-fills n bytes of memory from a memory heap. More...
 
UNIV_INLINE void * mem_heap_alloc (mem_heap_t *heap, ulint n)
 Allocates n bytes of memory from a memory heap. More...
 
UNIV_INLINE bytemem_heap_get_heap_top (mem_heap_t *heap)
 Returns a pointer to the heap top. More...
 
UNIV_INLINE 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...
 
UNIV_INLINE void mem_heap_empty (mem_heap_t *heap)
 Empties a memory heap. More...
 
UNIV_INLINE void * mem_heap_get_top (mem_heap_t *heap, ulint n)
 Returns a pointer to the topmost element in a memory heap. More...
 
UNIV_INLINE 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...
 
UNIV_INLINE 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...
 
UNIV_INLINE 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...
 
UNIV_INLINE 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...
 
UNIV_INLINE void mem_heap_free_top (mem_heap_t *heap, ulint n)
 Frees the topmost element in a memory heap. More...
 
UNIV_INLINE ulint mem_heap_get_size (mem_heap_t *heap)
 Returns the space in bytes occupied by a memory heap. More...
 
UNIV_INLINE char * mem_strdup (const char *str)
 Duplicates a NUL-terminated string. More...
 
UNIV_INLINE 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...
 
UNIV_INLINE 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...
 
UNIV_INLINE 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

const int MEM_NO_MANS_LAND = 16
 
const byte MEM_NO_MANS_LAND_BEFORE_BYTE = 0xCE
 
const byte MEM_NO_MANS_LAND_AFTER_BYTE = 0xDF
 

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_MAGIC_N

#define MEM_BLOCK_MAGIC_N   0x445566778899AABB

◆ MEM_BLOCK_STANDARD_SIZE

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

◆ MEM_BLOCK_START_SIZE

#define 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.

The standard size is the maximum (payload) size of the blocks used for allocations of small buffers.

◆ MEM_FREED_BLOCK_MAGIC_N

#define MEM_FREED_BLOCK_MAGIC_N   0xBBAA998877665544

◆ MEM_HEAP_BTR_SEARCH

#define MEM_HEAP_BTR_SEARCH
Value:
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. */

◆ MEM_HEAP_BUFFER

#define MEM_HEAP_BUFFER   1

◆ mem_heap_create

#define mem_heap_create (   size)    mem_heap_create_func((size), __FILE__, __LINE__, MEM_HEAP_DYNAMIC)

Macro for memory heap creation.

Parameters
[in]sizeDesired start block size.

◆ mem_heap_create_typed

#define mem_heap_create_typed (   size,
  type 
)    mem_heap_create_func((size), __FILE__, __LINE__, (type))

Macro for memory heap creation.

Parameters
[in]sizeDesired start block size.
[in]typeHeap type

◆ MEM_HEAP_DYNAMIC

#define MEM_HEAP_DYNAMIC   0 /* the most common type */

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.

◆ MEM_HEAP_FOR_BTR_SEARCH

#define MEM_HEAP_FOR_BTR_SEARCH   (MEM_HEAP_BTR_SEARCH | MEM_HEAP_BUFFER)

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

◆ MEM_HEAP_FOR_LOCK_HEAP

#define MEM_HEAP_FOR_LOCK_HEAP   (MEM_HEAP_BUFFER)

◆ MEM_HEAP_FOR_PAGE_HASH

#define MEM_HEAP_FOR_PAGE_HASH   (MEM_HEAP_DYNAMIC)

◆ MEM_HEAP_FOR_RECV_SYS

#define MEM_HEAP_FOR_RECV_SYS   (MEM_HEAP_BUFFER)

◆ MEM_MAX_ALLOC_IN_BUF

#define MEM_MAX_ALLOC_IN_BUF
Value:
((UNIV_PAGE_SIZE - MEM_BLOCK_HEADER_SIZE - 2 * MEM_NO_MANS_LAND) & \
~(UNIV_MEM_ALIGNMENT - 1))

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

◆ MEM_SPACE_NEEDED

#define MEM_SPACE_NEEDED (   N)    ut_calc_align(N + 2 * MEM_NO_MANS_LAND, UNIV_MEM_ALIGNMENT)

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.

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

UNIV_INLINE void mem_block_validate ( const mem_heap_t heap)

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

Parameters
[in]heapMemory heap to check

◆ mem_heap_alloc()

UNIV_INLINE void* mem_heap_alloc ( mem_heap_t heap,
ulint  n 
)

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_func()

UNIV_INLINE mem_heap_t* mem_heap_create_func ( ulint  size,
const char *  file_name,
ulint  line,
ulint  type 
)

Creates a memory heap.

NOTE: Use the corresponding macros instead of this function. A single user buffer of 'size' will fit in the block. 0 creates a default size block.

Parameters
[in]sizeDesired start block size.
[in]file_nameFile name where created
[in]lineLine 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

◆ mem_heap_dup_replace()

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

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

UNIV_INLINE void mem_heap_empty ( mem_heap_t heap)

Empties a memory heap.

The first memory block of the heap is not freed.

Parameters
[in]heapheap to empty

◆ mem_heap_free()

UNIV_INLINE void mem_heap_free ( mem_heap_t heap)

Frees the space occupied by a memory heap.

NOTE: Use the corresponding macro instead of this function.

Parameters
[in]heapHeap to be freed

◆ mem_heap_free_heap_top()

UNIV_INLINE void mem_heap_free_heap_top ( mem_heap_t heap,
byte old_top 
)

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

UNIV_INLINE void mem_heap_free_top ( mem_heap_t heap,
ulint  n 
)

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

UNIV_INLINE byte* mem_heap_get_heap_top ( mem_heap_t heap)

Returns a pointer to the heap top.

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

◆ mem_heap_get_size()

UNIV_INLINE ulint mem_heap_get_size ( mem_heap_t heap)

Returns the space in bytes occupied by a memory heap.

in: heap

◆ mem_heap_get_top()

UNIV_INLINE void* mem_heap_get_top ( mem_heap_t heap,
ulint  n 
)

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

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

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

◆ mem_heap_replace()

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

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

◆ 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

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

The code in this file is used to make a library for external tools. Duplicates a NUL-terminated string, allocated from a memory heap.

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

◆ mem_heap_strdup_replace()

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

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

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

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.

Asserts that the memory heap is consistent

Parameters
[in]heapMemory heap to validate

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

UNIV_INLINE void* mem_heap_zalloc ( mem_heap_t heap,
ulint  n 
)

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_strdup()

UNIV_INLINE char* mem_strdup ( const char *  str)

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

UNIV_INLINE char* mem_strdupl ( const char *  str,
ulint  len 
)

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_NO_MANS_LAND

const int MEM_NO_MANS_LAND = 16

◆ 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
MEM_BLOCK_HEADER_SIZE
#define MEM_BLOCK_HEADER_SIZE
Definition: mem0mem.h:378
MEM_NO_MANS_LAND
const int MEM_NO_MANS_LAND
Definition: mem0mem.h:95