Created
May 23, 2023 19:29
-
-
Save jaxFF/1cfce693fb420c38384ba30882e2ce85 to your computer and use it in GitHub Desktop.
shared interfacing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "shared.h" | |
global string DllInterfaceName = {"dll_001", 7}; | |
#ifndef DLL_C | |
#define DLL_C | |
// Start compilation unit | |
#define MAIN_C | |
#include "main.c" | |
typedef struct interface_module interface_module; | |
struct interface_module { | |
uint64_t Hash; | |
HANDLE TheModuleHandle; | |
}; | |
global interface_module* InterfacesForThisModule = {0}; | |
void* LoadAndPresentInterface(string, size_t); | |
global dll_struct* DLL01Interface = (dll_struct*)LoadAndPresentInterface( | |
string({"dll_001", 7}), sizeof(dll_struct)); | |
/// | |
#if 0 | |
void GetModuleInterface(string a, void* b) { | |
// Query InterfacesForThisModule | |
// Hash must match module+funcname, or return | |
// Call and return function result through void* | |
} | |
#endif | |
interface_hash GetInterfaceHash(string Name) { | |
u64 HashValue = MurmurHash3UpdateString(0x101, Name); | |
HashValue = MurmurHash3Finalize(HashValue); | |
u64 HashBucket = HashValue % (ArrayCount(TheInterfaces) - 1); | |
Assert(HashBucket < ArrayCount(TheInterfaces)); | |
#ifdef __cplusplus | |
return interface_hash{HashValue, HashBucket}; | |
#else | |
return (interface_hash){HashValue, HashBucket}; | |
#endif | |
} | |
void* LoadAndPresentInterface(string Name, size_t Size) { | |
printf("0x%02x\n", &TheInterfaces); | |
// Add to InterfacesForThisModule | |
interface_hash Hash = GetInterfaceHash(Name); | |
interfaces* Entry = TheInterfaces[Hash.Bucket]; | |
interfaces* TheInterface = Entry; | |
if (!TheInterfaces[Hash.Bucket]) { | |
TheInterface = (interfaces*)VirtualAlloc(0, sizeof(interfaces), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); | |
TheInterface->NextInHash = TheInterfaces[Hash.Bucket]; | |
TheInterface->Name = Name; // copy !!! | |
TheInterface->Hash = Hash.Value; | |
TheInterfaces[Hash.Bucket] = TheInterface; | |
Breakpoint; | |
} | |
if (TheInterface->InterfacePtr == 0) { | |
TheInterface->InterfacePtr = VirtualAlloc(0, Size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); | |
} | |
return 0; | |
} | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "shared.h" | |
#ifndef MAIN_C | |
#define MAIN_C | |
// Start compilation unit | |
#define DLL_C | |
#include "dll.cpp" | |
__declspec(dllexport) interfaces* TheInterfaces[4096] = { 0 }; | |
interface_hash GetInterfaceHash(string Name) { | |
u64 HashValue = MurmurHash3UpdateString(0x101, Name); | |
HashValue = MurmurHash3Finalize(HashValue); | |
u64 HashBucket = HashValue % (ArrayCount(TheInterfaces) - 1); | |
Assert(HashBucket < ArrayCount(TheInterfaces)); | |
#ifdef __cplusplus | |
return interface_hash{HashValue, HashBucket}; | |
#else | |
return (interface_hash){HashValue, HashBucket}; | |
#endif | |
} | |
__declspec(dllexport) void GetModuleInterface(string Name, void* InterfacePtr) { | |
// NOTE, TODO: Assumes only one underscore. It also must be on the right. | |
// TODO: We need a better way of getting the module name for the interface. | |
char* ModuleName[256] = {0}; | |
Assert(Name.Count < ArrayCount(ModuleName)); | |
strcpy(ModuleName, Name.Data); | |
{ | |
char* tmp = 0; | |
tmp = strchr(ModuleName, '_'); | |
*tmp = '\0'; | |
} | |
sprintf(ModuleName, "%s%s", ModuleName, ".dll"); | |
printf(ModuleName); | |
printf("\n"); | |
printf("%llu = ", GetInterfaceHash(Name).Value); | |
printf("%d\n", GetInterfaceHash(Name).Bucket); | |
// TODO: Start at the bucket | |
interface_hash Hash = GetInterfaceHash(Name); | |
interfaces* Entry = &TheInterfaces[Hash.Bucket]; | |
interfaces* TheInterface = Entry; | |
for (int Index = 0; Index < ArrayCount(TheInterfaces); ++Index) { | |
for (interfaces* At = TheInterfaces[Index]; At; At = At->NextInHash) { | |
if (strcmp(At->Name.Data, Name.Data) == 0) { | |
TheInterface = At; | |
break; | |
} | |
} | |
} | |
Breakpoint; | |
Assert(TheInterface); | |
} | |
int main(int argc, char* argv[]) { | |
string Name = {"dll_001", strlen("dll_001")}; | |
u64 Hash = MurmurHash3UpdateString(1234, Name); | |
Hash = MurmurHash3Finalize(Hash); | |
printf("%d\n", ArrayCount(TheInterfaces)); | |
printf("0x%02x\n", &TheInterfaces); | |
dll_struct* TheDLLInterface = 0; | |
GetModuleInterface((string){"dll_001", strlen("dll_001")}, | |
(void*)&TheDLLInterface); | |
return 0; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment