scsl  0.1.1
Shimmering Clarity Standard Library
Arena.h
Go to the documentation of this file.
1 
15 
16 #ifndef KIMODEM_ARENA_H
17 #define KIMODEM_ARENA_H
18 
19 
20 #include <iostream>
21 #include <sys/stat.h>
22 #include <cstddef>
23 #include <cstdint>
24 
25 #include "Exceptions.h"
26 
27 
28 #if defined(__WIN64__) || defined(__WIN32__) || defined(WIN32)
29 
30 #include <Windows.h>
31 #include <fileapi.h>
32 
33 #endif
34 
35 
36 namespace scsl {
37 
38 
42 enum class ArenaType
43  : uint8_t {
45  Uninit,
47  Static,
49  Alloc,
52 };
53 
54 
65 class Arena {
66 public:
68  Arena();
69 
70  ~Arena();
71 
81  int SetStatic(uint8_t *mem, size_t memSize);
82 
89  int SetAlloc(size_t allocSize);
90 
91 
99 #if defined(__posix__) || defined(__linux__) || defined(__APPLE__)
100  int MemoryMap(int memFileDes, size_t memSize);
101 #else
102 
103  int MemoryMap(int memFileDes, size_t memSize)
104  { (void)memFileDes; (void)memSize; throw NotImplemented("WIN32"); }
105 
106 #endif
115 #if defined(__posix__) || defined(__linux__) || defined(__APPLE__)
116  int Create(const char *path, size_t fileSize);
117 #elif defined(__WIN64__) || defined(__WIN32__) || defined(WIN32)
118  int Create(const char *path, size_t fileSize);
119 
120 #endif
121 
131  int Open(const char *path);
132 
136  uint8_t *Start() const
137  { return this->store; }
138 
142  uint8_t *End()
143  { return this->store + this->size; }
144 
150  bool CursorInArena(const uint8_t *cursor);
151 
155  size_t Size() const
156  { return this->size; }
157 
161  ArenaType Type() const
162  { return this->arenaType; }
163 
165  bool Ready() const
166  { return this->Type() != ArenaType::Uninit; };
167 
169  void Clear();
170 
174  void Destroy();
175 
183  int Write(const char *path);
184 
193  uint8_t &operator[](size_t index);
194 
195 private:
196  uint8_t *store;
197  size_t size;
198  int fd;
199  ArenaType arenaType;
200 };
201 
202 
216 std::ostream &operator<<(std::ostream &os, Arena &arena);
217 
218 
219 } // namespace scsl
220 
221 
222 #endif
Definition: Arena.h:65
int SetAlloc(size_t allocSize)
Definition: Arena.cc:50
int SetStatic(uint8_t *mem, size_t memSize)
Definition: Arena.cc:40
int Write(const char *path)
Definition: Arena.cc:319
uint8_t * End()
Definition: Arena.h:142
bool Ready() const
Ready returns whether the arena is initialized.
Definition: Arena.h:165
int Open(const char *path)
void Destroy()
Definition: Arena.cc:239
uint8_t & operator[](size_t index)
Definition: Arena.cc:346
void Clear()
Clear zeroizes the memory in the arena.
Definition: Arena.cc:228
ArenaType Type() const
Definition: Arena.h:161
bool CursorInArena(const uint8_t *cursor)
Definition: Arena.cc:208
size_t Size() const
Definition: Arena.h:155
Arena()
An Arena is initialized with no backing memory.
Definition: Arena.cc:27
uint8_t * Start() const
Definition: Arena.h:136
int MemoryMap(int memFileDes, size_t memSize)
Definition: Arena.h:103
Definition: Exceptions.h:19
scsl is the top-level namespace containing all the code in this library.
Definition: scsl.h:37
ArenaType
Definition: Arena.h:43
@ Static
Static is an arena backed by a static block of memory.
@ MemoryMapped
MemoryMapped is an arena backed by a memory-mapped file.
@ Uninit
Uninit is an unintialized arena.
@ Alloc
Alloc is an arena backed by allocated memory.
std::ostream & operator<<(std::ostream &os, Arena &arena)
Definition: Arena.cc:281