2020-07-10 19:46:12 +02:00
|
|
|
#pragma once
|
|
|
|
#include <memory>
|
2020-07-11 08:19:14 +02:00
|
|
|
#include "Game/debug/assert.h"
|
|
|
|
|
2020-07-10 19:46:12 +02:00
|
|
|
namespace T5M::Memory {
|
|
|
|
enum class MemoryUnit : size_t {
|
|
|
|
Byte = 1,
|
|
|
|
KByte = Byte*1024,
|
|
|
|
MByte = KByte*1024,
|
|
|
|
GByte = MByte*1024,
|
|
|
|
};
|
|
|
|
class LinearPool {
|
|
|
|
private:
|
|
|
|
const size_t allocatedBytes;
|
|
|
|
const std::unique_ptr<uint8_t> bytes;
|
|
|
|
size_t offset;
|
|
|
|
public:
|
|
|
|
LinearPool(size_t amount, MemoryUnit unit) : allocatedBytes(amount* static_cast<size_t>(unit)), bytes(new uint8_t[allocatedBytes]) {
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
[[nodiscard]]T* malloc(size_t count = 1) noexcept {
|
2020-07-11 08:19:14 +02:00
|
|
|
assertm(sizeof(T) * count >= 1, "Requested memory needs to be greater than 0!");
|
2020-07-10 19:46:12 +02:00
|
|
|
|
|
|
|
size_t requestedBytes = sizeof(T) * count;
|
2020-07-11 08:19:14 +02:00
|
|
|
|
|
|
|
assertm(offset + requestedBytes > allocatedBytes, "Memory must not overflow linear pool!");
|
2020-07-10 19:46:12 +02:00
|
|
|
T* returnValue = reinterpret_cast<T*>(bytes.get()[offset]);
|
|
|
|
offset += requestedBytes;
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void flush() noexcept {
|
2020-07-11 08:19:14 +02:00
|
|
|
std::memset(bytes.get(), 0, allocatedBytes);
|
2020-07-10 19:46:12 +02:00
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|