Rewrite the plugin backend, use smart pointers is possible, don't persist in structs and simplify code

This commit is contained in:
Maschell
2022-05-14 14:00:20 +02:00
parent fb6373ec63
commit cda9c3e055
41 changed files with 790 additions and 1888 deletions

View File

@@ -1,63 +1,15 @@
#include "PluginData.h"
#include "utils/logger.h"
#include "utils/utils.h"
#include "../utils/logger.h"
#include <malloc.h>
PluginData::PluginData(const PluginData &obj) {
this->buffer = obj.buffer;
this->heapHandle = obj.heapHandle;
this->memoryType = obj.memoryType;
this->length = obj.length;
}
void PluginData::freeMemory() {
if (buffer == nullptr) {
return;
}
switch (memoryType) {
default:
case eMemTypeExpHeap:
DEBUG_FUNCTION_LINE_VERBOSE("Free PluginData buffer %08X on heap %08X", buffer, this->heapHandle);
MEMFreeToExpHeap(this->heapHandle, buffer);
this->buffer = nullptr;
break;
case eMemTypeMEM2:
DEBUG_FUNCTION_LINE_VERBOSE("Free PluginData buffer %08X on default heap", buffer);
free(this->buffer);
this->buffer = nullptr;
break;
PluginData::PluginData(const std::vector<uint8_t> &input) : length(input.size()) {
auto data_copy = make_unique_nothrow<uint8_t[]>(length);
if (!data_copy) {
DEBUG_FUNCTION_LINE_ERR("Failed to allocate space on default heap");
this->length = 0;
} else {
DEBUG_FUNCTION_LINE_VERBOSE("Allocated %d kb on default heap", length / 1024);
memcpy(data_copy.get(), &input[0], length);
this->buffer = std::move(data_copy);
}
}
PluginData::PluginData(const std::vector<uint8_t> &buffer) : PluginData(buffer, nullptr, eMemTypeMEM2) {
}
PluginData::PluginData(const std::vector<uint8_t> &input, MEMHeapHandle heapHandle, eMemoryTypes memoryType) : heapHandle(heapHandle),
memoryType(memoryType),
length(input.size()) {
void *data_copy = nullptr;
switch (memoryType) {
default:
case eMemTypeExpHeap:
data_copy = MEMAllocFromExpHeapEx(heapHandle, length, 4);
DEBUG_FUNCTION_LINE_VERBOSE("Allocated %d kb on ExpHeap", length / 1024);
if (data_copy == nullptr) {
DEBUG_FUNCTION_LINE_ERR("Failed to allocate space on exp heap");
} else {
memcpy(data_copy, &input[0], length);
}
this->buffer = data_copy;
break;
case eMemTypeMEM2:
data_copy = memalign(4, length);
DEBUG_FUNCTION_LINE_VERBOSE("Allocated %d kb on default heap", length / 1024);
if (data_copy == nullptr) {
DEBUG_FUNCTION_LINE_ERR("Failed to allocate space on default heap");
} else {
memcpy(data_copy, &input[0], length);
}
this->buffer = data_copy;
break;
}
}