Initial commit

This commit is contained in:
2026-02-02 04:50:13 +01:00
commit 5b11698731
22592 changed files with 7677434 additions and 0 deletions

130
engines/m4/mem/mem.cpp Normal file
View File

@@ -0,0 +1,130 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "m4/mem/mem.h"
#include "m4/mem/memman.h"
#include "m4/core/errors.h"
#include "m4/vars.h"
namespace M4 {
#define MAX_REQUESTS 255
void mem_stash_init(int16 num_types) {
if (num_types > _MEMTYPE_LIMIT)
error_show(FL, 'MSIF', "num_types (%d) _MEMTYPE_LIMIT (%d)", num_types, _MEMTYPE_LIMIT);
for (int i = 0; i < _MEMTYPE_LIMIT; i++) {
_G(memBlock)[i] = nullptr;
_G(sizeMem)[i] = 0;
_G(requests)[i] = 0;
}
}
void mem_stash_shutdown(void) {
for (int i = 0; i < _MEMTYPE_LIMIT; i++) {
if (_G(memBlock)[i]) {
mem_free(_G(memBlock)[i]);
_G(memBlock)[i] = nullptr;
}
}
}
bool mem_register_stash_type(int32 *memType, int32 blockSize, int32 maxNumRequests, const Common::String &name) {
int32 i = 0;
bool found = false;
while ((i < _MEMTYPE_LIMIT) && (_G(sizeMem)[i] > 0) && (!found)) {
i++;
}
if (i == _MEMTYPE_LIMIT)
error_show(FL, 'MSIF', "stash: %s", name.c_str());
// Found a slot
if (found || (i < _MEMTYPE_LIMIT)) {
_G(sizeMem)[i] = blockSize;
*memType = i;
if (maxNumRequests > MAX_REQUESTS)
maxNumRequests = MAX_REQUESTS;
_G(requests)[i] = maxNumRequests;
_G(memBlock)[i] = mem_alloc((blockSize + sizeof(uintptr)) * maxNumRequests, name.c_str());
memset(_G(memBlock)[i], 0, (blockSize + sizeof(uintptr)) * maxNumRequests);
return true;
}
error_show(FL, 'MSIF', "stash: %s", name.c_str());
return false;
}
void mem_free_to_stash(void *mem, int32 memType) {
// _G(memBlock)[memType] is block associated with memType
int8 *b_ptr = (int8 *)_G(memBlock)[memType];
int32 index = ((intptr)mem - (intptr)_G(memBlock)[memType]) / (_G(sizeMem)[memType] + sizeof(uintptr));
if (index < 0 || index > _G(requests)[memType])
error_show(FL, 'MSGF');
b_ptr += index * (_G(sizeMem)[memType] + sizeof(uintptr));
*(uintptr *)b_ptr = 0;
}
void *mem_get_from_stash(int32 memType, const Common::String &name) {
int i;
int8 *b_ptr = (int8 *)_G(memBlock)[memType];
for (i = 0; i < _G(requests)[memType]; i++) {
if (!*(uintptr *)b_ptr) {
*(uintptr *)b_ptr = 1;
void *result = (void *)(b_ptr + sizeof(uintptr));
Common::fill((byte *)result, (byte *)result + _G(sizeMem)[memType], 0);
return result;
} else {
b_ptr += _G(sizeMem)[memType] + sizeof(uintptr);
}
}
error_show(FL, 'OOS!', "stash full %s", name.c_str());
return 0;
}
char *mem_strdup(const char *str) {
char *new_str = nullptr;
if (!str) {
new_str = (char *)mem_alloc(1, "string");
new_str[0] = '\0';
return new_str;
}
new_str = (char *)mem_alloc(strlen(str) + 1, "string");
if (!new_str)
return nullptr;
Common::strcpy_s(new_str, 256, str);
return new_str;
}
} // namespace M4

56
engines/m4/mem/mem.h Normal file
View File

@@ -0,0 +1,56 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef M4_MEM_MEM_H
#define M4_MEM_MEM_H
#include "common/str.h"
namespace M4 {
/**
* Initialize the memory manager.
*/
void mem_stash_init(int16 num_types);
/**
* Purges the memory manager
*/
void mem_stash_shutdown();
bool mem_register_stash_type(int32 *memType, int32 size, int32 numRequests, const Common::String &name);
/**
* To free a memory block whose size has been previously registered.
* @param myMem The pointer to be freed
*/
void mem_free_to_stash(void *myMem, int32 memType);
/**
* Deliver a memory block whose size has been previously registered.
*/
void *mem_get_from_stash(int32 memType, const Common::String &name);
char *mem_strdup(const char *str);
} // namespace M4
#endif

26
engines/m4/mem/memman.cpp Normal file
View File

@@ -0,0 +1,26 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "m4/mem/memman.h"
namespace M4 {
} // namespace M4

48
engines/m4/mem/memman.h Normal file
View File

@@ -0,0 +1,48 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef M4_MEM_MEMMAN_H
#define M4_MEM_MEMMAN_H
#include "common/algorithm.h"
#include "m4/m4_types.h"
namespace M4 {
#define _MEMTYPE_LIMIT 33
inline Handle mem_alloc(size_t size, const char *) {
byte *ptr = (byte *)malloc(size);
Common::fill(ptr, ptr + size, 0);
return (Handle)ptr;
}
inline void mem_free(Handle ptr) {
free(ptr);
}
inline Handle mem_realloc(Handle src, size_t new_size, const char *) {
return (Handle)realloc(src, new_size);
}
} // namespace M4
#endif

59
engines/m4/mem/reloc.cpp Normal file
View File

@@ -0,0 +1,59 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "m4/mem/reloc.h"
#include "common/textconsole.h"
namespace M4 {
struct HR {
void *_data;
};
MemHandle NewHandle(size_t size, const Common::String &str) {
HR *result = (HR *)malloc(sizeof(HR));
if (!result)
error("Unable to allocate memory - %zd bytes for %s", size, str.c_str());
result->_data = malloc(size);
return (MemHandle)result;
}
bool mem_ReallocateHandle(MemHandle h, size_t size, const Common::String &) {
HR *hr = (HR *)h;
assert(!hr->_data);
hr->_data = malloc(size);
return true;
}
MemHandle MakeNewHandle(size_t size, const Common::String &name) {
return NewHandle(size, name);
}
void DisposeHandle(MemHandle handle) {
free(*handle);
free(handle);
}
} // namespace M4

49
engines/m4/mem/reloc.h Normal file
View File

@@ -0,0 +1,49 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef M4_RELOC_H
#define M4_RELOC_H
#include "common/str.h"
#include "m4/m4_types.h"
namespace M4 {
typedef void **MemHandle;
typedef int32 Size;
inline void HLock(Handle h) {}
inline void HUnLock(Handle h) {}
inline void HPurge(Handle h) {}
inline void HNoPurge(Handle h) {}
inline bool MakeMem(size_t FreeBlockNeeded, const char *) {
return true;
}
MemHandle MakeNewHandle(size_t size, const Common::String &);
bool mem_ReallocateHandle(MemHandle h, size_t size, const Common::String &name);
MemHandle NewHandle(size_t size, const Common::String &str);
void DisposeHandle(MemHandle handle);
} // namespace M4
#endif

246
engines/m4/mem/res.cpp Normal file
View File

@@ -0,0 +1,246 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "common/file.h"
#include "common/textconsole.h"
#include "m4/mem/res.h"
#include "m4/mem/memman.h"
#include "m4/mem/reloc.h"
#include "m4/core/errors.h"
#include "m4/vars.h"
namespace M4 {
#define PRIME 8179
#define HASHSIZE MAX_RESOURCES
#define FULLY_BUFFERED 1
#define MARKED_PURGE 0x80
Resources::~Resources() {
delete _fp;
}
Resources::Entry *Resources::findAndSetResEntry(const Common::String &resourceName) {
int orig_hash_val;
int hash_val;
Entry *res = nullptr;
Common::String resName = resourceName;
if (_useLowercase)
resName.toLowercase();
orig_hash_val = hash_val = hash(resName);
// If empty slot at this hash, then we're done
if (!_resources[hash_val].Flags)
goto got_one;
// Flags is set, so scan until Flags is clear, or the resource name strings match
while ((_resources[hash_val].Flags & FULLY_BUFFERED)
&& !resName.equals(_resources[hash_val].name)) {
// if we searched every entry to no avail:
if ((hash_val = (hash_val + 1) & (HASHSIZE - 1)) == orig_hash_val)
goto test4;
}
goto got_one;
test4:
hash_val = orig_hash_val;
while (!(_resources[hash_val].Flags & MARKED_PURGE))
// if we searched every entry to no avail:
if ((hash_val = (hash_val + 1) & (HASHSIZE - 1)) == orig_hash_val) {
error("Out of resource space");
}
res = &_resources[hash_val];
delete[] res->RHandle;
res->RHandle = nullptr;
got_one:
res = &_resources[hash_val];
res->name = resName;
res->Flags = FULLY_BUFFERED;
return res;
}
int Resources::hash(const Common::String &sym) const {
uint ret_val = 0;
const char *s = sym.c_str();
while (*s) {
ret_val += *s++;
ret_val *= PRIME;
}
return ret_val & (HASHSIZE - 1);
}
MemHandle Resources::rget(const Common::String &resourceName, int32 *resourceSize) {
Entry *resEntry;
if (resourceSize)
*resourceSize = 0;
if (!(resEntry = findAndSetResEntry(resourceName))) {
term_message("rget:%s -> failed!", resourceName.c_str());
return nullptr;
}
// Check if resource is fully buffered
// All resources are currently fully buffered!
if (!(resEntry->Flags & FULLY_BUFFERED)) {
term_message("rget:%s -> failed!", resourceName.c_str());
return nullptr;
}
// If there's a handle with some memory in it
// Resource is cached; unmark purge flag and return handle
if (resEntry->RHandle && *resEntry->RHandle) {
if (resourceSize)
*resourceSize = resEntry->Size;
HNoPurge(resEntry->RHandle);
resEntry->Flags &= ~MARKED_PURGE;
term_message("rget:%s -> from memory", resourceName.c_str());
return resEntry->RHandle;
}
// Check if size known
if ((resEntry->Size = get_file(resEntry->name)) == -1)
error("Error getting entry %s", resEntry->name.c_str());
// Check if buffer size is set
if (resEntry->BufferSize != resEntry->Size)
resEntry->BufferSize = resEntry->Size;
// Check if resource handle allocated
if (!resEntry->RHandle)
resEntry->RHandle = MakeNewHandle(resEntry->BufferSize, resEntry->name.c_str());
// Check if resource handle has valid memory block allocated to it
if (!*resEntry->RHandle)
mem_ReallocateHandle(resEntry->RHandle, resEntry->BufferSize, resEntry->name.c_str());
if (!do_file(resEntry->RHandle))
error("rget: do_file -> %s", resourceName.c_str());
if (resourceSize) // xi change
*resourceSize = resEntry->Size;
term_message("rget:%s -> from disk", resourceName.c_str());
return resEntry->RHandle;
}
void Resources::rtoss(const Common::String &resourceName) {
int hash_val;
Entry *resEntry = nullptr;
Common::String resName = resourceName;
if (_useLowercase)
resName.toLowercase();
hash_val = hash(resName);
// Check if resource is in resource table
if (_resources[hash_val].Flags) {
for (int ctr = 0; ctr <= HASHSIZE && _resources[hash_val].Flags;
++ctr, hash_val = (hash_val + 1) % HASHSIZE) {
if (resName.equals(_resources[hash_val].name)) {
resEntry = &_resources[hash_val];
break;
}
}
}
if (!resEntry)
error_show(FL, 'RIOU', "rtoss: %s", resourceName.c_str());
else if (!(resEntry->Flags & FULLY_BUFFERED))
return;
if (!resEntry || !*resEntry->RHandle) {
term_message("bad rtoss no memory %s", resourceName.c_str());
return;
}
if (resEntry->Flags & MARKED_PURGE)
// This is actually okay. In Riddle, for example, "show script"
// is assigned to multiple MACH slots, and in ClearWSAssets will
// pass it to rtoss for each entry
term_message("multiple rtoss: %s", resourceName.c_str());
else
term_message("rtossing: %s", resourceName.c_str());
HUnLock(resEntry->RHandle);
HPurge(resEntry->RHandle);
resEntry->Flags |= MARKED_PURGE;
}
int32 Resources::get_file(const Common::String &name) {
assert(!_fp);
_fp = new SysFile(name);
assert(_fp);
if (!_fp->exists())
error("get_file - getting %s", name.c_str());
return _fp->size();
}
bool Resources::do_file(MemHandle buffer) {
_fp->seek(0);
bool result = true;
// read in resource from file
if (!_fp->read(buffer)) {
term_message("unable to read: %s", _fp->filename.c_str());
result = false;
}
delete _fp;
_fp = nullptr;
return result;
}
void Resources::dumpResources() {
if (gDebugLevel >= 2) {
debug(2, "List of active resources:");
for (int i = 0; i < MAX_RESOURCES; ++i) {
Entry &entry = _resources[i];
if (entry.Flags != 0 && (entry.Flags & MARKED_PURGE) == 0)
debug(2, "#%d - %s", i, entry.name.c_str());
}
debugN(2, "\n");
}
}
MemHandle rget(const Common::String &resourceName, int32 *resourceSize) {
return _G(resources).rget(resourceName, resourceSize);
}
void rtoss(const Common::String &resourceName) {
_G(resources).rtoss(resourceName);
}
} // namespace M4

71
engines/m4/mem/res.h Normal file
View File

@@ -0,0 +1,71 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef M4_MEM_RES_H
#define M4_MEM_RES_H
#include "common/str.h"
#include "m4/mem/memman.h"
#include "m4/fileio/sys_file.h"
namespace M4 {
#define MAX_RESOURCES (1 << 7)
class Resources {
struct Entry {
Common::String name;
MemHandle RHandle = nullptr;
int32 BufferSize = 0;
int32 Size = 0;
byte Flags = 0;
};
private:
Entry _resources[MAX_RESOURCES];
SysFile *_fp = nullptr;
bool _useLowercase = false;
Entry *findAndSetResEntry(const Common::String &resourceName);
int hash(const Common::String &sym) const;
int32 get_file(const Common::String &name);
bool do_file(MemHandle buffer);
public:
~Resources();
void setUseLowercase(bool flag) {
_useLowercase = flag;
}
MemHandle rget(const Common::String &resourceName, int32 *resourceSize);
void rtoss(const Common::String &resourceName);
/**
* Dumps a list of any active resources
*/
void dumpResources();
};
MemHandle rget(const Common::String &resourceName, int32 *ResourceSize);
void rtoss(const Common::String &resourceName);
} // namespace M4
#endif