Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-483
[bpt/emacs.git] / src / w32heap.c
CommitLineData
e9e23e23 1/* Heap management routines for GNU Emacs on the Microsoft W32 API.
95ed0025
RS
2 Copyright (C) 1994 Free Software Foundation, Inc.
3
3b7ad313 4This file is part of GNU Emacs.
95ed0025 5
3b7ad313
EN
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
95ed0025 10
3b7ad313
EN
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
95ed0025 15
3b7ad313
EN
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA.
95ed0025
RS
20
21 Geoff Voelker (voelker@cs.washington.edu) 7-29-94
22*/
23
4838e624
PJ
24#ifdef HAVE_CONFIG_H
25#include <config.h>
26#endif
3bbabc43 27
95ed0025
RS
28#include <stdlib.h>
29#include <stdio.h>
30
489f9371 31#include "w32heap.h"
8dfdd41f 32#include "lisp.h" /* for VALMASK */
95ed0025 33
30d2b1c2 34#undef RVA_TO_PTR
2e4f6477 35#define RVA_TO_PTR(rva) ((unsigned char *)((DWORD)(rva) + (DWORD)GetModuleHandle (NULL)))
30d2b1c2 36
95ed0025
RS
37/* This gives us the page size and the size of the allocation unit on NT. */
38SYSTEM_INFO sysinfo_cache;
b9cad9c1
GV
39
40/* This gives us version, build, and platform identification. */
41OSVERSIONINFO osinfo_cache;
42
3bbabc43 43unsigned long syspage_mask = 0;
95ed0025
RS
44
45/* These are defined to get Emacs to compile, but are not used. */
46int edata;
47int etext;
48
49/* The major and minor versions of NT. */
fbd6baed
GV
50int w32_major_version;
51int w32_minor_version;
39f1f652 52int w32_build_number;
95ed0025 53
801f68b9
GV
54/* Distinguish between Windows NT and Windows 95. */
55int os_subtype;
56
95ed0025
RS
57/* Cache information describing the NT system for later use. */
58void
59cache_system_info (void)
60{
ce20e03e 61 union
95ed0025 62 {
ce20e03e 63 struct info
95ed0025
RS
64 {
65 char major;
66 char minor;
67 short platform;
68 } info;
69 DWORD data;
70 } version;
71
72 /* Cache the version of the operating system. */
73 version.data = GetVersion ();
fbd6baed
GV
74 w32_major_version = version.info.major;
75 w32_minor_version = version.info.minor;
95ed0025 76
801f68b9
GV
77 if (version.info.platform & 0x8000)
78 os_subtype = OS_WIN95;
79 else
80 os_subtype = OS_NT;
81
95ed0025
RS
82 /* Cache page size, allocation unit, processor type, etc. */
83 GetSystemInfo (&sysinfo_cache);
3bbabc43 84 syspage_mask = sysinfo_cache.dwPageSize - 1;
b9cad9c1
GV
85
86 /* Cache os info. */
87 osinfo_cache.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
88 GetVersionEx (&osinfo_cache);
39f1f652
AI
89
90 w32_build_number = osinfo_cache.dwBuildNumber;
91 if (os_subtype == OS_WIN95)
92 w32_build_number &= 0xffff;
95ed0025
RS
93}
94
e54c8cd1
GV
95/* Emulate getpagesize. */
96int
97getpagesize (void)
98{
99 return sysinfo_cache.dwPageSize;
100}
101
30d2b1c2
AI
102/* Info for managing our preload heap, which is essentially a fixed size
103 data area in the executable. */
104PIMAGE_SECTION_HEADER preload_heap_section;
95ed0025
RS
105
106/* Info for keeping track of our heap. */
107unsigned char *data_region_base = NULL;
108unsigned char *data_region_end = NULL;
3bbabc43 109unsigned char *real_data_region_end = NULL;
011db670 110unsigned long reserved_heap_size = 0;
95ed0025
RS
111
112/* The start of the data segment. */
113unsigned char *
114get_data_start (void)
115{
116 return data_region_base;
117}
118
119/* The end of the data segment. */
120unsigned char *
121get_data_end (void)
122{
123 return data_region_end;
124}
125
011db670
GV
126static char *
127allocate_heap (void)
128{
30d2b1c2
AI
129 /* Try to get as much as possible of the address range from the end of
130 the preload heap section up to the usable address limit. Since GNU
131 malloc can handle gaps in the memory it gets from sbrk, we can
132 simply set the sbrk pointer to the base of the new heap region. */
133 unsigned long base =
134 ROUND_UP ((RVA_TO_PTR (preload_heap_section->VirtualAddress)
135 + preload_heap_section->Misc.VirtualSize),
136 get_allocation_unit ());
8dfdd41f 137 unsigned long end = 1 << VALBITS; /* 256MB */
709fd16b 138 void *ptr = NULL;
011db670 139
709fd16b
GV
140 while (!ptr && (base < end))
141 {
709fd16b
GV
142 reserved_heap_size = end - base;
143 ptr = VirtualAlloc ((void *) base,
144 get_reserved_heap_size (),
145 MEM_RESERVE,
146 PAGE_NOACCESS);
709fd16b
GV
147 base += 0x00100000; /* 1MB increment */
148 }
0d05360d 149
709fd16b 150 return ptr;
011db670 151}
011db670
GV
152
153
95ed0025
RS
154/* Emulate Unix sbrk. */
155void *
156sbrk (unsigned long increment)
157{
158 void *result;
159 long size = (long) increment;
ce20e03e 160
95ed0025 161 result = data_region_end;
ce20e03e 162
95ed0025 163 /* If size is negative, shrink the heap by decommitting pages. */
ce20e03e 164 if (size < 0)
95ed0025 165 {
3bbabc43
GV
166 int new_size;
167 unsigned char *new_data_region_end;
168
95ed0025
RS
169 size = -size;
170
171 /* Sanity checks. */
95ed0025
RS
172 if ((data_region_end - size) < data_region_base)
173 return NULL;
174
ce20e03e 175 /* We can only decommit full pages, so allow for
3bbabc43
GV
176 partial deallocation [cga]. */
177 new_data_region_end = (data_region_end - size);
178 new_data_region_end = (unsigned char *)
179 ((long) (new_data_region_end + syspage_mask) & ~syspage_mask);
180 new_size = real_data_region_end - new_data_region_end;
181 real_data_region_end = new_data_region_end;
30d2b1c2 182 if (new_size > 0)
3bbabc43
GV
183 {
184 /* Decommit size bytes from the end of the heap. */
30d2b1c2
AI
185 if (using_dynamic_heap
186 && !VirtualFree (real_data_region_end, new_size, MEM_DECOMMIT))
3bbabc43
GV
187 return NULL;
188 }
95ed0025
RS
189
190 data_region_end -= size;
ce20e03e 191 }
95ed0025 192 /* If size is positive, grow the heap by committing reserved pages. */
ce20e03e 193 else if (size > 0)
95ed0025
RS
194 {
195 /* Sanity checks. */
95ed0025
RS
196 if ((data_region_end + size) >
197 (data_region_base + get_reserved_heap_size ()))
198 return NULL;
199
200 /* Commit more of our heap. */
30d2b1c2
AI
201 if (using_dynamic_heap
202 && VirtualAlloc (data_region_end, size, MEM_COMMIT,
203 PAGE_READWRITE) == NULL)
95ed0025
RS
204 return NULL;
205 data_region_end += size;
3bbabc43
GV
206
207 /* We really only commit full pages, so record where
208 the real end of committed memory is [cga]. */
209 real_data_region_end = (unsigned char *)
210 ((long) (data_region_end + syspage_mask) & ~syspage_mask);
95ed0025 211 }
ce20e03e 212
95ed0025
RS
213 return result;
214}
215
30d2b1c2
AI
216/* Initialize the internal heap variables used by sbrk. When running in
217 preload phase (ie. in the undumped executable), we rely entirely on a
218 fixed size heap section included in the .exe itself; this is
219 preserved during dumping, and truncated to the size actually used.
220
221 When running in the dumped executable, we reserve as much as possible
222 of the address range that is addressable by Lisp object pointers, to
223 supplement what is left of the preload heap. Although we cannot rely
224 on the dynamically allocated arena being contiguous with the static
225 heap area, it is not a problem because sbrk can pretend that the gap
226 was allocated by something else; GNU malloc detects when there is a
227 jump in the sbrk values, and starts a new heap block. */
95ed0025 228void
30d2b1c2 229init_heap ()
95ed0025 230{
30d2b1c2
AI
231 PIMAGE_DOS_HEADER dos_header;
232 PIMAGE_NT_HEADERS nt_header;
233
234 dos_header = (PIMAGE_DOS_HEADER) RVA_TO_PTR (0);
ce20e03e 235 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
30d2b1c2
AI
236 dos_header->e_lfanew);
237 preload_heap_section = find_section ("EMHEAP", nt_header);
238
239 if (using_dynamic_heap)
240 {
241 data_region_base = allocate_heap ();
242 if (!data_region_base)
243 {
244 printf ("Error: Could not reserve dynamic heap area.\n");
245 exit (1);
246 }
247
3ae12c8d 248#if defined (NO_UNION_TYPE) && !defined (USE_LSB_TAG)
30d2b1c2
AI
249 /* Ensure that the addresses don't use the upper tag bits since
250 the Lisp type goes there. */
ce20e03e 251 if (((unsigned long) data_region_base & ~VALMASK) != 0)
30d2b1c2
AI
252 {
253 printf ("Error: The heap was allocated in upper memory.\n");
254 exit (1);
255 }
3ae12c8d 256#endif
30d2b1c2
AI
257 data_region_end = data_region_base;
258 real_data_region_end = data_region_end;
259 }
260 else
261 {
262 data_region_base = RVA_TO_PTR (preload_heap_section->VirtualAddress);
263 data_region_end = data_region_base;
264 real_data_region_end = data_region_end;
265 reserved_heap_size = preload_heap_section->Misc.VirtualSize;
266 }
801f68b9
GV
267
268 /* Update system version information to match current system. */
269 cache_system_info ();
95ed0025
RS
270}
271
272/* Round the heap up to the given alignment. */
273void
274round_heap (unsigned long align)
275{
276 unsigned long needs_to_be;
277 unsigned long need_to_alloc;
ce20e03e 278
30d2b1c2 279 needs_to_be = (unsigned long) ROUND_UP (get_heap_end (), align);
95ed0025 280 need_to_alloc = needs_to_be - (unsigned long) get_heap_end ();
ce20e03e
JB
281
282 if (need_to_alloc)
95ed0025
RS
283 sbrk (need_to_alloc);
284}
801f68b9 285
ce20e03e 286#if (_MSC_VER >= 1000 && _MSC_VER < 1300 && !defined(USE_CRT_DLL))
801f68b9
GV
287
288/* MSVC 4.2 invokes these functions from mainCRTStartup to initialize
289 a heap via HeapCreate. They are normally defined by the runtime,
290 but we override them here so that the unnecessary HeapCreate call
291 is not performed. */
292
293int __cdecl
294_heap_init (void)
295{
296 /* Stepping through the assembly indicates that mainCRTStartup is
297 expecting a nonzero success return value. */
298 return 1;
299}
300
301void __cdecl
302_heap_term (void)
303{
304 return;
305}
306
307#endif
ab5796a9
MB
308
309/* arch-tag: 9a6a9860-040d-422d-8905-450dd535cd9c
310 (do not change this comment) */