• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 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 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 
17 #ifndef MINDSPORE_INCLUDE_API_ALLOCATOR_H
18 #define MINDSPORE_INCLUDE_API_ALLOCATOR_H
19 
20 #include <memory>
21 #include "include/api/types.h"
22 
23 namespace mindspore {
24 /// \brief Allocator defined a memory pool for malloc memory and free memory dynamically.
25 class MS_API Allocator {
26  public:
27   /// \brief Destructor of MindSpore Allocator.
28   virtual ~Allocator() = default;
29 
30   /// \brief Method to request memory.
31   ///
32   /// \param[in] size Define the memory size to request.
33   virtual void *Malloc(size_t size) = 0;
34 
35   /// \brief Method to request memory.
36   ///
37   /// \param[in] weight Defines the width of memory to request
38   /// \param[in] height Defines the height of memory to request
39   /// \param[in] type Defines the data type of memory to request
Malloc(size_t weight,size_t height,DataType type)40   virtual void *Malloc(size_t weight, size_t height, DataType type) {
41     return nullptr;
42   }
43 
44   /// \brief Method to free memory.
45   ///
46   /// \param[in] ptr Define the pointer of a certain memory.
47   virtual void Free(void *ptr) = 0;
48 
49   /// \brief Reference count of a certain memory.
50   ///
51   /// \param[in] ptr Define the pointer of a certain memory.
52   ///
53   /// \return Reference count of a certain memory currently.
54   virtual int RefCount(void *ptr) = 0;
55 
56   /// \brief Set reference count of a certain memory.
57   ///
58   /// \param[in] ptr Define the pointer of a certain memory.
59   /// \param[in] ref_count Define the reference count to set.
60   ///
61   /// \return Reference count of a certain memory after setting.
62   virtual int SetRefCount(void *ptr, int ref_count) = 0;
63 
64   /// \brief Decrease the reference count of a certain memory.
65   ///
66   /// \param[in] ptr Define the pointer of a certain memory.
67   /// \param[in] ref_count Define the reference count to reduce.
68   ///
69   /// \return Reference count of a certain memory after decreating.
70   virtual int DecRefCount(void *ptr, int ref_count) = 0;
71 
72   /// \brief Increase the reference count of a certain memory.
73   ///
74   /// \param[in] ptr Define the pointer of a certain memory.
75   /// \param[in] ref_count Define the reference count to increase.
76   ///
77   /// \return Reference count of a certain memory after increasing.
78   virtual int IncRefCount(void *ptr, int ref_count) = 0;
79 
80   /// \brief Static method to create an allocator.
81   ///
82   /// \return Smart pointer of an allocator.
83   static std::shared_ptr<Allocator> Create();
84 
85   /// \brief Prepare a certain memory.
86   ///
87   /// \param[in] ptr Define the pointer of a certain memory to prepare.
88   ///
89   /// \return Pointer of ready memory.
Prepare(void * ptr)90   virtual void *Prepare(void *ptr) { return ptr; }
91 
92  protected:
93   // memory aligned bytes
94   size_t aligned_size_ = 32;
95 };
96 }  // namespace mindspore
97 #endif  // MINDSPORE_INCLUDE_API_ALLOCATOR_H
98