Skip to content

Commit e38762d

Browse files
committed
Support x64 for Doug Lea allocator
1 parent f4b8c2d commit e38762d

File tree

3 files changed

+42
-43
lines changed

3 files changed

+42
-43
lines changed

src/xrCore/Memory/doug_lea_allocator.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ static void __stdcall out_of_memory(mspace const space, void const* const parame
2323
xrDebug::Fatal(DEBUG_INFO, "not enough memory for arena [%s]", allocator->get_arena_id());
2424
}
2525

26-
doug_lea_allocator::doug_lea_allocator(void* arena, u32 arena_size, pcstr arena_id) : m_arena_id(arena_id)
26+
doug_lea_allocator::doug_lea_allocator(void* arena, size_t arena_size, pcstr arena_id) : m_arena_id(arena_id)
2727
{
2828
VERIFY(m_arena_id);
2929

3030
if (arena && arena_size)
3131
m_dl_arena = create_mspace_with_base(arena, arena_size, 0, &out_of_memory, (void*)this);
3232
else
33-
m_dl_arena = create_mspace(0, 0, 0, 0);
33+
m_dl_arena = create_mspace(0, 0, nullptr, nullptr);
3434
}
3535

3636
doug_lea_allocator::~doug_lea_allocator()
@@ -39,17 +39,17 @@ doug_lea_allocator::~doug_lea_allocator()
3939
destroy_mspace(m_dl_arena);
4040
}
4141

42-
void* doug_lea_allocator::malloc_impl(u32 size) { return mspace_malloc(m_dl_arena, size); }
43-
void* doug_lea_allocator::realloc_impl(void* pointer, u32 new_size)
42+
void* doug_lea_allocator::malloc_impl(size_t size) { return mspace_malloc(m_dl_arena, size); }
43+
void* doug_lea_allocator::realloc_impl(void* pointer, size_t new_size)
4444
{
4545
return mspace_realloc(m_dl_arena, pointer, new_size);
4646
}
4747

4848
void doug_lea_allocator::free_impl(void*& pointer)
4949
{
5050
mspace_free(m_dl_arena, pointer);
51-
pointer = 0;
51+
pointer = nullptr;
5252
}
5353

54-
u32 doug_lea_allocator::get_allocated_size() const { return (u32)mspace_mallinfo(m_dl_arena).uordblks; }
54+
size_t doug_lea_allocator::get_allocated_size() const { return mspace_mallinfo(m_dl_arena).uordblks; }
5555
#endif // USE_DOUG_LEA_ALLOCATOR

src/xrCore/Memory/doug_lea_allocator.h

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
class XRCORE_API doug_lea_allocator
1010
{
1111
public:
12-
doug_lea_allocator(void* arena, u32 arena_size, pcstr arena_id);
12+
doug_lea_allocator(void* arena, size_t arena_size, pcstr arena_id);
1313
~doug_lea_allocator();
14-
void* malloc_impl(u32 size);
15-
void* realloc_impl(void* pointer, u32 new_size);
16-
void free_impl(void*& pointer);
17-
u32 get_allocated_size() const;
14+
void* malloc_impl(size_t size);
15+
void* realloc_impl(void* pointer, size_t new_size);
16+
void free_impl(void* pointer);
17+
size_t get_allocated_size() const;
1818
pcstr get_arena_id() const { return m_arena_id; }
1919
template <typename T>
2020
void free_impl(T*& pointer)
@@ -23,7 +23,7 @@ class XRCORE_API doug_lea_allocator
2323
}
2424

2525
template <typename T>
26-
T* alloc_impl(u32 const count)
26+
T* alloc_impl(const size_t count)
2727
{
2828
return (T*)malloc_impl(count * sizeof(T));
2929
}
@@ -33,74 +33,73 @@ class XRCORE_API doug_lea_allocator
3333
void* m_dl_arena;
3434
};
3535

36-
extern doug_lea_allocator g_common_allocator;
36+
extern doug_lea_allocator g_common_doug_lea_allocator;
3737

38-
template <class T, doug_lea_allocator& _impl = common>
38+
template <class T, doug_lea_allocator& _impl = g_common_doug_lea_allocator>
3939
class doug_lea_alloc
4040
{
4141
constexpr static doug_lea_allocator& impl = _impl;
4242

4343
public:
4444
using size_type = size_t;
4545
using difference_type = ptrdiff_t;
46-
using pointer = T * ;
46+
using pointer = T*;
4747
using const_pointer = const T*;
48-
using reference = T & ;
48+
using reference = T&;
4949
using const_reference = const T&;
5050
using value_type = T;
5151

52-
template <class _Other>
52+
template <class Other>
5353
struct rebind
5454
{
55-
using other = doug_lea_alloc<_Other>;
55+
using other = doug_lea_alloc<Other>;
5656
};
5757

58-
doug_lea_alloc() {}
59-
doug_lea_alloc(const doug_lea_alloc<T>&) {}
58+
doug_lea_alloc() = default;
59+
doug_lea_alloc(const doug_lea_alloc<T, _impl>&) = default;
6060

61-
template <class _Other>
62-
doug_lea_alloc(const doug_lea_alloc<_Other>&) {}
61+
template <class Other>
62+
doug_lea_alloc(const doug_lea_alloc<Other>&) {}
6363

64-
template <class _Other>
65-
doug_lea_alloc<T>& operator=(const doug_lea_alloc<_Other>&)
64+
template <class Other>
65+
doug_lea_alloc<T>& operator=(const doug_lea_alloc<Other>&)
6666
{
6767
return *this;
6868
}
6969

70-
pointer address(reference _Val) const { return (&_Val); }
71-
const_pointer address(const_reference _Val) const { return (&_Val); }
70+
pointer address(reference ref) const { return &ref; }
71+
const_pointer address(const_reference ref) const { return &ref; }
7272

7373
pointer allocate(size_type n, const void* /*p*/ = nullptr) const
7474
{
75-
return static_cast<T*>(impl.malloc_impl(sizeof(T) * (u32)n));
75+
return static_cast<pointer>(impl.malloc_impl(sizeof(T) * n));
7676
}
7777

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

8483
size_type max_size() const
8584
{
86-
constexpr size_type _Count = static_cast<size_type>(-1) / sizeof(T);
87-
return 0 < _Count ? _Count : 1;
85+
constexpr auto count = std::numeric_limits<size_type>::max() / sizeof(T);
86+
return 0 < count ? count : 1;
8887
}
8988
};
9089

91-
template <class _Ty, class _Other>
92-
bool operator==(const doug_lea_alloc<_Ty>&, const doug_lea_alloc<_Other>&)
90+
template <class T, class Other>
91+
bool operator==(const doug_lea_alloc<T>&, const doug_lea_alloc<Other>&)
9392
{
94-
return (true);
93+
return true;
9594
}
9695

97-
template <class _Ty, class _Other>
98-
bool operator!=(const doug_lea_alloc<_Ty>&, const doug_lea_alloc<_Other>&)
96+
template <class T, class Other>
97+
bool operator!=(const doug_lea_alloc<T>&, const doug_lea_alloc<Other>&)
9998
{
100-
return (false);
99+
return false;
101100
}
102101

103-
template <doug_lea_allocator& _impl = common>
102+
template <doug_lea_allocator& _impl = g_common_doug_lea_allocator>
104103
struct doug_lea_allocator_wrapper
105104
{
106105
constexpr static doug_lea_allocator& impl = _impl;
@@ -111,7 +110,7 @@ struct doug_lea_allocator_wrapper
111110
using result = doug_lea_alloc<T, _impl>;
112111
};
113112

114-
static void* alloc(const u32& n) { return impl.malloc_impl((u32)n); }
113+
static void* alloc(const size_t& n) { return impl.malloc_impl(n); }
115114

116115
template <typename T>
117116
static void dealloc(T*& p)

src/xrMisc/xrMisc_xrMemory.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ void operator delete[](void* p) throw() { Memory.mem_free(p); }
2424
#ifdef USE_ARENA_ALLOCATOR
2525
constexpr static const u32 s_arena_size = 256 * 1024 * 1024;
2626
static char s_fake_array[s_arena_size];
27-
doug_lea_allocator g_common_allocator(s_fake_array, s_arena_size, "common");
27+
doug_lea_allocator g_common_doug_lea_allocator(s_fake_array, s_arena_size, "common");
2828
#else
29-
doug_lea_allocator g_common_allocator(nullptr, 0, "common");
29+
doug_lea_allocator g_common_doug_lea_allocator(nullptr, 0, "common");
3030
#endif
3131
#endif // USE_DOUG_LEA_ALLOCATOR

0 commit comments

Comments
 (0)