Home | History | Annotate | Download | only in tests
      1 
      2 #include <stdio.h>
      3 #include <stdlib.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 }
     96