mirror of
https://github.com/coop-deluxe/sm64coopdx.git
synced 2024-11-21 19:45:10 +00:00
Swapped out hashmap to C++ for sync objects
This commit is contained in:
parent
ca94b7387e
commit
e47f40af9e
6 changed files with 109 additions and 25 deletions
74
data/dynos_cmap.cpp
Normal file
74
data/dynos_cmap.cpp
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
typedef std::map<int64_t, void*> Map;
|
||||||
|
|
||||||
|
struct MapIter {
|
||||||
|
Map* map;
|
||||||
|
Map::iterator itr;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
|
||||||
|
void* hmap_create() {
|
||||||
|
return reinterpret_cast<void*> (new Map());
|
||||||
|
}
|
||||||
|
|
||||||
|
void* hmap_get(void* map, int64_t k) {
|
||||||
|
Map* m = reinterpret_cast<Map*> (map);
|
||||||
|
Map::iterator pos = m->find(k);
|
||||||
|
if (pos == m->end()) {
|
||||||
|
return NULL;
|
||||||
|
} else {
|
||||||
|
return pos->second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void hmap_put(void* map, int64_t k, void* v) {
|
||||||
|
Map* m = reinterpret_cast<Map*> (map);
|
||||||
|
if (m->count(k) > 0) {
|
||||||
|
m->erase(k);
|
||||||
|
}
|
||||||
|
m->insert(std::pair<int64_t, void*>(k, v));
|
||||||
|
}
|
||||||
|
|
||||||
|
void hmap_del(void* map, int64_t k) {
|
||||||
|
Map* m = reinterpret_cast<Map*> (map);
|
||||||
|
m->erase(k);
|
||||||
|
}
|
||||||
|
|
||||||
|
void hmap_clear(void* map) {
|
||||||
|
Map* m = reinterpret_cast<Map*> (map);
|
||||||
|
m->clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t hmap_len(void* map) {
|
||||||
|
Map* m = reinterpret_cast<Map*> (map);
|
||||||
|
return m->size();
|
||||||
|
}
|
||||||
|
|
||||||
|
void* hmap_iter(void* map) {
|
||||||
|
auto iter = new MapIter();
|
||||||
|
Map* m = reinterpret_cast<Map*> (map);
|
||||||
|
iter->map = m;
|
||||||
|
return reinterpret_cast<void*> (iter);
|
||||||
|
}
|
||||||
|
|
||||||
|
void* hmap_begin(void* iter) {
|
||||||
|
MapIter* i = reinterpret_cast<MapIter*> (iter);
|
||||||
|
i->itr = i->map->begin();
|
||||||
|
if (i->itr == i->map->end()) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return i->itr->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* hmap_next(void* iter) {
|
||||||
|
MapIter* i = reinterpret_cast<MapIter*> (iter);
|
||||||
|
i->itr++;
|
||||||
|
if (i->itr == i->map->end()) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return i->itr->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // extern "C"
|
17
data/dynos_cmap.cpp.h
Normal file
17
data/dynos_cmap.cpp.h
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
#ifndef DYNOS_CMAP_CPP_H
|
||||||
|
#define DYNOS_CMAP_CPP_H
|
||||||
|
#ifndef __cplusplus
|
||||||
|
|
||||||
|
void* hmap_create();
|
||||||
|
void* hmap_get(void* map, int64_t k);
|
||||||
|
void hmap_put(void* map, int64_t k, void* v);
|
||||||
|
void hmap_del(void* map, int64_t k);
|
||||||
|
void hmap_clear(void* map);
|
||||||
|
size_t hmap_len(void* map);
|
||||||
|
|
||||||
|
void* hmap_iter(void* map);
|
||||||
|
void* hmap_begin(void* iter);
|
||||||
|
void* hmap_next(void* iter);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
#endif
|
|
@ -1,5 +1,6 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "smlua.h"
|
#include "smlua.h"
|
||||||
|
#define STB_DS_IMPLEMENTATION 1
|
||||||
#include "pc/utils/stb_ds.h"
|
#include "pc/utils/stb_ds.h"
|
||||||
|
|
||||||
struct AllowList {
|
struct AllowList {
|
||||||
|
|
|
@ -10,15 +10,10 @@
|
||||||
#include "game/object_helpers.h"
|
#include "game/object_helpers.h"
|
||||||
#include "pc/debuglog.h"
|
#include "pc/debuglog.h"
|
||||||
#include "pc/utils/misc.h"
|
#include "pc/utils/misc.h"
|
||||||
|
#include "data/dynos_cmap.cpp.h"
|
||||||
|
|
||||||
#define STB_DS_IMPLEMENTATION 1
|
void* sSoMap = NULL;
|
||||||
#include "pc/utils/stb_ds.h"
|
void* sSoIter = NULL;
|
||||||
|
|
||||||
struct SyncObjectEntry {
|
|
||||||
u64 key;
|
|
||||||
struct SyncObject* value;
|
|
||||||
};
|
|
||||||
struct SyncObjectEntry* sSoMap = NULL;
|
|
||||||
|
|
||||||
struct SyncObjectForgetEntry {
|
struct SyncObjectForgetEntry {
|
||||||
struct SyncObject* so;
|
struct SyncObject* so;
|
||||||
|
@ -28,13 +23,17 @@ struct SyncObjectForgetEntry {
|
||||||
struct SyncObjectForgetEntry* sForgetList = NULL;
|
struct SyncObjectForgetEntry* sForgetList = NULL;
|
||||||
|
|
||||||
static u32 sNextSyncId = SYNC_ID_BLOCK_SIZE / 2;
|
static u32 sNextSyncId = SYNC_ID_BLOCK_SIZE / 2;
|
||||||
static u32 sIterateIndex = 0;
|
|
||||||
static bool sFreeingAll = false;
|
static bool sFreeingAll = false;
|
||||||
|
|
||||||
////////////
|
////////////
|
||||||
// system //
|
// system //
|
||||||
////////////
|
////////////
|
||||||
|
|
||||||
|
void sync_objects_init_system(void) {
|
||||||
|
sSoMap = hmap_create();
|
||||||
|
sSoIter = hmap_iter(sSoMap);
|
||||||
|
}
|
||||||
|
|
||||||
static bool sync_objects_forget_list_contains(struct SyncObject* so) {
|
static bool sync_objects_forget_list_contains(struct SyncObject* so) {
|
||||||
struct SyncObjectForgetEntry* entry = sForgetList;
|
struct SyncObjectForgetEntry* entry = sForgetList;
|
||||||
while (entry) {
|
while (entry) {
|
||||||
|
@ -80,8 +79,7 @@ void sync_objects_clear(void) {
|
||||||
sync_object_forget(so->id);
|
sync_object_forget(so->id);
|
||||||
}
|
}
|
||||||
sFreeingAll = false;
|
sFreeingAll = false;
|
||||||
hmfree(sSoMap);
|
hmap_clear(sSoMap);
|
||||||
hmdefault(sSoMap, NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sync_object_forget(u32 syncId) {
|
void sync_object_forget(u32 syncId) {
|
||||||
|
@ -111,7 +109,7 @@ void sync_object_forget(u32 syncId) {
|
||||||
so->owned = false;
|
so->owned = false;
|
||||||
|
|
||||||
if (!sFreeingAll) {
|
if (!sFreeingAll) {
|
||||||
hmdel(sSoMap, syncId);
|
hmap_del(sSoMap, syncId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// add it to a list to free later
|
// add it to a list to free later
|
||||||
|
@ -239,23 +237,15 @@ void sync_object_init_field_with_size(struct Object *o, void* field, u8 size) {
|
||||||
|
|
||||||
struct SyncObject* sync_object_get(u32 syncId) {
|
struct SyncObject* sync_object_get(u32 syncId) {
|
||||||
if (syncId == 0) { return NULL; }
|
if (syncId == 0) { return NULL; }
|
||||||
return hmget(sSoMap, syncId);
|
return hmap_get(sSoMap, syncId);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SyncObject* sync_object_get_first(void) {
|
struct SyncObject* sync_object_get_first(void) {
|
||||||
sIterateIndex = 0;
|
return hmap_begin(sSoIter);
|
||||||
if (sSoMap && sIterateIndex < hmlen(sSoMap)) {
|
|
||||||
return sSoMap[sIterateIndex].value;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SyncObject* sync_object_get_next(void) {
|
struct SyncObject* sync_object_get_next(void) {
|
||||||
sIterateIndex++;
|
return hmap_next(sSoIter);
|
||||||
if (sSoMap && sIterateIndex < hmlen(sSoMap)) {
|
|
||||||
return sSoMap[sIterateIndex].value;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Object* sync_object_get_object(u32 syncId) {
|
struct Object* sync_object_get_object(u32 syncId) {
|
||||||
|
@ -415,8 +405,8 @@ bool sync_object_set_id(struct Object* o) {
|
||||||
so = calloc(1, sizeof(struct SyncObject));
|
so = calloc(1, sizeof(struct SyncObject));
|
||||||
so->id = syncId;
|
so->id = syncId;
|
||||||
so->extendedModelId = 0xFFFF;
|
so->extendedModelId = 0xFFFF;
|
||||||
hmput(sSoMap, syncId, so);
|
hmap_put(sSoMap, syncId, so);
|
||||||
LOG_INFO("Allocated sync object @ %u, size %ld", syncId, hmlen(sSoMap));
|
LOG_INFO("Allocated sync object @ %u, size %ld", syncId, hmap_len(sSoMap));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!so) {
|
if (!so) {
|
||||||
|
|
|
@ -41,6 +41,7 @@ struct SyncObject {
|
||||||
////////////
|
////////////
|
||||||
// system //
|
// system //
|
||||||
////////////
|
////////////
|
||||||
|
void sync_objects_init_system(void);
|
||||||
void sync_objects_update(void);
|
void sync_objects_update(void);
|
||||||
void sync_objects_clear(void);
|
void sync_objects_clear(void);
|
||||||
void sync_object_forget(u32 syncId);
|
void sync_object_forget(u32 syncId);
|
||||||
|
|
|
@ -272,6 +272,7 @@ void main_func(void) {
|
||||||
const char *userpath = gCLIOpts.SavePath[0] ? gCLIOpts.SavePath : sys_user_path();
|
const char *userpath = gCLIOpts.SavePath[0] ? gCLIOpts.SavePath : sys_user_path();
|
||||||
fs_init(sys_ropaths, gamedir, userpath);
|
fs_init(sys_ropaths, gamedir, userpath);
|
||||||
|
|
||||||
|
sync_objects_init_system();
|
||||||
mods_init();
|
mods_init();
|
||||||
configfile_load(configfile_name());
|
configfile_load(configfile_name());
|
||||||
dynos_pack_init();
|
dynos_pack_init();
|
||||||
|
|
Loading…
Reference in a new issue