#ifndef TWO_MALLOCS_HPP #define TWO_MALLOCS_HPP #include // dlmalloc is only used on linux. extern "C" { #ifdef __linux__ void* dlmalloc(size_t x); void dlfree(void *p); void* dlrealloc(void*, size_t); #else void* dlmalloc(size_t x) { return malloc(x); } void dlfree(void *p) { free(p); } void* dlrealloc(void *p, size_t x) { return realloc(p,x); } #endif } // Return the current state of the dlmalloc allocator as a 30-bit hash. extern int dlmalloc_hash(); // EngAllocator: a class meant to be used as an STL Allocator. // Causes objects to be allocated using dlmalloc and dlfree. template class EngAllocator { public: using value_type = T; EngAllocator() noexcept {} template EngAllocator(EngAllocator const&) noexcept {} value_type* allocate(std::size_t n) { return static_cast(dlmalloc(n*sizeof(value_type))); } void deallocate(value_type* p, std::size_t) noexcept { dlfree(p); } }; template bool operator==(EngAllocator const&, EngAllocator const&) noexcept { return true; } template bool operator!=(EngAllocator const&, EngAllocator const&) noexcept { return false; } // DrvAllocator: a class meant to be used as an STL Allocator. // Causes objects to be allocated using malloc and free. template class DrvAllocator { public: using value_type = T; DrvAllocator() noexcept {} template DrvAllocator(DrvAllocator const&) noexcept {} value_type* allocate(std::size_t n) { return static_cast(dlmalloc(n*sizeof(value_type))); } void deallocate(value_type* p, std::size_t) noexcept { dlfree(p); } }; template bool operator==(DrvAllocator const&, DrvAllocator const&) noexcept { return true; } template bool operator!=(DrvAllocator const&, DrvAllocator const&) noexcept { return false; } #endif // TWO_MALLOCS_HPP