Update year information of license boilerplate
[platform/core/uifw/multi-assistant-service.git] / plugins / wakeup-manager / inc / heap_tracer.h
1 /*
2  * Copyright 2018-2019 Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __HEAP_TRACER_H__
18 #define __HEAP_TRACER_H__
19
20 #include <list>
21 #include <mutex>
22 #include <unordered_map>
23 #include <string>
24 #include <set>
25
26 #ifndef __MODULE__
27 #define __MODULE__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
28 #endif
29
30 #define vm_calloc_simple(nmemb, size) vm_calloc(nmemb, size, "%s: %s(%d)", __MODULE__, __func__, __LINE__)
31 #define vm_malloc_simple(size) vm_malloc(size, "%s: %s(%d)", __MODULE__, __func__, __LINE__)
32 #define vm_free_simple(ptr) vm_free(ptr, "%s: %s(%d)", __MODULE__, __func__, __LINE__)
33 #define vm_strdup_simple(ptr) vm_strdup(ptr, "%s: %s(%d)", __MODULE__, __func__, __LINE__)
34 #define vm_mark_unmanaged_simple(ptr) vm_mark_unmanaged(ptr, "%s: %s(%d)", __MODULE__, __func__, __LINE__)
35
36 void *vm_calloc(size_t nmemb, size_t size, const char *fmt, ...);
37 void *vm_malloc(size_t size, const char *fmt, ...);
38 void vm_free(void *ptr, const char *fmt, ...);
39 char *vm_strdup(const char *s, const char *fmt, ...);
40 void *vm_mark_unmanaged(void *ptr, const char *fmt, ...);
41
42 class HeapTracer {
43 public:
44         HeapTracer();
45         virtual ~HeapTracer();
46
47         bool Insert(void *ptr, std::string description, size_t size = 0);
48         bool Delete(void *ptr, std::string description);
49         std::string Find(void *ptr);
50
51         void MarkUnmanaged(void *ptr, std::string description);
52
53         bool Empty();
54         void Trace();
55 protected:
56         typedef struct {
57                 void *ptr{nullptr};
58                 std::string description;
59                 int size{0};
60         } AllocationEntry;
61
62         std::mutex mManagedTableMutex;
63         std::unordered_map<void*, AllocationEntry> mManagedTable;
64
65         std::mutex mUnmanagedRecordsMutex;
66         std::list<AllocationEntry> mUnmanagedRecords;
67         size_t mUnmanagedRecordsSize{0};
68
69         enum class EntryOperation {
70                 INSERT,
71                 DELETE,
72         };
73         typedef struct {
74                 AllocationEntry entry;
75                 EntryOperation operation;
76                 struct timespec ts;
77         } EntryHistory;
78         std::list<EntryHistory> mEntryHistory;
79 };
80
81 #endif /* __HEAP_TRACER_H__ */