Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Rubberduckycooly
GitHub Repository: Rubberduckycooly/RSDKv5-Decompilation
Path: blob/master/RSDKv5/RSDK/Storage/Storage.hpp
1167 views
1
#ifndef STORAGE_H
2
#define STORAGE_H
3
4
namespace RSDK
5
{
6
#define STORAGE_ENTRY_COUNT (0x1000)
7
8
enum StorageDataSets {
9
DATASET_STG = 0,
10
DATASET_MUS = 1,
11
DATASET_SFX = 2,
12
DATASET_STR = 3,
13
DATASET_TMP = 4,
14
DATASET_MAX, // used to signify limits
15
};
16
17
struct DataStorage {
18
uint32 *memoryTable;
19
uint32 usedStorage;
20
uint32 storageLimit;
21
uint32 **dataEntries[STORAGE_ENTRY_COUNT]; // pointer to the actual variable
22
uint32 *storageEntries[STORAGE_ENTRY_COUNT]; // pointer to the storage in "memoryTable"
23
uint32 entryCount;
24
uint32 clearCount;
25
};
26
27
template <typename T> class List
28
{
29
T *entries = NULL;
30
int32 count = 0;
31
int32 length = 0;
32
33
public:
34
List()
35
{
36
entries = NULL;
37
count = 0;
38
}
39
~List()
40
{
41
if (entries) {
42
free(entries);
43
entries = NULL;
44
}
45
}
46
T *Append()
47
{
48
if (count == length) {
49
length += 32;
50
size_t len = sizeof(T) * length;
51
T *entries_realloc = (T *)realloc(entries, len);
52
53
if (entries_realloc) {
54
entries = entries_realloc;
55
}
56
}
57
58
T *entry = &entries[count];
59
memset(entry, 0, sizeof(T));
60
count++;
61
return entry;
62
}
63
void Remove(uint32 index)
64
{
65
// move every item back one
66
for (int32 i = index; i < count; i++) {
67
if (i + 1 < count) {
68
entries[i] = entries[i + 1];
69
}
70
else { // Last Item, free it
71
count--;
72
}
73
}
74
75
if (count < length - 32) {
76
length -= 32;
77
size_t len = sizeof(T) * length;
78
T *entries_realloc = (T *)realloc(entries, len);
79
80
if (entries_realloc)
81
entries = entries_realloc;
82
}
83
}
84
85
inline T *At(int32 index) { return &entries[index]; }
86
87
inline void Clear(bool32 dealloc = false)
88
{
89
for (int32 i = count - 1; i >= 0; i--) {
90
Remove(i);
91
}
92
93
if (entries && dealloc) {
94
free(entries);
95
entries = NULL;
96
}
97
}
98
99
inline int32 Count() { return count; }
100
};
101
102
extern DataStorage dataStorage[DATASET_MAX];
103
104
bool32 InitStorage();
105
void ReleaseStorage();
106
107
void AllocateStorage(void **dataPtr, uint32 size, StorageDataSets dataSet, bool32 clear);
108
void DefragmentAndGarbageCollectStorage(StorageDataSets set);
109
void RemoveStorageEntry(void **dataPtr);
110
void CopyStorage(uint32 **src, uint32 **dst);
111
void GarbageCollectStorage(StorageDataSets dataSet);
112
113
#if RETRO_REV0U
114
#include "Legacy/UserStorageLegacy.hpp"
115
#endif
116
117
} // namespace RSDK
118
119
#endif // STORAGE_H
120
121