scsl/Arena.cc

378 lines
6.8 KiB
C++
Raw Normal View History

///
/// \file Arena.cc
/// \author K. Isom <kyle@imap.cc>
/// \date 2023-10-06
/// \brief Memory management using an arena.
///
/// Copyright 2023 K. Isom <kyle@imap.cc>
///
/// Permission to use, copy, modify, and/or distribute this software for
/// any purpose with or without fee is hereby granted, provided that
/// the above copyright notice and this permission notice appear in all /// copies.
///
/// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
/// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
/// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
/// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
/// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
/// OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
/// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
/// PERFORMANCE OF THIS SOFTWARE.
///
#include <cstdio>
#include <cstdlib>
#include <cstring>
2023-10-06 03:13:46 +00:00
2023-10-11 01:57:43 +00:00
#if defined(__posix__) || defined(__linux__) || defined(__APPLE__)
2023-10-06 03:13:46 +00:00
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define PROT_RW (PROT_WRITE|PROT_READ)
2023-10-06 03:13:46 +00:00
#endif
2023-10-06 06:08:35 +00:00
#include <ios>
2023-10-06 03:13:46 +00:00
#include "Arena.h"
2023-10-15 01:38:01 +00:00
namespace scsl {
Arena::Arena()
: store(nullptr), size(0), fd(0), arenaType(ArenaType::Uninit)
2023-10-10 13:02:21 +00:00
{
}
Arena::~Arena()
{
this->Destroy();
}
2023-10-06 06:08:35 +00:00
2023-10-06 03:13:46 +00:00
int
Arena::SetStatic(uint8_t *mem, size_t memSize)
2023-10-06 03:13:46 +00:00
{
this->store = mem;
this->size = memSize;
this->arenaType = ArenaType::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 = ArenaType::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;
}
2023-10-11 01:57:43 +00:00
#if defined(__posix__) || defined(__linux__) || defined(__APPLE__)
2023-10-06 03:13:46 +00:00
int
Arena::MemoryMap(int memFileDes, size_t memSize)
2023-10-06 03:13:46 +00:00
{
if (this->size > 0) {
this->Destroy();
2023-10-06 03:13:46 +00:00
}
this->arenaType = ArenaType::MemoryMapped;
this->size = memSize;
this->store = (uint8_t *) mmap(NULL, memSize, PROT_RW, MAP_SHARED,
memFileDes, 0);
if ((void *) this->store == MAP_FAILED) {
2023-10-06 03:13:46 +00:00
return -1;
}
this->fd = memFileDes;
2023-10-06 03:13:46 +00:00
return 0;
}
int
Arena::Open(const char *path)
2023-10-06 03:13:46 +00:00
{
struct stat st{};
2023-10-06 03:13:46 +00:00
if (this->size > 0) {
this->Destroy();
2023-10-06 03:13:46 +00:00
}
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 this->MemoryMap(this->fd, (size_t) st.st_size);
2023-10-06 03:13:46 +00:00
}
int
Arena::Create(const char *path, size_t fileSize)
2023-10-06 03:13:46 +00:00
{
FILE *fHandle = nullptr;
int newFileDes = 0;
2023-10-06 03:13:46 +00:00
if (this->size > 0) {
this->Destroy();
2023-10-06 03:13:46 +00:00
}
fHandle = fopen(path, "w");
if (fHandle == nullptr) {
2023-10-06 03:13:46 +00:00
return -1;
}
newFileDes = fileno(fHandle);
2023-10-06 03:13:46 +00:00
if (ftruncate(newFileDes, fileSize) == -1) {
2023-10-06 03:13:46 +00:00
return -1;
}
close(newFileDes);
return this->Open(path);
}
2023-10-10 13:02:21 +00:00
#elif defined(__WIN64__) || defined(__WIN32__) || defined(WIN32)
int
Arena::Open(const char *path)
{
2023-10-10 23:44:29 +00:00
HANDLE fHandle;
DWORD fRead = 0;
size_t fSize;
size_t fRemaining;
auto *cursor = this->store;
OVERLAPPED overlap = {0};
2023-10-10 13:02:21 +00:00
2023-10-10 02:59:21 +00:00
fHandle = CreateFileA(
2023-10-10 23:44:29 +00:00
(LPSTR) path,
2023-10-10 02:59:21 +00:00
GENERIC_READ,
2023-10-10 23:44:29 +00:00
(FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE),
2023-10-10 02:59:21 +00:00
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (fHandle == INVALID_HANDLE_VALUE) {
2023-10-10 23:44:29 +00:00
return Windows::DisplayWinError("CreateFileA", NULL);
2023-10-10 02:59:21 +00:00
}
2023-10-10 19:15:14 +00:00
if (SetFilePointer(fHandle, 0, 0, FILE_BEGIN) != 0) {
2023-10-10 23:44:29 +00:00
return Windows::DisplayWinError("SetFilePointer", fHandle);
2023-10-10 19:15:14 +00:00
}
2023-10-10 23:44:29 +00:00
if (GetFileSizeEx(fHandle, reinterpret_cast<PLARGE_INTEGER>(&fSize)) !=
TRUE) {
return Windows::DisplayWinError("GetFileSizeEx", fHandle);
2023-10-10 02:59:21 +00:00
}
this->SetAlloc(fSize);
2023-10-10 19:15:14 +00:00
cursor = this->NewCursor();
2023-10-06 03:13:46 +00:00
2023-10-10 19:15:14 +00:00
this->store[0] = 1;
2023-10-10 02:59:21 +00:00
fRemaining = fSize;
while (fRemaining != 0) {
overlap.Offset = (fSize - fRemaining);
2023-10-10 23:44:29 +00:00
if (ReadFile(fHandle, cursor, fSize - 1,
2023-10-10 13:02:21 +00:00
&fRead,
&overlap) != TRUE) {
2023-10-10 19:15:14 +00:00
auto errorCode = GetLastError();
if (errorCode != ERROR_HANDLE_EOF) {
this->Destroy();
2023-10-10 23:44:29 +00:00
return Windows::DisplayWinError("ReadFile", fHandle);
2023-10-10 19:15:14 +00:00
}
break;
2023-10-10 02:59:21 +00:00
}
cursor += fRead;
fRemaining -= fRead;
}
CloseHandle(fHandle);
return 0;
2023-10-06 03:13:46 +00:00
}
2023-10-10 13:02:21 +00:00
int
2023-10-10 23:44:29 +00:00
Arena::Create(const char *path, size_t fileSize)
2023-10-10 13:02:21 +00:00
{
2023-10-10 23:44:29 +00:00
auto errorCode = Windows::CreateFixedSizeFile(path, fileSize);
if (errorCode != 0) {
return errorCode;
2023-10-10 13:02:21 +00:00
}
return this->Open(path);
}
2023-10-10 23:44:29 +00:00
2023-10-10 13:02:21 +00:00
#endif
2023-10-11 01:57:43 +00:00
bool
Arena::CursorInArena(const 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 == ArenaType::Uninit) {
return;
2023-10-06 03:13:46 +00:00
}
switch (this->arenaType) {
case ArenaType::Static:
break;
case ArenaType::Alloc:
delete[] this->store;
break;
2023-10-11 01:57:43 +00:00
#if defined(__posix__) || defined(__linux__) || defined(__APPLE__)
2023-10-10 23:44:29 +00:00
case ArenaType::MemoryMapped:
if (munmap(this->store, this->size) == -1) {
abort();
return;
}
if (close(this->fd) == -1) {
abort();
}
this->fd = 0;
break;
#endif
default:
#if defined(NDEBUG)
2023-10-11 01:57:43 +00:00
return;
#else
abort();
#endif
2023-10-06 06:08:35 +00:00
2023-10-06 03:13:46 +00:00
}
this->arenaType = ArenaType::Uninit;
this->size = 0;
this->store = nullptr;
return;
2023-10-06 03:13:46 +00:00
}
std::ostream &
operator<<(std::ostream &os, Arena &arena)
2023-10-06 06:08:35 +00:00
{
2023-10-15 01:38:01 +00:00
auto cursor = arena.Start();
char cursorString[33] = {0};
snprintf(cursorString, 32, "%#016llx",
2023-10-10 23:44:29 +00:00
(long long unsigned int) cursor);
os << "Arena<";
switch (arena.Type()) {
case ArenaType::Uninit:
os << "uninitialized";
break;
case ArenaType::Static:
os << "static";
break;
case ArenaType::Alloc:
os << "allocated";
break;
2023-10-11 01:57:43 +00:00
#if defined(__posix__) || defined(__linux__) || defined(__APPLE__)
2023-10-10 23:44:29 +00:00
case ArenaType::MemoryMapped:
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>@";
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
{
2023-10-10 19:15:14 +00:00
FILE *arenaFile = nullptr;
int retc = -1;
2023-10-06 03:13:46 +00:00
2023-10-11 01:57:43 +00:00
#if defined(__posix__) || defined(__linux__) || defined(__APPLE__)
2023-10-06 03:13:46 +00:00
arenaFile = fopen(path, "w");
if (arenaFile == nullptr) {
#else
2023-10-10 23:44:29 +00:00
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;
}
uint8_t &
Arena::operator[](size_t index)
{
2023-10-10 23:44:29 +00:00
if (index > this->size) {
2023-10-16 00:09:31 +00:00
#if defined(SCSL_DESKTOP_BUILD) and !defined(SCSL_NOEXCEPT)
2023-10-10 23:44:29 +00:00
throw std::range_error("index out of range");
#else
2023-10-10 23:44:29 +00:00
abort();
#endif
2023-10-10 23:44:29 +00:00
}
return this->store[index];
}
2023-10-15 01:38:01 +00:00
} // namespace scsl