diff --git a/paddle/fluid/lite/host/target_wrapper.cc b/paddle/fluid/lite/host/target_wrapper.cc index 2e8393ef46689ef3b77fbb7945885809d7b70cea..af4269390cbe198b5f46e551ec611040b0fa332e 100644 --- a/paddle/fluid/lite/host/target_wrapper.cc +++ b/paddle/fluid/lite/host/target_wrapper.cc @@ -14,15 +14,29 @@ #include "paddle/fluid/lite/core/target_wrapper.h" #include +#include namespace paddle { namespace lite { +const int MALLOC_ALIGN = 64; + void* TargetWrapper::Malloc(size_t size) { - return new char[size]; + size_t offset = sizeof(void*) + MALLOC_ALIGN - 1; + char* p = static_cast(malloc(offset + size)); + if (!p) { + return nullptr; + } + void* r = reinterpret_cast(reinterpret_cast(p + offset) & + (~(MALLOC_ALIGN - 1))); + static_cast(r)[-1] = p; + memset(r, 0, size); + return r; } void TargetWrapper::Free(void* ptr) { - delete[] static_cast(ptr); + if (ptr) { + free(static_cast(ptr)[-1]); + } } void TargetWrapper::MemcpySync(void* dst, const void* src, size_t size, IoDirection dir) {