• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2019 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "minddata/dataset/util/memory_pool.h"
17 #include "securec.h"
18 
19 namespace mindspore {
20 namespace dataset {
DeMalloc(std::size_t s,void ** p,bool init_to_zero=false)21 Status DeMalloc(std::size_t s, void **p, bool init_to_zero = false) {
22   if (p == nullptr) {
23     RETURN_STATUS_UNEXPECTED("p is null");
24   }
25   void *q = ::malloc(s);
26   if (q == nullptr) {
27     RETURN_STATUS_OOM("Out of memory.");
28   } else {
29     *p = q;
30     if (init_to_zero) {
31       CHECK_FAIL_RETURN_UNEXPECTED(memset_s(q, s, 0, s) == EOK, "Failed to init memory to zero.");
32     }
33     return Status::OK();
34   }
35 }
36 }  // namespace dataset
37 }  // namespace mindspore
38 
operator new(std::size_t s,mindspore::Status * rc,std::shared_ptr<mindspore::dataset::MemoryPool> b)39 void *operator new(std::size_t s, mindspore::Status *rc, std::shared_ptr<mindspore::dataset::MemoryPool> b) {
40   void *ptr = nullptr;
41   *rc = b->Allocate(s, &ptr);
42   return ptr;
43 }
44 
operator new[](std::size_t s,mindspore::Status * rc,std::shared_ptr<mindspore::dataset::MemoryPool> b)45 void *operator new[](std::size_t s, mindspore::Status *rc, std::shared_ptr<mindspore::dataset::MemoryPool> b) {
46   void *ptr = nullptr;
47   *rc = b->Allocate(s, &ptr);
48   return ptr;
49 }
50 
operator delete(void * p,std::shared_ptr<mindspore::dataset::MemoryPool> b)51 void operator delete(void *p, std::shared_ptr<mindspore::dataset::MemoryPool> b) {
52   if (p != nullptr) {
53     b->Deallocate(p);
54   }
55 }
56 
operator delete[](void * p,std::shared_ptr<mindspore::dataset::MemoryPool> b)57 void operator delete[](void *p, std::shared_ptr<mindspore::dataset::MemoryPool> b) {
58   if (p != nullptr) {
59     b->Deallocate(p);
60   }
61 }
62