1 /* OpenBSD host-specific hook definitions.
2 Copyright (C) 2005 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2, or (at your
9 option) any later version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 MA 02111-1307, USA. */
20
21 #include <limits.h>
22 #include <unistd.h>
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "hosthooks.h"
28 #include "hosthooks-def.h"
29
30 #undef HOST_HOOKS_GT_PCH_GET_ADDRESS
31 #define HOST_HOOKS_GT_PCH_GET_ADDRESS openbsd_gt_pch_get_address
32
33 #undef HOST_HOOKS_GT_PCH_USE_ADDRESS
34 #define HOST_HOOKS_GT_PCH_USE_ADDRESS openbsd_gt_pch_use_address
35
36 /* Return the address of the PCH address space, if the PCH will fit in it. */
37
38 void *
openbsd_gt_pch_get_address(size_t size,int fd ATTRIBUTE_UNUSED)39 openbsd_gt_pch_get_address (size_t size, int fd ATTRIBUTE_UNUSED)
40 {
41 void *base, *addr;
42 size_t pgsz;
43
44 if (size > INT_MAX)
45 return NULL;
46
47 pgsz = sysconf(_SC_PAGESIZE);
48 if (pgsz == (size_t)-1)
49 return NULL;
50
51 base = sbrk(0);
52
53 /* round up to nearest page */
54 base = (void *)(((long)base + (pgsz - 1)) & ~(pgsz - 1));
55 if (brk(base) != 0)
56 return NULL;
57
58 /* attempt to allocate size */
59 addr = sbrk(size);
60 if (addr == (void *)-1)
61 return NULL;
62
63 /* deallocate the memory */
64 if (brk(base) != 0)
65 return NULL;
66
67 /* confirm addr is as expected */
68 if (addr != base)
69 return NULL;
70
71 return base;
72 }
73
74 /* Return 0 if we can reserve the PCH address space. */
75
76 int
openbsd_gt_pch_use_address(void * base,size_t size,int fd ATTRIBUTE_UNUSED,size_t off ATTRIBUTE_UNUSED)77 openbsd_gt_pch_use_address (void *base, size_t size, int fd ATTRIBUTE_UNUSED, size_t off ATTRIBUTE_UNUSED)
78 {
79 void *addr;
80
81 if (size == 0)
82 return -1;
83
84 /* sanity check base address */
85 addr = sbrk(0);
86 if (addr == (void *)-1 || base < addr)
87 return -1;
88
89 /* set base for sbrk */
90 if (brk(base) != 0)
91 return -1;
92
93 /* attempt to get the memory */
94 addr = sbrk(size);
95 if (addr == (void *)-1)
96 return -1;
97
98 /* sanity check the result */
99 if (addr != base) {
100 brk(base);
101 return -1;
102 }
103
104 return 0;
105 }
106
107 const struct host_hooks host_hooks = HOST_HOOKS_INITIALIZER;
108