提交 97c1011e 编写于 作者: X xulongteng

fix malloc bug

Change-Id: Ibb5d9d460a84a86e64eda00a69f9882008b00da2
上级 f96c5e89
...@@ -19,6 +19,17 @@ namespace baidu { ...@@ -19,6 +19,17 @@ namespace baidu {
namespace paddle_serving { namespace paddle_serving {
namespace predictor { namespace predictor {
struct MempoolRegion {
MempoolRegion(im::fugue::memory::Region *region,
im::Mempool *mempool) :
_region(region), _mempool(mempool){}
im::fugue::memory::Region *region() {return _region;}
im::Mempool *mempool() {return _mempool;}
im::fugue::memory::Region* _region;
im::Mempool* _mempool;
};
int MempoolWrapper::initialize() { int MempoolWrapper::initialize() {
if (THREAD_KEY_CREATE(&_bspec_key, NULL) != 0) { if (THREAD_KEY_CREATE(&_bspec_key, NULL) != 0) {
LOG(ERROR) << "unable to create thread_key of thrd_data"; LOG(ERROR) << "unable to create thread_key of thrd_data";
...@@ -33,16 +44,19 @@ int MempoolWrapper::initialize() { ...@@ -33,16 +44,19 @@ int MempoolWrapper::initialize() {
} }
int MempoolWrapper::thread_initialize() { int MempoolWrapper::thread_initialize() {
_region.init(); im::fugue::memory::Region *region = new im::fugue::memory::Region();
im::Mempool* p_mempool = new (std::nothrow) im::Mempool(&_region); region->init();
if (p_mempool == NULL) { im::Mempool* mempool = new (std::nothrow) im::Mempool(region);
MempoolRegion *mempool_region = new MempoolRegion(region, mempool);
if (mempool == NULL) {
LOG(ERROR) << "Failed create thread mempool"; LOG(ERROR) << "Failed create thread mempool";
return -1; return -1;
} }
if (THREAD_SETSPECIFIC(_bspec_key, p_mempool) != 0) { if (THREAD_SETSPECIFIC(_bspec_key, mempool_region) != 0) {
LOG(ERROR) << "unable to set the thrd_data"; LOG(ERROR) << "unable to set the thrd_data";
delete p_mempool; delete region;
delete mempool;
return -1; return -1;
} }
...@@ -51,23 +65,34 @@ int MempoolWrapper::thread_initialize() { ...@@ -51,23 +65,34 @@ int MempoolWrapper::thread_initialize() {
} }
int MempoolWrapper::thread_clear() { int MempoolWrapper::thread_clear() {
im::Mempool* p_mempool = (im::Mempool*)THREAD_GETSPECIFIC(_bspec_key); MempoolRegion* mempool_region = (MempoolRegion*)THREAD_GETSPECIFIC(_bspec_key);
if (p_mempool) { if (mempool_region == NULL) {
p_mempool->release_block(); LOG(WARNING) << "THREAD_GETSPECIFIC() returned NULL";
_region.reset(); return -1;
}
im::Mempool* mempool = mempool_region->mempool();
im::fugue::memory::Region* region = mempool_region->region();
if (mempool) {
mempool->release_block();
region->reset();
} }
return 0; return 0;
} }
void* MempoolWrapper::malloc(size_t size) { void* MempoolWrapper::malloc(size_t size) {
im::Mempool* p_mempool = (im::Mempool*)THREAD_GETSPECIFIC(_bspec_key); MempoolRegion* mempool_region = (MempoolRegion*)THREAD_GETSPECIFIC(_bspec_key);
if (!p_mempool) { if (mempool_region == NULL) {
LOG(WARNING) << "THREAD_GETSPECIFIC() returned NULL";
return NULL;
}
im::Mempool* mempool = mempool_region->mempool();
if (!mempool) {
LOG(WARNING) << "Cannot malloc memory:" << size LOG(WARNING) << "Cannot malloc memory:" << size
<< ", since mempool is not thread initialized"; << ", since mempool is not thread initialized";
return NULL; return NULL;
} }
return p_mempool->malloc(size); return mempool->malloc(size);
} }
} // namespace predictor } // namespace predictor
......
...@@ -39,7 +39,7 @@ class MempoolWrapper { ...@@ -39,7 +39,7 @@ class MempoolWrapper {
void* malloc(size_t size); void* malloc(size_t size);
private: private:
im::fugue::memory::Region _region; //im::fugue::memory::Region _region;
THREAD_KEY_T _bspec_key; THREAD_KEY_T _bspec_key;
}; };
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册