memory.cpp 2.1 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

W
wangguibao 已提交
15 16
#include "predictor/framework/memory.h"
#include "predictor/common/inner_common.h"
W
wangguibao 已提交
17 18 19 20 21 22

namespace baidu {
namespace paddle_serving {
namespace predictor {

int MempoolWrapper::initialize() {
W
wangguibao 已提交
23 24 25 26 27 28 29 30 31 32
  if (THREAD_KEY_CREATE(&_bspec_key, NULL) != 0) {
    LOG(ERROR) << "unable to create thread_key of thrd_data";
    return -1;
  }
  if (THREAD_SETSPECIFIC(_bspec_key, NULL) != 0) {
    LOG(ERROR) << "failed initialize bsepecific key to null";
    return -1;
  }

  return 0;
W
wangguibao 已提交
33 34 35
}

int MempoolWrapper::thread_initialize() {
W
wangguibao 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  _region.init();
  im::Mempool* p_mempool = new (std::nothrow) im::Mempool(&_region);
  if (p_mempool == NULL) {
    LOG(ERROR) << "Failed create thread mempool";
    return -1;
  }

  if (THREAD_SETSPECIFIC(_bspec_key, p_mempool) != 0) {
    LOG(ERROR) << "unable to set the thrd_data";
    delete p_mempool;
    return -1;
  }

  LOG(WARNING) << "Succ thread initialize mempool wrapper";
  return 0;
W
wangguibao 已提交
51 52 53
}

int MempoolWrapper::thread_clear() {
W
wangguibao 已提交
54 55 56 57 58 59 60
  im::Mempool* p_mempool = (im::Mempool*)THREAD_GETSPECIFIC(_bspec_key);
  if (p_mempool) {
    p_mempool->release_block();
    _region.reset();
  }

  return 0;
W
wangguibao 已提交
61 62 63
}

void* MempoolWrapper::malloc(size_t size) {
W
wangguibao 已提交
64 65 66 67 68 69 70
  im::Mempool* p_mempool = (im::Mempool*)THREAD_GETSPECIFIC(_bspec_key);
  if (!p_mempool) {
    LOG(WARNING) << "Cannot malloc memory:" << size
                 << ", since mempool is not thread initialized";
    return NULL;
  }
  return p_mempool->malloc(size);
W
wangguibao 已提交
71 72
}

W
wangguibao 已提交
73 74 75
}  // namespace predictor
}  // namespace paddle_serving
}  // namespace baidu