2023-10-06 03:13:46 +00:00
|
|
|
#ifndef KIMODEM_ARENA_H
|
|
|
|
#define KIMODEM_ARENA_H
|
|
|
|
|
|
|
|
|
2023-10-09 22:23:23 +00:00
|
|
|
#include <iostream>
|
2023-10-06 03:13:46 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
|
2023-10-09 22:23:23 +00:00
|
|
|
namespace klib {
|
|
|
|
|
|
|
|
|
|
|
|
enum ArenaType : uint8_t {
|
|
|
|
ARENA_UNINIT,
|
|
|
|
ARENA_STATIC,
|
|
|
|
ARENA_ALLOC,
|
|
|
|
ARENA_MMAP,
|
|
|
|
};
|
|
|
|
|
|
|
|
class Arena {
|
|
|
|
public:
|
|
|
|
Arena();
|
|
|
|
|
|
|
|
~Arena();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* InitializeArena is intended for use only with systems that
|
|
|
|
* do not initialize new variables to zero. It should be called
|
|
|
|
* exactly once, at the start of the program. Any other time the
|
|
|
|
* arena needs to be reset, it should be called with Clear or
|
|
|
|
* Destroy.
|
|
|
|
*/
|
|
|
|
void Initialize();
|
|
|
|
|
|
|
|
int SetStatic(uint8_t *, size_t);
|
|
|
|
|
|
|
|
int SetAlloc(size_t allocSize);
|
|
|
|
|
2023-10-06 03:13:46 +00:00
|
|
|
#if defined(__linux__)
|
2023-10-09 23:20:26 +00:00
|
|
|
int MemoryMap(int memFileDes, size_t memSize); // Arena will own fd.
|
|
|
|
int Create(const char *path, size_t fileSize, mode_t mode);
|
|
|
|
int Open(const char *path);
|
2023-10-06 03:13:46 +00:00
|
|
|
#endif
|
|
|
|
|
2023-10-09 22:23:23 +00:00
|
|
|
uint8_t *NewCursor() const { return this->store; }
|
|
|
|
uint8_t *End() { return this->store + this->size; }
|
2023-10-09 23:20:26 +00:00
|
|
|
bool CursorInArena(const uint8_t *cursor);
|
2023-10-09 22:23:23 +00:00
|
|
|
|
|
|
|
size_t Size() const
|
|
|
|
{ return this->size; }
|
|
|
|
|
|
|
|
uint8_t Type() const
|
|
|
|
{ return this->arenaType; }
|
|
|
|
|
2023-10-09 23:20:26 +00:00
|
|
|
uintptr_t Store() { return (uintptr_t)this->store; }
|
|
|
|
|
2023-10-09 22:23:23 +00:00
|
|
|
bool Ready() const { return this->Type() != ARENA_UNINIT; };
|
|
|
|
void Clear();
|
|
|
|
void Destroy(); /* dispose of any memory used by arena */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DANGER: if arena is file backed (mmap or open), DO NOT WRITE TO THE
|
|
|
|
* BACKING FILE!
|
|
|
|
*/
|
|
|
|
int Write(const char *path);
|
|
|
|
|
|
|
|
uint8_t &operator[](size_t index)
|
|
|
|
{ return this->store[index]; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint8_t *store;
|
|
|
|
size_t size;
|
|
|
|
int fd;
|
|
|
|
ArenaType arenaType;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-10-09 23:20:26 +00:00
|
|
|
std::ostream &operator<<(std::ostream& os, Arena &arena);
|
2023-10-06 03:13:46 +00:00
|
|
|
|
2023-10-06 06:08:35 +00:00
|
|
|
|
2023-10-09 22:23:23 +00:00
|
|
|
} // namespace klib
|
2023-10-06 03:13:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
#endif
|