2
* Copyright (c) 2005 Hewlett-Packard Development Company, L.P.
3
* Original Author: Hans Boehm
5
* This file may be redistributed and/or modified under the
6
* terms of the GNU General Public License as published by the Free Software
7
* Foundation; either version 2, or (at your option) any later version.
9
* It is distributed in the hope that it will be useful, but WITHOUT ANY
10
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
* FOR A PARTICULAR PURPOSE. See the GNU General Public License in the
12
* file doc/COPYING for more details.
15
#if defined(HAVE_CONFIG_H)
19
#include "run_parallel.inc"
23
#include "atomic_ops_malloc.h"
24
#define MAX_NTHREADS 100
25
#define N_REVERSALS 1000 /* must be even */
28
#ifdef USE_STANDARD_MALLOC
29
# define AO_malloc(n) malloc(n)
30
# define AO_free(p) free(p)
31
# define AO_malloc_enable_mmap()
34
typedef struct list_node {
35
struct list_node *next;
39
ln *cons(int d, ln *tail)
41
static size_t extra = 0;
42
size_t my_extra = extra;
51
result = AO_malloc(sizeof(ln) + sizeof(int)*my_extra);
54
fprintf(stderr, "Out of memory\n");
55
/* Normal for more than about 10 threads without mmap? */
60
result -> next = tail;
61
extras = (int *)(result+1);
62
for (i = 0; i < my_extra; ++i) extras[i] = 42;
66
void print_list(ln *l)
70
for (p = l; p != 0; p = p -> next)
72
fprintf(stderr, "%d, ", p -> data);
74
fprintf(stderr, "\n");
77
/* Check that l contains numbers from m to n inclusive in ascending order */
78
void check_list(ln *l, int m, int n)
83
for (p = l, i = m; p != 0; p = p -> next, ++i)
87
fprintf(stderr, "Found %d, expected %d\n", p -> data, i);
93
/* Create a list of integers from m to n */
95
make_list(int m, int n)
98
return cons(m, make_list(m+1, n));
101
/* Reverse list x, and concatenate it to y, deallocating no longer needed */
104
reverse(ln *x, ln *y)
108
if (x == 0) return y;
109
result = reverse(x -> next, cons(x -> data, y));
114
int dummy_test(void) { return 1; }
118
void * run_one_test(void * arg) {
119
ln * x = make_list(1, LENGTH);
121
char *p = AO_malloc(LARGE);
125
fprintf(stderr, "AO_malloc(%d) failed: This is normal without mmap\n",
129
p[0] = p[LARGE/2] = p[LARGE-1] = 'a';
130
q = AO_malloc(LARGE);
131
q[0] = q[LARGE/2] = q[LARGE-1] = 'b';
132
if (p[0] != 'a' || p[LARGE/2] != 'a' || p[LARGE-1] != 'a') {
133
fprintf(stderr, "First large allocation smashed\n");
137
if (q[0] != 'b' || q[LARGE/2] != 'b' || q[LARGE-1] != 'b') {
138
fprintf(stderr, "Second large allocation smashed\n");
143
# if 0 /* enable for debugging */
149
for (i = 0; i < N_REVERSALS; ++i) {
152
check_list(x, 1, LENGTH);
156
int main(int argc, char **argv) {
161
# if !defined(HAVE_MMAP)
166
} else if (2 == argc) {
167
nthreads = atoi(argv[1]);
168
if (nthreads < 1 || nthreads > MAX_NTHREADS) {
169
fprintf(stderr, "Invalid # of threads argument\n");
173
fprintf(stderr, "Usage: %s [# of threads]\n", argv[0]);
176
printf("Performing %d reversals of %d element lists in %d threads\n",
177
N_REVERSALS, LENGTH, nthreads);
178
AO_malloc_enable_mmap();
179
run_parallel(nthreads, run_one_test, dummy_test, "AO_malloc/AO_free");