File: heap.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 (52 lines) | stat: -rw-r--r-- 1,032 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
#include <stdio.h>
#include <stdlib.h>

#define NLIVE 1000000

#define NITERS (3*1000*1000)

char* arr[NLIVE];

int main ( int argc, char* argv[] )
{
   int i, j, nbytes = 0;
   int pdb = 0;
   int jpdb;

   if (argc > 1) {
      pdb = atoi(argv[1]);
   }

   printf("initialising\n");
   for (i = 0; i < NLIVE; i++)
      arr[i] = NULL;

   printf("running\n");
   j = -1;
   for (i = 0; i < NITERS; i++) {
      j++;
      if (j == NLIVE) j = 0;
      if (arr[j]) 
         free(arr[j]);
      arr[j] = malloc(nbytes);
      if (pdb > 0) {
         // create some partially defined bytes in arr[j]
         for (jpdb=0; jpdb<nbytes; jpdb = jpdb+pdb) {
            arr[j][jpdb] &= (jpdb & 0xff);
         }
      }

      // Cycle through the sizes 0,8,16,24,32.  Zero will get rounded up to
      // 8, so the 8B bucket will get twice as much traffic.
      nbytes += 8;
      if (nbytes > 32)
         nbytes = 0;
   }

   for (i = 0; i < NLIVE; i++)
      if (arr[i])
         free(arr[i]);

   printf("done\n");
   return 0;
}