diff --git a/include/wrenpp/def_configuration.hpp b/include/wrenpp/def_configuration.hpp index 13dd488..0a26623 100644 --- a/include/wrenpp/def_configuration.hpp +++ b/include/wrenpp/def_configuration.hpp @@ -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 diff --git a/src/def_configuration.cpp b/src/def_configuration.cpp index 1f060d4..4ebb2db 100644 --- a/src/def_configuration.cpp +++ b/src/def_configuration.cpp @@ -17,6 +17,7 @@ #include "wrenpp/def_configuration.hpp" #include +#include 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(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(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