tests/vg_regtest: Always evaluate prerequisite expressions with sh
[valgrind.git] / memcheck / tests / wrap5.c
blob79b475ce4915ec68dbf4e25dd3bcadff7b906c20
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "valgrind.h"
6 /* As wrap4.c, but also throw in various calls to another redirected
7 function (malloc) to check that that doesn't screw anything up.
8 */
10 typedef
11 struct _Lard {
12 struct _Lard* next;
13 char stuff[999];
15 Lard;
17 Lard* lard = NULL;
18 static int ctr = 0;
20 void addMoreLard ( void )
22 Lard* p;
23 ctr++;
24 if ((ctr % 3) == 1) {
25 p = malloc(sizeof(Lard));
26 p->next = lard;
27 lard = p;
32 static int fact1 ( int n );
33 static int fact2 ( int n );
35 /* This is needed to stop gcc4 turning 'fact' into a loop */
36 __attribute__((noinline))
37 int mul ( int x, int y ) { return x * y; }
39 int fact1 ( int n )
41 addMoreLard();
42 if (n == 0) return 1; else return mul(n, fact2(n-1));
44 int fact2 ( int n )
46 addMoreLard();
47 if (n == 0) return 1; else return mul(n, fact1(n-1));
51 int I_WRAP_SONAME_FNNAME_ZU(NONE,fact1) ( int n )
53 int r;
54 OrigFn fn;
55 VALGRIND_GET_ORIG_FN(fn);
56 printf("in wrapper1-pre: fact(%d)\n", n);
57 addMoreLard();
58 CALL_FN_W_W(r, fn, n);
59 addMoreLard();
60 printf("in wrapper1-post: fact(%d) = %d\n", n, r);
61 if (n >= 3) r += fact2(2);
62 return r;
65 int I_WRAP_SONAME_FNNAME_ZU(NONE,fact2) ( int n )
67 int r;
68 OrigFn fn;
69 VALGRIND_GET_ORIG_FN(fn);
70 printf("in wrapper2-pre: fact(%d)\n", n);
71 addMoreLard();
72 CALL_FN_W_W(r, fn, n);
73 addMoreLard();
74 printf("in wrapper2-post: fact(%d) = %d\n", n, r);
75 return r;
78 /* --------------- */
80 int main ( void )
82 int r;
83 Lard *p, *p_next;
84 printf("computing fact1(7)\n");
85 r = fact1(7);
86 printf("fact1(7) = %d\n", r);
88 printf("allocated %d Lards\n", ctr);
89 for (p = lard; p; p = p_next) {
90 p_next = p->next;
91 free(p);
94 return 0;