2016-12-30 04:35:25 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-01-15 20:45:21 +00:00
|
|
|
#include <memory>
|
2017-03-10 01:22:42 +00:00
|
|
|
#include <vector>
|
2017-01-15 20:45:21 +00:00
|
|
|
|
2017-03-20 07:28:16 +00:00
|
|
|
// Memory needed for the duration of a frame rendering
|
|
|
|
class RenderMemory
|
2016-12-30 04:35:25 +00:00
|
|
|
{
|
2017-03-20 07:28:16 +00:00
|
|
|
public:
|
|
|
|
void Clear();
|
2017-01-15 20:45:21 +00:00
|
|
|
|
2017-03-20 07:28:16 +00:00
|
|
|
template<typename T>
|
|
|
|
T *AllocMemory(int size = 1)
|
|
|
|
{
|
|
|
|
return (T*)AllocBytes(sizeof(T) * size);
|
|
|
|
}
|
2017-01-15 20:45:21 +00:00
|
|
|
|
2017-03-20 07:28:16 +00:00
|
|
|
template<typename T, typename... Types>
|
|
|
|
T *NewObject(Types &&... args)
|
|
|
|
{
|
|
|
|
void *ptr = AllocBytes(sizeof(T));
|
|
|
|
return new (ptr)T(std::forward<Types>(args)...);
|
|
|
|
}
|
2017-01-15 20:45:21 +00:00
|
|
|
|
2017-03-20 07:28:16 +00:00
|
|
|
private:
|
|
|
|
void *AllocBytes(int size);
|
2017-01-15 20:45:21 +00:00
|
|
|
|
2017-03-20 07:28:16 +00:00
|
|
|
enum { BlockSize = 1024 * 1024 };
|
2017-01-15 20:45:21 +00:00
|
|
|
|
2017-03-20 07:28:16 +00:00
|
|
|
struct MemoryBlock
|
|
|
|
{
|
|
|
|
MemoryBlock() : Data(new uint8_t[BlockSize]), Position(0) { }
|
|
|
|
~MemoryBlock() { delete[] Data; }
|
2017-01-15 20:45:21 +00:00
|
|
|
|
2017-03-20 07:28:16 +00:00
|
|
|
MemoryBlock(const MemoryBlock &) = delete;
|
|
|
|
MemoryBlock &operator=(const MemoryBlock &) = delete;
|
2017-01-15 20:45:21 +00:00
|
|
|
|
2017-03-20 07:28:16 +00:00
|
|
|
uint8_t *Data;
|
|
|
|
uint32_t Position;
|
2017-01-15 20:45:21 +00:00
|
|
|
};
|
2017-03-20 07:28:16 +00:00
|
|
|
std::vector<std::unique_ptr<MemoryBlock>> UsedBlocks;
|
|
|
|
std::vector<std::unique_ptr<MemoryBlock>> FreeBlocks;
|
|
|
|
};
|