~bkerensa/ubuntu/raring/valgrind/merge-from-deb

« back to all changes in this revision

Viewing changes to cachegrind/tests/wrap5.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2006-06-26 00:17:17 UTC
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: james.westby@ubuntu.com-20060626001717-qi51nzty57cb12q6
Tags: upstream-3.2.0
Import upstream version 3.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include <stdio.h>
 
3
#include <malloc.h>
 
4
#include "valgrind.h"
 
5
 
 
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
*/
 
9
 
 
10
typedef 
 
11
   struct _Lard {
 
12
      struct _Lard* next; 
 
13
      char stuff[999]; 
 
14
   }
 
15
   Lard;
 
16
 
 
17
Lard* lard = NULL;
 
18
static int ctr = 0;
 
19
 
 
20
void addMoreLard ( void )
 
21
{
 
22
   Lard* p;
 
23
   ctr++;
 
24
   if ((ctr % 3) == 1) {
 
25
      p = malloc(sizeof(Lard));
 
26
      p->next = lard;
 
27
      lard = p;
 
28
   }
 
29
}
 
30
 
 
31
 
 
32
static int fact1 ( int n );
 
33
static int fact2 ( int n );
 
34
 
 
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; }
 
38
 
 
39
int fact1 ( int n )
 
40
{
 
41
   addMoreLard();
 
42
   if (n == 0) return 1; else return mul(n, fact2(n-1));
 
43
}
 
44
int fact2 ( int n )
 
45
{
 
46
   addMoreLard();
 
47
   if (n == 0) return 1; else return mul(n, fact1(n-1));
 
48
}
 
49
 
 
50
 
 
51
int I_WRAP_SONAME_FNNAME_ZU(NONE,fact1) ( int n )
 
52
{
 
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;
 
63
}
 
64
 
 
65
int I_WRAP_SONAME_FNNAME_ZU(NONE,fact2) ( int n )
 
66
{
 
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;
 
76
}
 
77
 
 
78
/* --------------- */
 
79
 
 
80
int main ( void )
 
81
{
 
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);
 
87
 
 
88
   printf("allocated %d Lards\n", ctr);
 
89
   for (p = lard; p; p = p_next) {
 
90
      p_next = p->next;
 
91
      free(p);
 
92
   }
 
93
 
 
94
   return 0;
 
95
}