1 /* 2 * Copyright (c) 2023 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 ECMASCRIPT_MEM_ALLOCATION_INSPECTOR_H 17 #define ECMASCRIPT_MEM_ALLOCATION_INSPECTOR_H 18 19 #include "ecmascript/common.h" 20 #include "libpandabase/macros.h" 21 22 namespace panda::ecmascript { 23 class Heap; 24 class HeapSampling; 25 26 class AllocationInspector { 27 public: AllocationInspector(Heap * heap,uint64_t rate,HeapSampling * profiler)28 explicit AllocationInspector(Heap *heap, uint64_t rate, HeapSampling *profiler) 29 : rate_(rate), 30 profiler_(profiler), 31 heap_(heap) {} 32 ~AllocationInspector() = default; 33 NO_MOVE_SEMANTIC(AllocationInspector); 34 NO_COPY_SEMANTIC(AllocationInspector); 35 36 // When total at least *rate_* bytes have been allocated, this func can be called. 37 // *object* is an address pointing to allocated and uninitialized memory. 38 // *size* is the dimension of allocation corresponding to `object`. 39 void Step([[maybe_unused]] Address object, [[maybe_unused]] size_t size); 40 size_t GetNextStepSize(); 41 42 private: 43 const uint64_t rate_; 44 [[maybe_unused]] HeapSampling *const profiler_; 45 [[maybe_unused]] Heap *const heap_; 46 }; 47 48 // each space has an allocationCounter which inspector can be added to. 49 class AllocationCounter final { 50 public: 51 AllocationCounter() = default; 52 ~AllocationCounter() = default; 53 54 void AddAllocationInspector(AllocationInspector *inspector); 55 void ClearAllocationInspector(); 56 void AdvanceAllocationInspector(size_t allocated); 57 void InvokeAllocationInspector(Address object, size_t objectSize, size_t alignedObjectSize); 58 IsActive()59 bool IsActive() const 60 { 61 return inspector_ != nullptr; 62 } 63 NextBytes()64 size_t NextBytes() const 65 { 66 return nextCounter_ - currentCounter_; 67 } 68 69 private: 70 AllocationInspector *inspector_ = nullptr; 71 size_t currentCounter_ = 0; 72 size_t nextCounter_ = 0; 73 }; 74 } // namespace panda::ecmascript 75 76 #endif