1/*
2 * Copyright (c) 2021 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_TLAB_ALLOCATOR_H
17#define ECMASCRIPT_MEM_TLAB_ALLOCATOR_H
18
19#include "ecmascript/mem/allocator-inl.h"
20
21namespace panda::ecmascript {
22class Heap;
23
24class TlabAllocatorBase {
25public:
26    TlabAllocatorBase() = default;
27    ~TlabAllocatorBase() = default;
28    NO_COPY_SEMANTIC(TlabAllocatorBase);
29    NO_MOVE_SEMANTIC(TlabAllocatorBase);
30};
31
32class TlabAllocator : public TlabAllocatorBase {
33public:
34    TlabAllocator() = delete;
35    inline explicit TlabAllocator(Heap *heap);
36    ~TlabAllocator()
37    {
38        delete localSpace_;
39    }
40
41    NO_COPY_SEMANTIC(TlabAllocator);
42    NO_MOVE_SEMANTIC(TlabAllocator);
43
44    inline void Finalize();
45
46    inline uintptr_t Allocate(size_t size, MemSpaceType space);
47
48private:
49    inline uintptr_t AllocateInYoungSpace(size_t size);
50    inline uintptr_t AllocateInOldSpace(size_t size);
51    inline uintptr_t AllocateInCompressSpace(size_t size);
52
53    inline bool ExpandYoung();
54    inline bool ExpandCompressFromOld(size_t size);
55
56    Heap *heap_;
57
58    bool enableExpandYoung_;
59    bool enableStealOldRegion_;
60    BumpPointerAllocator youngAllocator_;
61
62    LocalSpace *localSpace_;
63};
64
65class SharedTlabAllocator : public TlabAllocatorBase {
66public:
67    SharedTlabAllocator() = delete;
68    inline explicit SharedTlabAllocator(SharedHeap *sHeap);
69    ~SharedTlabAllocator()
70    {
71        delete sLocalSpace_;
72    }
73
74    NO_COPY_SEMANTIC(SharedTlabAllocator);
75    NO_MOVE_SEMANTIC(SharedTlabAllocator);
76
77    inline void Finalize();
78
79    inline uintptr_t Allocate(size_t size, MemSpaceType space);
80
81private:
82    inline uintptr_t AllocateInCompressSpace(size_t size);
83    inline bool ExpandCompressFromOld(size_t size);
84
85    SharedHeap *sHeap_;
86    SharedLocalSpace *sLocalSpace_;
87};
88}  // namespace panda::ecmascript
89
90#endif  // ECMASCRIPT_MEM_TLAB_ALLOCATOR_H
91