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 ASSERT(allocator_ != nullptr); 26 auto area = reinterpret_cast<uintptr_t>(allocator_->AllocateBuffer(size)); 27 if (!area) { // LOCV_EXCL_BR_LINE 28 LOG_ECMA_MEM(FATAL) << "OOM WorkSpaceChunk : NewArea area is nullptr"; 29 UNREACHABLE(); 30 } 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 auto iter = areaList_.find(reinterpret_cast<uintptr_t>(mem)); 39 if (iter != areaList_.end()) { 40 areaList_.erase(iter); 41 } 42 allocator_->FreeBuffer(mem); 43 } 44 ReleaseMemory()45void WorkSpaceChunk::ReleaseMemory() 46 { 47 LockHolder lock(mtx_); 48 for (auto iter = areaList_.begin(); iter != areaList_.end(); ++iter) { 49 allocator_->FreeBuffer(reinterpret_cast<void *>(iter->second)); 50 } 51 areaList_.clear(); 52 } 53 } // namespace panda::ecmascript 54