26#ifndef TEMPTABLE_ALLOCATOR_H
27#define TEMPTABLE_ALLOCATOR_H
60 assert(
ram <= std::numeric_limits<
decltype(bytes)>::max() - bytes);
61 return ram.fetch_add(bytes) + bytes;
69 return ram.fetch_sub(bytes) - bytes;
87 assert(
mmap <= std::numeric_limits<
decltype(bytes)>::max() - bytes);
88 return mmap.fetch_add(bytes) + bytes;
95 assert(
mmap >= bytes);
96 return mmap.fetch_sub(bytes) - bytes;
117 static std::atomic<size_t>
ram;
118 static std::atomic<size_t>
mmap;
134 std::numeric_limits<
decltype(bytes)>::max() - bytes);
190template <
typename Block_size_policy,
typename Block_source_policy>
194 return Block_source_policy::block_source(
block_size,
195 table_resource_monitor);
197 static size_t block_size(
size_t number_of_blocks,
size_t n_bytes_requested) {
198 return Block_size_policy::block_size(number_of_blocks, n_bytes_requested);
257 assert(table_resource_monitor);
261 if (table_resource_monitor->
consumption() + block_size >
299 static size_t block_size(
size_t number_of_blocks,
size_t n_bytes_requested) {
300 size_t block_size_hint;
302 block_size_hint = (1ULL << number_of_blocks) * 1_MiB;
391 "T's with alignment-requirement larger than "
392 "Block::ALIGN_TO are not supported.");
393 static_assert(
sizeof(T) > 0,
"Zero sized objects are not supported");
466 template <
class U,
class... Args>
504template <
class T,
class AllocationScheme>
508 m_shared_block(shared_block),
509 m_table_resource_monitor(table_resource_monitor) {}
511template <
class T,
class AllocationScheme>
514 : m_state(other.m_state),
515 m_shared_block(other.m_shared_block),
516 m_table_resource_monitor(other.m_table_resource_monitor) {}
518template <
class T,
class AllocationScheme>
521 : m_state(std::move(other.m_state)),
522 m_shared_block(other.m_shared_block),
523 m_table_resource_monitor(other.m_table_resource_monitor) {}
525template <
class T,
class AllocationScheme>
528template <
class T,
class AllocationScheme>
535template <
class T,
class AllocationScheme>
539 return !(*
this == rhs);
542template <
class T,
class AllocationScheme>
544 assert(n_elements <= std::numeric_limits<size_type>::max() /
sizeof(T));
549 const size_t n_bytes_requested = n_elements *
sizeof(T);
550 if (n_bytes_requested == 0) {
556 if (m_shared_block && m_shared_block->is_empty()) {
557 const size_t block_size =
558 AllocationScheme::block_size(0, n_bytes_requested);
559 *m_shared_block =
Block(
561 AllocationScheme::block_source(block_size, &m_table_resource_monitor));
562 block = m_shared_block;
563 }
else if (m_shared_block &&
564 m_shared_block->can_accommodate(n_bytes_requested)) {
565 block = m_shared_block;
566 }
else if (m_state->current_block.is_empty() ||
567 !m_state->current_block.can_accommodate(n_bytes_requested)) {
568 const size_t block_size = AllocationScheme::block_size(
569 m_state->number_of_blocks, n_bytes_requested);
570 m_state->current_block =
Block(
572 AllocationScheme::block_source(block_size, &m_table_resource_monitor));
573 block = &m_state->current_block;
574 ++m_state->number_of_blocks;
576 block = &m_state->current_block;
579 m_table_resource_monitor.increase(n_bytes_requested);
582 reinterpret_cast<T *
>(block->
allocate(n_bytes_requested).
data());
583 assert(
reinterpret_cast<uintptr_t
>(chunk_data) %
alignof(T) == 0);
587template <
class T,
class AllocationScheme>
590 assert(
reinterpret_cast<uintptr_t
>(chunk_data) %
alignof(T) == 0);
592 if (chunk_data ==
nullptr) {
596 const size_t n_bytes_requested = n_elements *
sizeof(T);
599 const auto remaining_chunks =
600 block.deallocate(
Chunk(chunk_data), n_bytes_requested);
601 if (remaining_chunks == 0) {
602 if (m_shared_block && (block == *m_shared_block)) {
605 assert(m_state->number_of_blocks > 0);
611 if (block == m_state->current_block) {
612 m_state->current_block.destroy();
616 --m_state->number_of_blocks;
619 m_table_resource_monitor.decrease(n_bytes_requested);
622template <
class T,
class AllocationScheme>
623template <
class U,
class... Args>
625 new (
mem)
U(std::forward<Args>(args)...);
628template <
class T,
class AllocationScheme>
634template <
class T,
class AllocationScheme>
Block abstraction for temptable-allocator.
Chunk abstraction for temptable Block allocator.
Custom memory allocator.
Definition: allocator.h:389
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:495
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:624
T & reference
Definition: allocator.h:398
const T * const_pointer
Definition: allocator.h:397
void deallocate(T *ptr, size_t n_elements)
Free a memory allocated by allocate().
Definition: allocator.h:588
const T & const_reference
Definition: allocator.h:399
std::shared_ptr< AllocatorState > m_state
Shared state between all the copies and rebinds of this allocator.
Definition: allocator.h:488
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:505
T * pointer
Definition: allocator.h:392
T * allocate(size_t n_elements)
Allocate memory for storing n_elements number of elements.
Definition: allocator.h:543
size_t size_type
Definition: allocator.h:401
void destroy(U *p)
Destroy an object of type U.
Definition: allocator.h:630
static void init()
Initialize necessary structures.
Definition: allocator.h:635
bool operator!=(const Allocator< U > &rhs) const
Inequality operator.
Definition: allocator.h:537
TableResourceMonitor & m_table_resource_monitor
Table resource monitor control mechanism that limits the amount of resources that can be consumed at ...
Definition: allocator.h:499
bool operator==(const Allocator< U > &rhs) const
Equality operator.
Definition: allocator.h:530
ptrdiff_t difference_type
Definition: allocator.h:402
T value_type
Definition: allocator.h:400
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:162
static constexpr size_t ALIGN_TO
Block will self-adjust all requested allocation-sizes to the multiple of this value.
Definition: block.h:166
Chunk allocate(size_t chunk_size) noexcept
Allocate a Chunk from a Block.
Definition: block.h:350
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:447
Chunk is an abstraction with the purpose of representing a smallest logical memory-unit within the Bl...
Definition: chunk.h:67
uint8_t * data() const
Get the pointer to the data section which will be provided to the end-user.
Definition: chunk.h:157
Definition: allocator.h:127
TableResourceMonitor(size_t threshold)
Definition: allocator.h:129
size_t m_total_bytes
Definition: allocator.h:148
size_t increase(size_t bytes)
Definition: allocator.h:132
size_t decrease(size_t bytes)
Definition: allocator.h:138
size_t m_threshold
Definition: allocator.h:147
size_t threshold()
Definition: allocator.h:143
size_t consumption()
Definition: allocator.h:144
const char * p
Definition: ctype-mb.cc:1236
#define U
Definition: ctype-tis620.cc:74
Memory utilities for temptable-allocator.
#define DBUG_EXECUTE_IF(keyword, a1)
Definition: my_dbug.h:170
Common header for many mysys elements.
ulonglong temptable_max_ram
Definition: mysqld.cc:1159
bool temptable_use_mmap
Definition: mysqld.cc:1161
ulonglong temptable_max_mmap
Definition: mysqld.cc:1160
Definition: varlen_sort.h:183
Definition: allocator.h:44
constexpr size_t ALLOCATOR_MAX_BLOCK_MB_EXP
log2(allocator max block size in MiB).
Definition: constants.h:59
void Block_PSI_init()
Initialize the PSI memory engine.
Definition: block.cc:73
constexpr size_t ALLOCATOR_MAX_BLOCK_BYTES
Limit on the size of a block created by Allocator (in bytes).
Definition: constants.h:64
Source
Type of memory allocated.
Definition: memutils.h:67
@ 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:2588
static MEM_ROOT mem
Definition: sql_servers.cc:98
Definition: allocator.h:191
static size_t block_size(size_t number_of_blocks, size_t n_bytes_requested)
Definition: allocator.h:197
static Source block_source(size_t block_size, TableResourceMonitor *table_resource_monitor)
Definition: allocator.h:192
Shared state between all instances of a given allocator.
Definition: allocator.h:338
Block current_block
Current not-yet-full block to feed allocations from.
Definition: allocator.h:340
size_t number_of_blocks
Number of created blocks so far (by this Allocator object).
Definition: allocator.h:347
Definition: allocator.h:405
Allocator< U, AllocationScheme > other
Definition: allocator.h:406
Definition: allocator.h:291
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:299
Definition: allocator.h:81
static size_t increase(size_t bytes)
Log increments of MMAP-backed memory consumption.
Definition: allocator.h:86
static size_t threshold()
Get MMAP-backed memory threshold level.
Definition: allocator.h:102
static size_t decrease(size_t bytes)
Log decrements of MMAP-backed memory consumption.
Definition: allocator.h:94
static size_t consumption()
Get current level of MMAP-backed memory consumption.
Definition: allocator.h:112
Definition: allocator.h:54
static size_t consumption()
Get current level of heap-memory consumption.
Definition: allocator.h:78
static size_t decrease(size_t bytes)
Log decrements of heap-memory consumption.
Definition: allocator.h:67
static size_t increase(size_t bytes)
Log increments of heap-memory consumption.
Definition: allocator.h:59
static size_t threshold()
Get heap-memory threshold level.
Definition: allocator.h:74
Definition: allocator.h:53
static std::atomic< size_t > mmap
Definition: allocator.h:118
static std::atomic< size_t > ram
Total bytes allocated so far by all threads in RAM/MMAP.
Definition: allocator.h:117
Definition: allocator.h:254
static Source block_source(uint32_t block_size, TableResourceMonitor *table_resource_monitor)
Definition: allocator.h:255
Definition: allocator.h:214
static Source block_source(uint32_t block_size, TableResourceMonitor *=nullptr)
Definition: allocator.h:215