-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlatMemoryAllocator.h
More file actions
30 lines (26 loc) · 962 Bytes
/
FlatMemoryAllocator.h
File metadata and controls
30 lines (26 loc) · 962 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#pragma once
#include "IMemoryAllocator.h"
#include <vector>
#include <unordered_map>
#include <string>
class FlatMemoryAllocator : public IMemoryAllocator
{
public:
FlatMemoryAllocator(size_t maxBytes,
size_t perProc,
size_t frameBytes = 1);
void* allocate(size_t bytes, int pid) override;
void deallocate(int pid) override;
std::string visualizeMemory() const override;
void snapshot(int quantum) const override;
std::unordered_map<int, std::vector<size_t>> getPidOffsets() const override;
private:
size_t totalBytes;
size_t perProcBytes;
size_t frameBytes;
std::vector<char> memory;
std::unordered_map<size_t, bool> allocationMap;
std::unordered_map<int, std::vector<size_t>> pidToOffsets;
mutable std::mutex mtx; //mutable for "it to be locked in const member functions"
bool canAllocateAt(size_t index, size_t bytes) const;
};