• 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 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 
17 #ifndef MINDSPORE_LITE_SRC_RUNTIME_RUNTIME_ALLOCATOR_H_
18 #define MINDSPORE_LITE_SRC_RUNTIME_RUNTIME_ALLOCATOR_H_
19 
20 #include <memory>
21 #include <map>
22 #include <unordered_map>
23 #include "include/api/allocator.h"
24 #include "include/errorcode.h"
25 #include "src/tensor.h"
26 
27 namespace mindspore {
28 class RuntimeAllocator : public Allocator {
29  public:
30   explicit RuntimeAllocator(size_t aligned_size = 32);
31   ~RuntimeAllocator() override;
32 
33  public:
Malloc(size_t size)34   void *Malloc(size_t size) override { return nullptr; }
Free(void * ptr)35   void Free(void *ptr) override { return; }
RefCount(void * ptr)36   int RefCount(void *ptr) override { return (ptr == nullptr) ? RUNTIME_REFCOUNT : 0; }
SetRefCount(void * ptr,int ref_count)37   int SetRefCount(void *ptr, int ref_count) override { return 0; }
IncRefCount(void * ptr,int ref_count)38   int IncRefCount(void *ptr, int ref_count) override { return 0; }
DecRefCount(void * ptr,int ref_count)39   int DecRefCount(void *ptr, int ref_count) override { return 0; }
40 
41  public:
42   void SetDataOffset(lite::Tensor *tensor, size_t offset);
43   void MallocTensorData(lite::Tensor *tensor);
44   void FreeTensorData(lite::Tensor *tensor);
45   void *MallocOptData();
GetOffsetMap()46   const std::unordered_map<lite::Tensor *, size_t> &GetOffsetMap() const { return offset_map_; }
47   void Clear(AllocatorPtr default_allocator);
48 
49  private:
50   size_t FindMinFree(size_t size);
51 
52  private:
53   void *data_ = nullptr;
54   size_t total_size_ = 0;
55   std::unordered_map<lite::Tensor *, size_t> offset_map_;
56   std::map<size_t, size_t> free_list_; /* offset, size */
57   std::map<size_t, size_t> used_list_; /* offset, size */
58 };
59 
60 using RuntimeAllocatorPtr = std::shared_ptr<RuntimeAllocator>;
61 }  // namespace mindspore
62 
63 #endif  // MINDSPORE_LITE_SRC_RUNTIME_RUNTIME_ALLOCATOR_H_
64