GroveEngine/modules/BgfxRenderer/Frame/FrameAllocator.h
StillHammer d63d8d83fa feat: Add BgfxRenderer module skeleton
- Add complete BgfxRenderer module structure (24 files)
- RHI abstraction layer (no bgfx:: exposed outside BgfxDevice.cpp)
- Frame system with lock-free allocator
- RenderGraph with ClearPass, SpritePass, DebugPass
- SceneCollector for IIO message parsing (render:* topics)
- ResourceCache with thread-safe texture/shader caching
- Full IModule integration (config via IDataNode, comm via IIO)
- CMake with FetchContent for bgfx
- Windows build script (build_renderer.bat)
- Documentation (README.md, USER_GUIDE.md, PLAN_BGFX_RENDERER.md)
- Updated .gitignore for Windows builds

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-26 00:41:55 +08:00

61 lines
1.6 KiB
C++

#pragma once
#include <atomic>
#include <cstdint>
#include <cstddef>
#include <new>
namespace grove {
// ============================================================================
// Frame Allocator - Lock-free linear allocator, reset each frame
// ============================================================================
class FrameAllocator {
public:
static constexpr size_t DEFAULT_SIZE = 16 * 1024 * 1024; // 16 MB
explicit FrameAllocator(size_t size = DEFAULT_SIZE);
~FrameAllocator();
// Non-copyable
FrameAllocator(const FrameAllocator&) = delete;
FrameAllocator& operator=(const FrameAllocator&) = delete;
// Thread-safe, lock-free allocation
void* allocate(size_t size, size_t alignment = 16);
// Typed allocation with constructor
template<typename T, typename... Args>
T* allocate(Args&&... args) {
void* ptr = allocate(sizeof(T), alignof(T));
if (!ptr) return nullptr;
return new (ptr) T(std::forward<Args>(args)...);
}
// Array allocation
template<typename T>
T* allocateArray(size_t count) {
void* ptr = allocate(sizeof(T) * count, alignof(T));
if (!ptr) return nullptr;
for (size_t i = 0; i < count; ++i) {
new (static_cast<T*>(ptr) + i) T();
}
return static_cast<T*>(ptr);
}
// Reset (called once per frame, single-thread)
void reset();
// Stats
size_t getUsed() const;
size_t getCapacity() const { return m_capacity; }
private:
uint8_t* m_buffer;
size_t m_capacity;
std::atomic<size_t> m_offset;
};
} // namespace grove