summaryrefslogtreecommitdiff
path: root/sample/sample.c
blob: 3d66d07af22405e4aedeeb97fbdb3603fb3c7d9c (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright 2013  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.tizenopensource.org/license
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <errno.h>

#include <heap-monitor.h>

#define LIBC "/lib/libc-2.13.so"

int errno;

int main(int argc, char *argv[])
{
	void *ptr;
	void *o_ptr;
	int i;
	int reused;
	int cnt;
	unsigned long align;

	heap_monitor_add_target(argv[0]);
	heap_monitor_add_target(LIBC);
	heap_monitor_init();
	heap_monitor_start();

	o_ptr = NULL;
	reused = 0;
	cnt = 1000;
	for (i = 0; i < cnt; i++) {
		o_ptr = malloc(1000);
		memset(o_ptr, 0, 1000);
		free(o_ptr);

		ptr = malloc(2000);
		memset(ptr, 0, 1000);
		free(ptr);

		reused += (o_ptr == ptr);
	}

	align = 1024;
	if (posix_memalign(&ptr, align, 100)) {
		printf("Failed to allocate aligned memory\n");
	} else {
		void *abc;
		printf("posix_memalign aligned: %lu, %lu\n", (unsigned long)ptr, (unsigned long)ptr % align);

		abc = realloc(ptr, 200);
		if (abc) {
			printf("realloc aligned: %lu, %lu\n", (unsigned long)abc, (unsigned long)abc % align);
			ptr = abc;
		}
		free(ptr);
	}

	ptr = memalign(align, 100);
	if (!ptr) {
		printf("Failed to allocate aligned memory\n");
	} else {
		printf("memalign aligned: %lu, %lu\n", (unsigned long)ptr, (unsigned long)ptr % align);
		printf("memalign aligned: %lu, %lu\n", (unsigned long)ptr, (unsigned long)ptr % 4);
		free(ptr);
	}

	align = 100;
	ptr = memalign(align, 100);
	if (!ptr) {
		printf("Failed to allocate aligned memory\n");
	} else {
		printf(">> aligned: %lu, %lu\n", (unsigned long)ptr, (unsigned long)ptr % align);
		free(ptr);
	}

	ptr = malloc(100);
	printf("[%s] uses %d\n", argv[0], heap_monitor_target_usage(argv[0]));
	printf("[%s] uses %d\n", LIBC, heap_monitor_target_usage(LIBC));
	ptr = realloc(ptr, 200);
	printf("[%s] uses %d\n", argv[0], heap_monitor_target_usage(argv[0]));
	printf("[%s] uses %d\n", LIBC, heap_monitor_target_usage(LIBC));
	free(ptr);

	heap_monitor_stop();

	printf("[%s] uses %d\n", argv[0], heap_monitor_target_usage(argv[0]));
	printf("[%s] uses %d\n", LIBC, heap_monitor_target_usage(LIBC));
	if (heap_monitor_target_usage(argv[0]))
		printf("LEAK detected\n");

	printf("heap chunk resuing rates (%d/%d) %.0lf%%\n", reused, cnt, (double)reused * 100.0f/(double)cnt);
	heap_monitor_del_target(argv[0]);
	heap_monitor_del_target(LIBC);
	return 0;
}

/* End of a file */