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"
26 # define MAX_NTHREADS 100
29 #ifndef DEFAULT_NTHREADS
31 # define DEFAULT_NTHREADS 10
33 # define DEFAULT_NTHREADS 3
38 # define N_REVERSALS 1000 /* must be even */
42 # define LIST_LENGTH 1000
45 #ifndef LARGE_OBJ_SIZE
46 # define LARGE_OBJ_SIZE 200000
49 #ifdef USE_STANDARD_MALLOC
50 # define AO_malloc(n) malloc(n)
51 # define AO_free(p) free(p)
52 # define AO_malloc_enable_mmap()
55 typedef struct list_node {
56 struct list_node *next;
60 ln *cons(int d, ln *tail)
62 static size_t extra = 0;
63 size_t my_extra = extra;
72 result = AO_malloc(sizeof(ln) + sizeof(int)*my_extra);
75 fprintf(stderr, "Out of memory\n");
76 /* Normal for more than about 10 threads without mmap? */
81 result -> next = tail;
82 extras = (int *)(result+1);
83 for (i = 0; i < my_extra; ++i) extras[i] = 42;
87 void print_list(ln *l)
91 for (p = l; p != 0; p = p -> next)
93 fprintf(stderr, "%d, ", p -> data);
95 fprintf(stderr, "\n");
98 /* Check that l contains numbers from m to n inclusive in ascending order */
99 void check_list(ln *l, int m, int n)
104 for (p = l, i = m; p != 0; p = p -> next, ++i)
108 fprintf(stderr, "Found %d, expected %d\n", p -> data, i);
114 /* Create a list of integers from m to n */
116 make_list(int m, int n)
119 return cons(m, make_list(m+1, n));
122 /* Reverse list x, and concatenate it to y, deallocating no longer needed */
125 reverse(ln *x, ln *y)
129 if (x == 0) return y;
130 result = reverse(x -> next, cons(x -> data, y));
135 int dummy_test(void) { return 1; }
137 void * run_one_test(void * arg) {
138 ln * x = make_list(1, LIST_LENGTH);
140 char *p = AO_malloc(LARGE_OBJ_SIZE);
145 fprintf(stderr, "AO_malloc(%d) failed\n", LARGE_OBJ_SIZE);
147 fprintf(stderr, "AO_malloc(%d) failed: This is normal without mmap\n",
151 p[0] = p[LARGE_OBJ_SIZE/2] = p[LARGE_OBJ_SIZE-1] = 'a';
152 q = AO_malloc(LARGE_OBJ_SIZE);
155 fprintf(stderr, "Out of memory\n");
156 /* Normal for more than about 10 threads without mmap? */
159 q[0] = q[LARGE_OBJ_SIZE/2] = q[LARGE_OBJ_SIZE-1] = 'b';
160 if (p[0] != 'a' || p[LARGE_OBJ_SIZE/2] != 'a'
161 || p[LARGE_OBJ_SIZE-1] != 'a') {
162 fprintf(stderr, "First large allocation smashed\n");
166 if (q[0] != 'b' || q[LARGE_OBJ_SIZE/2] != 'b'
167 || q[LARGE_OBJ_SIZE-1] != 'b') {
168 fprintf(stderr, "Second large allocation smashed\n");
173 # ifdef DEBUG_RUN_ONE_TEST
179 for (i = 0; i < N_REVERSALS; ++i) {
182 check_list(x, 1, LIST_LENGTH);
186 int main(int argc, char **argv) {
190 nthreads = DEFAULT_NTHREADS;
191 } else if (2 == argc) {
192 nthreads = atoi(argv[1]);
193 if (nthreads < 1 || nthreads > MAX_NTHREADS) {
194 fprintf(stderr, "Invalid # of threads argument\n");
198 fprintf(stderr, "Usage: %s [# of threads]\n", argv[0]);
201 printf("Performing %d reversals of %d element lists in %d threads\n",
202 N_REVERSALS, LIST_LENGTH, nthreads);
203 AO_malloc_enable_mmap();
204 run_parallel(nthreads, run_one_test, dummy_test, "AO_malloc/AO_free");