OverviewFilesCommitsIssuesDocsReleasesPeers

dc-terrain / libdc / include / dc_pool.h

34 lines · c ref:
#ifndef DC_POOL_H
#define DC_POOL_H

#include <deque>
#include <cstddef>

// Simple arena allocator using std::deque for pointer stability.
// Unlike std::vector, deque never invalidates existing pointers on growth,
// which is critical when building trees with parent→child pointers.
template<typename T>
struct Pool {
    std::deque<T> storage;
    size_t next = 0;

    void reserve(size_t count) {
        storage.resize(count);
        next = 0;
    }

    T* alloc() {
        if (next >= storage.size()) {
            storage.resize(next + 64);
        }
        T* ptr = &storage[next++];
        *ptr = T{};
        return ptr;
    }

    void reset() { next = 0; }
    size_t used() const { return next; }
};

#endif // DC_POOL_H