memcheck: Handle Err_ReallocSizeZero in MC_(eq_Error)
[valgrind.git] / memcheck / tests / leak-cycle.c
blobcee967b32278fe902499ade7bfbc4ea141ea42d6
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "leak.h"
4 #include "../memcheck.h"
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*)) ];
13 struct n *mk(struct n *l, struct n *r)
15 struct n *n = malloc(sizeof(struct n));
16 n->l = l;
17 n->r = r;
19 return n;
22 static struct n *mkcycle()
24 register struct n *a, *b, *c;
26 a = mk(0,0);
27 b = mk(a,0);
28 c = mk(b,0);
29 a->l = c;
31 return a;
35 int main()
37 DECLARE_LEAK_COUNTERS;
39 struct n *volatile c1, *volatile c2;
41 GET_INITIAL_LEAK_COUNTS;
43 /* two simple cycles */
44 c1 = mkcycle();
45 c2 = mkcycle();
47 c1 = c2 = 0;
49 /* one cycle linked to another */
50 c1 = mkcycle();
51 c2 = mkcycle();
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;
60 c1 = c2 = 0;
62 /* two linked cycles */
63 c1 = mkcycle();
64 c2 = mkcycle();
66 c1->r = c2;
67 c2->r = c1;
69 c1 = c2 = 0;
71 CLEAR_CALLER_SAVED_REGS;
73 GET_FINAL_LEAK_COUNTS;
75 PRINT_LEAK_COUNTS(stderr);
77 return 0;