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 #ifndef ECMASCRIPT_THREAD_LOCAL_ALLOCATION_BUFFER_H
17 #define ECMASCRIPT_THREAD_LOCAL_ALLOCATION_BUFFER_H
18 
19 #include "ecmascript/mem/allocator-inl.h"
20 
21 namespace panda::ecmascript {
22 class ThreadLocalAllocationBuffer {
23 public:
24     // perhaps need calculate tlab size dynamically.
25     static constexpr size_t TLAB_SIZE = 8_KB;
26     static constexpr size_t INIT_WASTE_LIMIT = 64;
27     static constexpr size_t INCREASE_WASTE_SIZE = 64;
ThreadLocalAllocationBuffer(Heap *heap)28     ThreadLocalAllocationBuffer(Heap *heap) : heap_(heap) {}
29     ~ThreadLocalAllocationBuffer() = default;
30     NO_COPY_SEMANTIC(ThreadLocalAllocationBuffer);
31     NO_MOVE_SEMANTIC(ThreadLocalAllocationBuffer);
32 
33     void Reset(uintptr_t begin = 0, uintptr_t end = 0, uintptr_t top = 0);
34 
35     void FillBumpPointer();
36 
Allocate(size_t size)37     uintptr_t Allocate(size_t size)
38     {
39         return bpAllocator_.Allocate(size);
40     }
41 
Available() const42     size_t Available() const
43     {
44         return bpAllocator_.Available();
45     }
46 
GetTop() const47     uintptr_t GetTop() const
48     {
49         return bpAllocator_.GetTop();
50     }
51 
GetTopAddress()52     const uintptr_t *GetTopAddress()
53     {
54         return bpAllocator_.GetTopAddress();
55     }
56 
GetEndAddress()57     const uintptr_t *GetEndAddress()
58     {
59         return bpAllocator_.GetEndAddress();
60     }
61 
GetTlabWasteLimit() const62     size_t GetTlabWasteLimit() const
63     {
64         return tlabWasteLimit_;
65     }
66 
ComputeSize() const67     size_t ComputeSize() const
68     {
69         return TLAB_SIZE;
70     }
71 
NeedNewTlab(size_t size)72     inline bool NeedNewTlab(size_t size)
73     {
74         if (size > TLAB_SIZE) {
75             return false;
76         }
77         if (bpAllocator_.Available() >= tlabWasteLimit_) {
78             tlabWasteLimit_ += INCREASE_WASTE_SIZE;
79             return false;
80         }
81         if (disabled_) {
82             return false;
83         }
84         return true;
85     }
86 
DisableNewTlab()87     void DisableNewTlab()
88     {
89         disabled_ = true;
90     }
91 
92 private:
93     bool disabled_ {false};
94     Heap *heap_ {nullptr};
95     BumpPointerAllocator bpAllocator_;
96     size_t tlabWasteLimit_ {INIT_WASTE_LIMIT};
97 };
98 }  //  namespace panda::ecmascript
99 
100 #endif  // ECMASCRIPT_THREAD_LOCAL_ALLOCATION_BUFFER_H
101