Merge changes from emacs-23 branch.
[emacs.git] / src / w32heap.c
blob285325e3f8baa6858ad4465e8410e7468d341660
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 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
28 #include <stdio.h>
29 #include <setjmp.h>
31 #include "w32heap.h"
32 #include "lisp.h" /* for VALMASK */
34 #define RVA_TO_PTR(rva) ((unsigned char *)((DWORD)(rva) + (DWORD)GetModuleHandle (NULL)))
36 /* This gives us the page size and the size of the allocation unit on NT. */
37 SYSTEM_INFO sysinfo_cache;
39 /* This gives us version, build, and platform identification. */
40 OSVERSIONINFO osinfo_cache;
42 unsigned long syspage_mask = 0;
44 /* The major and minor versions of NT. */
45 int w32_major_version;
46 int w32_minor_version;
47 int w32_build_number;
49 /* Distinguish between Windows NT and Windows 95. */
50 int os_subtype;
52 /* Cache information describing the NT system for later use. */
53 void
54 cache_system_info (void)
56 union
58 struct info
60 char major;
61 char minor;
62 short platform;
63 } info;
64 DWORD data;
65 } version;
67 /* Cache the version of the operating system. */
68 version.data = GetVersion ();
69 w32_major_version = version.info.major;
70 w32_minor_version = version.info.minor;
72 if (version.info.platform & 0x8000)
73 os_subtype = OS_WIN95;
74 else
75 os_subtype = OS_NT;
77 /* Cache page size, allocation unit, processor type, etc. */
78 GetSystemInfo (&sysinfo_cache);
79 syspage_mask = sysinfo_cache.dwPageSize - 1;
81 /* Cache os info. */
82 osinfo_cache.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
83 GetVersionEx (&osinfo_cache);
85 w32_build_number = osinfo_cache.dwBuildNumber;
86 if (os_subtype == OS_WIN95)
87 w32_build_number &= 0xffff;
90 /* Emulate getpagesize. */
91 int
92 getpagesize (void)
94 return sysinfo_cache.dwPageSize;
97 /* Info for managing our preload heap, which is essentially a fixed size
98 data area in the executable. */
99 PIMAGE_SECTION_HEADER preload_heap_section;
101 /* Info for keeping track of our heap. */
102 unsigned char *data_region_base = NULL;
103 unsigned char *data_region_end = NULL;
104 unsigned char *real_data_region_end = NULL;
105 unsigned long reserved_heap_size = 0;
107 /* The start of the data segment. */
108 unsigned char *
109 get_data_start (void)
111 return data_region_base;
114 /* The end of the data segment. */
115 unsigned char *
116 get_data_end (void)
118 return data_region_end;
121 static char *
122 allocate_heap (void)
124 /* Try to get as much as possible of the address range from the end of
125 the preload heap section up to the usable address limit. Since GNU
126 malloc can handle gaps in the memory it gets from sbrk, we can
127 simply set the sbrk pointer to the base of the new heap region. */
128 unsigned long base =
129 ROUND_UP ((RVA_TO_PTR (preload_heap_section->VirtualAddress)
130 + preload_heap_section->Misc.VirtualSize),
131 get_allocation_unit ());
132 unsigned long end = 1 << VALBITS; /* 256MB */
133 void *ptr = NULL;
135 while (!ptr && (base < end))
137 reserved_heap_size = end - base;
138 ptr = VirtualAlloc ((void *) base,
139 get_reserved_heap_size (),
140 MEM_RESERVE,
141 PAGE_NOACCESS);
142 base += 0x00100000; /* 1MB increment */
145 return ptr;
149 /* Emulate Unix sbrk. */
150 void *
151 sbrk (unsigned long increment)
153 void *result;
154 long size = (long) increment;
156 result = data_region_end;
158 /* If size is negative, shrink the heap by decommitting pages. */
159 if (size < 0)
161 int new_size;
162 unsigned char *new_data_region_end;
164 size = -size;
166 /* Sanity checks. */
167 if ((data_region_end - size) < data_region_base)
168 return NULL;
170 /* We can only decommit full pages, so allow for
171 partial deallocation [cga]. */
172 new_data_region_end = (data_region_end - size);
173 new_data_region_end = (unsigned char *)
174 ((long) (new_data_region_end + syspage_mask) & ~syspage_mask);
175 new_size = real_data_region_end - new_data_region_end;
176 real_data_region_end = new_data_region_end;
177 if (new_size > 0)
179 /* Decommit size bytes from the end of the heap. */
180 if (using_dynamic_heap
181 && !VirtualFree (real_data_region_end, new_size, MEM_DECOMMIT))
182 return NULL;
185 data_region_end -= size;
187 /* If size is positive, grow the heap by committing reserved pages. */
188 else if (size > 0)
190 /* Sanity checks. */
191 if ((data_region_end + size) >
192 (data_region_base + get_reserved_heap_size ()))
193 return NULL;
195 /* Commit more of our heap. */
196 if (using_dynamic_heap
197 && VirtualAlloc (data_region_end, size, MEM_COMMIT,
198 PAGE_READWRITE) == NULL)
199 return NULL;
200 data_region_end += size;
202 /* We really only commit full pages, so record where
203 the real end of committed memory is [cga]. */
204 real_data_region_end = (unsigned char *)
205 ((long) (data_region_end + syspage_mask) & ~syspage_mask);
208 return result;
211 /* Initialize the internal heap variables used by sbrk. When running in
212 preload phase (ie. in the undumped executable), we rely entirely on a
213 fixed size heap section included in the .exe itself; this is
214 preserved during dumping, and truncated to the size actually used.
216 When running in the dumped executable, we reserve as much as possible
217 of the address range that is addressable by Lisp object pointers, to
218 supplement what is left of the preload heap. Although we cannot rely
219 on the dynamically allocated arena being contiguous with the static
220 heap area, it is not a problem because sbrk can pretend that the gap
221 was allocated by something else; GNU malloc detects when there is a
222 jump in the sbrk values, and starts a new heap block. */
223 void
224 init_heap (void)
226 PIMAGE_DOS_HEADER dos_header;
227 PIMAGE_NT_HEADERS nt_header;
229 dos_header = (PIMAGE_DOS_HEADER) RVA_TO_PTR (0);
230 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
231 dos_header->e_lfanew);
232 preload_heap_section = find_section ("EMHEAP", nt_header);
234 if (using_dynamic_heap)
236 data_region_base = allocate_heap ();
237 if (!data_region_base)
239 printf ("Error: Could not reserve dynamic heap area.\n");
240 exit (1);
243 #if !defined (USE_LISP_UNION_TYPE) && !defined (USE_LSB_TAG)
244 /* Ensure that the addresses don't use the upper tag bits since
245 the Lisp type goes there. */
246 if (((unsigned long) data_region_base & ~VALMASK) != 0)
248 printf ("Error: The heap was allocated in upper memory.\n");
249 exit (1);
251 #endif
252 data_region_end = data_region_base;
253 real_data_region_end = data_region_end;
255 else
257 data_region_base = RVA_TO_PTR (preload_heap_section->VirtualAddress);
258 data_region_end = data_region_base;
259 real_data_region_end = data_region_end;
260 reserved_heap_size = preload_heap_section->Misc.VirtualSize;
263 /* Update system version information to match current system. */
264 cache_system_info ();
267 /* Round the heap up to the given alignment. */
268 void
269 round_heap (unsigned long align)
271 unsigned long needs_to_be;
272 unsigned long need_to_alloc;
274 needs_to_be = (unsigned long) ROUND_UP (get_heap_end (), align);
275 need_to_alloc = needs_to_be - (unsigned long) get_heap_end ();
277 if (need_to_alloc)
278 sbrk (need_to_alloc);
281 #if (_MSC_VER >= 1000 && _MSC_VER < 1300 && !defined (USE_CRT_DLL))
283 /* MSVC 4.2 invokes these functions from mainCRTStartup to initialize
284 a heap via HeapCreate. They are normally defined by the runtime,
285 but we override them here so that the unnecessary HeapCreate call
286 is not performed. */
288 int __cdecl
289 _heap_init (void)
291 /* Stepping through the assembly indicates that mainCRTStartup is
292 expecting a nonzero success return value. */
293 return 1;
296 void __cdecl
297 _heap_term (void)
299 return;
302 #endif
304 /* arch-tag: 9a6a9860-040d-422d-8905-450dd535cd9c
305 (do not change this comment) */