From 89e094d3f832467cacb586f739bd3793c52d2803 Mon Sep 17 00:00:00 2001 From: cdemeyer-teachx Date: Fri, 29 Aug 2025 16:24:15 +0900 Subject: [PATCH] ability to enable/disable allocator --- include/nd-wfc/wfc_allocator.hpp | 68 +++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/include/nd-wfc/wfc_allocator.hpp b/include/nd-wfc/wfc_allocator.hpp index a74a911..fd64e04 100644 --- a/include/nd-wfc/wfc_allocator.hpp +++ b/include/nd-wfc/wfc_allocator.hpp @@ -10,7 +10,10 @@ #include #include +//#define WFC_USE_STACK_ALLOCATOR + inline void* allocate_aligned_memory(size_t alignment, size_t size) { +#ifdef WFC_USE_STACK_ALLOCATOR void* ptr = nullptr; #ifdef _MSC_VER @@ -30,9 +33,25 @@ inline void* allocate_aligned_memory(size_t alignment, size_t size) { #endif return ptr; +#else + // When not using stack allocator, use standard malloc with manual alignment + void* ptr = std::malloc(size + alignment - 1 + sizeof(void*)); + if (!ptr) return nullptr; + + void* aligned_ptr = static_cast(ptr) + sizeof(void*) + + (alignment - (reinterpret_cast(static_cast(ptr) + sizeof(void*)) % alignment)) % alignment; + + // Store original pointer for free + *(static_cast(aligned_ptr) - 1) = ptr; + + return aligned_ptr; +#endif } inline void free_aligned_memory(void* ptr) { + if (!ptr) return; + +#ifdef WFC_USE_STACK_ALLOCATOR #ifdef _MSC_VER _aligned_free(ptr); #elif defined(__GNUC__) || defined(__clang__) @@ -50,13 +69,19 @@ inline void free_aligned_memory(void* ptr) { free(ptr); #endif #endif +#else + // When not using stack allocator, free the original pointer + void* original_ptr = *(static_cast(ptr) - 1); + std::free(original_ptr); +#endif } namespace WFC { +#ifdef WFC_USE_STACK_ALLOCATOR /** * @brief Stack allocator specifically designed for WFC branching operations - * + * * This allocator uses a stack-based approach where memory is allocated in chunks * and automatically deallocated when branches go out of scope. It's optimized * for the recursive branching pattern used in the WFC algorithm. @@ -268,6 +293,47 @@ private: std::vector m_allocations; }; +#else // WFC_USE_STACK_ALLOCATOR not defined +/** + * @brief Simplified allocator using standard malloc/free + */ +class WFCStackAllocator { +public: + explicit WFCStackAllocator(size_t = 1024 * 1024) {} + ~WFCStackAllocator() = default; + + // Non-copyable, non-movable for consistency + WFCStackAllocator(const WFCStackAllocator&) = delete; + WFCStackAllocator& operator=(const WFCStackAllocator&) = delete; + WFCStackAllocator(WFCStackAllocator&&) = delete; + WFCStackAllocator& operator=(WFCStackAllocator&&) = delete; + + void* allocate(size_t size, size_t alignment = 8) { + return allocate_aligned_memory(alignment, size); + } + + void deallocate(void* ptr) { + free_aligned_memory(ptr); + } + + class StackFrame { + public: + StackFrame(WFCStackAllocator&) {} + ~StackFrame() = default; + StackFrame(const StackFrame&) = delete; + StackFrame& operator=(const StackFrame&) = delete; + StackFrame(StackFrame&&) = default; + StackFrame& operator=(StackFrame&&) = default; + }; + + StackFrame createFrame() { return StackFrame(*this); } + size_t getUsed() const { return 0; } + size_t getCapacity() const { return 0; } + size_t getAllocationCount() const { return 0; } + void reset() {} +}; +#endif // WFC_USE_STACK_ALLOCATOR + /** * @brief Custom allocator adapter for STL containers using WFCStackAllocator */