CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
110 lines
3.4 KiB
C++
110 lines
3.4 KiB
C++
// Copyright (c) 2026 Advanced Micro Devices, Inc.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
/// @file arena_alloc.h
|
|
/// @brief Fixed-size block pool allocator (free-list based).
|
|
///
|
|
/// Pre-allocates a contiguous buffer of N fixed-size blocks. Allocations
|
|
/// pop from a free-list in O(1); deallocations push back in O(1). When
|
|
/// the pool is exhausted or the requested allocation is larger than a
|
|
/// pool block, falls back to the global allocator.
|
|
///
|
|
/// Intended for use as a class-specific operator new/delete override to
|
|
/// eliminate malloc/free from hot paths. Thread-safety: NOT thread-safe.
|
|
/// Use one pool per thread.
|
|
|
|
#ifndef UTIL_ARENA_ALLOC_H_
|
|
#define UTIL_ARENA_ALLOC_H_
|
|
|
|
#include <cassert>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <new>
|
|
|
|
namespace util {
|
|
|
|
/// @brief Fixed-size block pool allocator.
|
|
/// @tparam BlockSize Size of each block in bytes.
|
|
/// @tparam NumBlocks Number of pre-allocated blocks.
|
|
/// @tparam BlockAlign Alignment of each block.
|
|
template <size_t BlockSize, size_t NumBlocks = 64, size_t BlockAlign = alignof(std::max_align_t)>
|
|
class ArenaAlloc {
|
|
static_assert(BlockSize >= sizeof(void *), "Block must hold a free-list pointer");
|
|
static_assert(NumBlocks > 0, "Must have at least one block");
|
|
|
|
public:
|
|
ArenaAlloc() { init_free_list(); }
|
|
|
|
/// @brief Allocate one block. O(1) from free-list; falls back to heap.
|
|
void *allocate(size_t size) {
|
|
if (size > BlockSize)
|
|
return ::operator new(size);
|
|
|
|
if (void *ptr = try_allocate(size))
|
|
return ptr;
|
|
// Pool exhausted — fall back to heap.
|
|
return ::operator new(size);
|
|
}
|
|
|
|
/// @brief Allocate one block without falling back to the global allocator.
|
|
///
|
|
/// @details Use this in C ABI paths that must report allocation failure as an
|
|
/// error code instead of allowing `operator new` to throw.
|
|
/// @returns Pointer to a block, or nullptr when the free-list is exhausted.
|
|
void *try_allocate(size_t size) {
|
|
if (size > BlockSize)
|
|
return nullptr;
|
|
if (free_head_ == nullptr)
|
|
return nullptr;
|
|
void *ptr = free_head_;
|
|
free_head_ = free_head_->next;
|
|
return ptr;
|
|
}
|
|
|
|
/// @brief Return a block to the free-list. O(1).
|
|
/// Heap-allocated overflow or oversized blocks are returned to the global
|
|
/// allocator because they are not backed by this pool's fixed-size buffer.
|
|
void deallocate(void *ptr) {
|
|
if (!owns(ptr)) {
|
|
::operator delete(ptr);
|
|
return;
|
|
}
|
|
|
|
auto *node = static_cast<FreeNode *>(ptr);
|
|
node->next = free_head_;
|
|
free_head_ = node;
|
|
}
|
|
|
|
/// @brief Check if a pointer was allocated from the pre-allocated buffer.
|
|
bool owns(const void *ptr) const {
|
|
auto addr = reinterpret_cast<std::uintptr_t>(ptr);
|
|
auto begin = reinterpret_cast<std::uintptr_t>(buffer_);
|
|
auto end = begin + sizeof(buffer_);
|
|
return addr >= begin && addr < end && ((addr - begin) % BlockSize) == 0;
|
|
}
|
|
|
|
static constexpr size_t BLOCK_SIZE = BlockSize;
|
|
static constexpr size_t NUM_BLOCKS = NumBlocks;
|
|
|
|
private:
|
|
struct FreeNode {
|
|
FreeNode *next;
|
|
};
|
|
|
|
void init_free_list() {
|
|
free_head_ = nullptr;
|
|
for (size_t i = NumBlocks; i > 0; --i) {
|
|
auto *node = reinterpret_cast<FreeNode *>(buffer_ + (i - 1) * BlockSize);
|
|
node->next = free_head_;
|
|
free_head_ = node;
|
|
}
|
|
}
|
|
|
|
alignas(BlockAlign) uint8_t buffer_[BlockSize * NumBlocks];
|
|
FreeNode *free_head_ = nullptr;
|
|
};
|
|
|
|
} // namespace util
|
|
|
|
#endif // UTIL_ARENA_ALLOC_H_
|