30#ifndef INCLUDE_MY_ALLOC_H_
31#define INCLUDE_MY_ALLOC_H_
47#if defined(MYSQL_SERVER)
96#if defined(MYSQL_SERVER)
115 other.m_current_block =
nullptr;
116 other.m_allocated_size = 0;
125 ::new (
this)
MEM_ROOT(std::move(other));
178 template <
class T,
class... Args>
180 static_assert(
alignof(T) <= 8,
"MEM_ROOT only returns 8-aligned memory.");
181 if (num *
sizeof(T) < num) {
185 T *ret =
static_cast<T *
>(
Alloc(num *
sizeof(T)));
186 if (ret ==
nullptr) {
192 for (
size_t i = 0; i < num; ++i) {
193 new (&ret[i]) T(args...);
204 void Claim(
bool claim);
317 std::pair<char *, char *>
Peek()
const {
354 block = block->
prev) {
355 if (ptr >= block && ptr < block->
end) {
436 const std::nothrow_t &arg
437 [[maybe_unused]] = std::nothrow)
noexcept {
442 const std::nothrow_t &arg
443 [[maybe_unused]] = std::nothrow)
noexcept {
448 const std::nothrow_t &)
noexcept {
453 const std::nothrow_t &)
noexcept {
459 if (ptr !=
nullptr) {
461 TRASH(
const_cast<std::remove_const_t<T> *
>(ptr),
sizeof(T));
467 static_assert(!std::is_pointer<T>::value,
468 "You're trying to destroy an array of pointers, "
469 "not an array of objects. This is probably not "
470 "what you intended.");
471 if (ptr !=
nullptr) {
490template <
typename T,
typename... Args>
494 T(std::forward<Args>(args)...));
Definition: my_alloc.h:481
void operator()(T *ptr) const
Definition: my_alloc.h:483
static MEM_ROOT mem_root
Definition: client_plugin.cc:113
static bool report_error(THD *thd, int error_code, Sql_condition::enum_severity_level level, Args... args)
Definition: error_handler.cc:290
unsigned int PSI_memory_key
Instrumented memory key.
Definition: psi_memory_bits.h:48
static void start(mysql_harness::PluginFuncEnv *env)
Definition: http_auth_backend_plugin.cc:176
#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:70
void destroy(T *ptr)
Definition: my_alloc.h:458
void destroy_array(T *ptr, size_t count)
Definition: my_alloc.h:466
unique_ptr_destroy_only< T > make_unique_destroy_only(MEM_ROOT *mem_root, Args &&... args)
Definition: my_alloc.h:491
void sql_alloc_error_handler()
Definition: mysql_tzinfo_to_sql.cc:330
std::unique_ptr< T, Destroy_only< T > > unique_ptr_destroy_only
std::unique_ptr, but only destroying.
Definition: my_alloc.h:488
Header for compiler-dependent features.
#define DBUG_EXECUTE_IF(keyword, a1)
Definition: my_dbug.h:170
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:35
static int count
Definition: myisam_ftdump.cc:44
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:75
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:191
Performance schema instrumentation interface.
required string key
Definition: replication_asynchronous_connection_failover.proto:59
Definition: my_alloc.h:84
char * end
Previous block; used for freeing.
Definition: my_alloc.h:86
Block * prev
Definition: my_alloc.h:85
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:82
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:352
void Claim(bool claim)
Claim all the allocated memory for the current thread in the performance schema.
Definition: my_alloc.cc:221
size_t m_block_size
Size of the next block we intend to allocate.
Definition: my_alloc.h:395
char * m_current_free_end
End (exclusive) of the current free block.
Definition: my_alloc.h:392
void set_block_size(size_t block_size)
Set the desired size of the next block to be allocated.
Definition: my_alloc.h:291
size_t get_max_capacity() const
Return maximum capacity for this MEM_ROOT.
Definition: my_alloc.h:249
size_t allocated_size() const
Amount of memory we have allocated from the operating system, not including overhead.
Definition: my_alloc.h:284
void RawCommit(size_t length)
Mark the first N bytes as the current block as used.
Definition: my_alloc.h:340
Block * m_current_block
The current block we are giving out memory from.
Definition: my_alloc.h:386
bool ForceNewBlock(size_t minimum_length)
Allocate a new block of at least “minimum_length” bytes; usually more.
Definition: my_alloc.cc:152
void Clear()
Deallocate all the RAM used.
Definition: my_alloc.cc:170
MEM_ROOT()
Definition: my_alloc.h:90
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:179
MEM_ROOT(PSI_memory_key key, size_t block_size)
Definition: my_alloc.h:92
PSI_memory_key m_psi_key
Definition: my_alloc.h:418
MEM_ROOT & operator=(MEM_ROOT &&other) noexcept
Definition: my_alloc.h:123
void ClearForReuse()
Similar to Clear(), but anticipates that the block will be reused for further allocations.
Definition: my_alloc.cc:188
size_t m_orig_block_size
The original block size the user asked for on construction.
Definition: my_alloc.h:398
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:107
void set_error_handler(void(*error_handler)(void))
Set the error handler on memory allocation failure (or nullptr for none).
Definition: my_alloc.h:276
size_t m_allocated_size
Total allocated size for this MEM_ROOT.
Definition: my_alloc.h:411
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:57
size_t m_max_capacity
Maximum amount of memory this MEM_ROOT can hold.
Definition: my_alloc.h:404
~MEM_ROOT()
Definition: my_alloc.h:129
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:267
bool m_error_for_capacity_exceeded
If enabled, exceeding the capacity will lead to a my_error() call.
Definition: my_alloc.h:414
char * m_current_free_start
Start (inclusive) of the current free block.
Definition: my_alloc.h:389
void set_error_for_capacity_exceeded(bool report_error)
Enable/disable error reporting for exceeding the maximum capacity.
Definition: my_alloc.h:259
void * Alloc(size_t length)
Allocate memory.
Definition: my_alloc.h:144
static void FreeBlocks(Block *start)
Free all blocks in a linked list, starting at the given block.
Definition: my_alloc.cc:210
static char s_dummy_target
Something to point on that exists solely to never return nullptr from Alloc(0).
Definition: my_alloc.h:369
bool inited() const
Whether the constructor has run or not.
Definition: my_alloc.h:235
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:244
void(* m_error_handler)(void)
Definition: my_alloc.h:416
std::pair< char *, char * > Peek() const
Get the bounds of the currently allocated memory block.
Definition: my_alloc.h:317