• 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_INTERFACES_HEAP_PROFILER_LISTENER_H
17 #define COMMON_INTERFACES_HEAP_PROFILER_LISTENER_H
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <functional>
22 #include <shared_mutex>
23 #include <mutex>
24 #include <map>
25 
26 namespace common {
27 class HeapProfilerListener {
28 public:
29     static HeapProfilerListener &GetInstance();
30 
31     uint32_t RegisterMoveEventCb(const std::function<void(uintptr_t, uintptr_t, size_t)> &cb);
32     void UnRegisterMoveEventCb(uint32_t index);
33     void OnMoveEvent(uintptr_t fromObj, uintptr_t toObj, size_t size);
34     void RegisterOutOfMemoryEventCb(const std::function<void(void *thread)> &cb);
35     void OnOutOfMemoryEventCb();
36 
37 private:
38     std::map<uint32_t, std::function<void(uintptr_t, uintptr_t, size_t)>> moveEventCbs_;
39     uint32_t moveEventCbId_ {1};
40     std::shared_mutex mutex_;
41     std::function<void(void *thread)> outOfMemoryEventCb_ = [](void *thread) {};
42 };
43 }  // namespace common
44 #endif  // COMMON_INTERFACES_HEAP_PROFILER_LISTENER_H