Add a reallocate_fn() to DefConfiguration implemented around new/delete

This commit is contained in:
King_DuckZ 2020-05-02 23:28:48 +02:00
parent 65189a5575
commit cad9f96739
2 changed files with 24 additions and 0 deletions

View file

@ -29,5 +29,6 @@ namespace wren {
public:
static void write_fn (VM*, const char* text);
static void error_fn (VM*, ErrorType, const char* module, int line, const char* msg);
static void* reallocate_fn(void* ptr, std::size_t size);
};
} //namespace wren

View file

@ -17,6 +17,7 @@
#include "wrenpp/def_configuration.hpp"
#include <iostream>
#include <algorithm>
namespace wren {
void DefConfiguration::write_fn (VM*, const char* text) {
@ -26,4 +27,26 @@ namespace wren {
void DefConfiguration::error_fn (VM*, ErrorType type, const char* module, int line, const char* message) {
std::cerr << "Wren error: " << message << " in " << module << ':' << line << '\n';
}
void* DefConfiguration::reallocate_fn (void* ptr, std::size_t size) {
if (ptr and size) {
//reallocate
char* const old_mem = static_cast<char*>(ptr);
char* const new_mem = new char[size];
std::copy(old_mem, old_mem + size, new_mem);
delete[] old_mem;
return new_mem;
}
else if (ptr and not size) {
//free
char* const old_mem = static_cast<char*>(ptr);
delete[] old_mem;
}
else if (not ptr and size) {
//allocate
char* const new_mem = new char[size];
return new_mem;
}
return nullptr;
}
} //namespace wren