1 /** 2 * Copyright 2023 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_PI_JIT_MEMPOOL_H 18 #define MINDSPORE_PI_JIT_MEMPOOL_H 19 20 #include <string> 21 #include <utility> 22 #include <vector> 23 24 namespace mindspore { 25 namespace pijit { 26 template <typename T> 27 class MemPool { 28 public: MemPool(const char * file,int line,const char * type_name)29 MemPool(const char *file, int line, const char *type_name) 30 : decs_(std::string(file) + ":" + std::to_string(line) + " mempool of " + type_name) {} ~MemPool()31 ~MemPool() { Clear(__FILE__, __LINE__); } 32 33 template <typename U, typename... Args> New(Args &&...args)34 U *New(Args &&... args) { 35 U *r = new U(std::forward<Args>(args)...); 36 pool_.emplace_back(r); 37 return r; 38 } 39 Clear(const char * f,int l)40 void Clear(const char *f, int l) { 41 for (auto &i : pool_) { 42 delete i; 43 } 44 pool_.clear(); 45 } 46 47 private: 48 std::vector<T *> pool_; 49 std::string decs_; 50 }; 51 } // namespace pijit 52 } // namespace mindspore 53 54 #endif // MINDSPORE_PI_JIT_MEMPOOL_H 55