scsl  0.2.4
Shimmering Clarity Standard Library
Arena.h
Go to the documentation of this file.
1 
30 
31 #ifndef KIMODEM_ARENA_H
32 #define KIMODEM_ARENA_H
33 
34 
35 #include <cstddef>
36 #include <cstdint>
37 #include <iostream>
38 #include <sys/stat.h>
39 
40 #include "Exceptions.h"
41 
42 
43 #if defined(__WIN64__) || defined(__WIN32__) || defined(WIN32)
44 
45 #include <Windows.h>
46 #include <fileapi.h>
47 
48 #endif
49 
50 
51 namespace scsl {
52 
53 
57 enum class ArenaType
58  : uint8_t {
60  Uninit,
62  Static,
64  Alloc,
67 };
68 
69 
80 class Arena {
81 public:
83  Arena();
84 
85  ~Arena();
86 
96  int SetStatic(uint8_t *mem, size_t memSize);
97 
104  int SetAlloc(size_t allocSize);
105 
106 
114 #if defined(__posix__) || defined(__linux__) || defined(__APPLE__)
115  int MemoryMap(int memFileDes, size_t memSize);
116 #else
117 
118  int MemoryMap(int memFileDes, size_t memSize)
119  { (void)memFileDes; (void)memSize; throw NotImplemented("WIN32"); }
120 
121 #endif
130  int Create(const char *path, size_t fileSize);
131 
141  int Open(const char *path);
142 
146  uint8_t *Start() const
147  { return this->store; }
148 
152  uint8_t *End()
153  { return this->store + this->size; }
154 
160  bool CursorInArena(const uint8_t *cursor);
161 
165  size_t Size() const
166  { return this->size; }
167 
171  ArenaType Type() const
172  { return this->arenaType; }
173 
175  bool Ready() const
176  { return this->Type() != ArenaType::Uninit; };
177 
179  void Clear();
180 
184  void Destroy();
185 
193  int Write(const char *path);
194 
203  uint8_t &operator[](size_t index);
204 
205 private:
206  uint8_t *store;
207  size_t size;
208  int fd;
209  ArenaType arenaType;
210 };
211 
212 
226 std::ostream &operator<<(std::ostream &os, Arena &arena);
227 
228 
229 } // namespace scsl
230 
231 
232 #endif
Custom exceptions for use in SCSL used in writing test programs.
Definition: Arena.h:80
int Create(const char *path, size_t fileSize)
int SetAlloc(size_t allocSize)
Definition: Arena.cc:68
int SetStatic(uint8_t *mem, size_t memSize)
Definition: Arena.cc:58
int Write(const char *path)
Definition: Arena.cc:337
uint8_t * End()
Definition: Arena.h:152
bool Ready() const
Ready returns whether the arena is initialized.
Definition: Arena.h:175
int Open(const char *path)
void Destroy()
Definition: Arena.cc:257
uint8_t & operator[](size_t index)
Definition: Arena.cc:364
void Clear()
Clear zeroizes the memory in the arena.
Definition: Arena.cc:246
ArenaType Type() const
Definition: Arena.h:171
bool CursorInArena(const uint8_t *cursor)
Definition: Arena.cc:226
size_t Size() const
Definition: Arena.h:165
Arena()
An Arena is initialized with no backing memory.
Definition: Arena.cc:45
~Arena()
Definition: Arena.cc:51
uint8_t * Start() const
Definition: Arena.h:146
int MemoryMap(int memFileDes, size_t memSize)
Definition: Arena.h:118
Definition: Exceptions.h:37
scsl is the top-level namespace containing all the code in this library.
Definition: scsl.h:43
ArenaType
Definition: Arena.h:58
@ 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:299