Mercurial > emacs
annotate src/w32heap.c @ 12328:dbc55496f684
(main): On MSDOS, don't change stdout to binary, and insist on an -o option.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Tue, 20 Jun 1995 02:52:38 +0000 |
parents | ea533622fb8b |
children | bd304be0b491 |
rev | line source |
---|---|
9803 | 1 /* Heap management routines for GNU Emacs on Windows NT. |
2 Copyright (C) 1994 Free Software Foundation, Inc. | |
3 | |
4 This file is part of GNU Emacs. | |
5 | |
6 GNU Emacs is free software; you can redistribute it and/or modify it | |
7 under the terms of the GNU General Public License as published by the | |
8 Free Software Foundation; either version 2, or (at your option) any later | |
9 version. | |
10 | |
11 GNU Emacs is distributed in the hope that it will be useful, but WITHOUT | |
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
14 more details. | |
15 | |
16 You should have received a copy of the GNU General Public License along | |
17 with GNU Emacs; see the file COPYING. If not, write to the Free Software | |
18 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | |
20 Geoff Voelker (voelker@cs.washington.edu) 7-29-94 | |
21 */ | |
22 | |
23 #include <stdlib.h> | |
24 #include <stdio.h> | |
25 | |
26 #include "ntheap.h" | |
27 | |
28 /* This gives us the page size and the size of the allocation unit on NT. */ | |
29 SYSTEM_INFO sysinfo_cache; | |
30 | |
31 /* These are defined to get Emacs to compile, but are not used. */ | |
32 int edata; | |
33 int etext; | |
34 | |
35 /* The major and minor versions of NT. */ | |
36 int nt_major_version; | |
37 int nt_minor_version; | |
38 | |
39 /* Cache information describing the NT system for later use. */ | |
40 void | |
41 cache_system_info (void) | |
42 { | |
43 union | |
44 { | |
45 struct info | |
46 { | |
47 char major; | |
48 char minor; | |
49 short platform; | |
50 } info; | |
51 DWORD data; | |
52 } version; | |
53 | |
54 /* Cache the version of the operating system. */ | |
55 version.data = GetVersion (); | |
56 nt_major_version = version.info.major; | |
57 nt_minor_version = version.info.minor; | |
58 | |
59 /* Cache page size, allocation unit, processor type, etc. */ | |
60 GetSystemInfo (&sysinfo_cache); | |
61 } | |
62 | |
63 /* Round ADDRESS up to be aligned with ALIGN. */ | |
64 unsigned char * | |
65 round_to_next (unsigned char *address, unsigned long align) | |
66 { | |
67 unsigned long tmp; | |
68 | |
69 tmp = (unsigned long) address; | |
70 tmp = (tmp + align - 1) / align; | |
71 | |
72 return (unsigned char *) (tmp * align); | |
73 } | |
74 | |
75 /* Info for keeping track of our heap. */ | |
76 unsigned char *data_region_base = NULL; | |
77 unsigned char *data_region_end = NULL; | |
78 unsigned long data_region_size = 0; | |
11943
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
79 unsigned long reserved_heap_size = 0; |
9803 | 80 |
81 /* The start of the data segment. */ | |
82 unsigned char * | |
83 get_data_start (void) | |
84 { | |
85 return data_region_base; | |
86 } | |
87 | |
88 /* The end of the data segment. */ | |
89 unsigned char * | |
90 get_data_end (void) | |
91 { | |
92 return data_region_end; | |
93 } | |
94 | |
11943
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
95 |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
96 #ifdef WINDOWS95 |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
97 static char * |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
98 allocate_heap (void) |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
99 { |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
100 unsigned long base = 0x00030000; |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
101 unsigned long end = 0x00D00000; |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
102 |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
103 reserved_heap_size = end - base; |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
104 |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
105 return VirtualAlloc ((void *) base, |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
106 get_reserved_heap_size (), |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
107 MEM_RESERVE, |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
108 PAGE_NOACCESS); |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
109 } |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
110 #else |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
111 static char * |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
112 allocate_heap (void) |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
113 { |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
114 unsigned long start = 0x400000; |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
115 unsigned long stop = 0xF00000; |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
116 unsigned long increment = 0x100000; |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
117 char *ptr, *begin = NULL, *end = NULL; |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
118 int i; |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
119 |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
120 for (i = start; i < stop; i += increment) |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
121 { |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
122 ptr = VirtualAlloc ((void *) i, increment, MEM_RESERVE, PAGE_NOACCESS); |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
123 if (ptr) |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
124 begin = begin ? begin : ptr; |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
125 else if (begin) |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
126 { |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
127 end = ptr; |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
128 break; |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
129 } |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
130 } |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
131 |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
132 if (begin && !end) |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
133 end = (char *) i; |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
134 |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
135 if (!begin) |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
136 /* We couldn't allocate any memory for the heap. Exit. */ |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
137 exit (-2); |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
138 |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
139 reserved_heap_size = end - begin; |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
140 return begin; |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
141 } |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
142 #endif |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
143 |
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
144 |
9803 | 145 /* Emulate Unix sbrk. */ |
146 void * | |
147 sbrk (unsigned long increment) | |
148 { | |
149 void *result; | |
150 long size = (long) increment; | |
151 | |
152 /* Allocate our heap if we haven't done so already. */ | |
153 if (!data_region_base) | |
154 { | |
11943
ea533622fb8b
(reserved_heap_size,allocate_heap): Defined.
Geoff Voelker <voelker@cs.washington.edu>
parents:
11385
diff
changeset
|
155 data_region_base = allocate_heap (); |
9803 | 156 if (!data_region_base) |
157 return NULL; | |
158 | |
159 /* Ensure that the addresses don't use the upper 8 bits since | |
160 the Lisp type goes there (yucko). */ | |
161 if (((unsigned long) data_region_base & 0xFF000000) != 0) | |
162 { | |
163 printf ("Error: The heap was allocated in upper memory.\n"); | |
164 exit (1); | |
165 } | |
166 | |
167 data_region_end = data_region_base; | |
168 data_region_size = get_reserved_heap_size (); | |
169 } | |
170 | |
171 result = data_region_end; | |
172 | |
173 /* If size is negative, shrink the heap by decommitting pages. */ | |
174 if (size < 0) | |
175 { | |
176 size = -size; | |
177 | |
178 /* Sanity checks. */ | |
179 if ((data_region_end - size) < data_region_base) | |
180 return NULL; | |
181 | |
182 /* Decommit size bytes from the end of the heap. */ | |
183 if (!VirtualFree (data_region_end - size, size, MEM_DECOMMIT)) | |
184 return NULL; | |
185 | |
186 data_region_end -= size; | |
187 } | |
188 /* If size is positive, grow the heap by committing reserved pages. */ | |
189 else if (size > 0) | |
190 { | |
191 /* Sanity checks. */ | |
192 if ((data_region_end + size) > | |
193 (data_region_base + get_reserved_heap_size ())) | |
194 return NULL; | |
195 | |
196 /* Commit more of our heap. */ | |
197 if (VirtualAlloc (data_region_end, size, MEM_COMMIT, | |
198 PAGE_READWRITE) == NULL) | |
199 return NULL; | |
200 data_region_end += size; | |
201 } | |
202 | |
203 return result; | |
204 } | |
205 | |
206 /* Recreate the heap from the data that was dumped to the executable. | |
207 EXECUTABLE_PATH tells us where to find the executable. */ | |
208 void | |
209 recreate_heap (char *executable_path) | |
210 { | |
211 unsigned char *tmp; | |
212 | |
213 /* First reserve the upper part of our heap. (We reserve first | |
214 because there have been problems in the past where doing the | |
215 mapping first has loaded DLLs into the VA space of our heap.) */ | |
216 tmp = VirtualAlloc ((void *) get_heap_end (), | |
217 get_reserved_heap_size () - get_committed_heap_size (), | |
218 MEM_RESERVE, | |
219 PAGE_NOACCESS); | |
220 if (!tmp) | |
221 exit (1); | |
222 | |
223 /* We read in the data for the .bss section from the executable | |
224 first and map in the heap from the executable second to prevent | |
225 any funny interactions between file I/O and file mapping. */ | |
226 read_in_bss (executable_path); | |
227 map_in_heap (executable_path); | |
228 } | |
229 | |
230 /* Round the heap up to the given alignment. */ | |
231 void | |
232 round_heap (unsigned long align) | |
233 { | |
234 unsigned long needs_to_be; | |
235 unsigned long need_to_alloc; | |
236 | |
237 needs_to_be = (unsigned long) round_to_next (get_heap_end (), align); | |
238 need_to_alloc = needs_to_be - (unsigned long) get_heap_end (); | |
239 | |
240 if (need_to_alloc) | |
241 sbrk (need_to_alloc); | |
242 } |