summaryrefslogtreecommitdiffstats
path: root/testalloc.c
blob: cefc5c1716bad2bed473f30ed1534b916fc3ebd4 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* ----------------------------------------------------------------------- *
 *   
 *   Copyright 2000-2008 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 Lesser General Public License as
 *   published by the Free Software Foundation, Inc.,
 *   59 Temple Place Ste 330, Boston MA 02111-1307, USA, version 2.1,
 *   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 "lpsm.h"

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

struct area_info {
  void   *ptr;
  size_t size;
  int atseq;
} areas[COUNT];

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

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

      if ( xs < end && xe > start ) {
	printf("OVERLAP ALERT: [%p,%p) -> [%p,%p) (from sequence %d)\n",
	       start, end, xs, xe, areas[i].atseq);
	abort();
      }
    }
  }
}

/* Volatile to make sure it's readily available to the debugger */
volatile int sequence = 0;

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));

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

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

  lpsm_checkpoint(0.0, PSMSYNC_WAIT);

  for ( i = 0 ; i < COUNT ; i++ ) {
    printf("Sequence number: %d\n", ++sequence);

    slot = rand() % SLOTS;

    if ( areas[slot].ptr ) {
      if ( rand() % 1 ) {
	printf("Free:    %zu (0x%zx) bytes at %p\n",
	       areas[slot].size, areas[slot].size, areas[slot].ptr);
	lpsm_free(areas[slot].ptr);
	areas[slot].ptr = NULL;
	occupied -= areas[slot].size;
	areas[slot].atseq = 0;
      } else {
	rnd = (double)rand()/RAND_MAX;
	newsize = (int)pow(2.0, MAXLOG*pow(rnd,3.0));
	nptr = lpsm_realloc(areas[slot].ptr, newsize);
	if ( nptr ) {
	  printf("Realloc: %zu bytes at %p -> %d bytes at %p\n",
		 areas[slot].size, areas[slot].ptr, newsize, nptr);
	  occupied += newsize;
	  occupied -= areas[slot].size;
	  areas[slot].size  = newsize;
	  areas[slot].ptr   = nptr;
	  areas[slot].atseq = sequence;
	  verify_no_overlap(slot);
	} else {
	  printf("Realloc: %zu bytes at %p -> %d bytes FAILED\n",
		 areas[slot].size, areas[slot].ptr, newsize);
	}
      }
    } else {
      rnd = (double)rand()/RAND_MAX;
      areas[slot].size = (int)pow(2.0, MAXLOG*pow(rnd,5.0));
      areas[slot].ptr  = lpsm_malloc(areas[slot].size);
      if ( areas[slot].ptr ) {
	printf("Alloc:   %zu (0x%zx) bytes at %p\n",
	       areas[slot].size, areas[slot].size, areas[slot].ptr);
	occupied += areas[slot].size;
	areas[slot].atseq = sequence;
      } else {
	printf("Alloc:   %zu (0x%zx) bytes FAILED\n",
	       areas[slot].size, areas[slot].size);
      }
      verify_no_overlap(slot);
    }

    if ( (i & 255) == 0 ) {
      printf("Arena:   checkpointing...\n");
      lpsm_checkpoint(0.1, PSMSYNC_SKIP);
    }
    printf("Arena:   %ld bytes officially occupied\n", occupied);
  }
  
  fprintf(stderr, "ready...\n");
  lpsm_checkpoint(0.0, PSMSYNC_SYNC); /* Synchronous checkpoint */

  return 0;
}