• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2022-2024 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_LIBPANDABASE_OS_WINDOWS_MEM_HOOKS_H_
17 #define PANDA_LIBPANDABASE_OS_WINDOWS_MEM_HOOKS_H_
18 
19 #include <crtdbg.h>
20 #include <iostream>
21 #include "libpandabase/macros.h"
22 
23 namespace ark::os::windows::mem_hooks {
24 
25 class PandaHooks {
26 public:
27     static void Initialize();
28     static void Enable();
29     static void Disable();
30 
31 private:
32     /*
33      * "PandaAllocHook" is an allocation hook function, following a prototype described in
34      * https://docs.microsoft.com/en-us/visualstudio/debugger/allocation-hook-functions.
35      * Installed it using "_CrtSetAllocHook", then it will be called every time memory is
36      * allocated, reallocated, or freed.
37      */
38     // CC-OFFNXT(G.FUN.01) solid logic
39     static int PandaAllocHook(int alloctype, void *data, std::size_t size, int blocktype, long request,
40                               const unsigned char *filename, int linenumber);
41 
42     static _CrtMemState begin, end, out;
43 };
44 
45 }  // namespace ark::os::windows::mem_hooks
46 
47 namespace ark::os::mem_hooks {
48 using PandaHooks = ark::os::windows::mem_hooks::PandaHooks;
49 }  // namespace ark::os::mem_hooks
50 
51 #endif  // PANDA_LIBPANDABASE_OS_WINDOWS_MEM_HOOKS_H_
52