Skip to content

Commit

Permalink
Refactor Doug Lea allocator usage for xrRender_R*
Browse files Browse the repository at this point in the history
  • Loading branch information
Xottab-DUTY committed Dec 23, 2017
1 parent 156b48b commit 3417488
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 95 deletions.
1 change: 0 additions & 1 deletion Externals/OPCODE/pch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "Common/Common.hpp"
#include "xrCore/xrCore.h"
#include "xrCore/Memory/doug_lea_allocator.h"
#include "xrCore/memory_allocator_options.h"

#ifdef USE_ARENA_ALLOCATOR
extern doug_lea_allocator g_collision_allocator;
Expand Down
3 changes: 2 additions & 1 deletion src/Layers/xrRender/RenderAllocator.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include "stdafx.h"
#include "RenderAllocator.hpp"
#include "xrCore/memory_allocator_options.h"

#ifdef USE_DOUG_LEA_ALLOCATOR_FOR_RENDER
#ifdef USE_ARENA_ALLOCATOR
static const u32 s_arena_size = 8 * 1024 * 1024;
static char s_fake_array[s_arena_size];
doug_lea_allocator g_render_allocator(s_fake_array, s_arena_size, "render");
#else
doug_lea_allocator g_render_allocator(nullptr, 0, "render");
#endif
#endif
10 changes: 10 additions & 0 deletions src/Layers/xrRender/RenderAllocator.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
#pragma once
#include "xrCore/Memory/doug_lea_allocator.h"

#ifdef USE_DOUG_LEA_ALLOCATOR_FOR_RENDER
extern doug_lea_allocator g_render_allocator;

template<class T>
using render_alloc = doug_lea_alloc<T, g_render_allocator>;
using render_allocator = doug_lea_allocator_wrapper<g_render_allocator>;
#else
template<class T>
using render_alloc = xalloc<T>;
using render_allocator = xr_allocator;
#endif
84 changes: 1 addition & 83 deletions src/Layers/xrRender/r__dsgraph_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,91 +3,9 @@
#include "xrCore/fixedmap.h"
#include "Layers/xrRender/RenderAllocator.hpp"

template <class T>
class doug_lea_alloc
{
public:
using size_type = size_t;
using difference_type = ptrdiff_t;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using value_type = T;

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

pointer address(reference _Val) const { return (&_Val); }
const_pointer address(const_reference _Val) const { return (&_Val); }
doug_lea_alloc() {}
doug_lea_alloc(const doug_lea_alloc<T>&) {}

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

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

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

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

size_type max_size() const
{
size_type _Count = (size_type)(-1) / sizeof(T);
return (0 < _Count ? _Count : 1);
}
};

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

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

struct doug_lea_allocator_wrapper
{
template <typename T>
struct helper
{
using result = doug_lea_alloc<T>;
};

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

template <typename T>
static void dealloc(T*& p)
{
g_render_allocator.free_impl((void*&)p);
}
};

#define render_alloc doug_lea_alloc
using render_allocator = doug_lea_allocator_wrapper;

class dxRender_Visual;

// #define USE_RESOURCE_DEBUGGER
//#define USE_RESOURCE_DEBUGGER

namespace R_dsgraph
{
Expand Down
3 changes: 3 additions & 0 deletions src/Layers/xrRenderPC_R1/xrRender_R1.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,9 @@
<ProjectReference Include="..\..\..\Externals\oalib.vcxproj">
<Project>{61d4856f-fa82-4f02-bb88-909ddfb1fe74}</Project>
</ProjectReference>
<ProjectReference Include="..\..\xrMisc\xrMisc.vcxproj">
<Project>{c964d17a-05a8-4bfa-b0a8-7af5c6b627ec}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
3 changes: 3 additions & 0 deletions src/Layers/xrRenderPC_R2/xrRender_R2.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,9 @@
<ProjectReference Include="..\..\..\Externals\oalib.vcxproj">
<Project>{61d4856f-fa82-4f02-bb88-909ddfb1fe74}</Project>
</ProjectReference>
<ProjectReference Include="..\..\xrMisc\xrMisc.vcxproj">
<Project>{c964d17a-05a8-4bfa-b0a8-7af5c6b627ec}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
3 changes: 3 additions & 0 deletions src/Layers/xrRenderPC_R3/xrRender_R3.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,9 @@
<ProjectReference Include="..\..\..\Externals\oalib.vcxproj">
<Project>{61d4856f-fa82-4f02-bb88-909ddfb1fe74}</Project>
</ProjectReference>
<ProjectReference Include="..\..\xrMisc\xrMisc.vcxproj">
<Project>{c964d17a-05a8-4bfa-b0a8-7af5c6b627ec}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
3 changes: 3 additions & 0 deletions src/Layers/xrRenderPC_R4/xrRender_R4.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,9 @@
<ProjectReference Include="..\..\..\Externals\oalib.vcxproj">
<Project>{61d4856f-fa82-4f02-bb88-909ddfb1fe74}</Project>
</ProjectReference>
<ProjectReference Include="..\..\xrMisc\xrMisc.vcxproj">
<Project>{c964d17a-05a8-4bfa-b0a8-7af5c6b627ec}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
1 change: 0 additions & 1 deletion src/xrCDB/StdAfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "Common/Common.hpp"
#include "xrCore/xrCore.h"
#include "xrCore/Memory/doug_lea_allocator.h"
#include "xrCore/memory_allocator_options.h"

#ifdef USE_ARENA_ALLOCATOR
extern doug_lea_allocator g_collision_allocator;
Expand Down
89 changes: 89 additions & 0 deletions src/xrCore/Memory/doug_lea_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
////////////////////////////////////////////////////////////////////////////
#pragma once

#include "xrCore/Memory/memory_allocator_options.h"

class XRCORE_API doug_lea_allocator
{
public:
Expand All @@ -31,3 +33,90 @@ class XRCORE_API doug_lea_allocator
pcstr m_arena_id;
void* m_dl_arena;
};

extern doug_lea_allocator common;

template <class T, doug_lea_allocator& _impl = common>
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 const_pointer = const T*;
using reference = T & ;
using const_reference = const T&;
using value_type = T;

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

doug_lea_alloc() {}
doug_lea_alloc(const doug_lea_alloc<T>&) {}

template <class _Other>
doug_lea_alloc(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 allocate(size_type n, const void* /*p*/ = nullptr) const
{
return static_cast<T*>(impl.malloc_impl(sizeof(T) * (u32)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 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;
}
};

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

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

template <doug_lea_allocator& _impl = common>
struct doug_lea_allocator_wrapper
{
constexpr static doug_lea_allocator& impl = _impl;

template <typename T>
struct helper
{
using result = doug_lea_alloc<T, _impl>;
};

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

template <typename T>
static void dealloc(T*& p)
{
impl.free_impl((void*&)p);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
// Author : Armen Abroyan
// Copyright (C) GSC Game World - 2009
////////////////////////////////////////////////////////////////////////////
#pragma once

#ifndef MEMORY_ALLOCATOR_OPTIONS_H_INCLUDED
#define MEMORY_ALLOCATOR_OPTIONS_H_INCLUDED

#define USE_DOUG_LEA_ALLOCATOR_FOR_RENDER
//#define USE_ARENA_ALLOCATOR

#endif // #ifndef MEMORY_ALLOCATOR_OPTIONS_H_INCLUDED
2 changes: 1 addition & 1 deletion src/xrCore/xrCore.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@
<ClInclude Include="Media\Image.hpp" />
<ClInclude Include="Memory\dlmalloc.h" />
<ClInclude Include="Memory\doug_lea_allocator.h" />
<ClInclude Include="Memory\memory_allocator_options.h" />
<ClInclude Include="Memory\xalloc.h" />
<ClInclude Include="memory_allocator_options.h" />
<ClInclude Include="memory_monitor.h" />
<ClInclude Include="net_utils.h" />
<ClInclude Include="os_clipboard.h" />
Expand Down
6 changes: 3 additions & 3 deletions src/xrCore/xrCore.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -521,9 +521,6 @@
<ClInclude Include="xrMemory_subst_msvc.h">
<Filter>Memory\Memory manager</Filter>
</ClInclude>
<ClInclude Include="memory_allocator_options.h">
<Filter>Memory\Memory manager\dlmalloc\wrapper</Filter>
</ClInclude>
<ClInclude Include="xrDebug.h">
<Filter>Debug core</Filter>
</ClInclude>
Expand Down Expand Up @@ -743,6 +740,9 @@
<ClInclude Include="Memory\doug_lea_allocator.h">
<Filter>Memory\Memory manager\dlmalloc\wrapper</Filter>
</ClInclude>
<ClInclude Include="Memory\memory_allocator_options.h">
<Filter>Memory\Memory manager\dlmalloc\wrapper</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="xrCore.rc">
Expand Down
9 changes: 9 additions & 0 deletions src/xrMisc/xrMisc_xrMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#endif

#include "xrCore/xrMemory.h"
#include "xrCore/Memory/doug_lea_allocator.h"

#ifdef XRCORE_EXPORTS
#error XRCORE_EXPORTS MUST NOT be defined when building xrMisc
Expand All @@ -28,3 +29,11 @@ void* operator new[](size_t size) { return Memory.mem_alloc(size); }

void operator delete(void* p) throw() { Memory.mem_free(p); }
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 common(s_fake_array, s_arena_size, "common");
#else
doug_lea_allocator common(nullptr, 0, "common");
#endif

0 comments on commit 3417488

Please sign in to comment.