scsl/Arena.cc

261 lines
3.9 KiB
C++
Raw Normal View History

2023-10-06 03:13:46 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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
#if defined(DESKTOP_BUILD)
#include <iostream>
#include <ios>
#endif
2023-10-06 03:13:46 +00:00
#include "Arena.h"
#define ARENA_UNINIT 0
#define ARENA_STATIC 1
#define ARENA_ALLOC 2
2023-10-06 06:08:35 +00:00
#if defined(__linux__)
2023-10-06 03:13:46 +00:00
#define ARENA_MMAP 3
#define PROT_RW PROT_READ|PROT_WRITE
2023-10-06 06:08:35 +00:00
#endif
void
InitializeArena(Arena &arena)
{
arena.Store = NULL;
arena.Size = 0;
arena.Type = ARENA_UNINIT;
arena.fd = 0;
}
2023-10-06 03:13:46 +00:00
int
2023-10-06 06:08:35 +00:00
NewStaticArena(Arena &arena, uint8_t *mem, size_t size)
2023-10-06 03:13:46 +00:00
{
2023-10-06 06:08:35 +00:00
arena.Store = mem;
arena.Size = size;
arena.Type = ARENA_STATIC;
2023-10-06 03:13:46 +00:00
return 0;
}
int
2023-10-06 06:08:35 +00:00
AllocNewArena(Arena & arena, size_t size)
2023-10-06 03:13:46 +00:00
{
2023-10-06 06:08:35 +00:00
if (arena.Size > 0) {
if (DestroyArena(arena) != 0) {
2023-10-06 03:13:46 +00:00
return -1;
}
}
2023-10-06 06:08:35 +00:00
arena.Type = ARENA_ALLOC;
arena.Size = size;
arena.Store = (uint8_t *)calloc(sizeof(uint8_t), size);
if (arena.Store == NULL) {
2023-10-06 03:13:46 +00:00
return -1;
}
return 0;
}
#if defined(__linux__)
int
2023-10-06 06:08:35 +00:00
MMapArena(Arena &arena, int fd, size_t Size)
2023-10-06 03:13:46 +00:00
{
2023-10-06 06:08:35 +00:00
if (arena.Size > 0) {
2023-10-06 03:13:46 +00:00
if (arena_destroy(arena) != 0) {
return -1;
}
}
2023-10-06 06:08:35 +00:00
arena.Type = ARENA_MMAP;
arena.Size = Size;
arena.store = (uint8_t *)mmap(NULL, Size, PROT_RW, MAP_SHARED, fd, 0);
2023-10-06 03:13:46 +00:00
if ((void *)arena.store == MAP_FAILED) {
return -1;
}
arena.fd = fd;
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;
2023-10-06 06:08:35 +00:00
if (arena.Size > 0) {
2023-10-06 03:13:46 +00:00
if (arena_destroy(arena) != 0) {
return -1;
}
}
if (stat(path, &st) != 0) {
return -1;
}
arena.fd = open(path, O_RDWR);
if (arena.fd == -1) {
return -1;
}
2023-10-06 06:08:35 +00:00
return MMapArena(arena, arena.fd, (size_t)st.st_size);
2023-10-06 03:13:46 +00:00
}
int
2023-10-06 06:08:35 +00:00
CreateArena(Arena &arena, const char *path, size_t Size, mode_t mode)
2023-10-06 03:13:46 +00:00
{
int fd = 0;
2023-10-06 06:08:35 +00:00
if (arena.Size > 0) {
2023-10-06 03:13:46 +00:00
if (arena_destroy(arena) != 0) {
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;
}
2023-10-06 06:08:35 +00:00
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
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
2023-10-06 06:08:35 +00:00
ClearArena(Arena &arena)
2023-10-06 03:13:46 +00:00
{
2023-10-06 06:08:35 +00:00
if (arena.Size == 0) {
2023-10-06 03:13:46 +00:00
return;
}
2023-10-06 06:08:35 +00:00
memset(arena.Store, 0, arena.Size);
2023-10-06 03:13:46 +00:00
}
int
2023-10-06 06:08:35 +00:00
DestroyArena(Arena &arena)
2023-10-06 03:13:46 +00:00
{
2023-10-06 06:08:35 +00:00
if (arena.Type == ARENA_UNINIT) {
2023-10-06 03:13:46 +00:00
return 0;
}
2023-10-06 06:08:35 +00:00
switch (arena.Type) {
2023-10-06 03:13:46 +00:00
case ARENA_STATIC:
break;
case ARENA_ALLOC:
2023-10-06 06:08:35 +00:00
free(arena.Store);
2023-10-06 03:13:46 +00:00
break;
2023-10-06 06:08:35 +00:00
#if defined(__linux__)
2023-10-06 03:13:46 +00:00
case ARENA_MMAP:
2023-10-06 06:08:35 +00:00
if (munmap(arena.store, arena.Size) == -1) {
2023-10-06 03:13:46 +00:00
return -1;
}
if (close(arena.fd) == -1) {
return -1;
}
arena.fd = 0;
break;
2023-10-06 06:08:35 +00:00
#endif
2023-10-06 03:13:46 +00:00
default:
2023-10-06 06:08:35 +00:00
#if defined(NDEBUG)
2023-10-06 03:13:46 +00:00
return -1;
2023-10-06 06:08:35 +00:00
#else
abort();
#endif
2023-10-06 03:13:46 +00:00
}
2023-10-06 06:08:35 +00:00
arena.Type = ARENA_UNINIT;
arena.Size = 0;
arena.Store = NULL;
2023-10-06 03:13:46 +00:00
return 0;
}
2023-10-06 06:08:35 +00:00
#if defined(DESKTOP_BUILD)
void
DisplayArena(const Arena &arena)
{
std::cout << "Arena @ 0x";
std::cout << std::hex << (uintptr_t)&arena << std::endl;
std::cout << std::dec;
std::cout << "\tStore is " << arena.Size << " bytes at address 0x";
std::cout << std::hex << (uintptr_t)&(arena.Store) << std::endl;
std::cout << "\tType: ";
switch (arena.Type) {
case ARENA_UNINIT:
std::cout << "uninitialized";
break;
case ARENA_STATIC:
std::cout << "static";
break;
case ARENA_ALLOC:
std::cout << "allocated";
break;
#if defined(__linux__)
case ARENA_MMAP:
std::cout << "mmap/file"
break;
#endif
default:
std::cout << "unknown (this is a bug)";
}
std::cout << std::endl;
}
#else
void
DisplayArena(const Arena &arena)
{
}
#endif
2023-10-06 03:13:46 +00:00
int
2023-10-06 06:08:35 +00:00
WriteArena(const Arena &arena, const char *path)
2023-10-06 03:13:46 +00:00
{
FILE *arenaFile = NULL;
int retc = -1;
arenaFile = fopen(path, "w");
if (arenaFile == NULL) {
return -1;
}
2023-10-06 06:08:35 +00:00
if (fwrite(arena.Store, sizeof(*arena.Store), arena.Size,
arenaFile) == arena.Size) {
2023-10-06 03:13:46 +00:00
retc = 0;
}
if (fclose(arenaFile) != 0) {
return -1;
}
return retc;
}