• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 COMMON_COMPONENTS_HEAP_ALLOCATOR_ALLOC_MEM_MAP_H
17 #define COMMON_COMPONENTS_HEAP_ALLOCATOR_ALLOC_MEM_MAP_H
18 
19 #include "common_interfaces/base/common.h"
20 #ifdef _WIN64
21 #include <handleapi.h>
22 #include <memoryapi.h>
23 #else
24 #include <sys/mman.h>
25 #endif
26 #include "common_components/heap/allocator/alloc_util.h"
27 #include "common_components/common/type_def.h"
28 
29 namespace common {
30 class MemoryMap {
31 public:
32 #ifdef _WIN64
33     static constexpr int MAP_PRIVATE = 2;
34     static constexpr int MAP_FIXED = 0x10;
35     static constexpr int MAP_ANONYMOUS = 0x20;
36     static constexpr int PROT_NONE = 0;
37     static constexpr int PROT_READ = 1;
38     static constexpr int PROT_WRITE = 2;
39     static constexpr int PROT_EXEC = 4;
40     static constexpr int DEFAULT_MEM_FLAGS = MAP_PRIVATE | MAP_ANONYMOUS;
41 #else
42     static constexpr int DEFAULT_MEM_FLAGS = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
43 #endif
44     static constexpr int DEFAULT_MEM_PROT = PROT_READ | PROT_WRITE;
45 
46     struct Option {         // optional args for mem map
47         const char* tag;    // name to identify the mapped memory
48         void* reqBase;      // a hint to mmap about start addr, not guaranteed
49         unsigned int flags; // mmap flags
50         int prot;           // initial access flags
51         bool protAll;       // applying prot to all pages in range
52     };
53     // by default, it tries to map memory in low addr space, with a random start
54     static constexpr Option DEFAULT_OPTIONS = { "arkcommon_unnamed", nullptr, DEFAULT_MEM_FLAGS,
55                                                 DEFAULT_MEM_PROT, false };
56 
57     // the only way to get a MemoryMap
58     static MemoryMap* MapMemory(size_t reqSize, size_t initSize, const Option& opt = DEFAULT_OPTIONS);
59 
60     static MemoryMap* MapMemoryAlignInner4G(uint64_t reqSize, uint64_t initSize, const Option& opt = DEFAULT_OPTIONS);
61 
62 #ifdef _WIN64
63     static void CommitMemory(void* addr, size_t size);
64 #endif
65 
66     // destroy a MemoryMap
DestroyMemoryMap(MemoryMap * & memMap)67     static void DestroyMemoryMap(MemoryMap*& memMap) noexcept
68     {
69         if (memMap != nullptr) {
70             delete memMap;
71             memMap = nullptr;
72         }
73     }
74 
GetBaseAddr()75     void* GetBaseAddr() const { return memBaseAddr_; }
GetCurrEnd()76     void* GetCurrEnd() const { return memCurrEndAddr_; }
GetMappedEndAddr()77     void* GetMappedEndAddr() const { return memMappedEndAddr_; }
GetCurrSize()78     size_t GetCurrSize() const { return memCurrSize_; }
GetMappedSize()79     size_t GetMappedSize() const { return memMappedSize_; }
80 
81     ~MemoryMap();
82     MemoryMap(const MemoryMap& that) = delete;
83     MemoryMap(MemoryMap&& that) = delete;
84     MemoryMap& operator=(const MemoryMap& that) = delete;
85     MemoryMap& operator=(MemoryMap&& that) = delete;
86 
87 private:
88     static bool ProtectMemInternal(void* addr, size_t size, int prot);
89 
90     void* memBaseAddr_;      // start of the mapped memory
91     void* memCurrEndAddr_;   // end of the memory **in use**
92     void* memMappedEndAddr_; // end of the mapped memory, always >= currEndAddr
93     size_t memCurrSize_;     // size of the memory **in use**
94     size_t memMappedSize_;   // size of the mapped memory, always >= currSize
95 
96     // MemoryMap is created via factory method
97     MemoryMap(void* baseAddr, size_t initSize, size_t mappedSize);
98 }; // class MemoryMap
99 } // namespace common
100 
101 #endif // COMMON_COMPONENTS_HEAP_ALLOCATOR_ALLOC_MEM_MAP_H
102