1169689Skan/* Darwin host-specific hook definitions.
2169689Skan   Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3169689Skan
4169689Skan   This file is part of GCC.
5169689Skan
6169689Skan   GCC is free software; you can redistribute it and/or modify it
7169689Skan   under the terms of the GNU General Public License as published
8169689Skan   by the Free Software Foundation; either version 2, or (at your
9169689Skan   option) any later version.
10169689Skan
11169689Skan   GCC is distributed in the hope that it will be useful, but WITHOUT
12169689Skan   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13169689Skan   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
14169689Skan   License for more details.
15169689Skan
16169689Skan   You should have received a copy of the GNU General Public License
17169689Skan   along with GCC; see the file COPYING.  If not, write to the
18169689Skan   Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
19169689Skan   MA 02110-1301, USA.  */
20169689Skan
21169689Skan#include "config.h"
22169689Skan#include "system.h"
23169689Skan#include "coretypes.h"
24169689Skan#include <sys/mman.h>
25169689Skan#include "toplev.h"
26169689Skan#include "config/host-darwin.h"
27169689Skan
28169689Skan/* Yes, this is really supposed to work.  */
29169689Skanstatic char pch_address_space[1024*1024*1024] __attribute__((aligned (4096)));
30169689Skan
31169689Skan/* Return the address of the PCH address space, if the PCH will fit in it.  */
32169689Skan
33169689Skanvoid *
34169689Skandarwin_gt_pch_get_address (size_t sz, int fd ATTRIBUTE_UNUSED)
35169689Skan{
36169689Skan  if (sz <= sizeof (pch_address_space))
37169689Skan    return pch_address_space;
38169689Skan  else
39169689Skan    return NULL;
40169689Skan}
41169689Skan
42169689Skan/* Check ADDR and SZ for validity, and deallocate (using munmap) that part of
43169689Skan   pch_address_space beyond SZ.  */
44169689Skan
45169689Skanint
46169689Skandarwin_gt_pch_use_address (void *addr, size_t sz, int fd, size_t off)
47169689Skan{
48169689Skan  const size_t pagesize = getpagesize();
49169689Skan  void *mmap_result;
50169689Skan  int ret;
51169689Skan
52169689Skan  gcc_assert ((size_t)pch_address_space % pagesize == 0
53169689Skan	      && sizeof (pch_address_space) % pagesize == 0);
54169689Skan
55169689Skan  ret = (addr == pch_address_space && sz <= sizeof (pch_address_space));
56169689Skan  if (! ret)
57169689Skan    sz = 0;
58169689Skan
59169689Skan  /* Round the size to a whole page size.  Normally this is a no-op.  */
60169689Skan  sz = (sz + pagesize - 1) / pagesize * pagesize;
61169689Skan
62169689Skan  if (munmap (pch_address_space + sz, sizeof (pch_address_space) - sz) != 0)
63169689Skan    fatal_error ("couldn't unmap pch_address_space: %m");
64169689Skan
65169689Skan  if (ret)
66169689Skan    {
67169689Skan      mmap_result = mmap (addr, sz,
68169689Skan			  PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED,
69169689Skan			  fd, off);
70169689Skan
71169689Skan      /* The file might not be mmap-able.  */
72169689Skan      ret = mmap_result != (void *) MAP_FAILED;
73169689Skan
74169689Skan      /* Sanity check for broken MAP_FIXED.  */
75169689Skan      gcc_assert (!ret || mmap_result == addr);
76169689Skan    }
77169689Skan
78169689Skan  return ret;
79169689Skan}
80