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_MALLOC_PROXY_ALLOCATOR_INL_H
16 #define PANDA_RUNTIME_MEM_MALLOC_PROXY_ALLOCATOR_INL_H
17
18 #include <cstring>
19
20 #include "libpandabase/utils/logger.h"
21 #include "libpandabase/os/mem.h"
22 #include "runtime/mem/alloc_config.h"
23 #include "libpandabase/utils/asan_interface.h"
24 #include "runtime/mem/malloc-proxy-allocator.h"
25
26 namespace panda::mem {
27
28 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
29 #define LOG_MALLOCPROXY_ALLOCATOR(level) LOG(level, ALLOC) << "MallocProxyAllocator: "
30
31 template <typename AllocConfigT>
~MallocProxyAllocator()32 MallocProxyAllocator<AllocConfigT>::~MallocProxyAllocator()
33 {
34 LOG_MALLOCPROXY_ALLOCATOR(INFO) << "Destroying MallocProxyAllocator";
35 }
36
37 template <typename AllocConfigT>
Alloc(size_t size,Alignment align)38 void *MallocProxyAllocator<AllocConfigT>::Alloc(size_t size, Alignment align)
39 {
40 if (size == 0) {
41 return nullptr;
42 }
43
44 // NOLINTNEXTLINE(readability-braces-around-statements, bugprone-suspicious-semicolon)
45 if constexpr (!DUMMY_ALLOC_CONFIG) {
46 lock_.Lock();
47 }
48 size_t alignment_in_bytes = GetAlignmentInBytes(align);
49 void *ret = os::mem::AlignedAlloc(alignment_in_bytes, size);
50 // NOLINTNEXTLINE(readability-braces-around-statements, bugprone-suspicious-semicolon)
51 if constexpr (!DUMMY_ALLOC_CONFIG) {
52 ASSERT(allocated_memory_.find(ret) == allocated_memory_.end());
53 allocated_memory_.insert({ret, size});
54 AllocConfigT::OnAlloc(size, type_allocation_, mem_stats_);
55 AllocConfigT::MemoryInit(ret, size);
56 lock_.Unlock();
57 }
58 LOG_MALLOCPROXY_ALLOCATOR(DEBUG) << "Allocate memory with size " << std::dec << size << " at addr " << std::hex
59 << ret;
60 return ret;
61 }
62
63 template <typename AllocConfigT>
Free(void * mem)64 void MallocProxyAllocator<AllocConfigT>::Free(void *mem)
65 {
66 if (mem == nullptr) {
67 return;
68 }
69 // NOLINTNEXTLINE(readability-braces-around-statements, bugprone-suspicious-semicolon)
70 if constexpr (!DUMMY_ALLOC_CONFIG) {
71 lock_.Lock();
72 }
73 os::mem::AlignedFree(mem);
74 // NOLINTNEXTLINE(readability-braces-around-statements, bugprone-suspicious-semicolon)
75 if constexpr (!DUMMY_ALLOC_CONFIG) {
76 auto iterator = allocated_memory_.find(mem);
77 ASSERT(iterator != allocated_memory_.end());
78 size_t size = iterator->second;
79 allocated_memory_.erase(mem);
80 AllocConfigT::OnFree(size, type_allocation_, mem_stats_);
81 lock_.Unlock();
82 }
83 LOG_MALLOCPROXY_ALLOCATOR(DEBUG) << "Free memory at " << mem;
84 }
85
86 #undef LOG_MALLOCPROXY_ALLOCATOR
87
88 } // namespace panda::mem
89 #endif // PANDA_RUNTIME_MEM_MALLOC_PROXY_ALLOCATOR_INL_H
90