annotate src/ntheap.c @ 412:697ef44129c6 r21-2-14

Import from CVS: tag r21-2-14
author cvs
date Mon, 13 Aug 2007 11:20:41 +0200
parents 501cfd01ee6d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1 /* Heap management routines for XEmacs on Windows NT.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2 Copyright (C) 1994 Free Software Foundation, Inc.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
3
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
4 This file is part of XEmacs.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
5
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
6 XEmacs is free software; you can redistribute it and/or modify it
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
7 under the terms of the GNU General Public License as published by the
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
8 Free Software Foundation; either version 2, or (at your option) any
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
9 later version.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
10
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
14 for more details.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
15
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
17 along with XEmacs; see the file COPYING. If not, write to the Free
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
19 02111-1307, USA.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
20
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
21 Geoff Voelker (voelker@cs.washington.edu) 7-29-94 */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
22
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
23 /* Adapted for XEmacs by David Hobley <david@spook-le0.cia.com.au> */
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
24 /* Synced with FSF Emacs 19.34.6 by Marc Paquette <marcpa@cam.org> */
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
25
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 209
diff changeset
26 #include <config.h>
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 209
diff changeset
27 #include "lisp.h" /* for VALMASK */
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
28
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
29 #include <stdlib.h>
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
30
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
31 #include "ntheap.h"
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
32
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
33 /* This gives us the page size and the size of the allocation unit on NT. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
34 SYSTEM_INFO sysinfo_cache;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
35 unsigned long syspage_mask = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
36
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
37 /* These are defined to get Emacs to compile, but are not used. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
38 int edata;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
39 int etext;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
40
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
41 /* The major and minor versions of NT. */
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
42 int nt_major_version;
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
43 int nt_minor_version;
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
44
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
45 /* Distinguish between Windows NT and Windows 95. */
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
46 int os_subtype;
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
47
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
48 /* Cache information describing the NT system for later use. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
49 void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
50 cache_system_info (void)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
51 {
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
52 union
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
53 {
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
54 struct info
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
55 {
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
56 char major;
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
57 char minor;
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
58 short platform;
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
59 } info;
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
60 DWORD data;
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
61 } version;
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
62
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
63 /* Cache the version of the operating system. */
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
64 version.data = GetVersion ();
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
65 nt_major_version = version.info.major;
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
66 nt_minor_version = version.info.minor;
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
67
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
68 if (version.info.platform & 0x8000)
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
69 os_subtype = OS_WIN95;
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
70 else
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
71 os_subtype = OS_NT;
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
72
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
73 /* Cache page size, allocation unit, processor type, etc. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
74 GetSystemInfo (&sysinfo_cache);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
75 syspage_mask = sysinfo_cache.dwPageSize - 1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
76 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
77
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
78 /* Round ADDRESS up to be aligned with ALIGN. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
79 unsigned char *
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
80 round_to_next (unsigned char *address, unsigned long align)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
81 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
82 unsigned long tmp;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
83
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
84 tmp = (unsigned long) address;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
85 tmp = (tmp + align - 1) / align;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
86
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
87 return (unsigned char *) (tmp * align);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
88 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
89
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
90 /* Info for keeping track of our heap. */
169
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
91 unsigned char *data_region_base = UNINIT_PTR;
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
92 unsigned char *data_region_end = UNINIT_PTR;
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
93 unsigned char *real_data_region_end = UNINIT_PTR;
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
94 unsigned long data_region_size = UNINIT_LONG;
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
95 unsigned long reserved_heap_size = UNINIT_LONG;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
96
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
97 /* The start of the data segment. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
98 unsigned char *
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
99 get_data_start (void)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
100 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
101 return data_region_base;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
102 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
103
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
104 /* The end of the data segment. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
105 unsigned char *
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
106 get_data_end (void)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
107 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
108 return data_region_end;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
109 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
110
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
111 static char *
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
112 allocate_heap (void)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
113 {
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 371
diff changeset
114 /* The base address for our GNU malloc heap is chosen in conjunction
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
115 with the link settings for temacs.exe which control the stack size,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
116 the initial default process heap size and the executable image base
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
117 address. The link settings and the malloc heap base below must all
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
118 correspond; the relationship between these values depends on how NT
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
119 and Win95 arrange the virtual address space for a process (and on
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
120 the size of the code and data segments in temacs.exe).
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
121
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
122 The most important thing is to make base address for the executable
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
123 image high enough to leave enough room between it and the 4MB floor
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
124 of the process address space on Win95 for the primary thread stack,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
125 the process default heap, and other assorted odds and ends
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
126 (eg. environment strings, private system dll memory etc) that are
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
127 allocated before temacs has a chance to grab its malloc arena. The
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
128 malloc heap base can then be set several MB higher than the
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
129 executable image base, leaving enough room for the code and data
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
130 segments.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
131
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
132 Because some parts of Emacs can use rather a lot of stack space
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
133 (for instance, the regular expression routines can potentially
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
134 allocate several MB of stack space) we allow 8MB for the stack.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
135
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
136 Allowing 1MB for the default process heap, and 1MB for odds and
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
137 ends, we can base the executable at 16MB and still have a generous
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
138 safety margin. At the moment, the executable has about 810KB of
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
139 code (for x86) and about 550KB of data - on RISC platforms the code
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
140 size could be roughly double, so if we allow 4MB for the executable
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
141 we will have plenty of room for expansion.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
142
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
143 Thus we would like to set the malloc heap base to 20MB. However,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
144 Win95 refuses to allocate the heap starting at this address, so we
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
145 set the base to 27MB to make it happy. Since Emacs now leaves
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
146 28 bits available for pointers, this lets us use the remainder of
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
147 the region below the 256MB line for our malloc arena - 229MB is
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
148 still a pretty decent arena to play in! */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
149
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
150 unsigned long base = 0x01B00000; /* 27MB */
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
151 unsigned long end = 1 << VALBITS; /* 256MB */
402
5a2589c672dc Import from CVS: tag r21-2-31
cvs
parents: 400
diff changeset
152 void *ptr = NULL;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
153
169
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
154 #define NTHEAP_PROBE_BASE 1
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
155 #if NTHEAP_PROBE_BASE /* This is never normally defined */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
156 /* Try various addresses looking for one the kernel will let us have. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
157 while (!ptr && (base < end))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
158 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
159 reserved_heap_size = end - base;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
160 ptr = VirtualAlloc ((void *) base,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
161 get_reserved_heap_size (),
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
162 MEM_RESERVE,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
163 PAGE_NOACCESS);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
164 base += 0x00100000; /* 1MB increment */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
165 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
166 #else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
167 reserved_heap_size = end - base;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
168 ptr = VirtualAlloc ((void *) base,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
169 get_reserved_heap_size (),
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
170 MEM_RESERVE,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
171 PAGE_NOACCESS);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
172 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
173
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
174 return ptr;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
175 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
176
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
177
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
178 /* Emulate Unix sbrk. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
179 void *
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
180 sbrk (unsigned long increment)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
181 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
182 void *result;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
183 long size = (long) increment;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
184
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
185 /* Allocate our heap if we haven't done so already. */
169
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
186 if (data_region_base == UNINIT_PTR)
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
187 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
188 data_region_base = allocate_heap ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
189 if (!data_region_base)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
190 return NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
191
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
192 data_region_end = data_region_base;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
193 real_data_region_end = data_region_end;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
194 data_region_size = get_reserved_heap_size ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
195 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
196
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
197 result = data_region_end;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
198
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
199 /* If size is negative, shrink the heap by decommitting pages. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
200 if (size < 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
201 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
202 int new_size;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
203 unsigned char *new_data_region_end;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
204
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
205 size = -size;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
206
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
207 /* Sanity checks. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
208 if ((data_region_end - size) < data_region_base)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
209 return NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
210
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
211 /* We can only decommit full pages, so allow for
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
212 partial deallocation [cga]. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
213 new_data_region_end = (data_region_end - size);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
214 new_data_region_end = (unsigned char *)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
215 ((long) (new_data_region_end + syspage_mask) & ~syspage_mask);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
216 new_size = real_data_region_end - new_data_region_end;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
217 real_data_region_end = new_data_region_end;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
218 if (new_size > 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
219 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
220 /* Decommit size bytes from the end of the heap. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
221 if (!VirtualFree (real_data_region_end, new_size, MEM_DECOMMIT))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
222 return NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
223 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
224
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
225 data_region_end -= size;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
226 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
227 /* If size is positive, grow the heap by committing reserved pages. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
228 else if (size > 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
229 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
230 /* Sanity checks. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
231 if ((data_region_end + size) >
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
232 (data_region_base + get_reserved_heap_size ()))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
233 return NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
234
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
235 /* Commit more of our heap. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
236 if (VirtualAlloc (data_region_end, size, MEM_COMMIT,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
237 PAGE_READWRITE) == NULL)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
238 return NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
239 data_region_end += size;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
240
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
241 /* We really only commit full pages, so record where
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
242 the real end of committed memory is [cga]. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
243 real_data_region_end = (unsigned char *)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
244 ((long) (data_region_end + syspage_mask) & ~syspage_mask);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
245 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
246
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
247 return result;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
248 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
249
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
250 #if !defined (CANNOT_DUMP) && !defined(HEAP_IN_DATA)
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
251
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
252 /* Recreate the heap from the data that was dumped to the executable.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
253 EXECUTABLE_PATH tells us where to find the executable. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
254 void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
255 recreate_heap (char *executable_path)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
256 {
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
257 unsigned char *tmp;
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
258
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
259 /* First reserve the upper part of our heap. (We reserve first
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
260 because there have been problems in the past where doing the
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
261 mapping first has loaded DLLs into the VA space of our heap.) */
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
262 tmp = VirtualAlloc ((void *) get_heap_end (),
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
263 get_reserved_heap_size () - get_committed_heap_size (),
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
264 MEM_RESERVE,
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
265 PAGE_NOACCESS);
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
266 if (!tmp)
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 408
diff changeset
267 exit (1);
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
268
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
269 /* We read in the data for the .bss section from the executable
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
270 first and map in the heap from the executable second to prevent
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
271 any funny interactions between file I/O and file mapping. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
272 read_in_bss (executable_path);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
273 map_in_heap (executable_path);
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
274
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
275 /* Update system version information to match current system. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
276 cache_system_info ();
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
277 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
278 #endif /* CANNOT_DUMP */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
279
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
280 /* Round the heap up to the given alignment. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
281 void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
282 round_heap (unsigned long align)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
283 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
284 unsigned long needs_to_be;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
285 unsigned long need_to_alloc;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
286
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
287 needs_to_be = (unsigned long) round_to_next (get_heap_end (), align);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
288 need_to_alloc = needs_to_be - (unsigned long) get_heap_end ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
289
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
290 if (need_to_alloc)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
291 sbrk (need_to_alloc);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
292 }
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
293
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
294 #if (_MSC_VER >= 1000)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
295
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
296 /* MSVC 4.2 invokes these functions from mainCRTStartup to initialize
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
297 a heap via HeapCreate. They are normally defined by the runtime,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
298 but we override them here so that the unnecessary HeapCreate call
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
299 is not performed. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
300
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
301 int __cdecl
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
302 _heap_init (void)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
303 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
304 /* Stepping through the assembly indicates that mainCRTStartup is
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
305 expecting a nonzero success return value. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
306 return 1;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
307 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
308
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
309 void __cdecl
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
310 _heap_term (void)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
311 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
312 return;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
313 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
314
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
315 #endif