Compare commits

..

2 Commits

Author SHA1 Message Date
7dd0eb961a Add Arena Allocator
- Adding files that were not staged
2026-08-15 02:05:44 -07:00
1c11f4fe7a Add Arena Allocator 2026-08-15 02:04:14 -07:00
9 changed files with 424 additions and 57 deletions

View File

@@ -7,12 +7,15 @@ project(mimir)
set(PUBLIC_HEADERS set(PUBLIC_HEADERS
include/mimir.h include/mimir.h
include/arena.h
include/block.h include/block.h
include/util.h include/util.h
) )
set(SRCS set(SRCS
src/arena.cpp
src/block.cpp src/block.cpp
src/mimir.cpp
src/util.cpp src/util.cpp
) )

59
include/arena.h Normal file
View File

@@ -0,0 +1,59 @@
//
// arena.h
// Kraken Engine / Mimir
//
// Copyright 2026 Kearwood Gilbert. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of Kearwood Gilbert.
//
#pragma once
#include <string>
#include <cstddef>
namespace mimir {
class Arena
{
public:
Arena(size_t minSize = 1ULL << 24, size_t maxSize = 1ULL << 32);
~Arena();
std::byte* alloc(size_t size);
std::byte* allocA16(size_t size);
std::byte* allocA64(size_t size);
void reset();
private:
std::byte* m_data;
size_t m_minSize;
size_t m_maxSize;
size_t m_committedSize;
size_t m_usedSize;
static const size_t kWatermarkLen = 8;
size_t m_watermark[kWatermarkLen];
}; // class Arena
} // namespace mimir

View File

@@ -39,7 +39,6 @@
#endif #endif
namespace mimir { namespace mimir {
void init();
class Block class Block
{ {
public: public:
@@ -127,6 +126,7 @@ private:
// Assert if not locked // Assert if not locked
void assertLocked(); void assertLocked();
}; }; // class Block
} // namespace mimir } // namespace mimir

View File

@@ -32,4 +32,9 @@
#pragma once #pragma once
#include "block.h" #include "block.h"
#include "arena.h"
#include "util.h" #include "util.h"
namespace mimir {
void init();
}

View File

@@ -40,5 +40,5 @@ std::string GetFileExtension(const std::string& name);
std::string GetFileBase(const std::string& name); std::string GetFileBase(const std::string& name);
std::string GetFilePath(const std::string& name); std::string GetFilePath(const std::string& name);
} // namespace mimir
} // namespace util } // namespace util
} // namespace mimir

203
src/arena.cpp Normal file
View File

@@ -0,0 +1,203 @@
//
// arena.cpp
// Kraken Engine
//
// Copyright 2026 Kearwood Gilbert. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of Kearwood Gilbert.
//
#include "../include/mimir.h"
#include "mimir_impl.h"
#if defined(__unix__) || defined(__APPLE__) || defined(ANDROID)
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif
#if defined(_WIN32) || defined(_WIN64)
#include <Windows.h>
#include <memoryapi.h>
#endif
#include <errno.h>
#include <cassert>
#include <cstring>
#include <bit>
namespace mimir {
struct ArenaControlBlock
{
size_t allocated;
size_t comitted;
size_t next;
};
static_assert(sizeof(ArenaControlBlock) < 4096);
Arena::Arena(size_t minSize, size_t maxSize)
: m_minSize(KRAKEN_MEM_ROUND_UP_PAGE(minSize))
, m_maxSize(KRAKEN_MEM_ROUND_UP_PAGE(maxSize))
, m_data(nullptr)
, m_usedSize(0)
, m_committedSize(0)
{
assert(maxSize >= minSize);
for (size_t i = 0; i < kWatermarkLen; i++) {
m_watermark[i] = 0;
}
}
Arena::~Arena()
{
if (m_data) {
#if defined(_WIN32) || defined(_WIN64)
if (!VirtualFree((void*)m_data, 0, MEM_RELEASE)) {
ReportWindowsLastError("VirtualAlloc");
}
#elif defined(__unix__) || defined(__APPLE__) || defined(ANDROID)
munmap(m_data, m_maxSize);
#else
static_assert(false, "Not Implemented");
#endif
}
}
std::byte* Arena::alloc(size_t size)
{
size_t neededSize = m_usedSize + size;
if (m_data == nullptr) {
#if defined(_WIN32) || defined(_WIN64)
m_data = (std::byte*)VirtualAlloc(NULL, m_maxSize, MEM_RESERVE, PAGE_NOACCESS);
if (m_data == nullptr) {
ReportWindowsLastError("VirtualAlloc");
return nullptr;
}
#elif defined(__unix__) || defined(__APPLE__) || defined(ANDROID)
m_data = (std::byte*)mmap(NULL, m_maxSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (m_data == MAP_FAILED) {
return nullptr;
}
#else
static_assert(false, "Not Implemented");
#endif
}
if (neededSize < m_committedSize)
{
size_t newSize = std::bit_ceil(neededSize) << 1;
if (newSize < m_minSize) {
newSize = m_minSize;
}
newSize = KRAKEN_MEM_ROUND_UP_PAGE(newSize);
#if defined(_WIN32) || defined(_WIN64)
if(VirtualAlloc(m_data + m_committedSize, newSize - m_committedSize, MEM_COMMIT, PAGE_READWRITE) == nullptr) {
ReportWindowsLastError("VirtualAlloc");
return nullptr;
}
#elif defined(__unix__) || defined(__APPLE__) || defined(ANDROID)
if (mprotect(m_data + m_committedSize, newSize - m_committedSize, PROT_READ | PROT_WRITE) != 0) {
return nullptr;
}
#else
static_assert(false, "Not Implemented");
#endif
m_committedSize = newSize;
} // if (neededSize < m_committedSize)
std::byte* ret = m_data + m_usedSize;
m_usedSize += neededSize;
return ret;
}
std::byte* Arena::allocA16(size_t size)
{
uint32_t nextByte = (m_usedSize + 15) & ~15;
uint32_t roundedSize = (size + 15) & ~15;
if (alloc(size) == nullptr) {
return nullptr;
}
return m_data + nextByte;
}
std::byte* Arena::allocA64(size_t size)
{
uint32_t nextByte = (m_usedSize + 63) & ~63;
uint32_t roundedSize = (size + 63) & ~63;
if (alloc(size) == nullptr) {
return nullptr;
}
return m_data + nextByte;
}
void Arena::reset()
{
size_t highWatermark = m_usedSize;
for (size_t i = 0; i < kWatermarkLen - 1; i++) {
m_watermark[i] = m_watermark[i + 1];
if (m_watermark[i] > highWatermark) {
highWatermark = m_watermark[i];
}
}
m_watermark[kWatermarkLen - 1] = m_usedSize;
size_t targetSize = std::bit_ceil(highWatermark) << 1;
size_t thresholdSize = targetSize << 1; // The threshold for shrinking is greater than the target to implement hysteresis
if (m_committedSize > thresholdSize) {
size_t newSize = KRAKEN_MEM_ROUND_UP_PAGE(targetSize);
std::byte* rangeStart = m_data + newSize;
size_t rangeLen = m_committedSize - newSize;
#if defined(_WIN32) || defined(_WIN64)
if (!VirtualAlloc(rangeStart, rangeLen, MEM_RESET, PAGE_NOACCESS)) {
ReportWindowsLastError("VirtualAlloc");
} else {
m_committedSize = newSize;
}
#elif defined(__unix__) || defined(__APPLE__) || defined(ANDROID)
if (madvise(rangeStart, rangeLen, MADV_DONTNEED) != 0) {
// TODO - Log Error or debug build assert
}
if (mprotect(rangeStart, rangeLen, PROT_NONE) != 0) {
// TODO - Log Error or debug build assert
}
#else
static_assert(false, "Not Implemented");
#endif
m_committedSize = newSize;
}
m_usedSize = 0;
}
} // namespace mimir

View File

@@ -30,6 +30,7 @@
// //
#include "../include/mimir.h" #include "../include/mimir.h"
#include "mimir_impl.h"
#if defined(__unix__) || defined(__APPLE__) || defined(ANDROID) #if defined(__unix__) || defined(__APPLE__) || defined(ANDROID)
#include <unistd.h> #include <unistd.h>
@@ -43,8 +44,6 @@
#include <cstring> #include <cstring>
#define KRENGINE_MIN_MMAP 32768 #define KRENGINE_MIN_MMAP 32768
#define KRAKEN_MEM_ROUND_DOWN_PAGE(x) ((x) & ~(KRENGINE_SYS_ALLOCATION_GRANULARITY - 1))
#define KRAKEN_MEM_ROUND_UP_PAGE(x) ((((x) - 1) & ~(KRENGINE_SYS_ALLOCATION_GRANULARITY - 1)) + KRENGINE_SYS_ALLOCATION_GRANULARITY)
namespace mimir { namespace mimir {
@@ -52,28 +51,6 @@ int m_mapCount = 0;
size_t m_mapSize = 0; size_t m_mapSize = 0;
size_t m_mapOverhead = 0; size_t m_mapOverhead = 0;
int KRENGINE_SYS_ALLOCATION_GRANULARITY;
int KRENGINE_SYS_PAGE_SIZE;
void init()
{
#if defined(_WIN32) || defined(_WIN64)
SYSTEM_INFO winSysInfo;
GetSystemInfo(&winSysInfo);
KRENGINE_SYS_ALLOCATION_GRANULARITY = winSysInfo.dwAllocationGranularity;
KRENGINE_SYS_PAGE_SIZE = winSysInfo.dwPageSize;
#elif defined(__APPLE__) || __linux__ || defined(ANDROID)
KRENGINE_SYS_PAGE_SIZE = getpagesize();
KRENGINE_SYS_ALLOCATION_GRANULARITY = KRENGINE_SYS_PAGE_SIZE;
#else
#error Unsupported
#endif
}
Block::Block() Block::Block()
{ {
m_data = NULL; m_data = NULL;
@@ -467,36 +444,6 @@ std::string Block::getString()
return ret; return ret;
} }
#if defined(_WIN32) || defined(_WIN64)
void ReportWindowsLastError(LPCTSTR lpszFunction)
{
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));
fprintf(stderr,
TEXT("%s failed with error %d: %s\n"),
lpszFunction, dw, (LPCTSTR)lpMsgBuf);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}
#endif
// Lock the memory, forcing it to be loaded into a contiguous block of address space // Lock the memory, forcing it to be loaded into a contiguous block of address space
void Block::lock() void Block::lock()
{ {

100
src/mimir.cpp Normal file
View File

@@ -0,0 +1,100 @@
//
// block.cpp
// Kraken Engine
//
// Copyright 2026 Kearwood Gilbert. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of Kearwood Gilbert.
//
#include "../include/mimir.h"
#include "mimir_impl.h"
#if defined(__unix__) || defined(__APPLE__) || defined(ANDROID)
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif
#include <errno.h>
#include <cassert>
#include <cstring>
namespace mimir {
int KRENGINE_SYS_ALLOCATION_GRANULARITY;
int KRENGINE_SYS_PAGE_SIZE;
void init()
{
#if defined(_WIN32) || defined(_WIN64)
SYSTEM_INFO winSysInfo;
GetSystemInfo(&winSysInfo);
KRENGINE_SYS_ALLOCATION_GRANULARITY = winSysInfo.dwAllocationGranularity;
KRENGINE_SYS_PAGE_SIZE = winSysInfo.dwPageSize;
#elif defined(__APPLE__) || __linux__ || defined(ANDROID)
KRENGINE_SYS_PAGE_SIZE = getpagesize();
KRENGINE_SYS_ALLOCATION_GRANULARITY = KRENGINE_SYS_PAGE_SIZE;
#else
#error Unsupported
#endif
}
#if defined(_WIN32) || defined(_WIN64)
void ReportWindowsLastError(LPCTSTR lpszFunction)
{
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));
fprintf(stderr,
TEXT("%s failed with error %d: %s\n"),
lpszFunction, dw, (LPCTSTR)lpMsgBuf);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}
#endif
} // namespace mimir

50
src/mimir_impl.h Normal file
View File

@@ -0,0 +1,50 @@
//
// mimir_impl.h
// Kraken Engine / Mimir
//
// Copyright 2026 Kearwood Gilbert. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of Kearwood Gilbert.
//
#pragma once
#if defined(_WIN32) || defined(_WIN64)
#include <Windows.h>
#endif
namespace mimir {
extern int KRENGINE_SYS_ALLOCATION_GRANULARITY;
extern int KRENGINE_SYS_PAGE_SIZE;
#define KRAKEN_MEM_ROUND_DOWN_PAGE(x) ((x) & ~(KRENGINE_SYS_ALLOCATION_GRANULARITY - 1))
#define KRAKEN_MEM_ROUND_UP_PAGE(x) ((((x) - 1) & ~(KRENGINE_SYS_ALLOCATION_GRANULARITY - 1)) + KRENGINE_SYS_ALLOCATION_GRANULARITY)
#if defined(_WIN32) || defined(_WIN64)
void ReportWindowsLastError(LPCTSTR lpszFunction);
#endif
} // namespace mimir