10063 basic support for smatch
[unleashed.git] / usr / src / lib / libmtmalloc / tests / exhaust.c
blob4aa0dc908ec9e2a45dc955b5e9cded716b179620
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 1998-2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <stdio.h>
30 #include "mtmalloc.h"
31 #include <unistd.h>
32 #include <thread.h>
33 #include <sys/types.h>
34 #include <sys/mman.h>
35 #include <fcntl.h>
36 #include <errno.h>
39 * This file tests for swap space exhaustion
41 * cc -O -o exhaust exhaust.c -lmtmalloc
44 void * be_thread(void *);
45 int iwin = 0;
47 main(int argc, char ** argv)
49 int ncpus;
50 thread_t tid[512];
51 int fd;
52 caddr_t stacks[512];
54 srand(getpid());
55 ncpus = sysconf(_SC_NPROCESSORS_CONF);
57 fd = open("/dev/zero", O_RDONLY);
59 if (fd < 0) {
60 perror("open");
61 exit(-1);
64 while (ncpus--)
65 stacks[ncpus] = mmap(0, 1<<23, PROT_READ|PROT_WRITE,
66 MAP_PRIVATE, fd, 0);
68 close(fd);
70 mallocctl(MTCHUNKSIZE, 150);
72 ncpus = sysconf(_SC_NPROCESSORS_CONF);
73 while (ncpus--)
74 thr_create(stacks[ncpus], 1<<23, be_thread, NULL, THR_BOUND,
75 &tid[ncpus]);
77 while (thr_join(NULL, NULL, NULL) == 0);
79 exit(0);
82 /* ARGSUSED */
83 void *
84 be_thread(void *foo)
86 char *p;
88 if (iwin) {
89 printf("why am I here\n");
90 return;
93 if ((p = malloc(rand())) == NULL) {
94 iwin = 1;
95 fprintf(stderr, "Errno is %d\n", errno);
96 perror("malloc");
97 } else {
98 be_thread(NULL);
99 printf("free %p\n", p);
100 free(p);