• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 PANDA_RUNTIME_MEM_MEM_HOOKS_H_
17 #define PANDA_RUNTIME_MEM_MEM_HOOKS_H_
18 
19 #include "libpandabase/mem/mem.h"
20 
21 namespace panda::mem {
22 
23 class PandaHooks {
24 public:
25     static void Enable();
26 
27     static void Disable();
28 
GetAllocViaStandard()29     static size_t GetAllocViaStandard() noexcept
30     {
31         return alloc_via_standard;
32     }
33 
34 private:
35     static constexpr size_t MAX_ALLOC_VIA_STANDARD = 4_MB;
36 
37     static void SaveMemHooks();
38 
39     static void SetMemHooks();
40 
41     static void *(*volatile old_malloc_hook)(size_t, const void *);
42     static void *(*volatile old_memalign_hook)(size_t, size_t, const void *);
43     static void (*volatile old_free_hook)(void *, const void *);
44 
45     static void *MallocHook(size_t size, const void *caller);
46     static void *MemalignHook(size_t alignment, size_t size, const void *caller);
47     static void FreeHook(void *ptr, const void *caller);
48 
49     static size_t alloc_via_standard;
50 };
51 
52 }  // namespace panda::mem
53 
54 #endif  // PANDA_RUNTIME_MEM_MEM_HOOKS_H_
55