Utility predicates for packing: UNBOUNDED-SC-P and UNBOUNDED-TN-P
[sbcl.git] / src / runtime / trymap.c
1 /*
2  * a utility for experimenting with mmap()-at-absolute-address-ranges
3  * as a crude way of checking whether it's reasonable for SBCL to
4  * reserve those address ranges for itself
5  */
6
7 /*
8  * This software is part of the SBCL system. See the README file for
9  * more information.
10  *
11  * This software is derived from the CMU CL system, which was
12  * written at Carnegie Mellon University and released into the
13  * public domain. The software is in the public domain and is
14  * provided with absolutely no warranty. See the COPYING and CREDITS
15  * files for more information.
16  */
17
18 #include <stdio.h>
19 #include <sys/types.h>
20 #include <sys/mman.h>
21
22 long
23 hexparse(char *s)
24 {
25     long result;
26     if (1 != sscanf(s, "%lx", &result)) {
27         fprintf(stderr, "can't parse \"%s\" as hexadecimal integer\n", s);
28         exit(1);
29     }
30     return result;
31 }
32
33 int
34 main(int argc, char *argv[])
35 {
36     char *addr;
37     char *requested_addr;
38
39     /* FIXME: It would be nice to make the no-command-line-arguments
40      * case of this program automatically check all the spaces that
41      * SBCL likes to map. Then we could execute this program as a
42      * sanity check in make-target-2.sh before we try to execute sbcl
43      * itself. */
44     if (argc != 3) {
45         fprintf(stderr, "usage: %s $addr $size\n", argv[0]);
46         exit(1);
47     }
48
49     requested_addr = (char*)hexparse(argv[1]);
50     addr = mmap(requested_addr,
51                 hexparse(argv[2]),
52                 0x7,
53                 MAP_PRIVATE | MAP_ANON | MAP_FIXED,
54                 -1,
55                 0);
56
57     /* FIXME: It would be nice to make this a stronger test. E.g.
58      * besides just trying to mmap() the area, we could check that the
59      * area is not already mapped (perhaps by checking that attempts
60      * to reference the to-be-mapped area cause SIGSEGV or SIGBUS).
61      * (At least on OpenBSD, "A successful mmap deletes any previous
62      * mapping in the allocated address range.") */
63     if (addr != requested_addr) {
64         perror("mmap");
65     }
66
67     exit(0);
68 }