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 #ifndef MINDSPORE_LITE_SRC_TRAIN_STATIC_ALLOCATOR_H_ 17 #define MINDSPORE_LITE_SRC_TRAIN_STATIC_ALLOCATOR_H_ 18 19 namespace mindspore { 20 class StaticAllocator : public Allocator { 21 public: SetContex(void * buf,size_t size)22 void SetContex(void *buf, size_t size) { 23 start_buf_ = buf; 24 size_ = size; 25 } SetRefCount(void * ptr,int ref_count)26 int SetRefCount(void *ptr, int ref_count) override { return 0; } DecRefCount(void * ptr,int ref_count)27 int DecRefCount(void *ptr, int ref_count) override { return 0; } IncRefCount(void * ptr,int ref_count)28 int IncRefCount(void *ptr, int ref_count) override { return 0; } total_size()29 size_t total_size() { return total_size_; } Clear()30 void Clear() {} Malloc(size_t size)31 void *Malloc(size_t size) override { 32 total_size_ += size; 33 return malloc(size); 34 } Free(void * ptr)35 void Free(void *ptr) override { 36 if (RefCount(ptr) != 0) free(ptr); 37 } 38 RefCount(void * ptr)39 int RefCount(void *ptr) override { 40 if (ptr == nullptr) return STATIC_ALLOCATION; 41 char *ptrc = reinterpret_cast<char *>(ptr); 42 char *bufc = reinterpret_cast<char *>(start_buf_); 43 return ((ptrc < bufc) || (ptrc >= bufc + size_)) ? 1 : 0; 44 } 45 46 private: 47 void *start_buf_; 48 size_t size_; 49 size_t total_size_ = 0; 50 }; 51 }; // namespace mindspore 52 #endif // MINDSPORE_LITE_SRC_TRAIN_STATIC_ALLOCATOR_H_ 53