(rmail-msg-restore-non-pruned-header): New
[emacs.git] / src / w32heap.c
blobeb878a45e94885e57c768bb2942feef426622166
1 /* Heap management routines for GNU Emacs on the Microsoft W32 API.
2 Copyright (C) 1994 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 Geoff Voelker (voelker@cs.washington.edu) 7-29-94
24 #include "config.h"
26 #include <stdlib.h>
27 #include <stdio.h>
29 #include "w32heap.h"
30 #include "lisp.h" /* for VALMASK */
32 #undef RVA_TO_PTR
33 #define RVA_TO_PTR(rva) ((unsigned char *)((DWORD)(rva) + (DWORD)GetModuleHandle (NULL)))
35 /* This gives us the page size and the size of the allocation unit on NT. */
36 SYSTEM_INFO sysinfo_cache;
38 /* This gives us version, build, and platform identification. */
39 OSVERSIONINFO osinfo_cache;
41 unsigned long syspage_mask = 0;
43 /* These are defined to get Emacs to compile, but are not used. */
44 int edata;
45 int etext;
47 /* The major and minor versions of NT. */
48 int w32_major_version;
49 int w32_minor_version;
50 int w32_build_number;
52 /* Distinguish between Windows NT and Windows 95. */
53 int os_subtype;
55 /* Cache information describing the NT system for later use. */
56 void
57 cache_system_info (void)
59 union
61 struct info
63 char major;
64 char minor;
65 short platform;
66 } info;
67 DWORD data;
68 } version;
70 /* Cache the version of the operating system. */
71 version.data = GetVersion ();
72 w32_major_version = version.info.major;
73 w32_minor_version = version.info.minor;
75 if (version.info.platform & 0x8000)
76 os_subtype = OS_WIN95;
77 else
78 os_subtype = OS_NT;
80 /* Cache page size, allocation unit, processor type, etc. */
81 GetSystemInfo (&sysinfo_cache);
82 syspage_mask = sysinfo_cache.dwPageSize - 1;
84 /* Cache os info. */
85 osinfo_cache.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
86 GetVersionEx (&osinfo_cache);
88 w32_build_number = osinfo_cache.dwBuildNumber;
89 if (os_subtype == OS_WIN95)
90 w32_build_number &= 0xffff;
93 /* Emulate getpagesize. */
94 int
95 getpagesize (void)
97 return sysinfo_cache.dwPageSize;
100 /* Info for managing our preload heap, which is essentially a fixed size
101 data area in the executable. */
102 PIMAGE_SECTION_HEADER preload_heap_section;
104 /* Info for keeping track of our heap. */
105 unsigned char *data_region_base = NULL;
106 unsigned char *data_region_end = NULL;
107 unsigned char *real_data_region_end = NULL;
108 unsigned long reserved_heap_size = 0;
110 /* The start of the data segment. */
111 unsigned char *
112 get_data_start (void)
114 return data_region_base;
117 /* The end of the data segment. */
118 unsigned char *
119 get_data_end (void)
121 return data_region_end;
124 static char *
125 allocate_heap (void)
127 /* Try to get as much as possible of the address range from the end of
128 the preload heap section up to the usable address limit. Since GNU
129 malloc can handle gaps in the memory it gets from sbrk, we can
130 simply set the sbrk pointer to the base of the new heap region. */
131 unsigned long base =
132 ROUND_UP ((RVA_TO_PTR (preload_heap_section->VirtualAddress)
133 + preload_heap_section->Misc.VirtualSize),
134 get_allocation_unit ());
135 unsigned long end = 1 << VALBITS; /* 256MB */
136 void *ptr = NULL;
138 while (!ptr && (base < end))
140 reserved_heap_size = end - base;
141 ptr = VirtualAlloc ((void *) base,
142 get_reserved_heap_size (),
143 MEM_RESERVE,
144 PAGE_NOACCESS);
145 base += 0x00100000; /* 1MB increment */
148 return ptr;
152 /* Emulate Unix sbrk. */
153 void *
154 sbrk (unsigned long increment)
156 void *result;
157 long size = (long) increment;
159 result = data_region_end;
161 /* If size is negative, shrink the heap by decommitting pages. */
162 if (size < 0)
164 int new_size;
165 unsigned char *new_data_region_end;
167 size = -size;
169 /* Sanity checks. */
170 if ((data_region_end - size) < data_region_base)
171 return NULL;
173 /* We can only decommit full pages, so allow for
174 partial deallocation [cga]. */
175 new_data_region_end = (data_region_end - size);
176 new_data_region_end = (unsigned char *)
177 ((long) (new_data_region_end + syspage_mask) & ~syspage_mask);
178 new_size = real_data_region_end - new_data_region_end;
179 real_data_region_end = new_data_region_end;
180 if (new_size > 0)
182 /* Decommit size bytes from the end of the heap. */
183 if (using_dynamic_heap
184 && !VirtualFree (real_data_region_end, new_size, MEM_DECOMMIT))
185 return NULL;
188 data_region_end -= size;
190 /* If size is positive, grow the heap by committing reserved pages. */
191 else if (size > 0)
193 /* Sanity checks. */
194 if ((data_region_end + size) >
195 (data_region_base + get_reserved_heap_size ()))
196 return NULL;
198 /* Commit more of our heap. */
199 if (using_dynamic_heap
200 && VirtualAlloc (data_region_end, size, MEM_COMMIT,
201 PAGE_READWRITE) == NULL)
202 return NULL;
203 data_region_end += size;
205 /* We really only commit full pages, so record where
206 the real end of committed memory is [cga]. */
207 real_data_region_end = (unsigned char *)
208 ((long) (data_region_end + syspage_mask) & ~syspage_mask);
211 return result;
214 /* Initialize the internal heap variables used by sbrk. When running in
215 preload phase (ie. in the undumped executable), we rely entirely on a
216 fixed size heap section included in the .exe itself; this is
217 preserved during dumping, and truncated to the size actually used.
219 When running in the dumped executable, we reserve as much as possible
220 of the address range that is addressable by Lisp object pointers, to
221 supplement what is left of the preload heap. Although we cannot rely
222 on the dynamically allocated arena being contiguous with the static
223 heap area, it is not a problem because sbrk can pretend that the gap
224 was allocated by something else; GNU malloc detects when there is a
225 jump in the sbrk values, and starts a new heap block. */
226 void
227 init_heap ()
229 PIMAGE_DOS_HEADER dos_header;
230 PIMAGE_NT_HEADERS nt_header;
232 dos_header = (PIMAGE_DOS_HEADER) RVA_TO_PTR (0);
233 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
234 dos_header->e_lfanew);
235 preload_heap_section = find_section ("EMHEAP", nt_header);
237 if (using_dynamic_heap)
239 data_region_base = allocate_heap ();
240 if (!data_region_base)
242 printf ("Error: Could not reserve dynamic heap area.\n");
243 exit (1);
246 /* Ensure that the addresses don't use the upper tag bits since
247 the Lisp type goes there. */
248 if (((unsigned long) data_region_base & ~VALMASK) != 0)
250 printf ("Error: The heap was allocated in upper memory.\n");
251 exit (1);
254 data_region_end = data_region_base;
255 real_data_region_end = data_region_end;
257 else
259 data_region_base = RVA_TO_PTR (preload_heap_section->VirtualAddress);
260 data_region_end = data_region_base;
261 real_data_region_end = data_region_end;
262 reserved_heap_size = preload_heap_section->Misc.VirtualSize;
265 /* Update system version information to match current system. */
266 cache_system_info ();
269 /* Round the heap up to the given alignment. */
270 void
271 round_heap (unsigned long align)
273 unsigned long needs_to_be;
274 unsigned long need_to_alloc;
276 needs_to_be = (unsigned long) ROUND_UP (get_heap_end (), align);
277 need_to_alloc = needs_to_be - (unsigned long) get_heap_end ();
279 if (need_to_alloc)
280 sbrk (need_to_alloc);
283 #if (_MSC_VER >= 1000 && !defined(USE_CRT_DLL))
285 /* MSVC 4.2 invokes these functions from mainCRTStartup to initialize
286 a heap via HeapCreate. They are normally defined by the runtime,
287 but we override them here so that the unnecessary HeapCreate call
288 is not performed. */
290 int __cdecl
291 _heap_init (void)
293 /* Stepping through the assembly indicates that mainCRTStartup is
294 expecting a nonzero success return value. */
295 return 1;
298 void __cdecl
299 _heap_term (void)
301 return;
304 #endif