2007-03-01 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / config / host-darwin.c
blobd8819515476a7d756469ab76ffc8bfb42f097ece
1 /* Darwin host-specific hook definitions.
2 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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, 51 Franklin Street, Fifth Floor, Boston,
19 MA 02110-1301, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include <sys/mman.h>
25 #include "toplev.h"
26 #include "config/host-darwin.h"
28 /* Yes, this is really supposed to work. */
29 static char pch_address_space[1024*1024*1024] __attribute__((aligned (4096)));
31 /* Return the address of the PCH address space, if the PCH will fit in it. */
33 void *
34 darwin_gt_pch_get_address (size_t sz, int fd ATTRIBUTE_UNUSED)
36 if (sz <= sizeof (pch_address_space))
37 return pch_address_space;
38 else
39 return NULL;
42 /* Check ADDR and SZ for validity, and deallocate (using munmap) that part of
43 pch_address_space beyond SZ. */
45 int
46 darwin_gt_pch_use_address (void *addr, size_t sz, int fd, size_t off)
48 const size_t pagesize = getpagesize();
49 void *mmap_result;
50 int ret;
52 gcc_assert ((size_t)pch_address_space % pagesize == 0
53 && sizeof (pch_address_space) % pagesize == 0);
55 ret = (addr == pch_address_space && sz <= sizeof (pch_address_space));
56 if (! ret)
57 sz = 0;
59 /* Round the size to a whole page size. Normally this is a no-op. */
60 sz = (sz + pagesize - 1) / pagesize * pagesize;
62 if (munmap (pch_address_space + sz, sizeof (pch_address_space) - sz) != 0)
63 fatal_error ("couldn't unmap pch_address_space: %m");
65 if (ret)
67 mmap_result = mmap (addr, sz,
68 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED,
69 fd, off);
71 /* The file might not be mmap-able. */
72 ret = mmap_result != (void *) MAP_FAILED;
74 /* Sanity check for broken MAP_FIXED. */
75 gcc_assert (!ret || mmap_result == addr);
78 return ret;