scsl/Arena.cc

282 lines
4.0 KiB
C++
Raw Normal View History

#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
2023-10-06 03:13:46 +00:00
#if defined(__linux__)
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#endif
2023-10-06 06:08:35 +00:00
#include <ios>
2023-10-06 03:13:46 +00:00
#include "Arena.h"
namespace klib {
Arena::Arena()
: store(nullptr), size(0), fd(0), arenaType(ARENA_UNINIT)
{}
Arena::~Arena()
{
this->Destroy();
}
2023-10-06 06:08:35 +00:00
void
Arena::Initialize()
2023-10-06 06:08:35 +00:00
{
assert(this->arenaType != ARENA_UNINIT);
this->store = nullptr;
this->size = 0;
this->arenaType = ARENA_UNINIT;
this->fd = 0;
2023-10-06 06:08:35 +00:00
}
2023-10-06 03:13:46 +00:00
int
Arena::SetStatic(uint8_t *mem, size_t allocSize)
2023-10-06 03:13:46 +00:00
{
this->store = mem;
this->size = allocSize;
this->arenaType = ARENA_STATIC;
2023-10-06 03:13:46 +00:00
return 0;
}
int
Arena::SetAlloc(size_t allocSize)
2023-10-06 03:13:46 +00:00
{
if (this->size > 0) {
this->Destroy();
2023-10-06 03:13:46 +00:00
}
this->arenaType = ARENA_ALLOC;
this->size = allocSize;
this->store = new uint8_t[allocSize];
if (this->store == nullptr) {
2023-10-06 03:13:46 +00:00
return -1;
}
this->Clear();
2023-10-06 03:13:46 +00:00
return 0;
}
#if defined(__linux__)
int
MMapArena(Arena &arena, int fd, size_t size)
2023-10-06 03:13:46 +00:00
{
if (this->size > 0) {
if (DestroyArena(arena) != 0) {
2023-10-06 03:13:46 +00:00
return -1;
}
}
this->arenaType = ARENA_MMAP;
this->size = size;
this->store = (uint8_t *)mmap(NULL, size, PROT_RW, MAP_SHARED, fd, 0);
if ((void *)this->store == MAP_FAILED) {
2023-10-06 03:13:46 +00:00
return -1;
}
this->fd = fd;
2023-10-06 03:13:46 +00:00
return 0;
}
int
2023-10-06 06:08:35 +00:00
OpenArena(Arena &arena, const char *path)
2023-10-06 03:13:46 +00:00
{
struct stat st;
if (this->size > 0) {
if (DestroyArena(arena) != 0) {
2023-10-06 03:13:46 +00:00
return -1;
}
}
if (stat(path, &st) != 0) {
return -1;
}
this->fd = open(path, O_RDWR);
if (this->fd == -1) {
2023-10-06 03:13:46 +00:00
return -1;
}
return MMapArena(arena, this->fd, (size_t)st.st_size);
2023-10-06 03:13:46 +00:00
}
int
CreateArena(Arena &arena, const char *path, size_t size, mode_t mode)
2023-10-06 03:13:46 +00:00
{
int fd = 0;
if (this->size > 0) {
if (DestroyArena(arena) != 0) {
2023-10-06 03:13:46 +00:00
return -1;
}
}
fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode);
2023-10-06 03:13:46 +00:00
if (fd == -1) {
return -1;
}
if (ftruncate(fd, size) == -1) {
2023-10-06 03:13:46 +00:00
return -1;
}
close(fd);
2023-10-06 06:08:35 +00:00
return OpenArena(arena, path);
2023-10-06 03:13:46 +00:00
}
#endif
bool
Arena::CursorInArena(uint8_t *cursor)
{
if (cursor < this->store) {
return false;
}
if (cursor >= this->End()) {
return false;
}
return true;
}
2023-10-06 06:08:35 +00:00
/*
* ClearArena clears the memory being used, removing any data
* present. It does not free the memory; it is effectively a
* wrapper around memset.
*/
2023-10-06 03:13:46 +00:00
void
Arena::Clear()
2023-10-06 03:13:46 +00:00
{
if (this->size == 0) {
2023-10-06 03:13:46 +00:00
return;
}
memset(this->store, 0, this->size);
2023-10-06 03:13:46 +00:00
}
void
Arena::Destroy()
2023-10-06 03:13:46 +00:00
{
if (this->arenaType == ARENA_UNINIT) {
return;
2023-10-06 03:13:46 +00:00
}
switch (this->arenaType) {
case ARENA_STATIC:
break;
case ARENA_ALLOC:
delete this->store;
break;
#if defined(__linux__)
case ARENA_MMAP:
if (munmap(this->store, this->size) == -1) {
return -1;
}
if (close(this->fd) == -1) {
return -1;
}
this->fd = 0;
break;
#endif
default:
#if defined(NDEBUG)
2023-10-06 03:13:46 +00:00
return -1;
#else
abort();
#endif
2023-10-06 06:08:35 +00:00
2023-10-06 03:13:46 +00:00
}
this->arenaType = ARENA_UNINIT;
this->size = 0;
this->store = nullptr;
return;
2023-10-06 03:13:46 +00:00
}
std::ostream &
operator<<(std::ostream &os, const Arena arena)
2023-10-06 06:08:35 +00:00
{
auto cursor = arena.NewCursor();
char cursorString[17] = {0};
snprintf(cursorString, 16, "%0p", cursor);
os << "Arena<";
switch (arena.Type()) {
case ARENA_UNINIT:
os << "uninitialized";
break;
case ARENA_STATIC:
os << "static";
break;
case ARENA_ALLOC:
os << "allocated";
break;
2023-10-06 06:08:35 +00:00
#if defined(__linux__)
case ARENA_MMAP:
os << "mmap/file";
break;
2023-10-06 06:08:35 +00:00
#endif
default:
os << "unknown (this is a bug)";
2023-10-06 06:08:35 +00:00
}
os << ">@0x";
os << std::hex << (uintptr_t) &arena;
os << std::dec;
os << ",store<" << arena.Size() << "B>@0x";
os << std::hex << cursorString;
os << std::dec;
return os;
2023-10-06 06:08:35 +00:00
}
2023-10-06 03:13:46 +00:00
int
Arena::Write(const char *path)
2023-10-06 03:13:46 +00:00
{
FILE *arenaFile = NULL;
int retc = -1;
2023-10-06 03:13:46 +00:00
#if defined(__posix__)
2023-10-06 03:13:46 +00:00
arenaFile = fopen(path, "w");
if (arenaFile == NULL) {
#else
if (fopen_s(&arenaFile, path, "w") != 0) {
#endif
2023-10-06 03:13:46 +00:00
return -1;
}
if (fwrite(this->store, sizeof(*this->store), this->size,
arenaFile) == this->size) {
2023-10-06 03:13:46 +00:00
retc = 0;
}
if (fclose(arenaFile) != 0) {
return -1;
}
return retc;
}
} // namespace klib