Skip to content

Commit 3417488

Browse files
committed
Refactor Doug Lea allocator usage for xrRender_R*
1 parent 156b48b commit 3417488

File tree

14 files changed

+129
-95
lines changed

14 files changed

+129
-95
lines changed

Externals/OPCODE/pch.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "Common/Common.hpp"
66
#include "xrCore/xrCore.h"
77
#include "xrCore/Memory/doug_lea_allocator.h"
8-
#include "xrCore/memory_allocator_options.h"
98

109
#ifdef USE_ARENA_ALLOCATOR
1110
extern doug_lea_allocator g_collision_allocator;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#include "stdafx.h"
22
#include "RenderAllocator.hpp"
3-
#include "xrCore/memory_allocator_options.h"
43

4+
#ifdef USE_DOUG_LEA_ALLOCATOR_FOR_RENDER
55
#ifdef USE_ARENA_ALLOCATOR
66
static const u32 s_arena_size = 8 * 1024 * 1024;
77
static char s_fake_array[s_arena_size];
88
doug_lea_allocator g_render_allocator(s_fake_array, s_arena_size, "render");
99
#else
1010
doug_lea_allocator g_render_allocator(nullptr, 0, "render");
1111
#endif
12+
#endif
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
#pragma once
22
#include "xrCore/Memory/doug_lea_allocator.h"
33

4+
#ifdef USE_DOUG_LEA_ALLOCATOR_FOR_RENDER
45
extern doug_lea_allocator g_render_allocator;
6+
7+
template<class T>
8+
using render_alloc = doug_lea_alloc<T, g_render_allocator>;
9+
using render_allocator = doug_lea_allocator_wrapper<g_render_allocator>;
10+
#else
11+
template<class T>
12+
using render_alloc = xalloc<T>;
13+
using render_allocator = xr_allocator;
14+
#endif

src/Layers/xrRender/r__dsgraph_types.h

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -3,91 +3,9 @@
33
#include "xrCore/fixedmap.h"
44
#include "Layers/xrRender/RenderAllocator.hpp"
55

6-
template <class T>
7-
class doug_lea_alloc
8-
{
9-
public:
10-
using size_type = size_t;
11-
using difference_type = ptrdiff_t;
12-
using pointer = T*;
13-
using const_pointer = const T*;
14-
using reference = T&;
15-
using const_reference = const T&;
16-
using value_type = T;
17-
18-
template <class _Other>
19-
struct rebind
20-
{
21-
using other = doug_lea_alloc<_Other>;
22-
};
23-
24-
pointer address(reference _Val) const { return (&_Val); }
25-
const_pointer address(const_reference _Val) const { return (&_Val); }
26-
doug_lea_alloc() {}
27-
doug_lea_alloc(const doug_lea_alloc<T>&) {}
28-
29-
template <class _Other>
30-
doug_lea_alloc(const doug_lea_alloc<_Other>&) {}
31-
32-
template <class _Other>
33-
doug_lea_alloc<T>& operator=(const doug_lea_alloc<_Other>&)
34-
{
35-
return (*this);
36-
}
37-
38-
pointer allocate(size_type n, const void* /*p*/ = nullptr) const
39-
{
40-
return (T*)g_render_allocator.malloc_impl(sizeof(T) * (u32)n);
41-
}
42-
43-
void deallocate(pointer p, size_type) const { g_render_allocator.free_impl((void*&)p); }
44-
void deallocate(void* p, size_type n) const { g_render_allocator.free_impl(p); }
45-
char* __charalloc(size_type n) { return (char*)allocate(n); }
46-
void construct(pointer p, const T& _Val) { new(p) T(_Val); }
47-
void destroy(pointer p) { p->~T(); }
48-
49-
size_type max_size() const
50-
{
51-
size_type _Count = (size_type)(-1) / sizeof(T);
52-
return (0 < _Count ? _Count : 1);
53-
}
54-
};
55-
56-
template <class _Ty, class _Other>
57-
bool operator==(const doug_lea_alloc<_Ty>&, const doug_lea_alloc<_Other>&)
58-
{
59-
return (true);
60-
}
61-
62-
template <class _Ty, class _Other>
63-
bool operator!=(const doug_lea_alloc<_Ty>&, const doug_lea_alloc<_Other>&)
64-
{
65-
return (false);
66-
}
67-
68-
struct doug_lea_allocator_wrapper
69-
{
70-
template <typename T>
71-
struct helper
72-
{
73-
using result = doug_lea_alloc<T>;
74-
};
75-
76-
static void* alloc(const u32& n) { return g_render_allocator.malloc_impl((u32)n); }
77-
78-
template <typename T>
79-
static void dealloc(T*& p)
80-
{
81-
g_render_allocator.free_impl((void*&)p);
82-
}
83-
};
84-
85-
#define render_alloc doug_lea_alloc
86-
using render_allocator = doug_lea_allocator_wrapper;
87-
886
class dxRender_Visual;
897

90-
// #define USE_RESOURCE_DEBUGGER
8+
//#define USE_RESOURCE_DEBUGGER
919

9210
namespace R_dsgraph
9311
{

src/Layers/xrRenderPC_R1/xrRender_R1.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,9 @@
473473
<ProjectReference Include="..\..\..\Externals\oalib.vcxproj">
474474
<Project>{61d4856f-fa82-4f02-bb88-909ddfb1fe74}</Project>
475475
</ProjectReference>
476+
<ProjectReference Include="..\..\xrMisc\xrMisc.vcxproj">
477+
<Project>{c964d17a-05a8-4bfa-b0a8-7af5c6b627ec}</Project>
478+
</ProjectReference>
476479
</ItemGroup>
477480
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
478481
<ImportGroup Label="ExtensionTargets">

src/Layers/xrRenderPC_R2/xrRender_R2.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,9 @@
517517
<ProjectReference Include="..\..\..\Externals\oalib.vcxproj">
518518
<Project>{61d4856f-fa82-4f02-bb88-909ddfb1fe74}</Project>
519519
</ProjectReference>
520+
<ProjectReference Include="..\..\xrMisc\xrMisc.vcxproj">
521+
<Project>{c964d17a-05a8-4bfa-b0a8-7af5c6b627ec}</Project>
522+
</ProjectReference>
520523
</ItemGroup>
521524
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
522525
<ImportGroup Label="ExtensionTargets">

src/Layers/xrRenderPC_R3/xrRender_R3.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,9 @@
565565
<ProjectReference Include="..\..\..\Externals\oalib.vcxproj">
566566
<Project>{61d4856f-fa82-4f02-bb88-909ddfb1fe74}</Project>
567567
</ProjectReference>
568+
<ProjectReference Include="..\..\xrMisc\xrMisc.vcxproj">
569+
<Project>{c964d17a-05a8-4bfa-b0a8-7af5c6b627ec}</Project>
570+
</ProjectReference>
568571
</ItemGroup>
569572
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
570573
<ImportGroup Label="ExtensionTargets">

src/Layers/xrRenderPC_R4/xrRender_R4.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,9 @@
576576
<ProjectReference Include="..\..\..\Externals\oalib.vcxproj">
577577
<Project>{61d4856f-fa82-4f02-bb88-909ddfb1fe74}</Project>
578578
</ProjectReference>
579+
<ProjectReference Include="..\..\xrMisc\xrMisc.vcxproj">
580+
<Project>{c964d17a-05a8-4bfa-b0a8-7af5c6b627ec}</Project>
581+
</ProjectReference>
579582
</ItemGroup>
580583
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
581584
<ImportGroup Label="ExtensionTargets">

src/xrCDB/StdAfx.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "Common/Common.hpp"
44
#include "xrCore/xrCore.h"
55
#include "xrCore/Memory/doug_lea_allocator.h"
6-
#include "xrCore/memory_allocator_options.h"
76

87
#ifdef USE_ARENA_ALLOCATOR
98
extern doug_lea_allocator g_collision_allocator;

src/xrCore/Memory/doug_lea_allocator.h

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
////////////////////////////////////////////////////////////////////////////
66
#pragma once
77

8+
#include "xrCore/Memory/memory_allocator_options.h"
9+
810
class XRCORE_API doug_lea_allocator
911
{
1012
public:
@@ -31,3 +33,90 @@ class XRCORE_API doug_lea_allocator
3133
pcstr m_arena_id;
3234
void* m_dl_arena;
3335
};
36+
37+
extern doug_lea_allocator common;
38+
39+
template <class T, doug_lea_allocator& _impl = common>
40+
class doug_lea_alloc
41+
{
42+
constexpr static doug_lea_allocator& impl = _impl;
43+
44+
public:
45+
using size_type = size_t;
46+
using difference_type = ptrdiff_t;
47+
using pointer = T * ;
48+
using const_pointer = const T*;
49+
using reference = T & ;
50+
using const_reference = const T&;
51+
using value_type = T;
52+
53+
template <class _Other>
54+
struct rebind
55+
{
56+
using other = doug_lea_alloc<_Other>;
57+
};
58+
59+
doug_lea_alloc() {}
60+
doug_lea_alloc(const doug_lea_alloc<T>&) {}
61+
62+
template <class _Other>
63+
doug_lea_alloc(const doug_lea_alloc<_Other>&) {}
64+
65+
template <class _Other>
66+
doug_lea_alloc<T>& operator=(const doug_lea_alloc<_Other>&)
67+
{
68+
return *this;
69+
}
70+
71+
pointer address(reference _Val) const { return (&_Val); }
72+
const_pointer address(const_reference _Val) const { return (&_Val); }
73+
74+
pointer allocate(size_type n, const void* /*p*/ = nullptr) const
75+
{
76+
return static_cast<T*>(impl.malloc_impl(sizeof(T) * (u32)n));
77+
}
78+
79+
void deallocate(pointer p, size_type) const { impl.free_impl((void*&)p); }
80+
void deallocate(void* p, size_type n) const { impl.free_impl(p); }
81+
char* __charalloc(size_type n) { return (char*)allocate(n); }
82+
void construct(pointer p, const T& _Val) { new(p) T(_Val); }
83+
void destroy(pointer p) { p->~T(); }
84+
85+
size_type max_size() const
86+
{
87+
constexpr size_type _Count = static_cast<size_type>(-1) / sizeof(T);
88+
return 0 < _Count ? _Count : 1;
89+
}
90+
};
91+
92+
template <class _Ty, class _Other>
93+
bool operator==(const doug_lea_alloc<_Ty>&, const doug_lea_alloc<_Other>&)
94+
{
95+
return (true);
96+
}
97+
98+
template <class _Ty, class _Other>
99+
bool operator!=(const doug_lea_alloc<_Ty>&, const doug_lea_alloc<_Other>&)
100+
{
101+
return (false);
102+
}
103+
104+
template <doug_lea_allocator& _impl = common>
105+
struct doug_lea_allocator_wrapper
106+
{
107+
constexpr static doug_lea_allocator& impl = _impl;
108+
109+
template <typename T>
110+
struct helper
111+
{
112+
using result = doug_lea_alloc<T, _impl>;
113+
};
114+
115+
static void* alloc(const u32& n) { return impl.malloc_impl((u32)n); }
116+
117+
template <typename T>
118+
static void dealloc(T*& p)
119+
{
120+
impl.free_impl((void*&)p);
121+
}
122+
};

0 commit comments

Comments
 (0)