scsl/Arena.cc

428 lines
7.2 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__)
2023-10-06 03:13:46 +00:00
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define PROT_RW (PROT_WRITE|PROT_READ)
2023-10-10 13:02:21 +00:00
#elif defined(__WIN64__) || defined(__WIN32__) || defined(WIN32)
2023-10-10 02:59:21 +00:00
#include <Windows.h>
2023-10-10 13:02:21 +00:00
#include <winbase.h>
#include <fileapi.h>
2023-10-10 13:02:21 +00:00
#include <strsafe.h>
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"
namespace klib {
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
void
Arena::Initialize()
2023-10-06 06:08:35 +00:00
{
assert(this->arenaType != ArenaType::Uninit);
this->store = nullptr;
this->size = 0;
this->arenaType = ArenaType::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 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;
}
#if defined(__linux__)
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, mode_t mode)
2023-10-06 03:13:46 +00:00
{
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
}
newFileDes = open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
if (newFileDes == -1) {
2023-10-06 03:13:46 +00:00
return -1;
}
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)
static void
displayWinErr(LPTSTR lpszFunction)
{
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
// Display the error message and exit the process
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}
int
Arena::Open(const char *path)
{
2023-10-10 13:02:21 +00:00
HANDLE fHandle;
DWORD fRead = 0;
size_t fSize;
size_t fRemaining;
auto *cursor = this->store;
2023-10-10 19:15:14 +00:00
OVERLAPPED overlap = {0};
2023-10-10 13:02:21 +00:00
2023-10-10 02:59:21 +00:00
fHandle = CreateFileA(
(LPSTR)path,
GENERIC_READ,
(FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE),
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (fHandle == INVALID_HANDLE_VALUE) {
2023-10-10 13:02:21 +00:00
displayWinErr("CreateFileA");
2023-10-10 02:59:21 +00:00
return -1;
}
2023-10-10 19:15:14 +00:00
if (SetFilePointer(fHandle, 0, 0, FILE_BEGIN) != 0) {
displayWinErr("SetFilePointer");
CloseHandle(fHandle);
return -1;
}
if (GetFileSizeEx(fHandle, reinterpret_cast<PLARGE_INTEGER>(&fSize)) != TRUE) {
2023-10-10 13:02:21 +00:00
displayWinErr("GetFileSizeEx");
2023-10-10 02:59:21 +00:00
CloseHandle(fHandle);
return -1;
}
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 19:15:14 +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) {
displayWinErr("ReadFile");
CloseHandle(fHandle);
this->Destroy();
return -1;
}
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
Arena::Create(const char *path, size_t fileSize, DWORD mode)
{
HANDLE fHandle;
fHandle = CreateFileA(
(LPSTR)path,
GENERIC_READ|GENERIC_WRITE,
mode,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (fHandle == INVALID_HANDLE_VALUE) {
displayWinErr("Create::CreateFileA");
return -1;
}
if (SetFileValidData(fHandle, fileSize) != fileSize) {
displayWinErr("SetFileValidData");
CloseHandle(fHandle);
return -1;
}
CloseHandle(fHandle);
return this->Open(path);
}
#endif
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;
#if defined(__linux__)
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-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 = 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
{
auto cursor = arena.NewCursor();
char cursorString[33] = {0};
snprintf(cursorString, 32, "%#016llx",
(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-06 06:08:35 +00:00
#if defined(__linux__)
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
#if defined(__posix__) || defined(__linux__)
2023-10-06 03:13:46 +00:00
arenaFile = fopen(path, "w");
if (arenaFile == nullptr) {
#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;
}
uint8_t &
Arena::operator[](size_t index)
{
if (index > this->size) {
#if defined(DESKTOP_BUILD) and !defined(KLIB_NO_ASSERT)
throw std::range_error("index out of range");
#else
abort();
#endif
}
return this->store[index];
}
} // namespace klib