Skip to content

Commit

Permalink
Work of Memory.
Browse files Browse the repository at this point in the history
  • Loading branch information
intorr committed Mar 4, 2018
1 parent 899da7b commit 0b190db
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
19 changes: 0 additions & 19 deletions src/xrCore/xrMemory.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "stdafx.h"

#include <Psapi.h>
#include "tbb/scalable_allocator.h"

xrMemory Memory;
// Also used in src\xrCore\xrDebug.cpp to prevent use of g_pStringContainer before it initialized
Expand Down Expand Up @@ -82,24 +81,6 @@ void xrMemory::mem_compact()
SetProcessWorkingSetSize(GetCurrentProcess(), size_t(-1), size_t(-1));
}

void* xrMemory::mem_alloc(size_t size)
{
stat_calls++;
return scalable_malloc(size);
}

void xrMemory::mem_free(void* P)
{
stat_calls++;
scalable_free(P);
}

void* xrMemory::mem_realloc(void* P, const size_t size)
{
stat_calls++;
return scalable_realloc(P, size);
}

// xr_strdup
pstr xr_strdup(pcstr string)
{
Expand Down
59 changes: 41 additions & 18 deletions src/xrCore/xrMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
#include "_types.h"

#include "tbb/tbb_allocator.h"
#include "tbb/scalable_allocator.h"

/*
Можно заключить - прокси перехватывает не всегда и/или не всё используемые функции.
И в очень малом количестве случаев это приводит к странностям при работе с памятью.
Поэтому всё-же стоит переопределять для большинства случаев операторы new и delete.
А для остального мы будем полагать (и надеяться), что прокси справится без проблем.
*/
#include "tbb/tbbmalloc_proxy.h"

class XRCORE_API xrMemory
Expand All @@ -16,10 +24,10 @@ class XRCORE_API xrMemory

public:
size_t mem_usage();
void mem_compact();
void* mem_alloc(size_t size);
void* mem_realloc(void* p, const size_t size);
void mem_free(void* p);
void mem_compact();
inline void* mem_alloc (size_t size) { stat_calls++; return scalable_malloc ( size); };
inline void* mem_realloc(void* ptr, size_t size) { stat_calls++; return scalable_realloc(ptr, size); };
inline void mem_free (void* ptr) { stat_calls++; scalable_free (ptr); };
};

extern XRCORE_API xrMemory Memory;
Expand All @@ -31,29 +39,44 @@ extern XRCORE_API xrMemory Memory;
#define CopyMemory(a, b, c) memcpy(a, b, c)
#define FillMemory(a, b, c) memset(a, c, b)

#define xr_delete(x)\
{\
delete (x);\
(x) = nullptr;\
/*
Начиная со стандарта C++11 нет необходимости объявлять все формы операторов new и delete.
*/

inline void* operator new(size_t size)
{
return Memory.mem_alloc(size);
}

// generic "C"-like allocations/deallocations
template <class T>
T* xr_alloc(const size_t count)
{ return (T*)Memory.mem_alloc(count * sizeof(T)); }
inline void operator delete(void* ptr) noexcept
{
Memory.mem_free(ptr);
}

template <class T>
inline void xr_delete(T*& ptr) noexcept
{
delete ptr;
ptr = nullptr;
}

// generic "C"-like allocations/deallocations
template <class T>
inline T* xr_alloc(size_t count)
{
return (T*)Memory.mem_alloc(count * sizeof(T));
}
template <class T>
void xr_free(T*& P) noexcept
inline void xr_free(T*& ptr) noexcept
{
if (P)
if (ptr)
{
Memory.mem_free((void*)P);
P = nullptr;
Memory.mem_free((void*)ptr);
ptr = nullptr;
}
}
inline void* xr_malloc(const size_t size) { return Memory.mem_alloc(size); }
inline void* xr_realloc(void* P, const size_t size) { return Memory.mem_realloc(P, size); }
inline void* xr_malloc (size_t size) { return Memory.mem_alloc (size); }
inline void* xr_realloc(void* ptr, size_t size) { return Memory.mem_realloc(ptr, size); }

XRCORE_API pstr xr_strdup(pcstr string);

Expand Down
3 changes: 1 addition & 2 deletions src/xrEngine/PS_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,5 @@ void CPS_Instance::PSI_destroy()
//----------------------------------------------------
void CPS_Instance::PSI_internal_delete()
{
CPS_Instance* self = this;
xr_delete(self);
delete this;
}

0 comments on commit 0b190db

Please sign in to comment.