WiiUPluginLoaderBackend/source/plugin/PluginData.cpp

64 lines
1.9 KiB
C++
Raw Permalink Normal View History

#include "PluginData.h"
2020-12-26 14:17:50 +01:00
#include <utility>
#include <malloc.h>
#include "../utils/logger.h"
2020-05-17 20:43:04 +02:00
PluginData::PluginData(const PluginData &obj) {
this->buffer = obj.buffer;
this->heapHandle = obj.heapHandle;
this->memoryType = obj.memoryType;
this->length = obj.length;
}
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:
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:
free(this->buffer);
2020-12-26 14:17:50 +01:00
this->buffer = nullptr;
2020-05-03 12:30:15 +02:00
break;
}
}
2020-12-26 14:17:50 +01:00
PluginData::PluginData(const std::vector<uint8_t>& buffer) : PluginData(buffer, nullptr, eMemTypeMEM2) {
}
2020-12-26 14:17:50 +01:00
PluginData::PluginData(const std::vector<uint8_t>& input, MEMHeapHandle heapHandle, eMemoryTypes memoryType) :
2020-05-03 12:30:15 +02:00
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);
DEBUG_FUNCTION_LINE("Allocated %d kb on ExpHeap", length / 1024);
2020-12-26 14:17:50 +01:00
if (data_copy == nullptr) {
2020-05-03 12:30:15 +02:00
DEBUG_FUNCTION_LINE("Failed to allocate space on exp heap");
} else {
memcpy(data_copy, &input[0], length);
}
this->buffer = data_copy;
DEBUG_FUNCTION_LINE("copied data to exp heap");
break;
case eMemTypeMEM2:
data_copy = memalign(4, length);
DEBUG_FUNCTION_LINE("Allocated %d kb on default heap", length / 1024);
2020-12-26 14:17:50 +01:00
if (data_copy == nullptr) {
2020-05-03 12:30:15 +02:00
DEBUG_FUNCTION_LINE("Failed to allocate space on default heap");
} else {
memcpy(data_copy, &input[0], length);
}
this->buffer = data_copy;
break;
}
2020-12-26 14:17:50 +01:00
}