Memory#

Interface#

std::unique_ptr<MemoryPool> paimon::GetMemoryPool()#

Create a default implementation of memory pool.

Returns:

Unique pointer to a newly created MemoryPool instance.

std::shared_ptr<MemoryPool> paimon::GetDefaultPool()#

Get a system-wide singleton memory pool.

Returns:

Shared pointer to the singleton MemoryPool instance.

class MemoryPool#

Abstract base class for memory pool implementations that provides controlled memory management.

Public Functions

virtual ~MemoryPool() = default#
MemoryPool &operator=(const MemoryPool &other) = delete#
MemoryPool &operator=(MemoryPool &other) = delete#
MemoryPool &operator=(MemoryPool &&other) = delete#
virtual void *Malloc(uint64_t size, uint64_t alignment = 0) = 0#

Allocate memory from the pool.

Allocates a block of memory with the specified size and alignment. The returned memory is uninitialized and must be properly constructed before use.

Parameters:
  • size – Number of bytes to allocate.

  • alignment – Memory alignment requirement (0 for default alignment).

Returns:

Pointer to allocated memory, or nullptr on failure.

virtual void *Realloc(void *p, size_t old_size, size_t new_size, uint64_t alignment = 0) = 0#

Reallocate memory to a new size.

Changes the size of a previously allocated memory block. The contents of the memory block are preserved up to the minimum of the old and new sizes.

Parameters:
  • p – Pointer to the memory block to reallocate.

  • old_size – Current size of the memory block.

  • new_size – New desired size of the memory block.

  • alignment – Memory alignment requirement (0 for default alignment).

Returns:

Pointer to the reallocated memory block.

virtual void Free(void *p, uint64_t size) = 0#

Deallocate memory back to the pool.

Releases a previously allocated memory block back to the pool. The size must match the size used during allocation.

Parameters:
  • p – Pointer to the memory block to deallocate.

  • size – Size of the memory block (must match allocation size).

virtual uint64_t CurrentUsage() const = 0#

Get current memory usage.

Returns the amount of memory currently allocated from this pool. This includes all outstanding allocations that have not been freed.

Returns:

Current memory usage in bytes.

virtual uint64_t MaxMemoryUsage() const = 0#

Get peak memory usage.

Returns the maximum amount of memory that has been allocated from this pool at any point in time since its creation.

Returns:

Peak memory usage in bytes.

template<typename T, typename ...Args>
inline std::unique_ptr<T, AllocatorDelete<T>> AllocateUnique(Args&&... args)#

Allocate an object on the pool and return a unique_ptr with custom deleter.

This method provides a convenient way to allocate objects that are automatically managed by the memory pool. The returned unique_ptr will automatically deallocate the memory through the pool when it goes out of scope.

Template Parameters:
  • T – Type of object to allocate.

  • Args – Types of constructor arguments.

Parameters:

args – Constructor arguments to forward to T’s constructor.

Returns:

unique_ptr managing the allocated object with pool-aware deleter.

template<typename T>
class AllocatorDelete#

Custom deleter for use with std::unique_ptr that integrates with memory pools.

AllocatorDelete provides automatic memory deallocation through the memory pool when used with std::unique_ptr. It stores both the pool reference and the allocation size to ensure proper cleanup.

Template Parameters:

T – Type of object being managed.

Public Functions

inline AllocatorDelete()#
inline AllocatorDelete(MemoryPool &_pool, size_t _size)#
AllocatorDelete(AllocatorDelete const&) = default#
template<typename U>
inline AllocatorDelete &operator=(const AllocatorDelete<U> &other)#
inline AllocatorDelete &operator=(AllocatorDelete const &other)#
inline AllocatorDelete &operator=(AllocatorDelete &&other)#
template<typename U>
inline AllocatorDelete(const AllocatorDelete<U> &other)#
inline MemoryPool &GetPool() const#
inline size_t GetSize() const#
inline void operator()(T *p) const#
class Bytes#

A memory-managed byte array class that provides efficient storage and manipulation of binary data.

Bytes is a RAII wrapper around raw memory that integrates with Paimon’s memory pool system for efficient memory allocation and deallocation. It provides safe access to byte arrays with automatic memory management and supports move semantics for efficient transfers.

Public Functions

Bytes() = default#

Default constructor creating an empty Bytes object.

Creates a Bytes object with no allocated memory. The object will have null data pointer and zero size.

Bytes(size_t size, MemoryPool *pool)#

Constructor that allocates memory of specified size.

Allocates a new byte array of the given size using the provided memory pool. The allocated memory is uninitialized.

Parameters:
  • size – Number of bytes to allocate.

  • pool – Memory pool to use for allocation.

Bytes(const std::string &str, MemoryPool *pool)#

Constructor that creates a copy of a string.

Allocates memory and copies the contents of the provided string into the new Bytes object.

Parameters:
  • str – String to copy data from.

  • pool – Memory pool to use for allocation.

Bytes(const Bytes&) = delete#
void operator=(const Bytes&) = delete#
Bytes(Bytes&&) noexcept#
Bytes &operator=(Bytes&&) noexcept#
~Bytes()#
bool operator==(const Bytes &other) const#
bool operator<(const Bytes &other) const#
char &operator[](size_t idx) const#
int32_t compare(const Bytes &other) const#

Compare this Bytes object with another.

Returns:

Negative value if this < other; 0 if equal; positive value if this > other.

inline char *data() const#

Get pointer to the raw data.

Returns:

A pointer to the underlying byte array. The pointer remains valid as long as the Bytes object exists and is not moved from.

inline size_t size() const#

Get the size of the byte array.

Public Static Functions

static PAIMON_UNIQUE_PTR<Bytes> CopyOf(const Bytes &bytes, size_t size, MemoryPool *pool)#

Create a copy of existing Bytes with specified size.

Creates a new Bytes object by copying data from an existing Bytes object. The copy will have the specified size, which may be different from the source object’s size (truncation or padding may occur).

Parameters:
  • bytes – Source Bytes object to copy from.

  • size – Size of the new Bytes object.

  • pool – Memory pool to use for allocation.

Returns:

Unique pointer to the newly created Bytes object.

static PAIMON_UNIQUE_PTR<Bytes> AllocateBytes(int32_t length, MemoryPool *pool)#

Allocate a new Bytes object with specified length.

Factory method that creates a new Bytes object with uninitialized memory of the specified length.

Parameters:
  • length – Number of bytes to allocate.

  • pool – Memory pool to use for allocation.

Returns:

Unique pointer to the newly allocated Bytes object.

static PAIMON_UNIQUE_PTR<Bytes> AllocateBytes(const std::string &str, MemoryPool *pool)#

Allocate a new Bytes object from string data.

Factory method that creates a new Bytes object and copies the contents of the provided string into it.

Parameters:
  • str – String to copy data from.

  • pool – Memory pool to use for allocation.

Returns:

Unique pointer to the newly allocated Bytes object.