From acee3901668437cfa37750efd22eb585272db792 Mon Sep 17 00:00:00 2001
From: nakst <>
Date: Sun, 19 Dec 2021 14:48:26 +0000
Subject: [PATCH] data structure cleanup

---
 apps/file_manager/main.cpp |  2 --
 apps/installer.cpp         |  6 ++--
 desktop/api.cpp            |  3 +-
 desktop/theme.cpp          |  2 +-
 drivers/esfs2.cpp          | 64 +++++++++++++++++++-------------------
 drivers/iso9660.cpp        |  2 +-
 kernel/elf.cpp             |  2 +-
 kernel/kernel.h            | 25 +++++++++------
 kernel/memory.cpp          |  8 -----
 kernel/module.h            | 23 ++++++++------
 shared/arena.cpp           |  6 ----
 shared/avl_tree.cpp        | 36 ++++++++-------------
 shared/bitset.cpp          |  6 ----
 shared/hash.cpp            | 20 ++++--------
 shared/math.cpp            |  4 +--
 shared/range_set.cpp       |  6 ----
 16 files changed, 89 insertions(+), 126 deletions(-)

diff --git a/apps/file_manager/main.cpp b/apps/file_manager/main.cpp
index 1a3b423..891d113 100644
--- a/apps/file_manager/main.cpp
+++ b/apps/file_manager/main.cpp
@@ -8,8 +8,6 @@
 #include <shared/hash_table.cpp>
 #include <shared/array.cpp>
 #include <shared/arena.cpp>
-#define IMPLEMENTATION
-#include <shared/arena.cpp>
 
 // TODO Possible candidates for moving in the core API:
 // 	- String/paths utils
diff --git a/apps/installer.cpp b/apps/installer.cpp
index abdbee4..c65d1d4 100644
--- a/apps/installer.cpp
+++ b/apps/installer.cpp
@@ -624,15 +624,15 @@ EsError InstallGPT(EsBlockDeviceInformation driveInformation, EsMessageDevice dr
 	partitionTable[1].firstLBA = partitionTable[0].lastLBA + 1;
 	partitionTable[1].lastLBA = header->lastUsableLBA;
 
-	header->tableCRC32 = CalculateCRC32(partitionTable, partitionEntryCount * sizeof(GPTEntry));
-	header->headerCRC32 = CalculateCRC32(header, header->headerBytes);
+	header->tableCRC32 = CalculateCRC32(partitionTable, partitionEntryCount * sizeof(GPTEntry), 0);
+	header->headerCRC32 = CalculateCRC32(header, header->headerBytes, 0);
 
 	GPTHeader *backupHeader = (GPTHeader *) EsHeapAllocate(driveInformation.sectorSize, true);
 	EsMemoryCopy(backupHeader, header, driveInformation.sectorSize);
 	backupHeader->headerSelfLBA = header->headerBackupLBA;
 	backupHeader->headerBackupLBA = header->headerSelfLBA;
 	backupHeader->headerCRC32 = 0;
-	backupHeader->headerCRC32 = CalculateCRC32(backupHeader, header->headerBytes);
+	backupHeader->headerCRC32 = CalculateCRC32(backupHeader, header->headerBytes, 0);
 
 	parameters[0] = 0; 
 	parameters[1] = driveInformation.sectorSize;
diff --git a/desktop/api.cpp b/desktop/api.cpp
index a01ad4a..e26b4d8 100644
--- a/desktop/api.cpp
+++ b/desktop/api.cpp
@@ -27,7 +27,6 @@
 #define SHARED_COMMON_WANT_ALL
 #define SHARED_MATH_WANT_ALL
 #include <shared/ini.h>
-#include <shared/avl_tree.cpp>
 #include <shared/heap.cpp>
 #include <shared/linked_list.cpp>
 #include <shared/hash.cpp>
@@ -1901,7 +1900,7 @@ const void *EsBundleFind(const EsBundle *bundle, const char *_name, ptrdiff_t na
 
 	const BundleHeader *header = bundle->base;
 	const BundleFile *files = (const BundleFile *) (header + 1);
-	uint64_t name = CalculateCRC64(_name, nameBytes);
+	uint64_t name = CalculateCRC64(_name, nameBytes, 0);
 
 	for (uintptr_t i = 0; i < header->fileCount; i++) {
 		if (files[i].nameCRC64 == name) {
diff --git a/desktop/theme.cpp b/desktop/theme.cpp
index 11fccce..bf01640 100644
--- a/desktop/theme.cpp
+++ b/desktop/theme.cpp
@@ -1245,7 +1245,7 @@ const void *GetConstant(const char *cKey, size_t *byteCount, bool *scale) {
 	const ThemeHeader *header = (const ThemeHeader *) EsBufferRead(&data, sizeof(ThemeHeader));
 	EsBufferRead(&data, sizeof(ThemeStyle) * header->styleCount);
 
-	uint64_t hash = CalculateCRC64(EsLiteral(cKey));
+	uint64_t hash = CalculateCRC64(EsLiteral(cKey), 0);
 
 	for (uintptr_t i = 0; i < header->constantCount; i++) {
 		const ThemeConstant *constant = (const ThemeConstant *) EsBufferRead(&data, sizeof(ThemeConstant));
diff --git a/drivers/esfs2.cpp b/drivers/esfs2.cpp
index c4f427c..0f8c893 100644
--- a/drivers/esfs2.cpp
+++ b/drivers/esfs2.cpp
@@ -51,7 +51,7 @@ static bool AccessBlock(Volume *volume, uint64_t index, uint64_t count, void *bu
 static bool ValidateIndexVertex(Superblock *superblock, IndexVertex *vertex) {
 	uint32_t checksum = vertex->checksum;
 	vertex->checksum = 0;
-	uint32_t calculated = CalculateCRC32(vertex, superblock->blockSize);
+	uint32_t calculated = CalculateCRC32(vertex, superblock->blockSize, 0);
 
 	ESFS_CHECK(checksum == calculated, "ValidateIndexVertex - Invalid vertex checksum.");
 	ESFS_CHECK(0 == EsMemoryCompare(vertex->signature, ESFS_INDEX_VERTEX_SIGNATURE, 4), "ValidateIndexVertex - Invalid vertex signature.");
@@ -69,7 +69,7 @@ static EsError FindDirectoryEntryReferenceFromIndex(Volume *volume, uint8_t *buf
 	Superblock *superblock = &volume->superblock;
 
 	// Get the root vertex.
-	uint64_t nameHash = CalculateCRC64(name, nameLength);
+	uint64_t nameHash = CalculateCRC64(name, nameLength, 0);
 	IndexVertex *vertex = (IndexVertex *) buffer;
 
 	if (!AccessBlock(volume, rootBlock, 1, vertex, FS_BLOCK_ACCESS_CACHED, K_ACCESS_READ)) {
@@ -112,7 +112,7 @@ static EsError FindDirectoryEntryReferenceFromIndex(Volume *volume, uint8_t *buf
 static bool ValidateDirectoryEntry(Volume *volume, DirectoryEntry *entry) {
 	uint32_t checksum = entry->checksum;
 	entry->checksum = 0;
-	uint32_t calculated = CalculateCRC32(entry, sizeof(DirectoryEntry));
+	uint32_t calculated = CalculateCRC32(entry, sizeof(DirectoryEntry), 0);
 	entry->checksum = calculated;
 
 	ESFS_CHECK_VA(checksum == calculated, "ValidateDirectoryEntry - Invalid checksum (%x, calculated %x).", checksum, calculated);
@@ -346,7 +346,7 @@ static void Sync(KNode *_directory, KNode *node) {
 
 	{
 		file->entry.checksum = 0;
-		file->entry.checksum = CalculateCRC32(&file->entry, sizeof(DirectoryEntry));
+		file->entry.checksum = CalculateCRC32(&file->entry, sizeof(DirectoryEntry), 0);
 	}
 
 	uint8_t *blockBuffer = (uint8_t *) EsHeapAllocate(superblock->blockSize, false, K_FIXED);
@@ -462,14 +462,14 @@ static uint64_t FindLargestExtent(uint8_t *bitmap, Superblock *superblock) {
 static bool ValidateGroupDescriptor(GroupDescriptor *descriptor) {
 	uint32_t checksum = descriptor->checksum;
 	descriptor->checksum = 0;
-	uint32_t calculated = CalculateCRC32(descriptor, sizeof(GroupDescriptor));
+	uint32_t calculated = CalculateCRC32(descriptor, sizeof(GroupDescriptor), 0);
 	ESFS_CHECK(checksum == calculated, "ValidateGroupDescriptor - Invalid checksum.");
 	ESFS_CHECK(0 == EsMemoryCompare(descriptor->signature, ESFS_GROUP_DESCRIPTOR_SIGNATURE, 4), "ValidateGroupDescriptor - Invalid signature.");
 	return true;
 }
 
 static bool ValidateBlockBitmap(GroupDescriptor *descriptor, uint8_t *bitmap, Superblock *superblock) {
-	uint32_t calculated = CalculateCRC32(bitmap, superblock->blocksPerGroupBlockBitmap * superblock->blockSize);
+	uint32_t calculated = CalculateCRC32(bitmap, superblock->blocksPerGroupBlockBitmap * superblock->blockSize, 0);
 	ESFS_CHECK(calculated == descriptor->bitmapChecksum, "ValidateBlockBitmap - Invalid checksum.");
 
 	uint32_t blocksUsed = 0;
@@ -586,9 +586,9 @@ static bool AllocateExtent(Volume *volume, uint64_t nearby, uint64_t increaseBlo
 
 		target->largestExtent = FindLargestExtent(bitmap, superblock);
 		target->blocksUsed += *extentCount;
-		target->bitmapChecksum = CalculateCRC32(bitmap, superblock->blocksPerGroupBlockBitmap * superblock->blockSize);
+		target->bitmapChecksum = CalculateCRC32(bitmap, superblock->blocksPerGroupBlockBitmap * superblock->blockSize, 0);
 		target->checksum = 0;
-		target->checksum = CalculateCRC32(target, sizeof(GroupDescriptor));
+		target->checksum = CalculateCRC32(target, sizeof(GroupDescriptor), 0);
 	}
 
 	*extentStart += (target - volume->groupDescriptorTable) * superblock->blocksPerGroup;
@@ -662,10 +662,10 @@ static bool FreeExtent(Volume *volume, uint64_t extentStart, uint64_t extentCoun
 	}
 
 	target->largestExtent = FindLargestExtent(bitmap, superblock);
-	target->bitmapChecksum = CalculateCRC32(bitmap, superblock->blocksPerGroupBlockBitmap * superblock->blockSize);
+	target->bitmapChecksum = CalculateCRC32(bitmap, superblock->blocksPerGroupBlockBitmap * superblock->blockSize, 0);
 	target->blocksUsed -= extentCount;
 	target->checksum = 0;
-	target->checksum = CalculateCRC32(target, sizeof(GroupDescriptor));
+	target->checksum = CalculateCRC32(target, sizeof(GroupDescriptor), 0);
 	superblock->blocksUsed -= extentCount;
 	volume->spaceUsed -= extentCount * superblock->blockSize;
 
@@ -984,7 +984,7 @@ static bool IndexModifyKey(Volume *volume, uint64_t newKey, DirectoryEntryRefere
 						&& keys[i].data.offsetIntoBlock + sizeof(DirectoryEntry) <= superblock->blockSize, 
 						"IndexModifyKey - Invalid key entry.");
 				keys[i].data = reference;
-				vertex->checksum = 0; vertex->checksum = CalculateCRC32(vertex, superblock->blockSize);
+				vertex->checksum = 0; vertex->checksum = CalculateCRC32(vertex, superblock->blockSize, 0);
 				return AccessBlock(volume, block, 1, vertex, FS_BLOCK_ACCESS_CACHED, K_ACCESS_WRITE);
 			}
 		}
@@ -1162,8 +1162,8 @@ static bool IndexAddKey(Volume *volume, uint64_t newKey, DirectoryEntryReference
 
 		// Write the blocks.
 
-		sibling->checksum = 0; sibling->checksum = CalculateCRC32(sibling, superblock->blockSize);
-		vertex->checksum = 0; vertex->checksum = CalculateCRC32(vertex, superblock->blockSize);
+		sibling->checksum = 0; sibling->checksum = CalculateCRC32(sibling, superblock->blockSize, 0);
+		vertex->checksum = 0; vertex->checksum = CalculateCRC32(vertex, superblock->blockSize, 0);
 		ESFS_CHECK(AccessBlock(volume, siblingBlock, 1, sibling, FS_BLOCK_ACCESS_CACHED, K_ACCESS_WRITE), "IndexAddKey - Could not update index.");
 		ESFS_CHECK(AccessBlock(volume, blocks[depth], 1, vertex, FS_BLOCK_ACCESS_CACHED, K_ACCESS_WRITE), "IndexAddKey - Could not update index.");
 
@@ -1175,7 +1175,7 @@ static bool IndexAddKey(Volume *volume, uint64_t newKey, DirectoryEntryReference
 
 	// Write the block.
 
-	vertex->checksum = 0; vertex->checksum = CalculateCRC32(vertex, superblock->blockSize);
+	vertex->checksum = 0; vertex->checksum = CalculateCRC32(vertex, superblock->blockSize, 0);
 	ESFS_CHECK(AccessBlock(volume, blocks[depth], 1, vertex, FS_BLOCK_ACCESS_CACHED, K_ACCESS_WRITE), "IndexAddKey - Could not update index.");
 
 	return true;
@@ -1245,7 +1245,7 @@ static bool IndexRemoveKey(Volume *volume, uint64_t removeKey, uint64_t *rootBlo
 		ESFS_VERTEX_KEY(vertex, position)->value = ESFS_VERTEX_KEY(search, 0)->value;
 		ESFS_VERTEX_KEY(vertex, position)->data  = ESFS_VERTEX_KEY(search, 0)->data;
 
-		vertex->checksum = 0; vertex->checksum = CalculateCRC32(vertex, superblock->blockSize);
+		vertex->checksum = 0; vertex->checksum = CalculateCRC32(vertex, superblock->blockSize, 0);
 		ESFS_CHECK(AccessBlock(volume, blocks[startDepth], 1, vertex, FS_BLOCK_ACCESS_CACHED, K_ACCESS_WRITE), "IndexRemoveKey - Could not write index.");
 
 		EsMemoryCopy(vertex, search, superblock->blockSize);
@@ -1264,7 +1264,7 @@ static bool IndexRemoveKey(Volume *volume, uint64_t removeKey, uint64_t *rootBlo
 
 	if (vertex->count >= (vertex->maxCount - 1) / 2) {
 		// EsPrint("Vertex has enough keys, exiting...\n");
-		vertex->checksum = 0; vertex->checksum = CalculateCRC32(vertex, superblock->blockSize);
+		vertex->checksum = 0; vertex->checksum = CalculateCRC32(vertex, superblock->blockSize, 0);
 		ESFS_CHECK(AccessBlock(volume, blocks[depth], 1, vertex, FS_BLOCK_ACCESS_CACHED, K_ACCESS_WRITE), "IndexRemoveKey - Could not write index.");
 		return true;
 	}
@@ -1281,7 +1281,7 @@ static bool IndexRemoveKey(Volume *volume, uint64_t removeKey, uint64_t *rootBlo
 			*rootBlock = vertex->keys[0].child;
 		} else {
 			// EsPrint("Vertex is at root, exiting...\n");
-			vertex->checksum = 0; vertex->checksum = CalculateCRC32(vertex, superblock->blockSize);
+			vertex->checksum = 0; vertex->checksum = CalculateCRC32(vertex, superblock->blockSize, 0);
 			ESFS_CHECK(AccessBlock(volume, blocks[depth], 1, vertex, FS_BLOCK_ACCESS_CACHED, K_ACCESS_WRITE), "IndexRemoveKey - Could not write index.");
 		}
 
@@ -1325,9 +1325,9 @@ static bool IndexRemoveKey(Volume *volume, uint64_t removeKey, uint64_t *rootBlo
 
 			sibling->count--, vertex->count++;
 
-			vertex->checksum = 0; 	vertex->checksum = 	CalculateCRC32(vertex, 	superblock->blockSize);
-			sibling->checksum = 0; 	sibling->checksum = 	CalculateCRC32(sibling, superblock->blockSize);
-			parent->checksum = 0; 	parent->checksum = 	CalculateCRC32(parent, 	superblock->blockSize);
+			vertex->checksum = 0; 	vertex->checksum = 	CalculateCRC32(vertex, 	superblock->blockSize, 0);
+			sibling->checksum = 0; 	sibling->checksum = 	CalculateCRC32(sibling, superblock->blockSize, 0);
+			parent->checksum = 0; 	parent->checksum = 	CalculateCRC32(parent, 	superblock->blockSize, 0);
 
 			ESFS_CHECK(AccessBlock(volume, ESFS_VERTEX_KEY(parent, positionInParent - 1)->child, 1, sibling, FS_BLOCK_ACCESS_CACHED, K_ACCESS_WRITE), "IndexRemoveKey - Could not write index.");
 			ESFS_CHECK(AccessBlock(volume, blocks[depth], 1, vertex, FS_BLOCK_ACCESS_CACHED, K_ACCESS_WRITE), "IndexRemoveKey - Could not write index.");
@@ -1353,9 +1353,9 @@ static bool IndexRemoveKey(Volume *volume, uint64_t removeKey, uint64_t *rootBlo
 
 			sibling->count--, vertex->count++;
 
-			vertex->checksum = 0; 	vertex->checksum = 	CalculateCRC32(vertex, 	superblock->blockSize);
-			sibling->checksum = 0; 	sibling->checksum = 	CalculateCRC32(sibling, superblock->blockSize);
-			parent->checksum = 0; 	parent->checksum = 	CalculateCRC32(parent, 	superblock->blockSize);
+			vertex->checksum = 0; 	vertex->checksum = 	CalculateCRC32(vertex, 	superblock->blockSize, 0);
+			sibling->checksum = 0; 	sibling->checksum = 	CalculateCRC32(sibling, superblock->blockSize, 0);
+			parent->checksum = 0; 	parent->checksum = 	CalculateCRC32(parent, 	superblock->blockSize, 0);
 
 			ESFS_CHECK(AccessBlock(volume, ESFS_VERTEX_KEY(parent, positionInParent + 1)->child, 1, sibling, FS_BLOCK_ACCESS_CACHED, K_ACCESS_WRITE), "IndexRemoveKey - Could not write index.");
 			ESFS_CHECK(AccessBlock(volume, blocks[depth], 1, vertex, FS_BLOCK_ACCESS_CACHED, K_ACCESS_WRITE), "IndexRemoveKey - Could not write index.");
@@ -1397,7 +1397,7 @@ static bool IndexRemoveKey(Volume *volume, uint64_t removeKey, uint64_t *rootBlo
 			ESFS_CHECK(FreeExtent(volume, blocks[depth], 1), "IndexRemoveKey - Could not free merged vertex.");
 		}
 
-		sibling->checksum = 0; 	sibling->checksum = 	CalculateCRC32(sibling, superblock->blockSize);
+		sibling->checksum = 0; 	sibling->checksum = 	CalculateCRC32(sibling, superblock->blockSize, 0);
 		ESFS_CHECK(AccessBlock(volume, ESFS_VERTEX_KEY(parent, positionInParent - 1)->child, 1, sibling, FS_BLOCK_ACCESS_CACHED, K_ACCESS_WRITE), "IndexRemoveKey - Could not write index.");
 
 		EsMemoryCopy(vertex, parent, superblock->blockSize);
@@ -1453,7 +1453,7 @@ static EsError RemoveDirectoryEntry(FSNode *file, uint8_t *blockBuffers /* super
 				((FSNode *) node->driverNode)->reference = file->reference;
 			}
 
-			uint64_t key = CalculateCRC64(filename->filename, filename->length);
+			uint64_t key = CalculateCRC64(filename->filename, filename->length, 0);
 			// EsPrint("\tModify index key for %s\n", filename->length, filename->filename);
 			ESFS_CHECK_TO_ERROR(IndexModifyKey(volume, key, file->reference, directoryAttribute->indexRootBlock, blockBuffers + superblock->blockSize), 
 					"Remove - Could not update index (2).", ES_ERROR_DRIVE_CONTROLLER_REPORTED);
@@ -1475,7 +1475,7 @@ static EsError RemoveDirectoryEntry(FSNode *file, uint8_t *blockBuffers /* super
 	// EsPrint("\tRemoving %s from index\n", filename->length, filename->filename);
 
 	if (filename) {
-		uint64_t removeKey = CalculateCRC64(filename->filename, filename->length);
+		uint64_t removeKey = CalculateCRC64(filename->filename, filename->length, 0);
 		ESFS_CHECK_TO_ERROR(IndexRemoveKey(volume, removeKey, &directoryAttribute->indexRootBlock), "Remove - Could not update index.", ES_ERROR_DRIVE_CONTROLLER_REPORTED);
 	}
 
@@ -1646,7 +1646,7 @@ static bool CreateInternal(const char *name, size_t nameLength, EsNodeType type,
 	}
 
 	entry->checksum = 0;
-	entry->checksum = CalculateCRC32(entry, sizeof(DirectoryEntry));
+	entry->checksum = CalculateCRC32(entry, sizeof(DirectoryEntry), 0);
 	if (!ValidateDirectoryEntry(volume, entry)) KernelPanic("EsFS::CreateInternal - Created directory entry is invalid.\n");
 
 	// Write the directory entry.
@@ -1658,7 +1658,7 @@ static bool CreateInternal(const char *name, size_t nameLength, EsNodeType type,
 
 	// Add the node into the index. 
 
-	uint64_t newKey = CalculateCRC64(name, nameLength);
+	uint64_t newKey = CalculateCRC64(name, nameLength, 0);
 	ESFS_CHECK(IndexAddKey(volume, newKey, reference, &directoryAttribute->indexRootBlock), "Create - Could not add file to index.");
 
 	directoryAttribute->childNodes++;
@@ -1681,7 +1681,7 @@ static EsError Move(KNode *_oldDirectory, KNode *_file, KNode *_newDirectory, co
 	if (oldDirectory->type != ES_NODE_DIRECTORY || newDirectory->type != ES_NODE_DIRECTORY) KernelPanic("EsFS::Move - Incorrect node types.\n");
 
 	file->entry.checksum = 0;
-	file->entry.checksum = CalculateCRC32(&file->entry, sizeof(DirectoryEntry));
+	file->entry.checksum = CalculateCRC32(&file->entry, sizeof(DirectoryEntry), 0);
 	if (!ValidateDirectoryEntry(volume, &file->entry)) KernelPanic("EsFS::Move - Existing entry is invalid.\n");
 
 	uint8_t *buffers = (uint8_t *) EsHeapAllocate(superblock->blockSize * 2, true, K_FIXED);
@@ -1854,7 +1854,7 @@ static bool Mount(Volume *volume, EsFileOffsetDifference *rootDirectoryChildren)
 
 	uint32_t checksum = volume->superblock.checksum;
 	volume->superblock.checksum = 0;
-	uint32_t calculated = CalculateCRC32(&volume->superblock, sizeof(Superblock));
+	uint32_t calculated = CalculateCRC32(&volume->superblock, sizeof(Superblock), 0);
 	ESFS_CHECK_FATAL(checksum == calculated, "Invalid superblock checksum.");
 
 	ESFS_CHECK_FATAL(0 == EsMemoryCompare(volume->superblock.signature, ESFS_SIGNATURE_STRING, 16), "Invalid superblock signature.");
@@ -1881,7 +1881,7 @@ static bool Mount(Volume *volume, EsFileOffsetDifference *rootDirectoryChildren)
 	if (!volume->readOnly) {
 		superblock->mounted = true;
 		superblock->checksum = 0;
-		superblock->checksum = CalculateCRC32(superblock, sizeof(Superblock));
+		superblock->checksum = CalculateCRC32(superblock, sizeof(Superblock), 0);
 		ESFS_CHECK_ERROR_READ_ONLY(volume->Access(ESFS_BOOT_SUPER_BLOCK_SIZE, ESFS_BOOT_SUPER_BLOCK_SIZE, 
 					K_ACCESS_WRITE, (uint8_t *) superblock, ES_FLAGS_DEFAULT), "Could not mark volume as mounted.");
 	}
@@ -1957,7 +1957,7 @@ static void Unmount(KFileSystem *fileSystem) {
 
 		superblock->mounted = false;
 		superblock->checksum = 0;
-		superblock->checksum = CalculateCRC32(superblock, sizeof(Superblock));
+		superblock->checksum = CalculateCRC32(superblock, sizeof(Superblock), 0);
 		volume->Access(ESFS_BOOT_SUPER_BLOCK_SIZE, ESFS_BOOT_SUPER_BLOCK_SIZE, K_ACCESS_WRITE, 
 				(uint8_t *) superblock, ES_FLAGS_DEFAULT);
 	}
diff --git a/drivers/iso9660.cpp b/drivers/iso9660.cpp
index 1dc996a..83bf500 100644
--- a/drivers/iso9660.cpp
+++ b/drivers/iso9660.cpp
@@ -516,7 +516,7 @@ static void DeviceAttach(KDevice *parent) {
 	volume->spaceUsed = volume->primaryDescriptor.volumeSize.x * volume->primaryDescriptor.logicalBlockSize.x;
 	volume->spaceTotal = volume->spaceUsed;
 
-	uint64_t crc64 = CalculateCRC64(&volume->primaryDescriptor, sizeof(PrimaryDescriptor));
+	uint64_t crc64 = CalculateCRC64(&volume->primaryDescriptor, sizeof(PrimaryDescriptor), 0);
 	EsMemoryCopy(&volume->identifier, &crc64, sizeof(crc64));
 
 	volume->nameBytes = sizeof(volume->primaryDescriptor.volumeIdentifier);
diff --git a/kernel/elf.cpp b/kernel/elf.cpp
index cd22246..360e1b0 100644
--- a/kernel/elf.cpp
+++ b/kernel/elf.cpp
@@ -168,7 +168,7 @@ EsError KLoadELF(KNode *node, KLoadedExecutable *executable) {
 
 			// Look for the executable in the bundle.
 
-			uint64_t name = CalculateCRC64(EsLiteral("$Executables/" K_ARCH_NAME));
+			uint64_t name = CalculateCRC64(EsLiteral("$Executables/" K_ARCH_NAME), 0);
 			BundleFile *files = (BundleFile *) ((BundleHeader *) header.mapAddress + 1);
 
 			bool found = false;
diff --git a/kernel/kernel.h b/kernel/kernel.h
index 9689030..f9fdfaf 100644
--- a/kernel/kernel.h
+++ b/kernel/kernel.h
@@ -181,10 +181,23 @@ extern "C" {
 // Kernel components.
 // ---------------------------------------------------------------------------------------------------------------
 
-#include <shared/avl_tree.cpp>
+#ifndef IMPLEMENTATION
 #include <shared/bitset.cpp>
-#include <shared/range_set.cpp>
 #include <shared/arena.cpp>
+#include <shared/avl_tree.cpp>
+#include <shared/range_set.cpp>
+#include <shared/ini.h>
+#include <shared/partitions.cpp>
+#include <shared/heap.cpp>
+#include <shared/hash.cpp>
+
+#define ARRAY_IMPLEMENTATION_ONLY
+#include <shared/array.cpp>
+
+#define SHARED_COMMON_WANT_ALL
+#include <shared/strings.cpp>
+#include <shared/common.cpp>
+#endif
 
 #include "objects.cpp"
 #include "memory.cpp"
@@ -203,14 +216,6 @@ extern "C" {
 #include "posix.cpp"
 #endif
 
-#ifndef IMPLEMENTATION
-#define ARRAY_IMPLEMENTATION_ONLY
-#include <shared/array.cpp>
-#include <shared/heap.cpp>
-#include <shared/partitions.cpp>
-#include <shared/ini.h>
-#endif
-
 // ---------------------------------------------------------------------------------------------------------------
 // Miscellaneous.
 // ---------------------------------------------------------------------------------------------------------------
diff --git a/kernel/memory.cpp b/kernel/memory.cpp
index 3d6482d..e6a8bc8 100644
--- a/kernel/memory.cpp
+++ b/kernel/memory.cpp
@@ -205,14 +205,6 @@ struct PMM {
 // See MMPhysicalAllocate.
 alignas(K_PAGE_SIZE) uint8_t earlyZeroBuffer[K_PAGE_SIZE];
 
-// Memory spaces.
-// kernelMMSpace - Whence the kernel allocates memory.
-// coreMMSpace - Whence other memory managers allocate memory.
-
-extern MMSpace _kernelMMSpace, _coreMMSpace;
-#define kernelMMSpace (&_kernelMMSpace)
-#define coreMMSpace (&_coreMMSpace)
-
 // Constants.
 
 // MMArchMapPage.
diff --git a/kernel/module.h b/kernel/module.h
index c730efa..7df4c11 100644
--- a/kernel/module.h
+++ b/kernel/module.h
@@ -110,21 +110,17 @@ void KUnregisterMSI(uintptr_t tag);
 // Common data types, algorithms and things.
 // ---------------------------------------------------------------------------------------------------------------
 
+#define SHARED_MATH_WANT_BASIC_UTILITIES
+#include <shared/math.cpp>
+
 #ifndef K_IN_CORE_KERNEL
 #define SHARED_DEFINITIONS_ONLY
 #endif
-
-#define SHARED_MATH_WANT_BASIC_UTILITIES
 #include <shared/unicode.cpp>
-#include <shared/math.cpp>
 #include <shared/linked_list.cpp>
-#include <shared/hash.cpp>
 
-#ifdef K_IN_CORE_KERNEL
-#define SHARED_COMMON_WANT_ALL
-#include <shared/strings.cpp>
-#include <shared/common.cpp>
-#endif
+uint32_t CalculateCRC32(const void *_buffer, size_t length, uint32_t carry);
+uint64_t CalculateCRC64(const void *_buffer, size_t length, uint64_t carry);
 
 // ---------------------------------------------------------------------------------------------------------------
 // Processor IO.
@@ -340,6 +336,15 @@ struct MMSpace;
 MMSpace *MMGetKernelSpace();
 MMSpace *MMGetCurrentProcessSpace();
 
+#ifdef K_IN_CORE_KERNEL
+// Memory spaces.
+// kernelMMSpace - Whence the kernel allocates memory.
+// coreMMSpace - Whence other memory managers allocate memory.
+extern MMSpace _kernelMMSpace, _coreMMSpace;
+#define kernelMMSpace (&_kernelMMSpace)
+#define coreMMSpace (&_coreMMSpace)
+#endif
+
 #define MM_REGION_FIXED              (0x01) // A region where all the physical pages are allocated up-front, and cannot be removed from the working set.
 #define MM_REGION_NOT_CACHEABLE      (0x02) // Do not cache the pages in the region.
 #define MM_REGION_NO_COMMIT_TRACKING (0x04) // Page committing is manually tracked.
diff --git a/shared/arena.cpp b/shared/arena.cpp
index 277418d..96f7715 100644
--- a/shared/arena.cpp
+++ b/shared/arena.cpp
@@ -2,8 +2,6 @@
 // It is released under the terms of the MIT license -- see LICENSE.md.
 // Written by: nakst.
 
-#ifndef IMPLEMENTATION
-
 struct Arena {
 	// Arenas are not thread-safe!
 	// You can use different arenas in different threads, though.
@@ -15,8 +13,6 @@ void *ArenaAllocate(Arena *arena, bool zero); // Not thread-safe.
 void ArenaFree(Arena *arena, void *pointer); // Not thread-safe.
 void ArenaInitialise(Arena *arena, size_t blockSize, size_t itemSize);
 
-#else
-
 struct ArenaSlot {
 	uintptr_t indexInBlock;
 	ArenaSlot *nextEmpty, **previousEmpty;
@@ -120,5 +116,3 @@ void ArenaInitialise(Arena *arena, size_t blockSize, size_t itemSize) {
 	if (arena->slotsPerBlock < 32) arena->slotsPerBlock = 32;
 	arena->blockSize = arena->slotsPerBlock * arena->slotSize;
 }
-
-#endif
diff --git a/shared/avl_tree.cpp b/shared/avl_tree.cpp
index 1d646ad..b25ab8a 100644
--- a/shared/avl_tree.cpp
+++ b/shared/avl_tree.cpp
@@ -2,18 +2,10 @@
 // It is released under the terms of the MIT license -- see LICENSE.md.
 // Written by: nakst.
 
-#ifndef IMPLEMENTATION
-
 #ifdef DEBUG_BUILD
 #define TREE_VALIDATE
 #endif
 
-#ifdef KERNEL
-#define AVLPanic KernelPanic
-#else
-#define AVLPanic(...) do { EsPrint(__VA_ARGS__); EsAssert(false); } while (0)
-#endif
-
 enum TreeSearchMode {
 	TREE_SEARCH_EXACT,
 	TREE_SEARCH_SMALLEST_ABOVE_OR_EQUAL,
@@ -118,19 +110,19 @@ template <class T>
 int TreeValidate(AVLItem<T> *root, bool before, AVLTree<T> *tree, AVLItem<T> *parent = nullptr, int depth = 0) {
 #ifdef TREE_VALIDATE
 	if (!root) return 0;
-	if (root->parent != parent) AVLPanic("TreeValidate - Invalid binary tree 1 (%d).\n", before);
-	if (root->tree != tree) AVLPanic("TreeValidate - Invalid binary tree 4 (%d).\n", before);
+	if (root->parent != parent) EsPanic("TreeValidate - Invalid binary tree 1 (%d).\n", before);
+	if (root->tree != tree) EsPanic("TreeValidate - Invalid binary tree 4 (%d).\n", before);
 
 	AVLItem<T> *left  = root->children[0];
 	AVLItem<T> *right = root->children[1];
 
-	if (left  && TreeCompare(tree, &left->key,  &root->key) > 0) AVLPanic("TreeValidate - Invalid binary tree 2 (%d).\n", before);
-	if (right && TreeCompare(tree, &right->key, &root->key) < 0) AVLPanic("TreeValidate - Invalid binary tree 3 (%d).\n", before);
+	if (left  && TreeCompare(tree, &left->key,  &root->key) > 0) EsPanic("TreeValidate - Invalid binary tree 2 (%d).\n", before);
+	if (right && TreeCompare(tree, &right->key, &root->key) < 0) EsPanic("TreeValidate - Invalid binary tree 3 (%d).\n", before);
 
 	int leftHeight = TreeValidate(left, before, tree, root, depth + 1);
 	int rightHeight = TreeValidate(right, before, tree, root, depth + 1);
 	int height = (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
-	if (height != root->height) AVLPanic("TreeValidate - Invalid AVL tree 1 (%d).\n", before);
+	if (height != root->height) EsPanic("TreeValidate - Invalid AVL tree 1 (%d).\n", before);
 
 #if 0
 	static int maxSeenDepth = 0;
@@ -203,14 +195,14 @@ enum AVLDuplicateKeyPolicy {
 
 template <class T>
 bool TreeInsert(AVLTree<T> *tree, AVLItem<T> *item, T *thisItem, AVLKey key, AVLDuplicateKeyPolicy duplicateKeyPolicy = AVL_DUPLICATE_KEYS_PANIC) {
-	if (tree->modCheck) AVLPanic("TreeInsert - Concurrent modification\n");
+	if (tree->modCheck) EsPanic("TreeInsert - Concurrent modification\n");
 	tree->modCheck = true; EsDefer({tree->modCheck = false;});
 
 	TreeValidate(tree->root, true, tree);
 
 #ifdef TREE_VALIDATE
 	if (item->tree) {
-		AVLPanic("TreeInsert - Item %x already in tree %x (adding to %x).\n", item, item->tree, tree);
+		EsPanic("TreeInsert - Item %x already in tree %x (adding to %x).\n", item, item->tree, tree);
 	}
 
 	item->tree = tree;
@@ -234,7 +226,7 @@ bool TreeInsert(AVLTree<T> *tree, AVLItem<T> *item, T *thisItem, AVLKey key, AVL
 
 		if (TreeCompare(tree, &item->key, &node->key) == 0) {
 			if (duplicateKeyPolicy == AVL_DUPLICATE_KEYS_PANIC) {
-				AVLPanic("TreeInsertRecursive - Duplicate keys: %x and %x both have key %x.\n", item, node, node->key);
+				EsPanic("TreeInsertRecursive - Duplicate keys: %x and %x both have key %x.\n", item, node, node->key);
 			} else if (duplicateKeyPolicy == AVL_DUPLICATE_KEYS_FAIL) {
 				return false;
 			}
@@ -310,14 +302,14 @@ AVLItem<T> *TreeFindRecursive(AVLTree<T> *tree, AVLItem<T> *root, AVLKey *key, T
 			return TreeFindRecursive(tree, root->children[0], key, mode); 
 		}
 	} else {
-		AVLPanic("TreeFindRecursive - Invalid search mode.\n");
+		EsPanic("TreeFindRecursive - Invalid search mode.\n");
 		return nullptr;
 	}
 }
 
 template <class T>
 AVLItem<T> *TreeFind(AVLTree<T> *tree, AVLKey key, TreeSearchMode mode) {
-	if (tree->modCheck) AVLPanic("TreeFind - Concurrent access\n");
+	if (tree->modCheck) EsPanic("TreeFind - Concurrent access\n");
 
 	TreeValidate(tree->root, true, tree);
 	return TreeFindRecursive(tree, tree->root, &key, mode);
@@ -334,13 +326,13 @@ int TreeGetBalance(AVLItem<T> *item) {
 
 template <class T>
 void TreeRemove(AVLTree<T> *tree, AVLItem<T> *item) {
-	if (tree->modCheck) AVLPanic("TreeRemove - Concurrent modification\n");
+	if (tree->modCheck) EsPanic("TreeRemove - Concurrent modification\n");
 	tree->modCheck = true; EsDefer({tree->modCheck = false;});
 
 	TreeValidate(tree->root, true, tree);
 
 #ifdef TREE_VALIDATE
-	if (item->tree != tree) AVLPanic("TreeRemove - Item %x not in tree %x (in %x).\n", item, tree, item->tree);
+	if (item->tree != tree) EsPanic("TreeRemove - Item %x not in tree %x (in %x).\n", item, tree, item->tree);
 #endif
 
 	AVLItem<T> fakeRoot = {};
@@ -395,11 +387,9 @@ void TreeRemove(AVLTree<T> *tree, AVLItem<T> *item) {
 	tree->root = fakeRoot.children[0];
 
 	if (tree->root) {
-		if (tree->root->parent != &fakeRoot) AVLPanic("TreeRemove - Incorrect root parent.\n");
+		if (tree->root->parent != &fakeRoot) EsPanic("TreeRemove - Incorrect root parent.\n");
 		tree->root->parent = nullptr;
 	}
 
 	TreeValidate(tree->root, false, tree);
 }
-
-#endif
diff --git a/shared/bitset.cpp b/shared/bitset.cpp
index 8ed02df..0b74335 100644
--- a/shared/bitset.cpp
+++ b/shared/bitset.cpp
@@ -2,8 +2,6 @@
 // It is released under the terms of the MIT license -- see LICENSE.md.
 // Written by: nakst.
 
-#ifndef IMPLEMENTATION
-
 struct Bitset {
 	void Initialise(size_t count, bool mapAll = false);
 	void PutAll();
@@ -24,8 +22,6 @@ struct Bitset {
 #endif
 };
 
-#else
-
 void Bitset::Initialise(size_t count, bool mapAll) {
 	singleCount = (count + 31) & ~31;
 	groupCount = singleCount / BITSET_GROUP_SIZE + 1;
@@ -192,5 +188,3 @@ void Bitset::Put(uintptr_t index) {
 		groupUsage[index / BITSET_GROUP_SIZE]++;
 	}
 }
-
-#endif
diff --git a/shared/hash.cpp b/shared/hash.cpp
index 47ea388..464046f 100644
--- a/shared/hash.cpp
+++ b/shared/hash.cpp
@@ -2,9 +2,9 @@
 // Currently used for: EsFS, theme constants, build core configuration hash, make bundle.
 
 #ifdef __cplusplus
-constexpr static uint32_t crc32Table[] = { 
+constexpr uint32_t crc32Table[] = { 
 #else
-static uint32_t crc32Table[] = { 
+uint32_t crc32Table[] = { 
 #endif
 	0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, 
 	0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
@@ -41,9 +41,9 @@ static uint32_t crc32Table[] = {
 };
 
 #ifdef __cplusplus
-constexpr static uint64_t crc64Table[] = { 
+constexpr uint64_t crc64Table[] = { 
 #else
-static uint64_t crc64Table[] = { 
+uint64_t crc64Table[] = { 
 #endif
 	0x0000000000000000UL, 0x7AD870C830358979UL, 0xF5B0E190606B12F2UL, 0x8F689158505E9B8BUL, 0xC038E5739841B68FUL, 0xBAE095BBA8743FF6UL, 0x358804E3F82AA47DUL, 0x4F50742BC81F2D04UL, 
 	0xAB28ECB46814FE75UL, 0xD1F09C7C5821770CUL, 0x5E980D24087FEC87UL, 0x24407DEC384A65FEUL, 0x6B1009C7F05548FAUL, 0x11C8790FC060C183UL, 0x9EA0E857903E5A08UL, 0xE478989FA00BD371UL, 
@@ -79,11 +79,7 @@ static uint64_t crc64Table[] = {
 	0x66E7A46C27F3AA2CUL, 0x1C3FD4A417C62355UL, 0x935745FC4798B8DEUL, 0xE98F353477AD31A7UL, 0xA6DF411FBFB21CA3UL, 0xDC0731D78F8795DAUL, 0x536FA08FDFD90E51UL, 0x29B7D047EFEC8728UL,
 };
 
-#ifdef __cplusplus
-constexpr static uint32_t CalculateCRC32(const void *_buffer, size_t length, uint32_t carry = 0) {
-#else
-static uint32_t CalculateCRC32(const void *_buffer, size_t length, uint32_t carry) {
-#endif
+uint32_t CalculateCRC32(const void *_buffer, size_t length, uint32_t carry) {
 	const uint8_t *buffer = (const uint8_t *) _buffer;
 	uint32_t x = ~carry;
 	
@@ -94,11 +90,7 @@ static uint32_t CalculateCRC32(const void *_buffer, size_t length, uint32_t carr
 	return ~x;
 }
 
-#ifdef __cplusplus
-constexpr static uint64_t CalculateCRC64(const void *_buffer, size_t length, uint64_t carry = 0) {
-#else
-static uint64_t CalculateCRC64(const void *_buffer, size_t length, uint64_t carry) {
-#endif
+uint64_t CalculateCRC64(const void *_buffer, size_t length, uint64_t carry) {
 	const uint8_t *buffer = (const uint8_t *) _buffer;
 	uint64_t x = ~carry;
 	
diff --git a/shared/math.cpp b/shared/math.cpp
index c85eb88..727a9a4 100644
--- a/shared/math.cpp
+++ b/shared/math.cpp
@@ -9,14 +9,14 @@
 #if defined(SHARED_MATH_WANT_BASIC_UTILITIES) || defined(SHARED_MATH_WANT_ALL)
 
 template <class T>
-T RoundDown(T value, T divisor) {
+inline T RoundDown(T value, T divisor) {
 	value /= divisor;
 	value *= divisor;
 	return value;
 }
 
 template <class T>
-T RoundUp(T value, T divisor) {
+inline T RoundUp(T value, T divisor) {
 	value += divisor - 1;
 	value /= divisor;
 	value *= divisor;
diff --git a/shared/range_set.cpp b/shared/range_set.cpp
index 3ec209d..199fc58 100644
--- a/shared/range_set.cpp
+++ b/shared/range_set.cpp
@@ -2,8 +2,6 @@
 // It is released under the terms of the MIT license -- see LICENSE.md.
 // Written by: nakst.
 
-#ifndef IMPLEMENTATION
-
 struct Range {
 	uintptr_t from, to;
 };
@@ -21,8 +19,6 @@ struct RangeSet {
 	bool Clear(uintptr_t from, uintptr_t to, intptr_t *delta, bool modify);
 };
 
-#else
-
 Range *RangeSet::Find(uintptr_t offset, bool touching) {
 	if (!ranges.Length()) return nullptr;
 
@@ -351,8 +347,6 @@ bool RangeSet::Clear(uintptr_t from, uintptr_t to, intptr_t *delta, bool modify)
 	return true;
 }
 
-#endif
-
 #if 0
 int main(int argc, char **argv) {
 	RangeSet set = {};