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
|
#ident "$Id$"
/* ----------------------------------------------------------------------- *
*
* Copyright 2000-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 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
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 = 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++ ) {
slot = rand() % SLOTS;
if ( areas[slot] ) {
if ( rand() % 1 ) {
printf("Free: %d (0x%08x) bytes at %p\n",
sizes[slot], sizes[slot], areas[slot]);
lpsm_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 = lpsm_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] = lpsm_malloc(sizes[slot]);
if ( areas[slot] ) {
printf("Alloc: %d (0x%08x) bytes at %p\n",
sizes[slot], sizes[slot], areas[slot]);
occupied += sizes[slot];
} else {
printf("Alloc: %d (0x%08x) bytes FAILED\n",
sizes[slot], sizes[slot]);
}
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;
}
|