1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PANDA_LIBPANDABASE_MEM_MEM_POOL_H_ 17 #define PANDA_LIBPANDABASE_MEM_MEM_POOL_H_ 18 19 #include <cstddef> 20 #include "macros.h" 21 #include "mem.h" 22 #include "pool_map.h" 23 24 namespace panda { 25 class Arena; 26 27 class Pool { 28 public: Pool(size_t size,void * mem)29 explicit constexpr Pool(size_t size, void *mem) : size_(size), mem_(mem) {} Pool(std::pair<size_t,void * > pool)30 explicit Pool(std::pair<size_t, void *> pool) : size_(pool.first), mem_(pool.second) {} 31 GetSize()32 size_t GetSize() const 33 { 34 return size_; 35 } 36 GetMem()37 void *GetMem() const 38 { 39 return mem_; 40 } 41 42 bool operator==(const Pool &other) const 43 { 44 return (this->size_ == other.size_) && (this->mem_ == other.mem_); 45 } 46 47 ~Pool() = default; 48 49 DEFAULT_COPY_SEMANTIC(Pool); 50 DEFAULT_MOVE_SEMANTIC(Pool); 51 52 private: 53 size_t size_; 54 void *mem_; 55 }; 56 57 static constexpr Pool NULLPOOL {0, nullptr}; 58 59 template <class MemPoolImplT> 60 class MemPool { 61 public: 62 virtual ~MemPool() = default; MemPool(std::string pool_name)63 explicit MemPool(std::string pool_name) : name_(std::move(pool_name)) {} 64 DEFAULT_NOEXCEPT_MOVE_SEMANTIC(MemPool); 65 DEFAULT_COPY_SEMANTIC(MemPool); 66 67 /** 68 * Allocates arena with size bytes 69 * @tparam ArenaT - type of Arena 70 * @param size - size of buffer in arena in bytes 71 * @param space_type - type of the space for which Arena is allocated 72 * @param allocator_type - type of the allocator for which Arena is allocated 73 * @param allocator_addr - address of the allocator header 74 * @return pointer to allocated arena 75 */ 76 template <class ArenaT = Arena> 77 inline ArenaT *AllocArena(size_t size, SpaceType space_type, AllocatorType allocator_type, 78 void *allocator_addr = nullptr) 79 { 80 // CODECHECK-NOLINTNEXTLINE(C_RULE_ID_HORIZON_SPACE) 81 return static_cast<MemPoolImplT *>(this)->template AllocArenaImpl<ArenaT>(size, space_type, allocator_type, 82 allocator_addr); 83 } 84 85 /** 86 * Frees allocated arena 87 * @tparam ArenaT - arena type 88 * @param arena - pointer to the arena 89 */ 90 template <class ArenaT = Arena> FreeArena(ArenaT * arena)91 inline void FreeArena(ArenaT *arena) 92 { 93 // CODECHECK-NOLINTNEXTLINE(C_RULE_ID_HORIZON_SPACE) 94 static_cast<MemPoolImplT *>(this)->template FreeArenaImpl<ArenaT>(arena); 95 } 96 97 /** 98 * Allocates pool with minimal size in bytes. 99 * @param size - minimal size of a pool in bytes 100 * @param space_type - type of the space for which Arena is allocated 101 * @param allocator_type - type of the allocator for which Arena is allocated 102 * @param allocator_addr - address of the allocator header 103 * If it is not defined, it means that allocator header will be located at the first byte of the returned pool. 104 * @return pool info with the size and a pointer 105 */ 106 Pool AllocPool(size_t size, SpaceType space_type, AllocatorType allocator_type, void *allocator_addr = nullptr) 107 { 108 return static_cast<MemPoolImplT *>(this)->AllocPoolImpl(size, space_type, allocator_type, allocator_addr); 109 } 110 111 /** 112 * Frees allocated pool 113 * @param mem - pointer to an allocated pool 114 * @param size - size of the allocated pool in bytes 115 */ FreePool(void * mem,size_t size)116 void FreePool(void *mem, size_t size) 117 { 118 static_cast<MemPoolImplT *>(this)->FreePoolImpl(mem, size); 119 } 120 121 /** 122 * Gets info about the allocator in which this address is used 123 * @param addr 124 * @return Allocator info with a type and pointer to the allocator header 125 */ GetAllocatorInfoForAddr(void * addr)126 AllocatorInfo GetAllocatorInfoForAddr(void *addr) 127 { 128 return static_cast<MemPoolImplT *>(this)->GetAllocatorInfoForAddrImpl(addr); 129 } 130 131 /** 132 * Gets space type which this address used for 133 * @param addr 134 * @return space type 135 */ GetSpaceTypeForAddr(void * addr)136 SpaceType GetSpaceTypeForAddr(void *addr) 137 { 138 return static_cast<MemPoolImplT *>(this)->GetSpaceTypeForAddrImpl(addr); 139 } 140 141 /** 142 * Gets address of pool start for input address 143 * @param addr address in pool 144 * @return address of pool start 145 */ GetStartAddrPoolForAddr(void * addr)146 const void *GetStartAddrPoolForAddr(void *addr) const 147 { 148 return static_cast<MemPoolImplT *>(this)->GetStartAddrPoolForAddrImpl(addr); 149 } 150 151 private: 152 std::string name_; 153 }; 154 155 } // namespace panda 156 157 #endif // PANDA_LIBPANDABASE_MEM_MEM_POOL_H_ 158