*** empty log message ***
[emacs.git] / src / w32heap.c
blob69144b250388a3145cb928eb5f0a0fc1564dbdbb
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) ((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;
51 /* Distinguish between Windows NT and Windows 95. */
52 int os_subtype;
54 /* Cache information describing the NT system for later use. */
55 void
56 cache_system_info (void)
58 union
60 struct info
62 char major;
63 char minor;
64 short platform;
65 } info;
66 DWORD data;
67 } version;
69 /* Cache the version of the operating system. */
70 version.data = GetVersion ();
71 w32_major_version = version.info.major;
72 w32_minor_version = version.info.minor;
74 if (version.info.platform & 0x8000)
75 os_subtype = OS_WIN95;
76 else
77 os_subtype = OS_NT;
79 /* Cache page size, allocation unit, processor type, etc. */
80 GetSystemInfo (&sysinfo_cache);
81 syspage_mask = sysinfo_cache.dwPageSize - 1;
83 /* Cache os info. */
84 osinfo_cache.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
85 GetVersionEx (&osinfo_cache);
88 /* Emulate getpagesize. */
89 int
90 getpagesize (void)
92 return sysinfo_cache.dwPageSize;
95 /* Info for managing our preload heap, which is essentially a fixed size
96 data area in the executable. */
97 PIMAGE_SECTION_HEADER preload_heap_section;
99 /* Info for keeping track of our heap. */
100 unsigned char *data_region_base = NULL;
101 unsigned char *data_region_end = NULL;
102 unsigned char *real_data_region_end = NULL;
103 unsigned long reserved_heap_size = 0;
105 /* The start of the data segment. */
106 unsigned char *
107 get_data_start (void)
109 return data_region_base;
112 /* The end of the data segment. */
113 unsigned char *
114 get_data_end (void)
116 return data_region_end;
119 static char *
120 allocate_heap (void)
122 /* Try to get as much as possible of the address range from the end of
123 the preload heap section up to the usable address limit. Since GNU
124 malloc can handle gaps in the memory it gets from sbrk, we can
125 simply set the sbrk pointer to the base of the new heap region. */
126 unsigned long base =
127 ROUND_UP ((RVA_TO_PTR (preload_heap_section->VirtualAddress)
128 + preload_heap_section->Misc.VirtualSize),
129 get_allocation_unit ());
130 unsigned long end = 1 << VALBITS; /* 256MB */
131 void *ptr = NULL;
133 while (!ptr && (base < end))
135 reserved_heap_size = end - base;
136 ptr = VirtualAlloc ((void *) base,
137 get_reserved_heap_size (),
138 MEM_RESERVE,
139 PAGE_NOACCESS);
140 base += 0x00100000; /* 1MB increment */
143 return ptr;
147 /* Emulate Unix sbrk. */
148 void *
149 sbrk (unsigned long increment)
151 void *result;
152 long size = (long) increment;
154 result = data_region_end;
156 /* If size is negative, shrink the heap by decommitting pages. */
157 if (size < 0)
159 int new_size;
160 unsigned char *new_data_region_end;
162 size = -size;
164 /* Sanity checks. */
165 if ((data_region_end - size) < data_region_base)
166 return NULL;
168 /* We can only decommit full pages, so allow for
169 partial deallocation [cga]. */
170 new_data_region_end = (data_region_end - size);
171 new_data_region_end = (unsigned char *)
172 ((long) (new_data_region_end + syspage_mask) & ~syspage_mask);
173 new_size = real_data_region_end - new_data_region_end;
174 real_data_region_end = new_data_region_end;
175 if (new_size > 0)
177 /* Decommit size bytes from the end of the heap. */
178 if (using_dynamic_heap
179 && !VirtualFree (real_data_region_end, new_size, MEM_DECOMMIT))
180 return NULL;
183 data_region_end -= size;
185 /* If size is positive, grow the heap by committing reserved pages. */
186 else if (size > 0)
188 /* Sanity checks. */
189 if ((data_region_end + size) >
190 (data_region_base + get_reserved_heap_size ()))
191 return NULL;
193 /* Commit more of our heap. */
194 if (using_dynamic_heap
195 && VirtualAlloc (data_region_end, size, MEM_COMMIT,
196 PAGE_READWRITE) == NULL)
197 return NULL;
198 data_region_end += size;
200 /* We really only commit full pages, so record where
201 the real end of committed memory is [cga]. */
202 real_data_region_end = (unsigned char *)
203 ((long) (data_region_end + syspage_mask) & ~syspage_mask);
206 return result;
209 /* Initialize the internal heap variables used by sbrk. When running in
210 preload phase (ie. in the undumped executable), we rely entirely on a
211 fixed size heap section included in the .exe itself; this is
212 preserved during dumping, and truncated to the size actually used.
214 When running in the dumped executable, we reserve as much as possible
215 of the address range that is addressable by Lisp object pointers, to
216 supplement what is left of the preload heap. Although we cannot rely
217 on the dynamically allocated arena being contiguous with the static
218 heap area, it is not a problem because sbrk can pretend that the gap
219 was allocated by something else; GNU malloc detects when there is a
220 jump in the sbrk values, and starts a new heap block. */
221 void
222 init_heap ()
224 PIMAGE_DOS_HEADER dos_header;
225 PIMAGE_NT_HEADERS nt_header;
227 dos_header = (PIMAGE_DOS_HEADER) RVA_TO_PTR (0);
228 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
229 dos_header->e_lfanew);
230 preload_heap_section = find_section ("EMHEAP", nt_header);
232 if (using_dynamic_heap)
234 data_region_base = allocate_heap ();
235 if (!data_region_base)
237 printf ("Error: Could not reserve dynamic heap area.\n");
238 exit (1);
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);
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)
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