WiiUPluginLoaderBackend/source/plugin/PluginData.cpp

63 lines
2.3 KiB
C++
Raw Normal View History

#include "PluginData.h"
2020-12-26 14:17:50 +01:00
#include "../utils/logger.h"
2022-02-04 16:25:44 +01:00
#include <malloc.h>
2020-05-17 20:43:04 +02:00
PluginData::PluginData(const PluginData &obj) {
2022-02-04 16:25:44 +01:00
this->buffer = obj.buffer;
2020-05-17 20:43:04 +02:00
this->heapHandle = obj.heapHandle;
this->memoryType = obj.memoryType;
2022-02-04 16:25:44 +01:00
this->length = obj.length;
2020-05-17 20:43:04 +02:00
}
void PluginData::freeMemory() {
2020-12-26 14:17:50 +01:00
if (buffer == nullptr) {
return;
}
2020-05-03 12:30:15 +02:00
switch (memoryType) {
default:
case eMemTypeExpHeap:
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_VERBOSE("Free PluginData buffer %08X on heap %08X", buffer, this->heapHandle);
2020-05-03 12:30:15 +02:00
MEMFreeToExpHeap(this->heapHandle, buffer);
2020-12-26 14:17:50 +01:00
this->buffer = nullptr;
2020-05-03 12:30:15 +02:00
break;
case eMemTypeMEM2:
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_VERBOSE("Free PluginData buffer %08X on default heap", buffer);
2020-05-03 12:30:15 +02:00
free(this->buffer);
2020-12-26 14:17:50 +01:00
this->buffer = nullptr;
2020-05-03 12:30:15 +02:00
break;
}
}
2021-09-25 14:26:18 +02:00
PluginData::PluginData(const std::vector<uint8_t> &buffer) : PluginData(buffer, nullptr, eMemTypeMEM2) {
}
2022-02-04 16:25:44 +01:00
PluginData::PluginData(const std::vector<uint8_t> &input, MEMHeapHandle heapHandle, eMemoryTypes memoryType) : heapHandle(heapHandle),
memoryType(memoryType),
length(input.size()) {
2020-12-26 14:17:50 +01:00
void *data_copy = nullptr;
2020-05-03 12:30:15 +02:00
switch (memoryType) {
default:
case eMemTypeExpHeap:
data_copy = MEMAllocFromExpHeapEx(heapHandle, length, 4);
2021-02-19 19:41:04 +01:00
DEBUG_FUNCTION_LINE_VERBOSE("Allocated %d kb on ExpHeap", length / 1024);
2020-12-26 14:17:50 +01:00
if (data_copy == nullptr) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("Failed to allocate space on exp heap");
2020-05-03 12:30:15 +02:00
} else {
memcpy(data_copy, &input[0], length);
}
this->buffer = data_copy;
break;
case eMemTypeMEM2:
data_copy = memalign(4, length);
2021-02-19 19:41:04 +01:00
DEBUG_FUNCTION_LINE_VERBOSE("Allocated %d kb on default heap", length / 1024);
2020-12-26 14:17:50 +01:00
if (data_copy == nullptr) {
2022-04-22 22:55:53 +02:00
DEBUG_FUNCTION_LINE_ERR("Failed to allocate space on default heap");
2020-05-03 12:30:15 +02:00
} else {
memcpy(data_copy, &input[0], length);
}
this->buffer = data_copy;
break;
}
2020-12-26 14:17:50 +01:00
}