31#ifndef INCLUDE_MY_ALLOC_H_
32#define INCLUDE_MY_ALLOC_H_
48#if defined(MYSQL_SERVER)
97#if defined(MYSQL_SERVER)
116 other.m_current_block =
nullptr;
117 other.m_allocated_size = 0;
126 ::new (
this)
MEM_ROOT(std::move(other));
179 template <
class T,
class... Args>
181 static_assert(
alignof(T) <= 8,
"MEM_ROOT only returns 8-aligned memory.");
182 if (num *
sizeof(T) < num) {
186 T *ret =
static_cast<T *
>(
Alloc(num *
sizeof(T)));
187 if (ret ==
nullptr) {
193 for (
size_t i = 0; i < num; ++i) {
194 new (&ret[i]) T(args...);
205 void Claim(
bool claim);
318 std::pair<char *, char *>
Peek()
const {
355 block = block->
prev) {
356 if (ptr >= block && ptr < block->
end) {
437 const std::nothrow_t &arg
438 [[maybe_unused]] = std::nothrow)
noexcept {
443 const std::nothrow_t &arg
444 [[maybe_unused]] = std::nothrow)
noexcept {
449 const std::nothrow_t &)
noexcept {
454 const std::nothrow_t &)
noexcept {
460 assert(ptr !=
nullptr);
462 TRASH(
const_cast<std::remove_const_t<T> *
>(ptr),
sizeof(T));
479template <
typename T,
typename... Args>
483 T(std::forward<Args>(args)...));
Definition: my_alloc.h:470
void operator()(T *ptr) const
Definition: my_alloc.h:472
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
static bool report_error(THD *thd, int error_code, Sql_condition::enum_severity_level level, Args... args)
Definition: error_handler.cc:291
unsigned int PSI_memory_key
Instrumented memory key.
Definition: psi_memory_bits.h:49
static void start(mysql_harness::PluginFuncEnv *env)
Definition: http_auth_backend_plugin.cc:180
#define malloc(A)
Definition: lexyy.cc:914
Various macros useful for communicating with memory debuggers, such as Valgrind.
void TRASH(void *ptr, size_t length)
Put bad content in memory to be sure it will segfault if dereferenced.
Definition: memory_debugging.h:71
unique_ptr_destroy_only< T > make_unique_destroy_only(MEM_ROOT *mem_root, Args &&...args)
Definition: my_alloc.h:480
void destroy_at(T *ptr)
Definition: my_alloc.h:459
void sql_alloc_error_handler()
Definition: mysql_tzinfo_to_sql.cc:331
std::unique_ptr< T, Destroy_only< T > > unique_ptr_destroy_only
std::unique_ptr, but only destroying.
Definition: my_alloc.h:477
Header for compiler-dependent features.
#define DBUG_EXECUTE_IF(keyword, a1)
Definition: my_dbug.h:171
Some integer typedefs for easier portability.
Some macros for dealing with pointer arithmetic, e.g., aligning of buffers to a given size.
#define ALIGN_SIZE(A)
Definition: my_pointer_arithmetic.h:36
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
size_t size(const char *const c)
Definition: base64.h:46
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:192
Performance schema instrumentation interface.
required string key
Definition: replication_asynchronous_connection_failover.proto:60
Definition: my_alloc.h:85
char * end
Previous block; used for freeing.
Definition: my_alloc.h:87
Block * prev
Definition: my_alloc.h:86
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
bool Contains(void *ptr) const
Returns whether this MEM_ROOT contains the given pointer, ie., whether it was given back from Alloc(n...
Definition: my_alloc.h:353
void Claim(bool claim)
Claim all the allocated memory for the current thread in the performance schema.
Definition: my_alloc.cc:223
size_t m_block_size
Size of the next block we intend to allocate.
Definition: my_alloc.h:396
char * m_current_free_end
End (exclusive) of the current free block.
Definition: my_alloc.h:393
void set_block_size(size_t block_size)
Set the desired size of the next block to be allocated.
Definition: my_alloc.h:292
size_t get_max_capacity() const
Return maximum capacity for this MEM_ROOT.
Definition: my_alloc.h:250
size_t allocated_size() const
Amount of memory we have allocated from the operating system, not including overhead.
Definition: my_alloc.h:285
void RawCommit(size_t length)
Mark the first N bytes as the current block as used.
Definition: my_alloc.h:341
Block * m_current_block
The current block we are giving out memory from.
Definition: my_alloc.h:387
bool ForceNewBlock(size_t minimum_length)
Allocate a new block of at least 'minimum_length' bytes; usually more.
Definition: my_alloc.cc:154
void Clear()
Deallocate all the RAM used.
Definition: my_alloc.cc:172
MEM_ROOT()
Definition: my_alloc.h:91
T * ArrayAlloc(size_t num, Args... args)
Allocate 'num' objects of type T, and initialize them to a default value that is created by passing t...
Definition: my_alloc.h:180
MEM_ROOT(PSI_memory_key key, size_t block_size)
Definition: my_alloc.h:93
PSI_memory_key m_psi_key
Definition: my_alloc.h:419
MEM_ROOT & operator=(MEM_ROOT &&other) noexcept
Definition: my_alloc.h:124
void ClearForReuse()
Similar to Clear(), but anticipates that the block will be reused for further allocations.
Definition: my_alloc.cc:190
size_t m_orig_block_size
The original block size the user asked for on construction.
Definition: my_alloc.h:399
MEM_ROOT & operator=(const MEM_ROOT &)=delete
void * AllocSlow(size_t length)
Allocate memory that doesn't fit into the current free block.
Definition: my_alloc.cc:109
void set_error_handler(void(*error_handler)(void))
Set the error handler on memory allocation failure (or nullptr for none).
Definition: my_alloc.h:277
size_t m_allocated_size
Total allocated size for this MEM_ROOT.
Definition: my_alloc.h:412
Block * AllocBlock(size_t wanted_length, size_t minimum_length)
Allocate a new block of the given length (plus overhead for the block header).
Definition: my_alloc.cc:59
size_t m_max_capacity
Maximum amount of memory this MEM_ROOT can hold.
Definition: my_alloc.h:405
~MEM_ROOT()
Definition: my_alloc.h:130
bool get_error_for_capacity_exceeded() const
Return whether error is to be reported when maximum capacity exceeds for MEM_ROOT.
Definition: my_alloc.h:268
bool m_error_for_capacity_exceeded
If enabled, exceeding the capacity will lead to a my_error() call.
Definition: my_alloc.h:415
char * m_current_free_start
Start (inclusive) of the current free block.
Definition: my_alloc.h:390
void set_error_for_capacity_exceeded(bool report_error)
Enable/disable error reporting for exceeding the maximum capacity.
Definition: my_alloc.h:260
void * Alloc(size_t length)
Allocate memory.
Definition: my_alloc.h:145
static void FreeBlocks(Block *start)
Free all blocks in a linked list, starting at the given block.
Definition: my_alloc.cc:212
static char s_dummy_target
Something to point on that exists solely to never return nullptr from Alloc(0).
Definition: my_alloc.h:370
bool inited() const
Whether the constructor has run or not.
Definition: my_alloc.h:236
MEM_ROOT(const MEM_ROOT &)=delete
void set_max_capacity(size_t max_capacity)
Set maximum capacity for this MEM_ROOT.
Definition: my_alloc.h:245
void(* m_error_handler)(void)
Definition: my_alloc.h:417
std::pair< char *, char * > Peek() const
Get the bounds of the currently allocated memory block.
Definition: my_alloc.h:318