1 /* 2 * Copyright (c) 2024 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 #include "ecmascript/mem/work_space_chunk.h" 17 18 #include "ecmascript/mem/native_area_allocator.h" 19 20 namespace panda::ecmascript { WorkSpaceChunk(NativeAreaAllocator * allocator)21WorkSpaceChunk::WorkSpaceChunk(NativeAreaAllocator *allocator) : allocator_(allocator) {} 22 NewArea(size_t size)23uintptr_t WorkSpaceChunk::NewArea(size_t size) 24 { 25 auto area = reinterpret_cast<uintptr_t>(allocator_->AllocateBuffer(size)); 26 if (!area) { 27 LOG_ECMA_MEM(FATAL) << "OOM WorkSpaceChunk : NewArea area is nullptr"; 28 UNREACHABLE(); 29 } 30 allocator_->IncreaseNativeSizeStats(size, NativeFlag::CHUNK_MEM); 31 areaList_.emplace(area, area); 32 return area; 33 } 34 Free(void * mem)35void WorkSpaceChunk::Free([[maybe_unused]] void *mem) 36 { 37 LockHolder lock(mtx_); 38 if (cachedAreaList_.size() < MAX_WORK_SPACE_CHUNK_SIZE / WORKNODE_SPACE_SIZE) { 39 cachedAreaList_.emplace_back(reinterpret_cast<uintptr_t>(mem)); 40 } else { 41 auto iter = areaList_.find(reinterpret_cast<uintptr_t>(mem)); 42 if (iter != areaList_.end()) { 43 areaList_.erase(iter); 44 } 45 allocator_->FreeBuffer(mem); 46 } 47 } 48 ReleaseMemory()49void WorkSpaceChunk::ReleaseMemory() 50 { 51 LockHolder lock(mtx_); 52 cachedAreaList_.clear(); 53 for (auto iter = areaList_.begin(); iter != areaList_.end(); ++iter) { 54 allocator_->FreeBuffer(reinterpret_cast<void *>(iter->second)); 55 } 56 areaList_.clear(); 57 } 58 } // namespace panda::ecmascript 59