• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "leak.h"
4 #include "../memcheck.h"
5 
6 struct n {
7 	struct n *l;
8 	struct n *r;
9         // This ensures it's the same size on 32-bit and 64-bit platforms.
10         char padding[ 2 * (8 - sizeof(struct n*)) ];
11 };
12 
mk(struct n * l,struct n * r)13 struct n *mk(struct n *l, struct n *r)
14 {
15 	struct n *n = malloc(sizeof(struct n));
16 	n->l = l;
17 	n->r = r;
18 
19 	return n;
20 }
21 
mkcycle()22 static struct n *mkcycle()
23 {
24 	register struct n *a, *b, *c;
25 
26 	a = mk(0,0);
27 	b = mk(a,0);
28 	c = mk(b,0);
29 	a->l = c;
30 
31 	return a;
32 }
33 
34 
main()35 int main()
36 {
37 	DECLARE_LEAK_COUNTERS;
38 
39 	struct n *volatile c1, *volatile c2;
40 
41         GET_INITIAL_LEAK_COUNTS;
42 
43 	/* two simple cycles */
44 	c1 = mkcycle();
45 	c2 = mkcycle();
46 
47 	c1 = c2 = 0;
48 
49 	/* one cycle linked to another */
50 	c1 = mkcycle();
51 	c2 = mkcycle();
52 
53 	/* This is to make sure we end up merging cliques; see
54 	   mc_leakcheck.c */
55 	if (c1 < c2)
56 		c2->r = c1;
57 	else
58 		c1->r = c2;
59 
60 	c1 = c2 = 0;
61 
62 	/* two linked cycles */
63 	c1 = mkcycle();
64 	c2 = mkcycle();
65 
66 	c1->r = c2;
67 	c2->r = c1;
68 
69 	c1 = c2 = 0;
70 
71 	CLEAR_CALLER_SAVED_REGS;
72 
73 	GET_FINAL_LEAK_COUNTS;
74 
75 	PRINT_LEAK_COUNTS(stderr);
76 
77 	return 0;
78 }
79