Skip to content

Commit 42f2580

Browse files
author
Chunwei
committed
Merge branch 'hongming/fix_bus_error' into 'incubate/lite'
fix bus error which occurs in some armv7 devices See merge request inference/paddlelite!89
2 parents dbcc745 + 43a362c commit 42f2580

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

paddle/fluid/lite/host/target_wrapper.cc

+16-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,29 @@
1414

1515
#include "paddle/fluid/lite/core/target_wrapper.h"
1616
#include <cstring>
17+
#include <memory>
1718

1819
namespace paddle {
1920
namespace lite {
2021

22+
const int MALLOC_ALIGN = 64;
23+
2124
void* TargetWrapper<TARGET(kHost)>::Malloc(size_t size) {
22-
return new char[size];
25+
size_t offset = sizeof(void*) + MALLOC_ALIGN - 1;
26+
char* p = static_cast<char*>(malloc(offset + size));
27+
if (!p) {
28+
return nullptr;
29+
}
30+
void* r = reinterpret_cast<void*>(reinterpret_cast<size_t>(p + offset) &
31+
(~(MALLOC_ALIGN - 1)));
32+
static_cast<void**>(r)[-1] = p;
33+
memset(r, 0, size);
34+
return r;
2335
}
2436
void TargetWrapper<TARGET(kHost)>::Free(void* ptr) {
25-
delete[] static_cast<char*>(ptr);
37+
if (ptr) {
38+
free(static_cast<void**>(ptr)[-1]);
39+
}
2640
}
2741
void TargetWrapper<TARGET(kHost)>::MemcpySync(void* dst, const void* src,
2842
size_t size, IoDirection dir) {

0 commit comments

Comments
 (0)