Move Lua allow lists over to using a hashmap

This commit is contained in:
MysterD 2022-04-22 18:51:25 -07:00
parent 0d29a29c1b
commit 9e71aba050
2 changed files with 1190 additions and 63 deletions

View file

@ -1,14 +1,16 @@
#include <stdio.h>
#include "smlua.h"
#define STB_DS_IMPLEMENTATION 1
#include "pc/utils/stb_ds.h"
struct CAllowListNode {
u64 pointer;
struct CAllowListNode* next;
struct AllowList {
u64 key;
u8 value;
};
#define LOT_COUNT (LOT_MAX + (LOT_AUTOGEN_MAX - LOT_AUTOGEN_MIN))
static struct CAllowListNode* sObjectAllowList[LOT_COUNT] = { 0 };
static u16 sCachedObjectAllowed[LOT_COUNT] = { 0 };
static struct AllowList* sObjectAllowList[LOT_COUNT] = { 0 };
static u64 sCachedObjectAllowed[LOT_COUNT] = { 0 };
static u16 smlua_lot_mapping(u16 lot) {
if (lot >= LOT_MAX) {
@ -25,12 +27,11 @@ void smlua_cobject_allowlist_init(void) {
void smlua_cobject_allowlist_shutdown(void) {
for (s32 i = 0; i < LOT_COUNT; i++) {
sCachedObjectAllowed[i] = 0;
struct CAllowListNode* node = sObjectAllowList[i];
while (node != NULL) {
struct CAllowListNode* nextNode = node->next;
free(node);
node = nextNode;
while (sObjectAllowList[i] && hmlen(sObjectAllowList[i]) > 0) {
hmdel(sObjectAllowList[i], sObjectAllowList[i]->key);
}
sObjectAllowList[i] = NULL;
}
}
@ -43,22 +44,8 @@ void smlua_cobject_allowlist_add(u16 lot, u64 pointer) {
if (sCachedObjectAllowed[m] == pointer) { return; }
sCachedObjectAllowed[m] = pointer;
struct CAllowListNode* curNode = sObjectAllowList[m];
struct CAllowListNode* prevNode = NULL;
while (curNode != NULL) {
if (pointer == curNode->pointer) { return; }
if (pointer < curNode->pointer) { break; }
prevNode = curNode;
curNode = curNode->next;
}
struct CAllowListNode* node = malloc(sizeof(struct CAllowListNode));
node->pointer = pointer;
node->next = curNode;
if (prevNode == NULL) {
sObjectAllowList[m] = node;
} else {
prevNode->next = node;
if (!hmget(sObjectAllowList[m], pointer)) {
hmput(sObjectAllowList[m], pointer, 1);
}
}
@ -69,19 +56,13 @@ bool smlua_cobject_allowlist_contains(u16 lot, u64 pointer) {
u16 m = smlua_lot_mapping(lot);
if (sCachedObjectAllowed[m] == pointer) { return true; }
struct CAllowListNode* node = sObjectAllowList[m];
while (node != NULL) {
if (pointer == node->pointer) { return true; }
if (pointer < node->pointer) { return false; }
node = node->next;
}
return false;
return hmget(sObjectAllowList[m], pointer);
}
/////////////////////////////
static struct CAllowListNode* sPointerAllowList[LVT_MAX] = { 0 };
static u16 sCachedPointerAllowed[LVT_MAX] = { 0 };
static struct AllowList* sPointerAllowList[LVT_MAX] = { 0 };
static u64 sCachedPointerAllowed[LVT_MAX] = { 0 };
void smlua_cpointer_allowlist_init(void) {
smlua_cpointer_allowlist_shutdown();
@ -90,12 +71,11 @@ void smlua_cpointer_allowlist_init(void) {
void smlua_cpointer_allowlist_shutdown(void) {
for (s32 i = 0; i < LVT_MAX; i++) {
sCachedPointerAllowed[i] = 0;
struct CAllowListNode* node = sPointerAllowList[i];
while (node != NULL) {
struct CAllowListNode* nextNode = node->next;
free(node);
node = nextNode;
while (sPointerAllowList[i] && hmlen(sPointerAllowList[i]) > 0) {
hmdel(sPointerAllowList[i], sPointerAllowList[i]->key);
}
sPointerAllowList[i] = NULL;
}
}
@ -107,22 +87,8 @@ void smlua_cpointer_allowlist_add(u16 lvt, u64 pointer) {
if (sCachedPointerAllowed[lvt] == pointer) { return; }
sCachedPointerAllowed[lvt] = pointer;
struct CAllowListNode* curNode = sPointerAllowList[lvt];
struct CAllowListNode* prevNode = NULL;
while (curNode != NULL) {
if (pointer == curNode->pointer) { return; }
if (pointer < curNode->pointer) { break; }
prevNode = curNode;
curNode = curNode->next;
}
struct CAllowListNode* node = malloc(sizeof(struct CAllowListNode));
node->pointer = pointer;
node->next = curNode;
if (prevNode == NULL) {
sPointerAllowList[lvt] = node;
} else {
prevNode->next = node;
if (!hmget(sPointerAllowList[lvt], pointer)) {
hmput(sPointerAllowList[lvt], pointer, 1);
}
}
@ -132,11 +98,5 @@ bool smlua_cpointer_allowlist_contains(u16 lvt, u64 pointer) {
if (sCachedPointerAllowed[lvt] == pointer) { return true; }
struct CAllowListNode* node = sPointerAllowList[lvt];
while (node != NULL) {
if (pointer == node->pointer) { return true; }
if (pointer < node->pointer) { return false; }
node = node->next;
}
return false;
return hmget(sPointerAllowList[lvt], pointer);
}

1167
src/pc/utils/stb_ds.h Normal file

File diff suppressed because it is too large Load diff