refactor(memory): make memory allocation the exclusive responsibility of bc from now on

This commit is contained in:
phaneron 2025-03-31 14:19:47 -04:00
parent 92fcb3266f
commit 09e7076a9b
18 changed files with 94 additions and 73 deletions

View file

@ -1,6 +1,6 @@
#include "storm/Big.hpp"
#include "storm/big/Ops.hpp"
#include "storm/Memory.hpp"
#include <bc/Memory.hpp>
#include <cstring>
void SBigAdd(BigData* a, BigData* b, BigData* c) {

View file

@ -1,8 +1,10 @@
#include "storm/Command.hpp"
#include "bc/os/file/Types.hpp"
#include "storm/Error.hpp"
#include "storm/String.hpp"
#include "storm/Memory.hpp"
#include <bc/Memory.hpp>
#include "storm/error/Defines.hpp"
#include <bc/os/File.hpp>
#include <bc/os/CommandLine.hpp>
@ -32,7 +34,7 @@ static void GenerateError(CMDERRORCALLBACK errorcallback, uint32_t errorcode, co
case STORM_COMMAND_ERROR_NOT_ENOUGH_ARGUMENTS:
strid = 1;
break;
case STORM_COMMAND_ERROR_OPEN_FAILED:
case ERROR_OPEN_FAILED:
strid = 2;
break;
default:
@ -325,7 +327,7 @@ static int32_t ProcessToken(const char* string, int32_t quoted, PROCESSING* proc
}
if (errorcallback) {
GenerateError(errorcallback, STORM_COMMAND_ERROR_OPEN_FAILED, string);
GenerateError(errorcallback, ERROR_OPEN_FAILED, string);
}
}
@ -354,10 +356,9 @@ static int32_t ProcessString(const char** stringptr, PROCESSING* processing, CMD
static int32_t ProcessFile(const char* filename, PROCESSING* processing, CMDDEF** nextarg, CMDEXTRACALLBACK extracallback, CMDERRORCALLBACK errorcallback) {
// TODO
auto file = OsCreateFile(filename, OS_GENERIC_READ, OS_FILE_SHARE_READ, OS_OPEN_EXISTING, OS_FILE_FLAG_SEQUENTIAL_SCAN, 0);
if (!file) {
if (file == HOSFILE_INVALID) {
if (errorcallback) {
GenerateError(errorcallback, STORM_COMMAND_ERROR_OPEN_FAILED, filename);
GenerateError(errorcallback, ERROR_OPEN_FAILED, filename);
}
return false;
}
@ -383,11 +384,13 @@ int32_t SCmdRegisterArgument(uint32_t flags, uint32_t id, const char* name, void
auto namelength = SStrLen(name);
STORM_VALIDATE(namelength < 16, ERROR_INVALID_PARAMETER, 0);
STORM_VALIDATE((!variablebytes) || variableptr, ERROR_INVALID_PARAMETER, 0);
STORM_VALIDATE(((STORM_COMMAND_GET_ARG(flags) != STORM_COMMAND_ARG_REQUIRED) || !s_addedoptional), ERROR_INVALID_PARAMETER, 0);
STORM_VALIDATE((STORM_COMMAND_GET_ARG(flags) != STORM_COMMAND_ARG_FLAGGED) || (namelength > 0), ERROR_INVALID_PARAMETER, 0);
STORM_VALIDATE((STORM_COMMAND_GET_TYPE(flags) != STORM_COMMAND_TYPE_BOOL) || (!variableptr) || (variablebytes == sizeof(uint32_t)), ERROR_INVALID_PARAMETER, 0);
STORM_VALIDATE_BEGIN;
STORM_VALIDATE(namelength < 16);
STORM_VALIDATE((!variablebytes) || variableptr);
STORM_VALIDATE((STORM_COMMAND_GET_ARG(flags) != STORM_COMMAND_ARG_REQUIRED) || !s_addedoptional);
STORM_VALIDATE((STORM_COMMAND_GET_ARG(flags) != STORM_COMMAND_ARG_FLAGGED) || (namelength > 0));
STORM_VALIDATE((STORM_COMMAND_GET_TYPE(flags) != STORM_COMMAND_TYPE_BOOL) || (!variableptr) || (variablebytes == sizeof(uint32_t)));
STORM_VALIDATE_END;
// If argument is flagged, it goes in the flag list
auto listptr = &s_arglist;
@ -419,7 +422,9 @@ int32_t SCmdRegisterArgument(uint32_t flags, uint32_t id, const char* name, void
}
int32_t SCmdRegisterArgList(ARGLIST* listptr, uint32_t numargs) {
STORM_VALIDATE(listptr, ERROR_INVALID_PARAMETER, 0);
STORM_VALIDATE_BEGIN;
STORM_VALIDATE(listptr);
STORM_VALIDATE_END;
for (int32_t i = 0; i < numargs; i++) {
if (!SCmdRegisterArgument(listptr->flags, listptr->id, listptr->name, 0, 0, 1, 0xFFFFFFFF, listptr->callback)) {
@ -433,7 +438,9 @@ int32_t SCmdRegisterArgList(ARGLIST* listptr, uint32_t numargs) {
}
int32_t SCmdProcess(const char* cmdline, int32_t skipprogname, CMDEXTRACALLBACK extracallback, CMDERRORCALLBACK errorcallback) {
STORM_VALIDATE(cmdline, ERROR_INVALID_PARAMETER, 0);
STORM_VALIDATE_BEGIN;
STORM_VALIDATE(cmdline);
STORM_VALIDATE_END;
if (skipprogname) {
SStrTokenize(&cmdline, nullptr, 0, STORM_COMMAND_WHITESPACE_CHARS, nullptr);
@ -490,12 +497,12 @@ int32_t SCmdGetBool(uint32_t id) {
}
int32_t SCmdGetString(uint32_t id, char* buffer, uint32_t bufferchars) {
if (buffer) {
*buffer = '\0';
}
STORM_VALIDATE_BEGIN;
STORM_VALIDATE(buffer);
STORM_VALIDATE(bufferchars);
STORM_VALIDATE_END;
STORM_VALIDATE(buffer, ERROR_INVALID_PARAMETER, 0);
STORM_VALIDATE(bufferchars, ERROR_INVALID_PARAMETER, 0);
*buffer = '\0';
for (int32_t flaglist = 0; flaglist <= 1; flaglist++) {
auto& list = flaglist ? s_flaglist : s_arglist;

View file

@ -1,7 +1,7 @@
#include "storm/Log.hpp"
#include "storm/Thread.hpp"
#include "storm/Error.hpp"
#include "storm/Memory.hpp"
#include <bc/Memory.hpp>
#include <cstdio>
#include <cstdlib>
#include <ctime>

View file

@ -1,15 +0,0 @@
#ifndef STORM_MEMORY_HPP
#define STORM_MEMORY_HPP
#include <cstdint>
#include <cstdlib>
void* SMemAlloc(size_t bytes, const char* filename, int32_t linenumber, uint32_t flags);
void SMemFree(void* ptr);
void SMemFree(void* ptr, const char* filename, int32_t linenumber, uint32_t flags);
void* SMemReAlloc(void* ptr, size_t bytes, const char* filename, int32_t linenumber, uint32_t flags);
#endif

View file

@ -1,12 +1,13 @@
#include "storm/String.hpp"
#include "storm/Error.hpp"
#include "storm/Memory.hpp"
#include <bc/Memory.hpp>
#include "storm/string/bjhash.hpp"
#include <cctype>
#include <cmath>
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <algorithm>
#if defined(WHOA_SYSTEM_WIN)
#include <string.h>
@ -207,8 +208,9 @@ void SStrInitialize() {
}
char* SStrChr(char* string, char search) {
STORM_ASSERT(string);
STORM_VALIDATE(string, ERROR_INVALID_PARAMETER, nullptr);
STORM_VALIDATE_BEGIN;
STORM_VALIDATE(string);
STORM_VALIDATE_END;
if (!*string) {
return nullptr;
@ -226,8 +228,9 @@ char* SStrChr(char* string, char search) {
}
const char* SStrChr(const char* string, char search) {
STORM_ASSERT(string);
STORM_VALIDATE(string, ERROR_INVALID_PARAMETER, nullptr);
STORM_VALIDATE_BEGIN;
STORM_VALIDATE(string);
STORM_VALIDATE_END;
if (!*string) {
return nullptr;
@ -245,8 +248,9 @@ const char* SStrChr(const char* string, char search) {
}
char* SStrChrR(char* string, char search) {
STORM_ASSERT(string);
STORM_VALIDATE(string, ERROR_INVALID_PARAMETER, nullptr);
STORM_VALIDATE_BEGIN;
STORM_VALIDATE(string);
STORM_VALIDATE_END;
char* result;
@ -260,8 +264,9 @@ char* SStrChrR(char* string, char search) {
}
const char* SStrChrR(const char* string, char search) {
STORM_ASSERT(string);
STORM_VALIDATE(string, ERROR_INVALID_PARAMETER, nullptr);
STORM_VALIDATE_BEGIN;
STORM_VALIDATE(string);
STORM_VALIDATE_END;
const char* result;
@ -289,9 +294,10 @@ int32_t SStrCmpI(const char* string1, const char* string2, size_t maxchars) {
}
size_t SStrCopy(char* dest, const char* source, size_t destsize) {
STORM_ASSERT(dest);
STORM_ASSERT(source);
STORM_VALIDATE(dest && source, ERROR_INVALID_PARAMETER, 0);
STORM_VALIDATE_BEGIN;
STORM_VALIDATE(dest);
STORM_VALIDATE(source);
STORM_VALIDATE_END;
char* destbuf = dest;
@ -322,6 +328,21 @@ size_t SStrCopy(char* dest, const char* source, size_t destsize) {
return static_cast<size_t>(destbuf - dest);
}
size_t SStrNCopy(char* dest, const char* source, size_t maxchars, size_t destsize) {
STORM_VALIDATE_BEGIN;
STORM_VALIDATE(dest);
STORM_VALIDATE(source);
STORM_VALIDATE_END;
auto length = strnlen(source, maxchars);
if (destsize) {
auto count = std::min(length, destsize - 1);
memcpy(dest, source, count);
dest[count] = '\0';
}
return length;
}
char* SStrDupA(const char* string, const char* filename, uint32_t linenumber) {
size_t len = SStrLen(string) + 1;
char* dup = static_cast<char*>(SMemAlloc(len, filename, linenumber, 0x0));
@ -362,7 +383,9 @@ uint32_t SStrHashHT(const char* string) {
}
size_t SStrLen(const char* string) {
STORM_ASSERT(string);
STORM_VALIDATE_BEGIN;
STORM_VALIDATE(string);
STORM_VALIDATE_END;
auto stringEnd = string;
while (*stringEnd) {
@ -460,14 +483,13 @@ const char* SStrStr(const char* string, const char* search) {
}
void SStrTokenize(const char** string, char* buffer, size_t bufferchars, const char* whitespace, int32_t* quoted) {
STORM_ASSERT(string);
STORM_VALIDATE(string, ERROR_INVALID_PARAMETER);
STORM_ASSERT(*string);
STORM_VALIDATE(*string, ERROR_INVALID_PARAMETER);
STORM_ASSERT(buffer || !bufferchars);
STORM_VALIDATE(buffer || !bufferchars, ERROR_INVALID_PARAMETER);
STORM_ASSERT(whitespace);
STORM_VALIDATE(whitespace, ERROR_INVALID_PARAMETER);
STORM_VALIDATE_BEGIN;
STORM_VALIDATE(string);
STORM_VALIDATE(*string);
STORM_VALIDATE(buffer || !bufferchars);
STORM_VALIDATE(whitespace);
STORM_VALIDATE_END;
int32_t inquotes = 0;
int32_t usedquotes = 0;
@ -551,7 +573,9 @@ LABEL_35:
}
float SStrToFloat(const char* string) {
STORM_ASSERT(string);
STORM_VALIDATE_BEGIN;
STORM_VALIDATE(string);
STORM_VALIDATE_END;
SStrInitialize();
@ -646,7 +670,9 @@ float SStrToFloat(const char* string) {
}
int32_t SStrToInt(const char* string) {
STORM_ASSERT(string);
STORM_VALIDATE_BEGIN;
STORM_VALIDATE(string);
STORM_VALIDATE_END;
int32_t result = 0;
bool negative = false;
@ -670,7 +696,9 @@ int32_t SStrToInt(const char* string) {
}
uint32_t SStrToUnsigned(const char* string) {
STORM_ASSERT(string);
STORM_VALIDATE_BEGIN;
STORM_VALIDATE(string);
STORM_VALIDATE_END;
uint32_t result = 0;

View file

@ -1,9 +1,9 @@
#ifndef STORM_ARRAY_TS_FIXED_ARRAY_HPP
#define STORM_ARRAY_TS_FIXED_ARRAY_HPP
#include "storm/Memory.hpp"
#include "storm/array/TSBaseArray.hpp"
#include <bc/Memory.hpp>
#include <cstdint>
#include "storm/array/TSBaseArray.hpp"
template <class T>
class TSFixedArray : public TSBaseArray<T> {

View file

@ -2,10 +2,10 @@
#define STORM_ARRAY_TS_GROWABLE_ARRAY_HPP
#include "storm/array/TSFixedArray.hpp"
#include <bc/Memory.hpp>
#include <algorithm>
#include <cstdint>
#include <cstring>
#include <new>
template <class T>
class TSGrowableArray : public TSFixedArray<T> {

View file

@ -1,5 +1,6 @@
#include <bc/Memory.hpp>
#include "storm/hash/Hashkey.hpp"
#include "storm/Memory.hpp"
#include "storm/String.hpp"
bool HASHKEY_NONE::operator==(const HASHKEY_NONE& key) {

View file

@ -1,7 +1,7 @@
#ifndef STORM_LIST_TS_LIST_HPP
#define STORM_LIST_TS_LIST_HPP
#include "storm/Memory.hpp"
#include <bc/Memory.hpp>
#include "storm/list/TSGetLink.hpp"
#include "storm/list/TSLink.hpp"
#include <cstddef>

View file

@ -1,5 +1,5 @@
#include "storm/thread/S_Thread.hpp"
#include "storm/Memory.hpp"
#include <bc/Memory.hpp>
void* S_Thread::s_SLaunchThread(void* threadParam) {
// TODO

View file

@ -1,5 +1,5 @@
#include "storm/Thread.hpp"
#include "storm/Memory.hpp"
#include <bc/Memory.hpp>
#include "storm/String.hpp"
#include "storm/thread/S_Thread.hpp"
#include <cstdio>

View file

@ -1,5 +1,5 @@
#include "storm/thread/S_Thread.hpp"
#include "storm/Memory.hpp"
#include <bc/Memory.hpp>
uint32_t S_Thread::s_SLaunchThread(void* threadParam) {
// TODO

View file

@ -1,5 +1,5 @@
#include "storm/Thread.hpp"
#include "storm/Memory.hpp"
#include <bc/Memory.hpp>
#include "storm/String.hpp"
#include "storm/thread/S_Thread.hpp"
#include "storm/thread/mac/SThreadRunner.h"

View file

@ -1,5 +1,5 @@
#include "storm/thread/S_Thread.hpp"
#include "storm/Memory.hpp"
#include <bc/Memory.hpp>
DWORD WINAPI S_Thread::s_SLaunchThread(void* threadParam) {
auto params = static_cast<SThreadParmBlock*>(threadParam);

View file

@ -1,5 +1,5 @@
#include "storm/Thread.hpp"
#include "storm/Memory.hpp"
#include <bc/Memory.hpp>
#include "storm/String.hpp"
#include "storm/thread/S_Thread.hpp"
#include <new>