OSDN Git Service

2007-04-16 H.J. Lu <hongjiu.lu@intel.com>
[pf3gnuchains/gcc-fork.git] / gcc / config / i386 / host-mingw32.c
1 /* mingw32 host-specific hook definitions.
2    Copyright (C) 2004 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, 51 Franklin Street, Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "hosthooks.h"
25 #include "hosthooks-def.h"
26 #include "toplev.h"
27 #include "diagnostic.h"
28
29
30 #define WIN32_LEAN_AND_MEAN  /* Not so important if we have windows.h.gch.  */
31 #include <windows.h>
32
33 static void * mingw32_gt_pch_get_address (size_t, int);
34 static int mingw32_gt_pch_use_address (void *, size_t, int, size_t);
35 static size_t mingw32_gt_pch_alloc_granularity (void);
36
37 #undef HOST_HOOKS_GT_PCH_GET_ADDRESS
38 #define HOST_HOOKS_GT_PCH_GET_ADDRESS mingw32_gt_pch_get_address
39 #undef HOST_HOOKS_GT_PCH_USE_ADDRESS
40 #define HOST_HOOKS_GT_PCH_USE_ADDRESS mingw32_gt_pch_use_address
41 #undef HOST_HOOKS_GT_PCH_ALLOC_GRANULARITY
42 #define HOST_HOOKS_GT_PCH_ALLOC_GRANULARITY mingw32_gt_pch_alloc_granularity
43
44 static inline void w32_error(const char*, const char*, int, const char*);
45
46 /* FIXME: Is this big enough?  */
47 static const size_t pch_VA_max_size  = 128 * 1024 * 1024;
48
49 /* Granularity for reserving address space.  */
50 static const size_t va_granularity = 0x10000;
51
52 /* Print out the GetLastError() translation.  */ 
53 static inline void
54 w32_error (const char* function, const char* file, int line,
55            const char* my_msg)
56 {
57   LPSTR w32_msgbuf;
58   FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER
59                   | FORMAT_MESSAGE_FROM_SYSTEM
60                   | FORMAT_MESSAGE_IGNORE_INSERTS
61                   | FORMAT_MESSAGE_MAX_WIDTH_MASK,
62                   NULL, GetLastError(),
63                   MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
64                   (LPSTR) &w32_msgbuf, 0, NULL);
65   fprintf(stderr, "internal error in %s, at %s:%d: %s: %s\n",
66           function, trim_filename (file), line, my_msg, w32_msgbuf);
67   LocalFree ((HLOCAL)w32_msgbuf);
68 }
69
70 /* Granularity for reserving address space.  */
71 static size_t mingw32_gt_pch_alloc_granularity (void)
72 {
73   return va_granularity;
74 }
75
76 /* Identify an address that's likely to be free in a subsequent invocation
77    of the compiler.  The area should be able to hold SIZE bytes.  FD is an
78    open file descriptor if the host would like to probe with mmap.  */
79
80 static void *
81 mingw32_gt_pch_get_address (size_t size, int fd  ATTRIBUTE_UNUSED)
82 {
83   void* res;
84   size = (size + va_granularity - 1) & ~(va_granularity - 1);
85   if (size > pch_VA_max_size)
86     return NULL;
87
88   /* FIXME: We let system determine base by setting first arg to NULL.
89      Allocating at top of available address space avoids unnecessary
90      fragmentation of "ordinary" (malloc's)  address space but may not be safe
91      with delayed load of system dll's. Preferred addresses for NT system
92      dlls is in 0x70000000 to 0x78000000 range.
93      If we allocate at bottom we need to reserve the address as early as possible
94      and at the same point in each invocation. */
95  
96   res = VirtualAlloc (NULL, pch_VA_max_size,
97                       MEM_RESERVE | MEM_TOP_DOWN,
98                       PAGE_NOACCESS);
99   if (!res)
100     w32_error (__FUNCTION__, __FILE__, __LINE__, "VirtualAlloc");
101   else
102     /* We do not need the address space for now, so free it.  */
103     VirtualFree (res, 0, MEM_RELEASE);
104
105   return res; 
106 }
107
108 /* ADDR is an address returned by gt_pch_get_address.  Attempt to allocate
109    SIZE bytes at the same address and load it with the data from FD at 
110    OFFSET.  Return -1 if we couldn't allocate memory at ADDR, return 0
111    if the memory is allocated but the data not loaded, return 1 if done.  */
112
113 static int
114 mingw32_gt_pch_use_address (void *addr, size_t size, int fd,
115                             size_t offset)
116 {
117   void * mmap_addr;
118   static HANDLE mmap_handle;
119
120   if (size == 0)
121     return 0;
122   
123   /* Offset must be also be a multiple of allocation granularity for
124      this to work.  We can't change the offset. */ 
125   if ((offset & (va_granularity - 1)) != 0 || size > pch_VA_max_size)
126     return -1;
127
128   mmap_handle = CreateFileMapping ((HANDLE) _get_osfhandle (fd),
129                                    NULL, PAGE_WRITECOPY | SEC_COMMIT,
130                                    0, 0,  NULL);
131   if (mmap_handle == NULL)
132     {
133       w32_error (__FUNCTION__,  __FILE__, __LINE__, "CreateFileMapping");
134       return -1; 
135     }
136   mmap_addr = MapViewOfFileEx (mmap_handle, FILE_MAP_COPY, 0, offset,
137                                size, addr);
138   if (mmap_addr != addr)
139     {
140       w32_error (__FUNCTION__, __FILE__, __LINE__, "MapViewOfFileEx");
141       CloseHandle(mmap_handle);
142       return  -1;
143     }
144
145   return 1;
146 }
147
148 const struct host_hooks host_hooks = HOST_HOOKS_INITIALIZER;