Skip to content

Commit

Permalink
Support x64 for Doug Lea allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
Xottab-DUTY committed Jan 7, 2018
1 parent f4b8c2d commit e38762d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 43 deletions.
12 changes: 6 additions & 6 deletions src/xrCore/Memory/doug_lea_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ static void __stdcall out_of_memory(mspace const space, void const* const parame
xrDebug::Fatal(DEBUG_INFO, "not enough memory for arena [%s]", allocator->get_arena_id());
}

doug_lea_allocator::doug_lea_allocator(void* arena, u32 arena_size, pcstr arena_id) : m_arena_id(arena_id)
doug_lea_allocator::doug_lea_allocator(void* arena, size_t arena_size, pcstr arena_id) : m_arena_id(arena_id)
{
VERIFY(m_arena_id);

if (arena && arena_size)
m_dl_arena = create_mspace_with_base(arena, arena_size, 0, &out_of_memory, (void*)this);
else
m_dl_arena = create_mspace(0, 0, 0, 0);
m_dl_arena = create_mspace(0, 0, nullptr, nullptr);
}

doug_lea_allocator::~doug_lea_allocator()
Expand All @@ -39,17 +39,17 @@ doug_lea_allocator::~doug_lea_allocator()
destroy_mspace(m_dl_arena);
}

void* doug_lea_allocator::malloc_impl(u32 size) { return mspace_malloc(m_dl_arena, size); }
void* doug_lea_allocator::realloc_impl(void* pointer, u32 new_size)
void* doug_lea_allocator::malloc_impl(size_t size) { return mspace_malloc(m_dl_arena, size); }
void* doug_lea_allocator::realloc_impl(void* pointer, size_t new_size)
{
return mspace_realloc(m_dl_arena, pointer, new_size);
}

void doug_lea_allocator::free_impl(void*& pointer)
{
mspace_free(m_dl_arena, pointer);
pointer = 0;
pointer = nullptr;
}

u32 doug_lea_allocator::get_allocated_size() const { return (u32)mspace_mallinfo(m_dl_arena).uordblks; }
size_t doug_lea_allocator::get_allocated_size() const { return mspace_mallinfo(m_dl_arena).uordblks; }
#endif // USE_DOUG_LEA_ALLOCATOR
69 changes: 34 additions & 35 deletions src/xrCore/Memory/doug_lea_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
class XRCORE_API doug_lea_allocator
{
public:
doug_lea_allocator(void* arena, u32 arena_size, pcstr arena_id);
doug_lea_allocator(void* arena, size_t arena_size, pcstr arena_id);
~doug_lea_allocator();
void* malloc_impl(u32 size);
void* realloc_impl(void* pointer, u32 new_size);
void free_impl(void*& pointer);
u32 get_allocated_size() const;
void* malloc_impl(size_t size);
void* realloc_impl(void* pointer, size_t new_size);
void free_impl(void* pointer);
size_t get_allocated_size() const;
pcstr get_arena_id() const { return m_arena_id; }
template <typename T>
void free_impl(T*& pointer)
Expand All @@ -23,7 +23,7 @@ class XRCORE_API doug_lea_allocator
}

template <typename T>
T* alloc_impl(u32 const count)
T* alloc_impl(const size_t count)
{
return (T*)malloc_impl(count * sizeof(T));
}
Expand All @@ -33,74 +33,73 @@ class XRCORE_API doug_lea_allocator
void* m_dl_arena;
};

extern doug_lea_allocator g_common_allocator;
extern doug_lea_allocator g_common_doug_lea_allocator;

template <class T, doug_lea_allocator& _impl = common>
template <class T, doug_lea_allocator& _impl = g_common_doug_lea_allocator>
class doug_lea_alloc
{
constexpr static doug_lea_allocator& impl = _impl;

public:
using size_type = size_t;
using difference_type = ptrdiff_t;
using pointer = T * ;
using pointer = T*;
using const_pointer = const T*;
using reference = T & ;
using reference = T&;
using const_reference = const T&;
using value_type = T;

template <class _Other>
template <class Other>
struct rebind
{
using other = doug_lea_alloc<_Other>;
using other = doug_lea_alloc<Other>;
};

doug_lea_alloc() {}
doug_lea_alloc(const doug_lea_alloc<T>&) {}
doug_lea_alloc() = default;
doug_lea_alloc(const doug_lea_alloc<T, _impl>&) = default;

template <class _Other>
doug_lea_alloc(const doug_lea_alloc<_Other>&) {}
template <class Other>
doug_lea_alloc(const doug_lea_alloc<Other>&) {}

template <class _Other>
doug_lea_alloc<T>& operator=(const doug_lea_alloc<_Other>&)
template <class Other>
doug_lea_alloc<T>& operator=(const doug_lea_alloc<Other>&)
{
return *this;
}

pointer address(reference _Val) const { return (&_Val); }
const_pointer address(const_reference _Val) const { return (&_Val); }
pointer address(reference ref) const { return &ref; }
const_pointer address(const_reference ref) const { return &ref; }

pointer allocate(size_type n, const void* /*p*/ = nullptr) const
{
return static_cast<T*>(impl.malloc_impl(sizeof(T) * (u32)n));
return static_cast<pointer>(impl.malloc_impl(sizeof(T) * n));
}

void deallocate(pointer p, size_type) const { impl.free_impl((void*&)p); }
void deallocate(void* p, size_type n) const { impl.free_impl(p); }
char* __charalloc(size_type n) { return (char*)allocate(n); }
void construct(pointer p, const T& _Val) { new(p) T(_Val); }
void deallocate(pointer p, size_type /*n*/) const { impl.free_impl((void*&)p); }
void deallocate(void* p, size_type /*n*/) const { impl.free_impl(p); }
void construct(pointer p, const T& ref) { new(p) T(ref); }
void destroy(pointer p) { p->~T(); }

size_type max_size() const
{
constexpr size_type _Count = static_cast<size_type>(-1) / sizeof(T);
return 0 < _Count ? _Count : 1;
constexpr auto count = std::numeric_limits<size_type>::max() / sizeof(T);
return 0 < count ? count : 1;
}
};

template <class _Ty, class _Other>
bool operator==(const doug_lea_alloc<_Ty>&, const doug_lea_alloc<_Other>&)
template <class T, class Other>
bool operator==(const doug_lea_alloc<T>&, const doug_lea_alloc<Other>&)
{
return (true);
return true;
}

template <class _Ty, class _Other>
bool operator!=(const doug_lea_alloc<_Ty>&, const doug_lea_alloc<_Other>&)
template <class T, class Other>
bool operator!=(const doug_lea_alloc<T>&, const doug_lea_alloc<Other>&)
{
return (false);
return false;
}

template <doug_lea_allocator& _impl = common>
template <doug_lea_allocator& _impl = g_common_doug_lea_allocator>
struct doug_lea_allocator_wrapper
{
constexpr static doug_lea_allocator& impl = _impl;
Expand All @@ -111,7 +110,7 @@ struct doug_lea_allocator_wrapper
using result = doug_lea_alloc<T, _impl>;
};

static void* alloc(const u32& n) { return impl.malloc_impl((u32)n); }
static void* alloc(const size_t& n) { return impl.malloc_impl(n); }

template <typename T>
static void dealloc(T*& p)
Expand Down
4 changes: 2 additions & 2 deletions src/xrMisc/xrMisc_xrMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ void operator delete[](void* p) throw() { Memory.mem_free(p); }
#ifdef USE_ARENA_ALLOCATOR
constexpr static const u32 s_arena_size = 256 * 1024 * 1024;
static char s_fake_array[s_arena_size];
doug_lea_allocator g_common_allocator(s_fake_array, s_arena_size, "common");
doug_lea_allocator g_common_doug_lea_allocator(s_fake_array, s_arena_size, "common");
#else
doug_lea_allocator g_common_allocator(nullptr, 0, "common");
doug_lea_allocator g_common_doug_lea_allocator(nullptr, 0, "common");
#endif
#endif // USE_DOUG_LEA_ALLOCATOR

0 comments on commit e38762d

Please sign in to comment.