aboutsummaryrefslogtreecommitdiffstats
path: root/testalloc.c
blob: 9a6d3ed15d402efce049e544d1773b7f1d5db0a7 (plain)
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
#ident "$Id$"
/* ----------------------------------------------------------------------- *
 *   
 *   Copyright 2001 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
 *   USA; either version 2 of the License, or (at your option) any later
 *   version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <limits.h>
#include "objstore.h"

#define COUNT	32768
#define SLOTS   1024
#define MAXLOG  26

void *areas[COUNT];
int sizes[COUNT];

void verify_no_overlap(int slot)
{
  char *start = areas[slot];
  char *end = start+sizes[slot];
  char *xs, *xe;
  int i;

  for ( i = 0 ; i < SLOTS ; i++ ) {
    if ( i != slot && areas[i] ) {
      xs = areas[i];
      xe = xs + sizes[i];

      if ( xs < end && xe > start ) {
	printf("OVERLAP ALERT: [%p,%p) -> [%p,%p)\n",
	       start, end, xs, xe);
	abort();
      }
    }
  }
}

int main(int argc, char *argv[])
{
  int i, slot, newsize;
  double rnd;
  void **root_ptr;
  void *nptr;
  unsigned long occupied = 0;

  unlink("arena.dat");
  unlink("arena.log");

  memset(areas, 0, sizeof(areas));
  memset(sizes, 0, sizeof(sizes));

  if ( !(root_ptr = objstore_arena_init("arena.dat", "arena.log")) ) {
    fprintf(stderr, "%s: objstore_arena_init() failed\n", argv[0]);
    return 1;
  }

  printf("Arena:   initialized, %d root pointers at %p\n",
	 ROOT_DATA_POINTERS, root_ptr);

  objstore_checkpoint(0.0);

  for ( i = 0 ; i < COUNT ; i++ ) {
    slot = rand() % SLOTS;

    if ( areas[slot] ) {
      if ( rand() % 1 ) {
	printf("Free:    %d (0x%08x) bytes at %p\n",
	       sizes[slot], sizes[slot], areas[slot]);
	objstore_free(areas[slot]);
	areas[slot] = NULL;
	occupied -= sizes[slot];
      } else {
	rnd = (double)rand()/RAND_MAX;
	newsize = (int)pow(2.0, MAXLOG*pow(rnd,3.0));
	nptr = objstore_realloc(areas[slot], newsize);
	if ( nptr ) {
	  printf("Realloc: %d bytes at %p -> %d bytes at %p\n",
		 sizes[slot], areas[slot], newsize, nptr);
	  occupied += newsize;
	  occupied -= sizes[slot];
	  sizes[slot] = newsize;
	  areas[slot] = nptr;
	  verify_no_overlap(slot);
	} else {
	  printf("Realloc: %d bytes at %p -> %d bytes FAILED\n",
		 sizes[slot], areas[slot], newsize);
	}
      }
    } else {
      rnd = (double)rand()/RAND_MAX;
      sizes[slot] = (int)pow(2.0, MAXLOG*pow(rnd,5.0));
      areas[slot] = objstore_malloc(sizes[slot]);
      printf("Alloc:   %d (0x%08x) bytes at %p\n",
	     sizes[slot], sizes[slot], areas[slot]);
      occupied += sizes[slot];
      verify_no_overlap(slot);
    }

    if ( (i & 255) == 0 ) {
      printf("Arena:   checkpointing...\n");
      objstore_checkpoint(0.1);
    }
    printf("Arena:   %ld bytes officially occupied\n", occupied);
  }

  objstore_checkpoint(0.0);

  return 0;
}