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>
 
  194    return Block_source_policy::block_source(
block_size);
 
  196  static size_t block_size(
size_t number_of_blocks, 
size_t n_bytes_requested) {
 
  197    return Block_size_policy::block_size(number_of_blocks, n_bytes_requested);
 
  276  static size_t block_size(
size_t number_of_blocks, 
size_t n_bytes_requested) {
 
  277    size_t block_size_hint;
 
  279      block_size_hint = (1ULL << number_of_blocks) * 1_MiB;
 
  314template <
class AllocationScheme>
 
  348      const size_t block_size =
 
  351          Block(block_size, AllocationScheme::block_source(block_size));
 
  380    AllocationScheme::block_freed(block.size(), block.type());
 
  437                "T's with alignment-requirement larger than " 
  438                "Block::ALIGN_TO are not supported.");
 
  439  static_assert(
sizeof(T) > 0, 
"Zero sized objects are not supported");
 
  512  template <
class U, 
class... Args>
 
  534  std::shared_ptr<AllocatorState<AllocationScheme>> 
m_state;
 
  550template <
class T, 
class AllocationScheme>
 
  554      m_shared_block(shared_block),
 
  555      m_table_resource_monitor(table_resource_monitor) {}
 
  557template <
class T, 
class AllocationScheme>
 
  560    : m_state(other.m_state),
 
  561      m_shared_block(other.m_shared_block),
 
  562      m_table_resource_monitor(other.m_table_resource_monitor) {}
 
  564template <
class T, 
class AllocationScheme>
 
  567    : m_state(std::move(other.m_state)),
 
  568      m_shared_block(other.m_shared_block),
 
  569      m_table_resource_monitor(other.m_table_resource_monitor) {}
 
  571template <
class T, 
class AllocationScheme>
 
  574template <
class T, 
class AllocationScheme>
 
  581template <
class T, 
class AllocationScheme>
 
  585  return !(*
this == rhs);
 
  588template <
class T, 
class AllocationScheme>
 
  590  assert(n_elements <= std::numeric_limits<size_type>::max() / 
sizeof(T));
 
  595  const size_t n_bytes_requested = n_elements * 
sizeof(T);
 
  596  if (n_bytes_requested == 0) {
 
  602  if (m_shared_block && m_shared_block->is_empty()) {
 
  603    const size_t block_size =
 
  604        AllocationScheme::block_size(0, n_bytes_requested);
 
  606        Block(block_size, AllocationScheme::block_source(block_size));
 
  607    block = m_shared_block;
 
  608  } 
else if (m_shared_block &&
 
  609             m_shared_block->can_accommodate(n_bytes_requested)) {
 
  610    block = m_shared_block;
 
  612    block = m_state->get_block_for_new_allocation(n_bytes_requested);
 
  627  if (m_table_resource_monitor.consumption() + n_bytes_requested >
 
  628      m_table_resource_monitor.threshold()) {
 
  631  m_table_resource_monitor.increase(n_bytes_requested);
 
  634      reinterpret_cast<T *
>(block->
allocate(n_bytes_requested).
data());
 
  635  assert(
reinterpret_cast<uintptr_t
>(chunk_data) % 
alignof(T) == 0);
 
  639template <
class T, 
class AllocationScheme>
 
  642  assert(
reinterpret_cast<uintptr_t
>(chunk_data) % 
alignof(T) == 0);
 
  644  if (chunk_data == 
nullptr) {
 
  648  const size_t n_bytes_requested = n_elements * 
sizeof(T);
 
  651  const auto remaining_chunks =
 
  652      block.deallocate(
Chunk(chunk_data), n_bytes_requested);
 
  653  if (remaining_chunks == 0) {
 
  654    if (m_shared_block && (block == *m_shared_block)) {
 
  657      m_state->block_is_not_used_anymore(block);
 
  660  m_table_resource_monitor.decrease(n_bytes_requested);
 
  663template <
class T, 
class AllocationScheme>
 
  664template <
class U, 
class... Args>
 
  666  new (
mem) 
U(std::forward<Args>(args)...);
 
  669template <
class T, 
class AllocationScheme>
 
  675template <
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:315
 
Block * get_block_for_new_allocation(size_t n_bytes_requested)
Gets a Block from which a new allocation of the specified size should be performed.
Definition: allocator.h:336
 
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:362
 
size_t number_of_blocks
Number of created blocks so far (by this Allocator object).
Definition: allocator.h:393
 
Block current_block
Current not-yet-full block to feed allocations from.
Definition: allocator.h:386
 
void free_block(Block &block) noexcept
Frees the specified block and takes care of all accounting.
Definition: allocator.h:379
 
~AllocatorState() noexcept
Destroys the state, deallocate the current_block if it was left empty.
Definition: allocator.h:320
 
Custom memory allocator.
Definition: allocator.h:435
 
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:541
 
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:665
 
T & reference
Definition: allocator.h:444
 
const T * const_pointer
Definition: allocator.h:443
 
void deallocate(T *ptr, size_t n_elements)
Free a memory allocated by allocate().
Definition: allocator.h:640
 
const T & const_reference
Definition: allocator.h:445
 
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:551
 
T * pointer
Definition: allocator.h:438
 
T * allocate(size_t n_elements)
Allocate memory for storing n_elements number of elements.
Definition: allocator.h:589
 
size_t size_type
Definition: allocator.h:447
 
void destroy(U *p)
Destroy an object of type U.
Definition: allocator.h:671
 
static void init()
Initialize necessary structures.
Definition: allocator.h:676
 
bool operator!=(const Allocator< U > &rhs) const
Inequality operator.
Definition: allocator.h:583
 
TableResourceMonitor & m_table_resource_monitor
Table resource monitor control mechanism that limits the amount of resources that can be consumed at ...
Definition: allocator.h:545
 
bool operator==(const Allocator< U > &rhs) const
Equality operator.
Definition: allocator.h:576
 
ptrdiff_t difference_type
Definition: allocator.h:448
 
std::shared_ptr< AllocatorState< AllocationScheme > > m_state
Shared state between all the copies and rebinds of this allocator.
Definition: allocator.h:534
 
T value_type
Definition: allocator.h:446
 
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:1235
 
#define U
Definition: ctype-tis620.cc:74
 
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:1185
 
bool temptable_use_mmap
Definition: mysqld.cc:1187
 
ulonglong temptable_max_mmap
Definition: mysqld.cc:1186
 
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:2595
 
static MEM_ROOT mem
Definition: sql_servers.cc:100
 
Definition: allocator.h:192
 
static size_t block_size(size_t number_of_blocks, size_t n_bytes_requested)
Definition: allocator.h:196
 
static Source block_source(size_t block_size)
Definition: allocator.h:193
 
static void block_freed(uint32_t block_size, Source block_source)
Definition: allocator.h:199
 
Definition: allocator.h:451
 
Allocator< U, AllocationScheme > other
Definition: allocator.h:452
 
Definition: allocator.h:268
 
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:276
 
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:216
 
static Source block_source(uint32_t block_size)
Definition: allocator.h:217
 
static void block_freed(uint32_t block_size, Source block_source)
Definition: allocator.h:237