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 */
43 # define LIST_LENGTH 1000
45 # define LIST_LENGTH 100
49 #ifndef LARGE_OBJ_SIZE
51 # define LARGE_OBJ_SIZE 200000
53 # define LARGE_OBJ_SIZE 20000
57 #ifdef USE_STANDARD_MALLOC
58 # define AO_malloc(n) malloc(n)
59 # define AO_free(p) free(p)
60 # define AO_malloc_enable_mmap()
63 typedef struct list_node {
64 struct list_node *next;
68 ln *cons(int d, ln *tail)
70 static size_t extra = 0;
71 size_t my_extra = extra;
80 result = AO_malloc(sizeof(ln) + sizeof(int)*my_extra);
83 fprintf(stderr, "Out of memory\n");
84 /* Normal for more than about 10 threads without mmap? */
89 result -> next = tail;
90 extras = (int *)(result+1);
91 for (i = 0; i < my_extra; ++i) extras[i] = 42;
95 void print_list(ln *l)
99 for (p = l; p != 0; p = p -> next)
101 fprintf(stderr, "%d, ", p -> data);
103 fprintf(stderr, "\n");
106 /* Check that l contains numbers from m to n inclusive in ascending order */
107 void check_list(ln *l, int m, int n)
112 for (p = l, i = m; p != 0; p = p -> next, ++i)
116 fprintf(stderr, "Found %d, expected %d\n", p -> data, i);
122 /* Create a list of integers from m to n */
124 make_list(int m, int n)
127 return cons(m, make_list(m+1, n));
130 /* Reverse list x, and concatenate it to y, deallocating no longer needed */
133 reverse(ln *x, ln *y)
137 if (x == 0) return y;
138 result = reverse(x -> next, cons(x -> data, y));
143 int dummy_test(void) { return 1; }
145 void * run_one_test(void * arg) {
146 ln * x = make_list(1, LIST_LENGTH);
148 char *p = AO_malloc(LARGE_OBJ_SIZE);
153 fprintf(stderr, "AO_malloc(%d) failed\n", LARGE_OBJ_SIZE);
155 fprintf(stderr, "AO_malloc(%d) failed: This is normal without mmap\n",
159 p[0] = p[LARGE_OBJ_SIZE/2] = p[LARGE_OBJ_SIZE-1] = 'a';
160 q = AO_malloc(LARGE_OBJ_SIZE);
163 fprintf(stderr, "Out of memory\n");
164 /* Normal for more than about 10 threads without mmap? */
167 q[0] = q[LARGE_OBJ_SIZE/2] = q[LARGE_OBJ_SIZE-1] = 'b';
168 if (p[0] != 'a' || p[LARGE_OBJ_SIZE/2] != 'a'
169 || p[LARGE_OBJ_SIZE-1] != 'a') {
170 fprintf(stderr, "First large allocation smashed\n");
174 if (q[0] != 'b' || q[LARGE_OBJ_SIZE/2] != 'b'
175 || q[LARGE_OBJ_SIZE-1] != 'b') {
176 fprintf(stderr, "Second large allocation smashed\n");
181 # ifdef DEBUG_RUN_ONE_TEST
187 for (i = 0; i < N_REVERSALS; ++i) {
190 check_list(x, 1, LIST_LENGTH);
194 int main(int argc, char **argv) {
198 nthreads = DEFAULT_NTHREADS;
199 } else if (2 == argc) {
200 nthreads = atoi(argv[1]);
201 if (nthreads < 1 || nthreads > MAX_NTHREADS) {
202 fprintf(stderr, "Invalid # of threads argument\n");
206 fprintf(stderr, "Usage: %s [# of threads]\n", argv[0]);
209 printf("Performing %d reversals of %d element lists in %d threads\n",
210 N_REVERSALS, LIST_LENGTH, nthreads);
211 AO_malloc_enable_mmap();
212 run_parallel(nthreads, run_one_test, dummy_test, "AO_malloc/AO_free");