scsl  1.1.1
Shimmering Clarity Standard Library
Arena.h
Go to the documentation of this file.
1 
25 #ifndef KIMODEM_ARENA_H
26 #define KIMODEM_ARENA_H
27 
28 
29 #include <cstddef>
30 #include <cstdint>
31 #include <iostream>
32 #include <sys/stat.h>
33 
34 #include "sctest/Exceptions.h"
35 
36 
37 #if defined(__WIN64__) || defined(__WIN32__) || defined(WIN32)
38 
39 #include <Windows.h>
40 #include <fileapi.h>
41 
42 #endif
43 
44 
45 namespace scsl {
46 
47 
51 enum class ArenaType
52  : uint8_t {
54  Uninit,
56  Static,
58  Alloc,
61 };
62 
63 
74 class Arena {
75 public:
77  Arena();
78 
79  ~Arena();
80 
90  int SetStatic(uint8_t *mem, size_t memSize);
91 
98  int SetAlloc(size_t allocSize);
99 
100 
108 #if defined(__posix__) || defined(__linux__) || defined(__APPLE__)
109  int MemoryMap(int memFileDes, size_t memSize);
110 #else
111 
112  int MemoryMap(int memFileDes, size_t memSize)
113  { (void)memFileDes; (void)memSize; throw NotImplemented("WIN32"); }
114 
115 #endif
124  int Create(const char *path, size_t fileSize);
125 
135  int Open(const char *path);
136 
140  uint8_t *Start() const
141  { return this->store; }
142 
146  uint8_t *End()
147  { return this->store + this->size; }
148 
154  bool CursorInArena(const uint8_t *cursor);
155 
159  size_t Size() const
160  { return this->size; }
161 
165  ArenaType Type() const
166  { return this->arenaType; }
167 
169  bool Ready() const
170  { return this->Type() != ArenaType::Uninit; };
171 
173  void Clear();
174 
178  void Destroy();
179 
187  int Write(const char *path);
188 
197  uint8_t &operator[](size_t index);
198 
199 private:
200  uint8_t *store;
201  size_t size;
202  int fd;
203  ArenaType arenaType;
204 };
205 
206 
220 std::ostream &operator<<(std::ostream &os, Arena &arena);
221 
222 
223 } // namespace scsl
224 
225 
226 #endif
Custom exceptions for use in SCSL used in writing test programs.
Fixed, pre-allocated memory.
Definition: Arena.h:74
int Create(const char *path, size_t fileSize)
int SetAlloc(size_t allocSize)
int SetStatic(uint8_t *mem, size_t memSize)
int Write(const char *path)
uint8_t * End()
Definition: Arena.h:146
bool Ready() const
Ready returns whether the arena is initialized.
Definition: Arena.h:169
int Open(const char *path)
void Destroy()
void Clear()
Clear zeroizes the memory in the arena.
ArenaType Type() const
Definition: Arena.h:165
bool CursorInArena(const uint8_t *cursor)
size_t Size() const
Definition: Arena.h:159
Arena()
An Arena is initialized with no backing memory.
uint8_t & operator[](size_t index)
uint8_t * Start() const
Definition: Arena.h:140
int MemoryMap(int memFileDes, size_t memSize)
Definition: Arena.h:112
scsl is the top-level namespace containing all the code in this library.
Definition: scsl.h:43
ArenaType
Definition: Arena.h:52
@ 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)