Region

High-level region management utilities.

Overview

Typical use cases:

  • Managing memory regions for allocation and deallocation.

  • Providing region merging and splitting functionalities.

Header

<RaeptorCogs/Region.hpp>

Metadata

Author

Estorc

Version

v1.0

Copyright

Copyright (c) 2025 Estorc MIT License.

Types

Type Definitions

Type

Description

RaeptorCogs::Region

Region type representing an interval [begin, end).

using RaeptorCogs::Region = std::pair<size_t, size_t>

Region type representing an interval [begin, end).

Used by RegionBuffer and RegionAllocator for managing memory regions.

RaeptorCogs::Region region = std::make_pair(0, 1024); // begin = 0, end = 1024

Classes

Classes

Class

Description

RaeptorCogs::RegionAllocator

RegionAllocator class.

RaeptorCogs::RegionBuffer

RegionBuffer class.

class RegionAllocator : public RaeptorCogs::RegionBuffer

RegionAllocator class.

Allocates and frees memory regions using an underlying RegionBuffer.

RaeptorCogs::RegionAllocator allocator;
size_t offset = allocator.allocate(256);
allocator.free(offset, offset + 256);

Note

RegionAllocator main goal is to save memory fragmentation by reusing freed regions.

Public Functions

inline size_t allocate(size_t size)

Allocate a region of the specified size.

Note

Allocates the first fitting region found.

Parameters:

size – The size of the region to allocate.

Returns:

size_t The beginning offset of the allocated region, or SIZE_MAX if allocation failed.

inline void free(size_t begin, size_t end)

Free a previously allocated region.

Note

Merges with existing regions automatically.

Parameters:
  • begin – The beginning of the region to free.

  • end – The end of the region to free.

class RegionBuffer

RegionBuffer class.

Manages a collection of memory regions, allowing for merging, splitting, and retrieval.

RaeptorCogs::RegionBuffer buffer;
buffer.push(0, 512);
buffer.push(1024, 2048);

Subclassed by RaeptorCogs::RegionAllocator

Public Functions

RegionBuffer() = default

Default constructor for RegionBuffer.

~RegionBuffer() = default

Destructor for RegionBuffer.

inline void push(size_t begin, size_t end)

Push a new region into the buffer.

Note

Merges overlapping or adjacent regions automatically.

Parameters:
  • begin – The beginning of the region.

  • end – The end of the region.

inline void clear()

Clear all regions from the buffer.

Note

Resets the RegionBuffer to an empty state.

inline void erase(size_t begin, size_t end)

Erase a region from the buffer.

Note

Splits regions if necessary.

Parameters:
  • begin – The beginning of the region to erase.

  • end – The end of the region to erase.

inline Region get()

Get the last region in the buffer.

Throws:

std::runtime_error – if no regions are available.

Returns:

Region The last region.

inline size_t size() const

Get the number of regions in the buffer.

Returns:

size_t The number of regions.

inline bool empty() const

Check if the buffer is empty.

Returns:

true if empty, false otherwise.

inline auto begin()

Iterator support for RegionBuffer.

inline auto end()

Iterator support for RegionBuffer.

inline auto begin() const

Const iterator support for RegionBuffer.

inline auto end() const

Const iterator support for RegionBuffer.

Private Members

std::vector<Region> regions

Underlying storage for the regions.

Stores a vector of Region pairs.