From 43a362c66d10edf9e08bad12ca2316e5fba28223 Mon Sep 17 00:00:00 2001 From: hong19860320 <9973393+hong19860320@users.noreply.github.com> Date: Fri, 28 Jun 2019 16:58:52 +0000 Subject: [PATCH] fix bus error which occurs in some armv7 devices test=develop --- paddle/fluid/lite/host/target_wrapper.cc | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/paddle/fluid/lite/host/target_wrapper.cc b/paddle/fluid/lite/host/target_wrapper.cc index 2e8393ef4..af4269390 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) { -- GitLab