ability to enable/disable allocator
This commit is contained in:
@@ -10,7 +10,10 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
//#define WFC_USE_STACK_ALLOCATOR
|
||||||
|
|
||||||
inline void* allocate_aligned_memory(size_t alignment, size_t size) {
|
inline void* allocate_aligned_memory(size_t alignment, size_t size) {
|
||||||
|
#ifdef WFC_USE_STACK_ALLOCATOR
|
||||||
void* ptr = nullptr;
|
void* ptr = nullptr;
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
@@ -30,9 +33,25 @@ inline void* allocate_aligned_memory(size_t alignment, size_t size) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
return ptr;
|
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<char*>(ptr) + sizeof(void*) +
|
||||||
|
(alignment - (reinterpret_cast<uintptr_t>(static_cast<char*>(ptr) + sizeof(void*)) % alignment)) % alignment;
|
||||||
|
|
||||||
|
// Store original pointer for free
|
||||||
|
*(static_cast<void**>(aligned_ptr) - 1) = ptr;
|
||||||
|
|
||||||
|
return aligned_ptr;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void free_aligned_memory(void* ptr) {
|
inline void free_aligned_memory(void* ptr) {
|
||||||
|
if (!ptr) return;
|
||||||
|
|
||||||
|
#ifdef WFC_USE_STACK_ALLOCATOR
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
_aligned_free(ptr);
|
_aligned_free(ptr);
|
||||||
#elif defined(__GNUC__) || defined(__clang__)
|
#elif defined(__GNUC__) || defined(__clang__)
|
||||||
@@ -50,13 +69,19 @@ inline void free_aligned_memory(void* ptr) {
|
|||||||
free(ptr);
|
free(ptr);
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
#else
|
||||||
|
// When not using stack allocator, free the original pointer
|
||||||
|
void* original_ptr = *(static_cast<void**>(ptr) - 1);
|
||||||
|
std::free(original_ptr);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace WFC {
|
namespace WFC {
|
||||||
|
|
||||||
|
#ifdef WFC_USE_STACK_ALLOCATOR
|
||||||
/**
|
/**
|
||||||
* @brief Stack allocator specifically designed for WFC branching operations
|
* @brief Stack allocator specifically designed for WFC branching operations
|
||||||
*
|
*
|
||||||
* This allocator uses a stack-based approach where memory is allocated in chunks
|
* 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
|
* and automatically deallocated when branches go out of scope. It's optimized
|
||||||
* for the recursive branching pattern used in the WFC algorithm.
|
* for the recursive branching pattern used in the WFC algorithm.
|
||||||
@@ -268,6 +293,47 @@ private:
|
|||||||
std::vector<Block> m_allocations;
|
std::vector<Block> 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
|
* @brief Custom allocator adapter for STL containers using WFCStackAllocator
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user