* display.texi (Face Attributes): Document negative line widths (Bug#6113).
[emacs.git] / src / w32heap.c
blob91bc8133b20078d7e29771a091148fb5dbeae9b1
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, 2011 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 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <setjmp.h>
32 #include "w32heap.h"
33 #include "lisp.h" /* for VALMASK */
35 #define RVA_TO_PTR(rva) ((unsigned char *)((DWORD)(rva) + (DWORD)GetModuleHandle (NULL)))
37 /* This gives us the page size and the size of the allocation unit on NT. */
38 SYSTEM_INFO sysinfo_cache;
40 /* This gives us version, build, and platform identification. */
41 OSVERSIONINFO osinfo_cache;
43 unsigned long syspage_mask = 0;
45 /* The major and minor versions of NT. */
46 int w32_major_version;
47 int w32_minor_version;
48 int w32_build_number;
50 /* Distinguish between Windows NT and Windows 95. */
51 int os_subtype;
53 /* Cache information describing the NT system for later use. */
54 void
55 cache_system_info (void)
57 union
59 struct info
61 char major;
62 char minor;
63 short platform;
64 } info;
65 DWORD data;
66 } version;
68 /* Cache the version of the operating system. */
69 version.data = GetVersion ();
70 w32_major_version = version.info.major;
71 w32_minor_version = version.info.minor;
73 if (version.info.platform & 0x8000)
74 os_subtype = OS_WIN95;
75 else
76 os_subtype = OS_NT;
78 /* Cache page size, allocation unit, processor type, etc. */
79 GetSystemInfo (&sysinfo_cache);
80 syspage_mask = sysinfo_cache.dwPageSize - 1;
82 /* Cache os info. */
83 osinfo_cache.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
84 GetVersionEx (&osinfo_cache);
86 w32_build_number = osinfo_cache.dwBuildNumber;
87 if (os_subtype == OS_WIN95)
88 w32_build_number &= 0xffff;
91 /* Emulate getpagesize. */
92 int
93 getpagesize (void)
95 return sysinfo_cache.dwPageSize;
98 /* Info for managing our preload heap, which is essentially a fixed size
99 data area in the executable. */
100 PIMAGE_SECTION_HEADER preload_heap_section;
102 /* Info for keeping track of our heap. */
103 unsigned char *data_region_base = NULL;
104 unsigned char *data_region_end = NULL;
105 unsigned char *real_data_region_end = NULL;
106 unsigned long reserved_heap_size = 0;
108 /* The start of the data segment. */
109 unsigned char *
110 get_data_start (void)
112 return data_region_base;
115 /* The end of the data segment. */
116 unsigned char *
117 get_data_end (void)
119 return data_region_end;
122 #if !defined (USE_LISP_UNION_TYPE) && !defined (USE_LSB_TAG)
123 static char *
124 allocate_heap (void)
126 /* Try to get as much as possible of the address range from the end of
127 the preload heap section up to the usable address limit. Since GNU
128 malloc can handle gaps in the memory it gets from sbrk, we can
129 simply set the sbrk pointer to the base of the new heap region. */
130 unsigned long base =
131 ROUND_UP ((RVA_TO_PTR (preload_heap_section->VirtualAddress)
132 + preload_heap_section->Misc.VirtualSize),
133 get_allocation_unit ());
134 unsigned long end = 1 << VALBITS; /* 256MB */
135 void *ptr = NULL;
137 while (!ptr && (base < end))
139 reserved_heap_size = end - base;
140 ptr = VirtualAlloc ((void *) base,
141 get_reserved_heap_size (),
142 MEM_RESERVE,
143 PAGE_NOACCESS);
144 base += 0x00100000; /* 1MB increment */
147 return ptr;
149 #else /* USE_LISP_UNION_TYPE || USE_LSB_TAG */
150 static char *
151 allocate_heap (void)
153 unsigned long size = 0x80000000; /* start by asking for 2GB */
154 void *ptr = NULL;
156 while (!ptr && size > 0x00100000)
158 reserved_heap_size = size;
159 ptr = VirtualAlloc (NULL,
160 get_reserved_heap_size (),
161 MEM_RESERVE,
162 PAGE_NOACCESS);
163 size -= 0x00800000; /* if failed, decrease request by 8MB */
166 return ptr;
168 #endif /* USE_LISP_UNION_TYPE || USE_LSB_TAG */
171 /* Emulate Unix sbrk. Note that ralloc.c expects the return value to
172 be the address of the _start_ (not end) of the new block in case of
173 success, and zero (not -1) in case of failure. */
174 void *
175 sbrk (unsigned long increment)
177 void *result;
178 long size = (long) increment;
180 result = data_region_end;
182 /* If size is negative, shrink the heap by decommitting pages. */
183 if (size < 0)
185 int new_size;
186 unsigned char *new_data_region_end;
188 size = -size;
190 /* Sanity checks. */
191 if ((data_region_end - size) < data_region_base)
192 return NULL;
194 /* We can only decommit full pages, so allow for
195 partial deallocation [cga]. */
196 new_data_region_end = (data_region_end - size);
197 new_data_region_end = (unsigned char *)
198 ((long) (new_data_region_end + syspage_mask) & ~syspage_mask);
199 new_size = real_data_region_end - new_data_region_end;
200 real_data_region_end = new_data_region_end;
201 if (new_size > 0)
203 /* Decommit size bytes from the end of the heap. */
204 if (using_dynamic_heap
205 && !VirtualFree (real_data_region_end, new_size, MEM_DECOMMIT))
206 return NULL;
209 data_region_end -= size;
211 /* If size is positive, grow the heap by committing reserved pages. */
212 else if (size > 0)
214 /* Sanity checks. */
215 if ((data_region_end + size) >
216 (data_region_base + get_reserved_heap_size ()))
217 return NULL;
219 /* Commit more of our heap. */
220 if (using_dynamic_heap
221 && VirtualAlloc (data_region_end, size, MEM_COMMIT,
222 PAGE_READWRITE) == NULL)
223 return NULL;
224 data_region_end += size;
226 /* We really only commit full pages, so record where
227 the real end of committed memory is [cga]. */
228 real_data_region_end = (unsigned char *)
229 ((long) (data_region_end + syspage_mask) & ~syspage_mask);
232 return result;
235 /* Initialize the internal heap variables used by sbrk. When running in
236 preload phase (ie. in the undumped executable), we rely entirely on a
237 fixed size heap section included in the .exe itself; this is
238 preserved during dumping, and truncated to the size actually used.
240 When running in the dumped executable, we reserve as much as possible
241 of the address range that is addressable by Lisp object pointers, to
242 supplement what is left of the preload heap. Although we cannot rely
243 on the dynamically allocated arena being contiguous with the static
244 heap area, it is not a problem because sbrk can pretend that the gap
245 was allocated by something else; GNU malloc detects when there is a
246 jump in the sbrk values, and starts a new heap block. */
247 void
248 init_heap ()
250 PIMAGE_DOS_HEADER dos_header;
251 PIMAGE_NT_HEADERS nt_header;
253 dos_header = (PIMAGE_DOS_HEADER) RVA_TO_PTR (0);
254 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
255 dos_header->e_lfanew);
256 preload_heap_section = find_section ("EMHEAP", nt_header);
258 if (using_dynamic_heap)
260 data_region_base = allocate_heap ();
261 if (!data_region_base)
263 printf ("Error: Could not reserve dynamic heap area.\n");
264 exit (1);
267 #if !defined (USE_LISP_UNION_TYPE) && !defined (USE_LSB_TAG)
268 /* Ensure that the addresses don't use the upper tag bits since
269 the Lisp type goes there. */
270 if (((unsigned long) data_region_base & ~VALMASK) != 0)
272 printf ("Error: The heap was allocated in upper memory.\n");
273 exit (1);
275 #endif
276 data_region_end = data_region_base;
277 real_data_region_end = data_region_end;
279 else
281 data_region_base = RVA_TO_PTR (preload_heap_section->VirtualAddress);
282 data_region_end = data_region_base;
283 real_data_region_end = data_region_end;
284 reserved_heap_size = preload_heap_section->Misc.VirtualSize;
287 /* Update system version information to match current system. */
288 cache_system_info ();
291 /* Round the heap up to the given alignment. */
292 void
293 round_heap (unsigned long align)
295 unsigned long needs_to_be;
296 unsigned long need_to_alloc;
298 needs_to_be = (unsigned long) ROUND_UP (get_heap_end (), align);
299 need_to_alloc = needs_to_be - (unsigned long) get_heap_end ();
301 if (need_to_alloc)
302 sbrk (need_to_alloc);
305 #if (_MSC_VER >= 1000 && _MSC_VER < 1300 && !defined (USE_CRT_DLL))
307 /* MSVC 4.2 invokes these functions from mainCRTStartup to initialize
308 a heap via HeapCreate. They are normally defined by the runtime,
309 but we override them here so that the unnecessary HeapCreate call
310 is not performed. */
312 int __cdecl
313 _heap_init (void)
315 /* Stepping through the assembly indicates that mainCRTStartup is
316 expecting a nonzero success return value. */
317 return 1;
320 void __cdecl
321 _heap_term (void)
323 return;
326 #endif
328 /* arch-tag: 9a6a9860-040d-422d-8905-450dd535cd9c
329 (do not change this comment) */