File: leak-pool.c

package info (click to toggle)
valgrind 1%3A3.14.0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 156,980 kB
  • sloc: ansic: 728,128; exp: 26,134; xml: 22,268; cpp: 7,638; asm: 7,312; makefile: 6,102; perl: 5,910; sh: 5,717
file content (126 lines) | stat: -rw-r--r-- 2,331 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>

#include "../memcheck.h"

struct cell
{  
  struct cell *next;
  int x;
};

struct pool
{
  size_t allocated;
  size_t used;
  uint8_t *buf;
};

void * 
allocate_from_pool(struct pool *p, size_t n)
{
  void *a = p->buf + p->used;
  assert(p->used + n < p->allocated);
  VALGRIND_MEMPOOL_ALLOC(p, a, n);
  p->used += n;
  return a;
}

struct pool *
allocate_pool()
{
  struct pool *p = malloc(sizeof(struct pool));
  assert(p);
  p->allocated = 4096;
  p->used = 0;
  p->buf = malloc(p->allocated);
  assert(p->buf);
  memset(p->buf, 0, p->allocated);
  VALGRIND_CREATE_MEMPOOL(p, 0, 0);
  (void) VALGRIND_MAKE_MEM_NOACCESS(p->buf, p->allocated);
  return p;
}

#define N 100

/* flags */
int static_roots = 0;
int trim_pool    = 0;
int destroy_pool = 0;
void set_flags ( int n )
{
  switch (n) {
     case 0:
        static_roots = 0;
        trim_pool    = 0;
        destroy_pool = 0;
        break;
     case 1:
        static_roots = 0;
        trim_pool    = 1;
        destroy_pool = 0;
        break;
     case 2:
        static_roots = 0;
        trim_pool    = 0;
        destroy_pool = 1;
        break;
     case 3:
        static_roots = 1;
        trim_pool    = 0;
        destroy_pool = 0;
        break;
     case 4:
        static_roots = 1;
        trim_pool    = 1;
        destroy_pool = 0;
        break;
     case 5:
        static_roots = 1;
        trim_pool    = 0;
        destroy_pool = 1;
        break;
     default:
        assert(0);
  }
}


struct cell *cells_static[N];


int main( int argc, char** argv )
{
  struct cell *cells_local[N];
  int arg;
  size_t i;
  struct pool *p      = allocate_pool();
  struct cell **cells = static_roots ? cells_static : cells_local;

  assert(argc == 2);
  assert(argv[1]);
  assert(strlen(argv[1]) == 1);
  assert(argv[1][0] >= '0' && argv[1][0] <= '5');
  arg = atoi( argv[1] );
  set_flags( arg );

  memset(cells_static, 0, sizeof(cells_static));
  memset(cells_local,  0, sizeof(cells_local));

  for (i = 0; i < N; ++i) {
    cells[i] = allocate_from_pool(p, sizeof(struct cell));  
  }

  if (trim_pool)
  VALGRIND_MEMPOOL_TRIM(p, 
			p->buf+(10 * sizeof(struct cell)), 
			20 * sizeof(struct cell) + 2);

  if (destroy_pool)
  VALGRIND_DESTROY_MEMPOOL(p);

  return 0;
}