27#ifndef TEMPTABLE_ALLOCATOR_H
28#define TEMPTABLE_ALLOCATOR_H
61 assert(
ram <= std::numeric_limits<
decltype(bytes)>::max() - bytes);
62 return ram.fetch_add(bytes) + bytes;
70 return ram.fetch_sub(bytes) - bytes;
88 assert(
mmap <= std::numeric_limits<
decltype(bytes)>::max() - bytes);
89 return mmap.fetch_add(bytes) + bytes;
96 assert(
mmap >= bytes);
97 return mmap.fetch_sub(bytes) - bytes;
118 static std::atomic<size_t>
ram;
119 static std::atomic<size_t>
mmap;
135 std::numeric_limits<
decltype(bytes)>::max() - bytes);
191template <
typename Block_size_policy,
typename Block_source_policy>
195 return Block_source_policy::block_source(
block_size,
196 table_resource_monitor);
198 static size_t block_size(
size_t number_of_blocks,
size_t n_bytes_requested) {
199 return Block_size_policy::block_size(number_of_blocks, n_bytes_requested);
269 assert(table_resource_monitor);
273 if (table_resource_monitor->
consumption() + block_size >
315 static size_t block_size(
size_t number_of_blocks,
size_t n_bytes_requested) {
316 size_t block_size_hint;
318 block_size_hint = (1ULL << number_of_blocks) * 1_MiB;
354template <
class AllocationScheme>
389 const size_t block_size =
393 AllocationScheme::block_source(block_size, table_resource_monitor));
422 AllocationScheme::block_freed(block.size(), block.type());
479 "T's with alignment-requirement larger than "
480 "Block::ALIGN_TO are not supported.");
481 static_assert(
sizeof(T) > 0,
"Zero sized objects are not supported");
554 template <
class U,
class... Args>
576 std::shared_ptr<AllocatorState<AllocationScheme>>
m_state;
592template <
class T,
class AllocationScheme>
596 m_shared_block(shared_block),
597 m_table_resource_monitor(table_resource_monitor) {}
599template <
class T,
class AllocationScheme>
602 : m_state(other.m_state),
603 m_shared_block(other.m_shared_block),
604 m_table_resource_monitor(other.m_table_resource_monitor) {}
606template <
class T,
class AllocationScheme>
609 : m_state(std::move(other.m_state)),
610 m_shared_block(other.m_shared_block),
611 m_table_resource_monitor(other.m_table_resource_monitor) {}
613template <
class T,
class AllocationScheme>
616template <
class T,
class AllocationScheme>
623template <
class T,
class AllocationScheme>
627 return !(*
this == rhs);
630template <
class T,
class AllocationScheme>
632 assert(n_elements <= std::numeric_limits<size_type>::max() /
sizeof(T));
637 const size_t n_bytes_requested = n_elements *
sizeof(T);
638 if (n_bytes_requested == 0) {
644 if (m_shared_block && m_shared_block->is_empty()) {
645 const size_t block_size =
646 AllocationScheme::block_size(0, n_bytes_requested);
647 *m_shared_block =
Block(
649 AllocationScheme::block_source(block_size, &m_table_resource_monitor));
650 block = m_shared_block;
651 }
else if (m_shared_block &&
652 m_shared_block->can_accommodate(n_bytes_requested)) {
653 block = m_shared_block;
655 block = m_state->get_block_for_new_allocation(n_bytes_requested,
656 &m_table_resource_monitor);
659 m_table_resource_monitor.increase(n_bytes_requested);
662 reinterpret_cast<T *
>(block->
allocate(n_bytes_requested).
data());
663 assert(
reinterpret_cast<uintptr_t
>(chunk_data) %
alignof(T) == 0);
667template <
class T,
class AllocationScheme>
670 assert(
reinterpret_cast<uintptr_t
>(chunk_data) %
alignof(T) == 0);
672 if (chunk_data ==
nullptr) {
676 const size_t n_bytes_requested = n_elements *
sizeof(T);
679 const auto remaining_chunks =
680 block.deallocate(
Chunk(chunk_data), n_bytes_requested);
681 if (remaining_chunks == 0) {
682 if (m_shared_block && (block == *m_shared_block)) {
685 m_state->block_is_not_used_anymore(block);
688 m_table_resource_monitor.decrease(n_bytes_requested);
691template <
class T,
class AllocationScheme>
692template <
class U,
class... Args>
694 new (
mem)
U(std::forward<Args>(args)...);
697template <
class T,
class AllocationScheme>
703template <
class T,
class AllocationScheme>
Block abstraction for temptable-allocator.
Chunk abstraction for temptable Block allocator.
Shared state between all instances of a given allocator.
Definition: allocator.h:355
void block_is_not_used_anymore(Block block) noexcept
Informs the state object of a block that has no data allocated inside of it anymore for garbage colle...
Definition: allocator.h:404
Block * get_block_for_new_allocation(size_t n_bytes_requested, TableResourceMonitor *table_resource_monitor)
Gets a Block from which a new allocation of the specified size should be performed.
Definition: allocator.h:376
size_t number_of_blocks
Number of created blocks so far (by this Allocator object).
Definition: allocator.h:435
Block current_block
Current not-yet-full block to feed allocations from.
Definition: allocator.h:428
void free_block(Block &block) noexcept
Frees the specified block and takes care of all accounting.
Definition: allocator.h:421
~AllocatorState() noexcept
Destroys the state, deallocate the current_block if it was left empty.
Definition: allocator.h:360
Custom memory allocator.
Definition: allocator.h:477
Block * m_shared_block
A block of memory which is a state external to this allocator and can be shared among different insta...
Definition: allocator.h:583
void construct(U *mem, Args &&...args)
Construct one object of type U on an already allocated chunk of memory, which must be large enough to...
Definition: allocator.h:693
T & reference
Definition: allocator.h:486
const T * const_pointer
Definition: allocator.h:485
void deallocate(T *ptr, size_t n_elements)
Free a memory allocated by allocate().
Definition: allocator.h:668
const T & const_reference
Definition: allocator.h:487
void operator=(const Allocator< U > &&)=delete
Move operator, not used, thus disabled.
Allocator(const Allocator &)=default
Allocator(Block *shared_block, TableResourceMonitor &table_resource_monitor)
Constructor.
Definition: allocator.h:593
T * pointer
Definition: allocator.h:480
T * allocate(size_t n_elements)
Allocate memory for storing n_elements number of elements.
Definition: allocator.h:631
size_t size_type
Definition: allocator.h:489
void destroy(U *p)
Destroy an object of type U.
Definition: allocator.h:699
static void init()
Initialize necessary structures.
Definition: allocator.h:704
bool operator!=(const Allocator< U > &rhs) const
Inequality operator.
Definition: allocator.h:625
TableResourceMonitor & m_table_resource_monitor
Table resource monitor control mechanism that limits the amount of resources that can be consumed at ...
Definition: allocator.h:587
bool operator==(const Allocator< U > &rhs) const
Equality operator.
Definition: allocator.h:618
ptrdiff_t difference_type
Definition: allocator.h:490
std::shared_ptr< AllocatorState< AllocationScheme > > m_state
Shared state between all the copies and rebinds of this allocator.
Definition: allocator.h:576
T value_type
Definition: allocator.h:488
void operator=(const Allocator< U > &)=delete
Assignment operator, not used, thus disabled.
Memory-block abstraction whose purpose is to serve as a building block for custom memory-allocator im...
Definition: block.h:163
bool is_empty() const
Check if Block is empty (not holding any data).
Definition: block.h:399
bool can_accommodate(size_t chunk_size) const
Check if Block can fit (allocate) a Chunk of given size.
Definition: block.h:403
size_t number_of_used_chunks() const
Get current number of Chunks allocated by the Block.
Definition: block.h:427
static constexpr size_t ALIGN_TO
Block will self-adjust all requested allocation-sizes to the multiple of this value.
Definition: block.h:167
Chunk allocate(size_t chunk_size) noexcept
Allocate a Chunk from a Block.
Definition: block.h:351
static size_t size_hint(size_t n_bytes)
For given size, how much memory will Block with single Chunk actually occupy.
Definition: block.h:448
Chunk is an abstraction with the purpose of representing a smallest logical memory-unit within the Bl...
Definition: chunk.h:68
uint8_t * data() const
Get the pointer to the data section which will be provided to the end-user.
Definition: chunk.h:158
Definition: allocator.h:128
TableResourceMonitor(size_t threshold)
Definition: allocator.h:130
size_t m_total_bytes
Definition: allocator.h:149
size_t increase(size_t bytes)
Definition: allocator.h:133
size_t decrease(size_t bytes)
Definition: allocator.h:139
size_t m_threshold
Definition: allocator.h:148
size_t threshold()
Definition: allocator.h:144
size_t consumption()
Definition: allocator.h:145
const char * p
Definition: ctype-mb.cc:1237
#define U
Definition: ctype-tis620.cc:75
Memory utilities for temptable-allocator.
#define DBUG_EXECUTE_IF(keyword, a1)
Definition: my_dbug.h:171
Common header for many mysys elements.
ulonglong temptable_max_ram
Definition: mysqld.cc:1166
bool temptable_use_mmap
Definition: mysqld.cc:1168
ulonglong temptable_max_mmap
Definition: mysqld.cc:1167
Definition: gcs_xcom_synode.h:64
Definition: allocator.h:45
constexpr size_t ALLOCATOR_MAX_BLOCK_MB_EXP
log2(allocator max block size in MiB).
Definition: constants.h:60
void Block_PSI_init()
Initialize the PSI memory engine.
Definition: block.cc:74
constexpr size_t ALLOCATOR_MAX_BLOCK_BYTES
Limit on the size of a block created by Allocator (in bytes).
Definition: constants.h:65
Source
Type of memory allocated.
Definition: memutils.h:68
@ MMAP_FILE
Memory is allocated on disk, using mmap()'ed file.
@ RAM
Memory is allocated from RAM, using malloc() for example.
std::enable_if_t<!std::is_array< T >::value, std::shared_ptr< T > > make_shared(Args &&...args)
Dynamically allocates storage for an object of type T.
Definition: ut0new.h:2591
static MEM_ROOT mem
Definition: sql_servers.cc:99
Definition: allocator.h:192
static size_t block_size(size_t number_of_blocks, size_t n_bytes_requested)
Definition: allocator.h:198
static Source block_source(size_t block_size, TableResourceMonitor *table_resource_monitor)
Definition: allocator.h:193
static void block_freed(uint32_t block_size, Source block_source)
Definition: allocator.h:201
Definition: allocator.h:493
Allocator< U, AllocationScheme > other
Definition: allocator.h:494
Definition: allocator.h:307
static size_t block_size(size_t number_of_blocks, size_t n_bytes_requested)
Given the current number of allocated blocks by the allocator, and number of bytes actually requested...
Definition: allocator.h:315
Definition: allocator.h:82
static size_t increase(size_t bytes)
Log increments of MMAP-backed memory consumption.
Definition: allocator.h:87
static size_t threshold()
Get MMAP-backed memory threshold level.
Definition: allocator.h:103
static size_t decrease(size_t bytes)
Log decrements of MMAP-backed memory consumption.
Definition: allocator.h:95
static size_t consumption()
Get current level of MMAP-backed memory consumption.
Definition: allocator.h:113
Definition: allocator.h:55
static size_t consumption()
Get current level of heap-memory consumption.
Definition: allocator.h:79
static size_t decrease(size_t bytes)
Log decrements of heap-memory consumption.
Definition: allocator.h:68
static size_t increase(size_t bytes)
Log increments of heap-memory consumption.
Definition: allocator.h:60
static size_t threshold()
Get heap-memory threshold level.
Definition: allocator.h:75
Definition: allocator.h:54
static std::atomic< size_t > mmap
Definition: allocator.h:119
static std::atomic< size_t > ram
Total bytes allocated so far by all threads in RAM/MMAP.
Definition: allocator.h:118
Definition: allocator.h:266
static void block_freed(uint32_t block_size, Source block_source)
Definition: allocator.h:280
static Source block_source(uint32_t block_size, TableResourceMonitor *table_resource_monitor)
Definition: allocator.h:267
Definition: allocator.h:218
static void block_freed(uint32_t block_size, Source block_source)
Definition: allocator.h:240
static Source block_source(uint32_t block_size, TableResourceMonitor *=nullptr)
Definition: allocator.h:219