• 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 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_SYSTEM_POOL_H_
17 #define MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_SYSTEM_POOL_H_
18 
19 #include <cstddef>
20 #include <cstdlib>
21 #include <limits>
22 #include <memory>
23 #include <new>
24 #include "./securec.h"
25 #include "minddata/dataset/util/allocator.h"
26 #include "minddata/dataset/util/memory_pool.h"
27 
28 namespace mindspore {
29 namespace dataset {
30 // This class demonstrate how to implement a simple MemoryPool
31 // for minddata/dataset using malloc/free/realloc.  We need to
32 // implement 4 virtual functions. Other MemoryPool
33 // implementation, e.g., are BuddyArena and CircularPool.  All
34 // these MemoryPool can be used together with Allocator.h for
35 // C++ STL containers.
36 class SystemPool : public MemoryPool {
37  public:
~SystemPool()38   ~SystemPool() override {}
39 
Allocate(size_t n,void ** pp)40   Status Allocate(size_t n, void **pp) override { return DeMalloc(n, pp, false); }
41 
Deallocate(void * p)42   void Deallocate(void *p) override {
43     if (p != nullptr) {
44       free(p);
45     }
46   }
47 
Reallocate(void ** p,size_t old_sz,size_t new_sz)48   Status Reallocate(void **p, size_t old_sz, size_t new_sz) override {
49     RETURN_UNEXPECTED_IF_NULL(p);
50     if (old_sz >= new_sz) {
51       // Do nothing if we shrink.
52       return Status::OK();
53     } else {
54       void *ptr = *p;
55       void *q = nullptr;
56       RETURN_IF_NOT_OK(DeMalloc(new_sz, &q, false));
57       errno_t err = memcpy_s(q, new_sz, ptr, old_sz);
58       if (err) {
59         free(q);
60         RETURN_STATUS_UNEXPECTED(std::to_string(err));
61       }
62       free(ptr);
63       *p = q;
64       return Status::OK();
65     }
66   }
67 
get_max_size()68   uint64_t get_max_size() const override { return std::numeric_limits<uint64_t>::max(); }
69 
PercentFree()70   int PercentFree() const override { return 100; }
71 
72   template <typename T>
GetAllocator()73   static Allocator<T> GetAllocator() {
74     return Allocator<T>(std::make_shared<SystemPool>());
75   }
76 };
77 }  // namespace dataset
78 }  // namespace mindspore
79 
80 #endif  // MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_SYSTEM_POOL_H_
81