using Alias

This commit is contained in:
Nils Gaitzsch 2020-07-11 17:36:05 +02:00
parent 22d4793b39
commit 23bcc2e1c0
8 changed files with 30 additions and 15 deletions

View file

@ -4,6 +4,7 @@
#include <vector> #include <vector>
#include <iostream> #include <iostream>
#include "pool.h" #include "pool.h"
#include <limits>
namespace T5M::Memory { namespace T5M::Memory {
template <typename T,typename Pool> template <typename T,typename Pool>
class PoolAllocator { class PoolAllocator {
@ -31,16 +32,16 @@ namespace T5M::Memory {
~PoolAllocator() {} ~PoolAllocator() {}
pointer allocate(size_type n, const void* hint = 0) { pointer allocate(size_type n, const void* hint = 0) {
return pool->malloc<value_type>(n); return (pool)->malloc<value_type>(n);
} }
void deallocate(T* ptr, size_type n) { void deallocate(T* ptr, size_type n) {
pool->free(ptr); (pool)->free(ptr);
} }
size_type max_size() const { size_type max_size() const {
#pragma push_macro("max") //thanks Microsoft #pragma push_macro("max") //thanks Microsoft
#undef max #undef max
return numeric_limits<size_type>::max(); return std::numeric_limits<size_type>::max();
#pragma pop_macro("max") #pragma pop_macro("max")
} }
}; };

View file

@ -2,16 +2,16 @@
#include "malloc.h" #include "malloc.h"
#include "door.h" #include "door.h"
#include "PoolAllocator.h" #include "PoolAllocator.h"
using namespace std; using namespace T5M::Memory;
char* malloc_buffer; char* malloc_buffer;
int malloc_size; int malloc_size;
char* malloc_ptr; char* malloc_ptr;
int malloc_free; int malloc_free;
int malloc_used; int malloc_used;
Pool<static_cast<unsigned>(BlockSize::Small)>* gameMemory; TGPool* gameMemory;
void init_game_malloc() noexcept void init_game_malloc() noexcept
{ {
gameMemory = new Pool<static_cast<size_t>(BlockSize::Small)>(8*1024*1024); gameMemory = new TGPool(8 * 1024 * 1024);
} }
void game_free(void* ptr) noexcept void game_free(void* ptr) noexcept

View file

@ -1,15 +1,17 @@
#pragma once #pragma once
#include "pool.h" #include "pool.h"
#include <utility>
#include "memory/memory.h"
extern char* malloc_buffer; extern char* malloc_buffer;
extern int malloc_size; extern int malloc_size;
extern char* malloc_ptr; extern char* malloc_ptr;
extern int malloc_free; extern int malloc_free;
extern int malloc_used; extern int malloc_used;
using namespace T5M::Memory; extern T5M::Memory::TGPool* gameMemory;
extern Pool<static_cast<unsigned>(BlockSize::Small)>* gameMemory;
template <typename T> template <typename T,typename ... Args>
[[nodiscard]] T* game_malloc(size_t count= 1) noexcept { [[nodiscard]] T* game_malloc(size_t count = 1,Args&&...args) noexcept {
return gameMemory->malloc<T>(count); return gameMemory->malloc<T>(count,std::forward<Args>(args)...);
} }
void init_game_malloc() noexcept; void init_game_malloc() noexcept;
void game_free(void* ptr) noexcept; void game_free(void* ptr) noexcept;

View file

@ -0,0 +1,8 @@
#pragma once
#include "pool.h"
#include "PoolAllocator.h"
#include <vector>
namespace T5M::Memory {
using TGPool = Pool<16>;
template<typename T> using vector = std::vector <T, PoolAllocator<T, TGPool>>;
}

View file

@ -114,8 +114,8 @@ namespace T5M::Memory {
} }
} }
public: public:
template <typename T> template <typename T,typename ... Args>
T* malloc(Size count = 1) { T* malloc(Size count = 1,Args&&...args) {
//Forbid allocation of size 0 //Forbid allocation of size 0
if (count < 1) return nullptr; if (count < 1) return nullptr;
//assertm(count >= 1,"Allocation Size must be greater than 0!"); //assertm(count >= 1,"Allocation Size must be greater than 0!");
@ -149,7 +149,7 @@ namespace T5M::Memory {
head->data.nextFreeBlock = currentNode->data.nextFreeBlock; head->data.nextFreeBlock = currentNode->data.nextFreeBlock;
head->data.managedBlocks = numFreeBlocksAfterSplit; head->data.managedBlocks = numFreeBlocksAfterSplit;
Log("Malloc: New Free Blocks : " << numFreeBlocksAfterSplit) Log("Malloc: New Free Blocks : " << numFreeBlocksAfterSplit)
return reinterpret_cast<T*>(blockToReturn); return new(blockToReturn)T(std::forward<Args>(args)...);
} }
void free(void* ptr) { void free(void* ptr) {

View file

@ -15,7 +15,7 @@ void InitialiseAutoGuns(short itemNumber)
item = &Items[itemNumber]; item = &Items[itemNumber];
item->meshBits = 1024; item->meshBits = 1024;
item->data = game_malloc<uint8_t[5702]>(); item->data = game_malloc<uint8_t>(5702);
} }
static void TriggerAutoGunSmoke(PHD_VECTOR* pos, char shade) static void TriggerAutoGunSmoke(PHD_VECTOR* pos, char shade)

View file

@ -142,6 +142,7 @@ xcopy /Y "$(ProjectDir)Scripting\Scripts\*.lua" "$(TargetDir)\Scripts"</Command>
<ClInclude Include="Game\memory\pool.h" /> <ClInclude Include="Game\memory\pool.h" />
<ClInclude Include="Game\memory\linearPoolAllocator.h" /> <ClInclude Include="Game\memory\linearPoolAllocator.h" />
<ClInclude Include="Game\memory\PoolAllocator.h" /> <ClInclude Include="Game\memory\PoolAllocator.h" />
<ClInclude Include="Game\memory\memory.h" />
<ClInclude Include="Game\phd_global.h" /> <ClInclude Include="Game\phd_global.h" />
<ClInclude Include="Game\memory\qmalloc.h" /> <ClInclude Include="Game\memory\qmalloc.h" />
<ClInclude Include="Game\room.h" /> <ClInclude Include="Game\room.h" />

View file

@ -849,6 +849,9 @@
<ClInclude Include="Game\memory\PoolAllocator.h"> <ClInclude Include="Game\memory\PoolAllocator.h">
<Filter>File di intestazione</Filter> <Filter>File di intestazione</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Game\memory\memory.h">
<Filter>File di intestazione</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Game\box.cpp"> <ClCompile Include="Game\box.cpp">