Path: blob/master/RSDKv5/RSDK/Storage/Storage.hpp
1167 views
#ifndef STORAGE_H1#define STORAGE_H23namespace RSDK4{5#define STORAGE_ENTRY_COUNT (0x1000)67enum StorageDataSets {8DATASET_STG = 0,9DATASET_MUS = 1,10DATASET_SFX = 2,11DATASET_STR = 3,12DATASET_TMP = 4,13DATASET_MAX, // used to signify limits14};1516struct DataStorage {17uint32 *memoryTable;18uint32 usedStorage;19uint32 storageLimit;20uint32 **dataEntries[STORAGE_ENTRY_COUNT]; // pointer to the actual variable21uint32 *storageEntries[STORAGE_ENTRY_COUNT]; // pointer to the storage in "memoryTable"22uint32 entryCount;23uint32 clearCount;24};2526template <typename T> class List27{28T *entries = NULL;29int32 count = 0;30int32 length = 0;3132public:33List()34{35entries = NULL;36count = 0;37}38~List()39{40if (entries) {41free(entries);42entries = NULL;43}44}45T *Append()46{47if (count == length) {48length += 32;49size_t len = sizeof(T) * length;50T *entries_realloc = (T *)realloc(entries, len);5152if (entries_realloc) {53entries = entries_realloc;54}55}5657T *entry = &entries[count];58memset(entry, 0, sizeof(T));59count++;60return entry;61}62void Remove(uint32 index)63{64// move every item back one65for (int32 i = index; i < count; i++) {66if (i + 1 < count) {67entries[i] = entries[i + 1];68}69else { // Last Item, free it70count--;71}72}7374if (count < length - 32) {75length -= 32;76size_t len = sizeof(T) * length;77T *entries_realloc = (T *)realloc(entries, len);7879if (entries_realloc)80entries = entries_realloc;81}82}8384inline T *At(int32 index) { return &entries[index]; }8586inline void Clear(bool32 dealloc = false)87{88for (int32 i = count - 1; i >= 0; i--) {89Remove(i);90}9192if (entries && dealloc) {93free(entries);94entries = NULL;95}96}9798inline int32 Count() { return count; }99};100101extern DataStorage dataStorage[DATASET_MAX];102103bool32 InitStorage();104void ReleaseStorage();105106void AllocateStorage(void **dataPtr, uint32 size, StorageDataSets dataSet, bool32 clear);107void DefragmentAndGarbageCollectStorage(StorageDataSets set);108void RemoveStorageEntry(void **dataPtr);109void CopyStorage(uint32 **src, uint32 **dst);110void GarbageCollectStorage(StorageDataSets dataSet);111112#if RETRO_REV0U113#include "Legacy/UserStorageLegacy.hpp"114#endif115116} // namespace RSDK117118#endif // STORAGE_H119120121