cd /news/developer-tools/custom-memory-resource-implementatio… · home topics developer-tools article
[ARTICLE · art-9900] src=gist.github.com ↗ pub= topic=developer-tools verified=true sentiment=· neutral

Custom memory_resource implementation with an atomic monotonic buffer allocator supporting aligned, lock-free allocations on a preallocated memory region.

This article describes a custom C++ memory resource implementation called `monotonic_atomic_buffer`, which provides lock-free, aligned memory allocations from a preallocated buffer using atomic operations. The allocator uses a monotonic bump-pointer strategy with atomic compare-and-exchange to safely manage concurrent allocations without mutexes, and it never deallocates individual allocations. The implementation is designed for high-performance scenarios requiring thread-safe allocation from a fixed-size memory region.

read2 min views16 publishedMay 22, 2026

monotonic_atomic_buffer.hpp

  This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.

Learn more about bidirectional Unicode characters

Show hidden characters
// Copyright (c) May 2026 Félix-Olivier Dumas. All rights reserved.

// Licensed under the terms described in the LICENSE file

#pragma once

#include <cstddef>

#include <atomic>

#include <stdexcept>

namespace exotic::memory {

class memory_resource {

public:

virtual ~memory_resource() = default;

public:

    [[nodiscard]] void* allocate(std::size_t bytes, std::size_t alignment) noexcept {

        if (alignment == 0 || (alignment & (alignment - 1)) != 0) [[unlikely]]

            throw std::invalid_argument("alignment must be a power of two");

        return do_allocate(bytes, alignment);

    }

    void deallocate(void* p, std::size_t bytes, std::size_t alignment) noexcept {

        if (alignment == 0 || (alignment & (alignment - 1)) != 0) [[unlikely]]

            throw std::invalid_argument("alignment must be a power of two");

        do_deallocate(p, bytes, alignment);

    }

    bool is_equal(const memory_resource& other) const noexcept {

        return do_is_equal(other);

    }

protected:

    virtual void* do_allocate(std::size_t bytes, std::size_t alignment) = 0;

    virtual void do_deallocate(void* ptr, std::size_t bytes, std::size_t alignment) = 0;

    virtual bool do_is_equal(const memory_resource& other) const noexcept = 0;

};

struct monotonic_atomic_buffer : public memory_resource {

public:

    explicit monotonic_atomic_buffer(std::size_t p_capacity)

        : capacity_(p_capacity)

        , offset_(0)

        , buffer_(new std::byte[p_capacity])

    {}

    monotonic_atomic_buffer(const monotonic_atomic_buffer&) = delete;

    monotonic_atomic_buffer& operator=(const monotonic_atomic_buffer&) = delete;

    ~monotonic_atomic_buffer() noexcept override {

        delete[] buffer_;

    }

protected:

    void* do_allocate(std::size_t bytes, std::size_t alignment) override {

        std::size_t current = offset_.load(std::memory_order_relaxed);

        while (true) {

            std::size_t aligned = (current + (alignment - 1)) & ~(alignment - 1);

            std::size_t next = aligned + bytes;

            if (next > capacity_)

                return nullptr;

            if (offset_.compare_exchange_weak(

                current,

                next,

                std::memory_order_relaxed)) {

                return buffer_ + aligned;

            }

        }

    }

    void do_deallocate(void*, std::size_t, std::size_t) override {}

    bool do_is_equal(const memory_resource& other) const noexcept override {

        return this == &other;

    }

public:

    [[nodiscard]] constexpr std::size_t capacity() const noexcept { return capacity_; }

    [[nodiscard]] std::size_t size() const noexcept { return offset_.load(std::memory_order_relaxed); }

private:

    std::byte* buffer_;

    std::atomic<std::size_t> offset_;

    std::size_t capacity_;

};

}
── more in #developer-tools 4 stories · sorted by recency
── more on @félix-olivier dumas 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/custom-memory-resour…] indexed:0 read:2min 2026-05-22 ·