Home | History | Annotate | Download | only in tests
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <assert.h>
      4 #include "../memcheck.h"
      5 #include "leak.h"
      6 
      7 int main(void)
      8 {
      9    int  x;
     10    int  y = 0;
     11    int* reachable __attribute__((unused));
     12    int* dubious;
     13    int* leaked __attribute__((unused));
     14    DECLARE_LEAK_COUNTERS;
     15 
     16    /* we require these longs to have same size as a machine word */
     17    assert(sizeof(long) == sizeof(void*));
     18 
     19    /* Error counting */
     20    fprintf(stderr, "errors: %d\n\n", VALGRIND_COUNT_ERRORS);
     21 
     22    if (x == 0) {
     23       y++;
     24    } else {
     25       y--;
     26    }
     27 
     28    fprintf(stderr, "errors: %d\n\n", VALGRIND_COUNT_ERRORS);
     29 
     30    // Get a baseline, after start-up and also after printf (because Darwin
     31    // printf allocates memory the first time it's called!)
     32    GET_INITIAL_LEAK_COUNTS;
     33 
     34    fprintf(stderr, "errors: %d\n\n", VALGRIND_COUNT_ERRORS);
     35 
     36    /* Leak checking */
     37    GET_FINAL_LEAK_COUNTS;
     38    PRINT_LEAK_COUNTS(stderr);
     39    fprintf(stderr, "\n");
     40 
     41    fprintf(stderr, "errors: %d\n\n", VALGRIND_COUNT_ERRORS);
     42 
     43    leaked = malloc(77);
     44    leaked = 0;
     45 
     46    dubious = malloc(88);
     47    dubious += 10;
     48 
     49    reachable = malloc(99);
     50 
     51    GET_FINAL_LEAK_COUNTS;
     52    PRINT_LEAK_COUNTS(stderr);
     53    fprintf(stderr, "\n");
     54 
     55    fprintf(stderr, "errors: %d\n", VALGRIND_COUNT_ERRORS);
     56 
     57    return 0;
     58 }
     59