• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 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 #ifndef PANDA_RUNTIME_MEM_FRAME_ALLOCATOR_H
16 #define PANDA_RUNTIME_MEM_FRAME_ALLOCATOR_H
17 
18 #include <securec.h>
19 #include <array>
20 
21 #include "libpandabase/mem/arena-inl.h"
22 #include "libpandabase/mem/mem.h"
23 #include "libpandabase/mem/mmap_mem_pool-inl.h"
24 
25 namespace panda::mem {
26 
27 //                                          Allocation flow looks like that:
28 //
29 //    Allocate arenas for frames                  Frames free              Return arenas   Second allocated arena
30 //            (stage 1)                            (stage 2)                 (stage 3)     will be bigger than the
31 //                                                                                         second at stage 1
32 //                        |-----|                             |-----|                                   |-----|
33 //                        |     |                             |     |                                   |     |
34 //              |-----|   |     |                   |-----|   |     |                                   |     |
35 //              |xxxxx|   |     |                   |     |   |     |                                   |     |
36 //    |-----|   |xxxxx|   |xxxxx|         |-----|   |     |   |     |         |-----|         |-----|   |     |
37 //    |xxxxx|   |xxxxx|   |xxxxx|  ---->  |     |   |     |   |     |  ---->  |     |  ---->  |xxxxx|   |xxxxx|
38 //    |xxxxx|   |xxxxx|   |xxxxx|         |     |   |     |   |     |         |     |         |xxxxx|   |xxxxx|
39 //    |xxxxx|   |xxxxx|   |xxxxx|         |     |   |     |   |     |         |     |         |xxxxx|   |xxxxx|
40 //    |xxxxx|   |xxxxx|   |xxxxx|         |     |   |     |   |     |         |     |         |xxxxx|   |xxxxx|
41 //    |xxxxx|   |xxxxx|   |xxxxx|         |xxxxx|   |     |   |     |         |xxxxx|         |xxxxx|   |xxxxx|
42 //    |-----|   |-----|   |-----|         |-----|   |-----|   |-----|         |-----|         |-----|   |-----|
43 
44 // Frame allocator uses arenas and works like a stack -
45 // it will give memory from the top and can delete only last allocated memory.
46 template <Alignment AlignmenT = DEFAULT_FRAME_ALIGNMENT, bool UseMemsetT = true>
47 class FrameAllocator {
48 public:
49     explicit FrameAllocator(bool use_malloc = false);
50     ~FrameAllocator();
51     FrameAllocator(const FrameAllocator &) noexcept = delete;
52     FrameAllocator(FrameAllocator &&) noexcept = default;
53     FrameAllocator &operator=(const FrameAllocator &) noexcept = delete;
54     FrameAllocator &operator=(FrameAllocator &&) noexcept = default;
55 
56     [[nodiscard]] void *Alloc(size_t size);
57 
58     // We must free objects allocated by this allocator strictly in reverse order
59     void Free(void *mem);
60 
61     /**
62      * \brief Returns true if address inside current allocator.
63      */
64     bool Contains(void *mem);
65 
GetAllocatorType()66     static constexpr AllocatorType GetAllocatorType()
67     {
68         return AllocatorType::FRAME_ALLOCATOR;
69     }
70 
GetAllocatedSize()71     size_t GetAllocatedSize() const
72     {
73         return allocated_size_;
74     }
75 
76 private:
77     using FramesArena = DoubleLinkedAlignedArena<AlignmenT>;
78     static constexpr size_t FIRST_ARENA_SIZE = 256_KB;
79     static_assert(FIRST_ARENA_SIZE % PANDA_POOL_ALIGNMENT_IN_BYTES == 0);
80     static constexpr size_t ARENA_SIZE_GREW_LEVEL = FIRST_ARENA_SIZE;
81     static constexpr size_t FRAME_ALLOC_MIN_FREE_MEMORY_THRESHOLD = FIRST_ARENA_SIZE / 2;
82     static constexpr size_t FRAME_ALLOC_MAX_FREE_ARENAS_THRESHOLD = 1;
83 
84     /**
85      * \brief Heuristic for arena size increase.
86      * @return new size
87      */
88     size_t GetNextArenaSize(size_t size);
89 
90     /**
91      * \brief Try to allocate an arena from the memory.
92      * @return true on success, or false on fail
93      */
94     bool TryAllocateNewArena(size_t size = ARENA_SIZE_GREW_LEVEL);
95 
96     /**
97      * \brief Try to allocate memory for a frame in the current arena or in the next one if it exists.
98      * @param size - size of the allocated memory
99      * @return pointer to the allocated memory on success, or nullptr on fail
100      */
101     void *TryToAllocate(size_t size);
102 
103     /**
104      * \brief Free last_allocated_arena_, i.e., free last arena in the list.
105      */
106     void FreeLastArena();
107 
108     /**
109      * \brief Try to allocate an arena from the memory.
110      * @param size - size of the required arena
111      * @return pointer on success, or nullptr on fail
112      */
113     FramesArena *AllocateArenaImpl(size_t size);
114 
115     /**
116      * \brief Free given arena
117      * @param arena - arena to free
118      */
119     void FreeArenaImpl(FramesArena *arena);
120 
121     // A pointer to the current arena with the last allocated frame
122     FramesArena *cur_arena_ {nullptr};
123 
124     // A pointer to the last allocated arena (so it is equal to the top arena in the list)
125     FramesArena *last_alloc_arena_ {nullptr};
126 
127     // The biggest arena size during FrameAllocator workflow. Needed for computing a new arena size.
128     size_t biggest_arena_size_ {0};
129 
130     // A marker which tells us if we need to increase the size of a new arena or not.
131     bool arena_size_need_to_grow_ {true};
132 
133     size_t empty_arenas_count_ {0};
134 
135     // Total allocated size
136     size_t allocated_size_ {0};
137 
138     MmapMemPool *mem_pool_alloc_ {nullptr};
139 
140     bool use_malloc_ {false};
141 
142     friend class FrameAllocatorTest;
143 };
144 
145 }  // namespace panda::mem
146 
147 #endif  // PANDA_RUNTIME_MEM_FRAME_ALLOCATOR_H
148