2022-02-23 23:08:28 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef TWO_MALLOCS_HPP
|
|
|
|
|
#define TWO_MALLOCS_HPP
|
|
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
|
|
|
|
|
|
// 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 T>
|
|
|
|
|
class EngAllocator
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
using value_type = T;
|
|
|
|
|
EngAllocator() noexcept {}
|
|
|
|
|
template <class U> EngAllocator(EngAllocator<U> const&) noexcept {}
|
|
|
|
|
|
|
|
|
|
value_type* allocate(std::size_t n)
|
|
|
|
|
{
|
|
|
|
|
return static_cast<value_type*>(dlmalloc(n*sizeof(value_type)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void deallocate(value_type* p, std::size_t) noexcept
|
|
|
|
|
{
|
|
|
|
|
dlfree(p);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
|
bool operator==(EngAllocator<T> const&, EngAllocator<U> const&) noexcept
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T, class U>
|
|
|
|
|
bool operator!=(EngAllocator<T> const&, EngAllocator<U> const&) noexcept
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 02:17:41 -05:00
|
|
|
// DrvAllocator: a class meant to be used as an STL Allocator.
|
|
|
|
|
// Causes objects to be allocated using malloc and free.
|
|
|
|
|
template <class T>
|
|
|
|
|
class DrvAllocator
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
using value_type = T;
|
|
|
|
|
DrvAllocator() noexcept {}
|
|
|
|
|
template <class U> DrvAllocator(DrvAllocator<U> const&) noexcept {}
|
2022-02-23 23:08:28 -05:00
|
|
|
|
2022-02-24 02:17:41 -05:00
|
|
|
value_type* allocate(std::size_t n)
|
|
|
|
|
{
|
|
|
|
|
return static_cast<value_type*>(dlmalloc(n*sizeof(value_type)));
|
|
|
|
|
}
|
2022-02-23 23:08:28 -05:00
|
|
|
|
2022-02-24 02:17:41 -05:00
|
|
|
void deallocate(value_type* p, std::size_t) noexcept
|
|
|
|
|
{
|
|
|
|
|
dlfree(p);
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-02-23 23:08:28 -05:00
|
|
|
|
2022-02-24 02:17:41 -05:00
|
|
|
template <class T, class U>
|
|
|
|
|
bool operator==(DrvAllocator<T> const&, DrvAllocator<U> const&) noexcept
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-02-23 23:08:28 -05:00
|
|
|
|
2022-02-24 02:17:41 -05:00
|
|
|
template <class T, class U>
|
|
|
|
|
bool operator!=(DrvAllocator<T> const&, DrvAllocator<U> const&) noexcept
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-02-23 23:08:28 -05:00
|
|
|
|
|
|
|
|
#endif // TWO_MALLOCS_HPP
|
|
|
|
|
|