gnus-util.el (gnus-macroexpand-all): Allow optional argument `environment'.
[emacs.git] / src / w32heap.c
blob39ff101746687c3517167e3d0d0b6425e3ea5c4f
1 /* Heap management routines for GNU Emacs on the Microsoft W32 API.
2 Copyright (C) 1994, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 2008, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 Geoff Voelker (voelker@cs.washington.edu) 7-29-94
24 #include <config.h>
25 #include <stdio.h>
26 #include <setjmp.h>
28 #include "w32heap.h"
29 #include "lisp.h" /* for VALMASK */
31 #define RVA_TO_PTR(rva) ((unsigned char *)((DWORD)(rva) + (DWORD)GetModuleHandle (NULL)))
33 /* This gives us the page size and the size of the allocation unit on NT. */
34 SYSTEM_INFO sysinfo_cache;
36 /* This gives us version, build, and platform identification. */
37 OSVERSIONINFO osinfo_cache;
39 unsigned long syspage_mask = 0;
41 /* The major and minor versions of NT. */
42 int w32_major_version;
43 int w32_minor_version;
44 int w32_build_number;
46 /* Distinguish between Windows NT and Windows 95. */
47 int os_subtype;
49 /* Cache information describing the NT system for later use. */
50 void
51 cache_system_info (void)
53 union
55 struct info
57 char major;
58 char minor;
59 short platform;
60 } info;
61 DWORD data;
62 } version;
64 /* Cache the version of the operating system. */
65 version.data = GetVersion ();
66 w32_major_version = version.info.major;
67 w32_minor_version = version.info.minor;
69 if (version.info.platform & 0x8000)
70 os_subtype = OS_WIN95;
71 else
72 os_subtype = OS_NT;
74 /* Cache page size, allocation unit, processor type, etc. */
75 GetSystemInfo (&sysinfo_cache);
76 syspage_mask = sysinfo_cache.dwPageSize - 1;
78 /* Cache os info. */
79 osinfo_cache.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
80 GetVersionEx (&osinfo_cache);
82 w32_build_number = osinfo_cache.dwBuildNumber;
83 if (os_subtype == OS_WIN95)
84 w32_build_number &= 0xffff;
87 /* Emulate getpagesize. */
88 int
89 getpagesize (void)
91 return sysinfo_cache.dwPageSize;
94 /* Info for managing our preload heap, which is essentially a fixed size
95 data area in the executable. */
96 PIMAGE_SECTION_HEADER preload_heap_section;
98 /* Info for keeping track of our heap. */
99 unsigned char *data_region_base = NULL;
100 unsigned char *data_region_end = NULL;
101 unsigned char *real_data_region_end = NULL;
102 unsigned long reserved_heap_size = 0;
104 /* The start of the data segment. */
105 unsigned char *
106 get_data_start (void)
108 return data_region_base;
111 /* The end of the data segment. */
112 unsigned char *
113 get_data_end (void)
115 return data_region_end;
118 static char *
119 allocate_heap (void)
121 /* Try to get as much as possible of the address range from the end of
122 the preload heap section up to the usable address limit. Since GNU
123 malloc can handle gaps in the memory it gets from sbrk, we can
124 simply set the sbrk pointer to the base of the new heap region. */
125 unsigned long base =
126 ROUND_UP ((RVA_TO_PTR (preload_heap_section->VirtualAddress)
127 + preload_heap_section->Misc.VirtualSize),
128 get_allocation_unit ());
129 unsigned long end = 1 << VALBITS; /* 256MB */
130 void *ptr = NULL;
132 while (!ptr && (base < end))
134 reserved_heap_size = end - base;
135 ptr = VirtualAlloc ((void *) base,
136 get_reserved_heap_size (),
137 MEM_RESERVE,
138 PAGE_NOACCESS);
139 base += 0x00100000; /* 1MB increment */
142 return ptr;
146 /* Emulate Unix sbrk. */
147 void *
148 sbrk (unsigned long increment)
150 void *result;
151 long size = (long) increment;
153 result = data_region_end;
155 /* If size is negative, shrink the heap by decommitting pages. */
156 if (size < 0)
158 int new_size;
159 unsigned char *new_data_region_end;
161 size = -size;
163 /* Sanity checks. */
164 if ((data_region_end - size) < data_region_base)
165 return NULL;
167 /* We can only decommit full pages, so allow for
168 partial deallocation [cga]. */
169 new_data_region_end = (data_region_end - size);
170 new_data_region_end = (unsigned char *)
171 ((long) (new_data_region_end + syspage_mask) & ~syspage_mask);
172 new_size = real_data_region_end - new_data_region_end;
173 real_data_region_end = new_data_region_end;
174 if (new_size > 0)
176 /* Decommit size bytes from the end of the heap. */
177 if (using_dynamic_heap
178 && !VirtualFree (real_data_region_end, new_size, MEM_DECOMMIT))
179 return NULL;
182 data_region_end -= size;
184 /* If size is positive, grow the heap by committing reserved pages. */
185 else if (size > 0)
187 /* Sanity checks. */
188 if ((data_region_end + size) >
189 (data_region_base + get_reserved_heap_size ()))
190 return NULL;
192 /* Commit more of our heap. */
193 if (using_dynamic_heap
194 && VirtualAlloc (data_region_end, size, MEM_COMMIT,
195 PAGE_READWRITE) == NULL)
196 return NULL;
197 data_region_end += size;
199 /* We really only commit full pages, so record where
200 the real end of committed memory is [cga]. */
201 real_data_region_end = (unsigned char *)
202 ((long) (data_region_end + syspage_mask) & ~syspage_mask);
205 return result;
208 /* Initialize the internal heap variables used by sbrk. When running in
209 preload phase (ie. in the undumped executable), we rely entirely on a
210 fixed size heap section included in the .exe itself; this is
211 preserved during dumping, and truncated to the size actually used.
213 When running in the dumped executable, we reserve as much as possible
214 of the address range that is addressable by Lisp object pointers, to
215 supplement what is left of the preload heap. Although we cannot rely
216 on the dynamically allocated arena being contiguous with the static
217 heap area, it is not a problem because sbrk can pretend that the gap
218 was allocated by something else; GNU malloc detects when there is a
219 jump in the sbrk values, and starts a new heap block. */
220 void
221 init_heap (void)
223 PIMAGE_DOS_HEADER dos_header;
224 PIMAGE_NT_HEADERS nt_header;
226 dos_header = (PIMAGE_DOS_HEADER) RVA_TO_PTR (0);
227 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
228 dos_header->e_lfanew);
229 preload_heap_section = find_section ("EMHEAP", nt_header);
231 if (using_dynamic_heap)
233 data_region_base = allocate_heap ();
234 if (!data_region_base)
236 printf ("Error: Could not reserve dynamic heap area.\n");
237 exit (1);
240 #if !defined (USE_LISP_UNION_TYPE) && !defined (USE_LSB_TAG)
241 /* Ensure that the addresses don't use the upper tag bits since
242 the Lisp type goes there. */
243 if (((unsigned long) data_region_base & ~VALMASK) != 0)
245 printf ("Error: The heap was allocated in upper memory.\n");
246 exit (1);
248 #endif
249 data_region_end = data_region_base;
250 real_data_region_end = data_region_end;
252 else
254 data_region_base = RVA_TO_PTR (preload_heap_section->VirtualAddress);
255 data_region_end = data_region_base;
256 real_data_region_end = data_region_end;
257 reserved_heap_size = preload_heap_section->Misc.VirtualSize;
260 /* Update system version information to match current system. */
261 cache_system_info ();
264 /* Round the heap up to the given alignment. */
265 void
266 round_heap (unsigned long align)
268 unsigned long needs_to_be;
269 unsigned long need_to_alloc;
271 needs_to_be = (unsigned long) ROUND_UP (get_heap_end (), align);
272 need_to_alloc = needs_to_be - (unsigned long) get_heap_end ();
274 if (need_to_alloc)
275 sbrk (need_to_alloc);
278 #if (_MSC_VER >= 1000 && _MSC_VER < 1300 && !defined (USE_CRT_DLL))
280 /* MSVC 4.2 invokes these functions from mainCRTStartup to initialize
281 a heap via HeapCreate. They are normally defined by the runtime,
282 but we override them here so that the unnecessary HeapCreate call
283 is not performed. */
285 int __cdecl
286 _heap_init (void)
288 /* Stepping through the assembly indicates that mainCRTStartup is
289 expecting a nonzero success return value. */
290 return 1;
293 void __cdecl
294 _heap_term (void)
296 return;
299 #endif