-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cpp
86 lines (70 loc) · 2.22 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include <string.h>
#include "mem_leak_checker.h"
void* malloc_and_half_free_thread(void*);
void* malloc_without_free_thread(void*);
int main(int argc, char** argv) {
pthread_t tid1, tid2;
fprintf(stderr, "enter main\n");
mem_leak_checker_init();
int rc2 = pthread_create(&tid2, NULL, malloc_and_half_free_thread, NULL);
if(rc2 != 0)
fprintf(stderr, "%s: %d\n",__func__, strerror(rc2));
int rc1 = pthread_create(&tid1, NULL, malloc_without_free_thread, NULL);
if(rc1 != 0)
printf("%s: %d\n",__func__, strerror(rc1));
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
mem_leak_checker_detail();
mem_leak_checker_clear();
fprintf(stderr, "leave main\n");
exit(0);
}
char* malloc_and_free() {
char* p = (char*)malloc(1);
return p;
}
void malloc_deep_2() {
char* p = (char*)malloc(1);
}
void malloc_deep() {
malloc_deep_2();
}
void malloc_without_free() {
malloc_deep();
}
void* malloc_and_half_free_thread(void* arg) {
fprintf(stderr, "malloc and free thread id is %u, start to malloc and free\n", (unsigned int)pthread_self());
char* alloc_addr[100];
// 在需要监控的上下文加上hook标志
pthread_mutex_t* lock = get_lock();
pthread_mutex_lock(lock);
start_hook();
for (int i= 0; i < 100; ++i) {
char*p = malloc_and_free();
alloc_addr[i] = p;
}
stop_hook();
pthread_mutex_unlock(lock);
// free的地方不需要加上hook标志,这样使用者可以减轻负担
for (int i= 0; i < 50; ++i) {
free(alloc_addr[i]);
}
fprintf(stderr, "malloc and free thread id is %u, malloc and free success\n", (unsigned int)pthread_self());
pthread_exit(0);
}
void* malloc_without_free_thread(void* arg) {
fprintf(stderr, "malloc without free thread id is %u, start to malloc\n", (unsigned int)pthread_self());
pthread_mutex_t* lock = get_lock();
pthread_mutex_lock(lock);
start_hook();
for (int i = 0; i < 3; ++i) {
malloc_without_free();
}
stop_hook();
pthread_mutex_unlock(lock);
fprintf(stderr, "malloc and free thread id is %u, malloc and free success\n", (unsigned int)pthread_self());
pthread_exit(0);
}