Mercurial > hg > xemacs-beta
annotate src/dumper.c @ 5839:d139eb1fead8
Check return value of fseek.
author | Marcus Crestani <marcus@crestani.de> |
---|---|
date | Sat, 13 Dec 2014 14:09:33 +0100 |
parents | b1500f1ec617 |
children | 93a18dbcfd8c |
rev | line source |
---|---|
442 | 1 /* Portable data dumper for XEmacs. |
2551 | 2 Copyright (C) 1999-2000,2004 Olivier Galibert |
458 | 3 Copyright (C) 2001 Martin Buchholz |
5059
c8f90d61dcf3
fix memory usage stats to include pdumped objects
Ben Wing <ben@xemacs.org>
parents:
4976
diff
changeset
|
4 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2010 Ben Wing. |
442 | 5 |
6 This file is part of XEmacs. | |
7 | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5169
diff
changeset
|
8 XEmacs is free software: you can redistribute it and/or modify it |
442 | 9 under the terms of the GNU General Public License as published by the |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5169
diff
changeset
|
10 Free Software Foundation, either version 3 of the License, or (at your |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5169
diff
changeset
|
11 option) any later version. |
442 | 12 |
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5169
diff
changeset
|
19 along with XEmacs. If not, see <http://www.gnu.org/licenses/>. */ |
442 | 20 |
21 /* Synched up with: Not in FSF. */ | |
22 | |
2367 | 23 /* This file has been Mule-ized, Ben Wing, 10-10-04. */ |
24 | |
25 /* #### Put in much more assertions. Whenever we store fixups in the | |
26 process or writing out data, make sure the fixups (offsets) point to the | |
27 beginning of an object, i.e. are registered. Same whenever we read in | |
28 -- verify offsets as registered, and when compute a fixup, verify the | |
29 pointer is pointing within the pdump area. registered and check within | |
30 pdump area. For specific types of pointers (e.g. to Lisp_Objects), | |
31 check if they're pointing to the right kinds of types. It should be | |
32 possible to check that a putative Lisp_Object is really a Lisp_Object | |
33 since it will follow a strict format in its header. */ | |
800 | 34 |
442 | 35 #include <config.h> |
36 #include "lisp.h" | |
37 | |
38 #include "specifier.h" | |
771 | 39 #include "file-coding.h" |
442 | 40 #include "elhash.h" |
1204 | 41 #include "lstream.h" |
442 | 42 #include "sysfile.h" |
43 #include "console-stream.h" | |
44 | |
45 #ifdef WIN32_NATIVE | |
771 | 46 #include "syswindows.h" |
442 | 47 #else |
48 #ifdef HAVE_MMAP | |
49 #include <sys/mman.h> | |
50 #endif | |
2720 | 51 #ifdef DUMP_IN_EXEC |
2015 | 52 #include "dump-data.h" |
442 | 53 #endif |
2720 | 54 #endif |
442 | 55 |
56 typedef struct | |
57 { | |
2367 | 58 const void *blockaddr; |
665 | 59 Bytecount size; |
1204 | 60 const struct memory_description *desc; |
61 } pdump_root_block; | |
452 | 62 |
63 typedef struct | |
64 { | |
1204 | 65 Dynarr_declare (pdump_root_block); |
66 } pdump_root_block_dynarr; | |
452 | 67 |
68 typedef struct | |
69 { | |
70 void **ptraddress; | |
1204 | 71 const struct sized_memory_description *desc; |
2367 | 72 } pdump_root_block_ptr; |
452 | 73 |
74 typedef struct | |
75 { | |
2367 | 76 Dynarr_declare (pdump_root_block_ptr); |
77 } pdump_root_block_ptr_dynarr; | |
452 | 78 |
458 | 79 typedef struct |
80 { | |
2551 | 81 const void *object; |
82 void *data; | |
83 Bytecount size; | |
84 EMACS_INT offset; | |
85 EMACS_INT dest_offset; | |
86 EMACS_INT save_offset; | |
87 const struct opaque_convert_functions *fcts; | |
88 } pdump_cv_data_info; | |
89 | |
90 typedef struct | |
91 { | |
92 Dynarr_declare (pdump_cv_data_info); | |
93 } pdump_cv_data_info_dynarr; | |
94 | |
95 typedef struct | |
96 { | |
97 EMACS_INT dest_offset; | |
98 EMACS_INT save_offset; | |
99 Bytecount size; | |
100 } pdump_cv_data_dump_info; | |
101 | |
102 typedef struct | |
103 { | |
104 const void *object; | |
105 void *data; | |
106 Bytecount size; | |
107 EMACS_INT index; | |
108 EMACS_INT save_offset; | |
109 const struct opaque_convert_functions *fcts; | |
110 } pdump_cv_ptr_info; | |
111 | |
112 typedef struct | |
113 { | |
114 Dynarr_declare (pdump_cv_ptr_info); | |
115 } pdump_cv_ptr_info_dynarr; | |
116 | |
117 typedef struct | |
118 { | |
119 EMACS_INT save_offset; | |
120 Bytecount size; | |
121 } pdump_cv_ptr_dump_info; | |
122 | |
123 typedef struct | |
124 { | |
125 EMACS_INT save_offset; | |
126 Bytecount size; | |
127 void *adr; | |
128 } pdump_cv_ptr_load_info; | |
129 | |
130 typedef struct | |
131 { | |
458 | 132 Lisp_Object *address; |
133 Lisp_Object value; | |
134 } pdump_static_Lisp_Object; | |
135 | |
136 typedef struct | |
137 { | |
2367 | 138 Rawbyte **address; /* Rawbyte * for ease of doing relocation */ |
139 Rawbyte * value; | |
458 | 140 } pdump_static_pointer; |
141 | |
1204 | 142 static pdump_root_block_dynarr *pdump_root_blocks; |
2367 | 143 static pdump_root_block_ptr_dynarr *pdump_root_block_ptrs; |
1204 | 144 static Lisp_Object_ptr_dynarr *pdump_root_lisp_objects; |
452 | 145 static Lisp_Object_ptr_dynarr *pdump_weak_object_chains; |
2551 | 146 static pdump_cv_data_info_dynarr *pdump_cv_data; |
147 static pdump_cv_ptr_info_dynarr *pdump_cv_ptr; | |
452 | 148 |
2367 | 149 /* Mark SIZE bytes at non-heap address BLOCKADDR for dumping, described |
150 by DESC. Called by outside callers during XEmacs initialization. */ | |
151 | |
452 | 152 void |
2367 | 153 dump_add_root_block (const void *blockaddr, Bytecount size, |
1204 | 154 const struct memory_description *desc) |
452 | 155 { |
1204 | 156 pdump_root_block info; |
2367 | 157 info.blockaddr = blockaddr; |
452 | 158 info.size = size; |
1204 | 159 info.desc = desc; |
160 if (pdump_root_blocks == NULL) | |
161 pdump_root_blocks = Dynarr_new (pdump_root_block); | |
162 Dynarr_add (pdump_root_blocks, info); | |
452 | 163 } |
164 | |
2367 | 165 /* Mark the block described by DESC and pointed to by the pointer at |
166 non-heap address PTRADDRESS for dumping. | |
167 All the objects reachable from this pointer will also be dumped. | |
168 Called by outside callers during XEmacs initialization. */ | |
452 | 169 void |
2367 | 170 dump_add_root_block_ptr (void *ptraddress, |
171 const struct sized_memory_description *desc) | |
452 | 172 { |
2367 | 173 pdump_root_block_ptr info; |
452 | 174 info.ptraddress = (void **) ptraddress; |
175 info.desc = desc; | |
2367 | 176 if (pdump_root_block_ptrs == NULL) |
177 pdump_root_block_ptrs = Dynarr_new (pdump_root_block_ptr); | |
178 Dynarr_add (pdump_root_block_ptrs, info); | |
452 | 179 } |
180 | |
181 /* Mark the Lisp_Object at non-heap address VARADDRESS for dumping. | |
2367 | 182 All the objects reachable from this var will also be dumped. |
183 Called by outside callers during XEmacs initialization. */ | |
452 | 184 void |
1204 | 185 dump_add_root_lisp_object (Lisp_Object *varaddress) |
452 | 186 { |
1204 | 187 if (pdump_root_lisp_objects == NULL) |
188 pdump_root_lisp_objects = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *); | |
189 Dynarr_add (pdump_root_lisp_objects, varaddress); | |
452 | 190 } |
191 | |
2367 | 192 /* Mark the list pointed to by the Lisp_Object at VARADDRESS for dumping. |
193 Called by outside callers during XEmacs initialization. */ | |
452 | 194 void |
195 dump_add_weak_object_chain (Lisp_Object *varaddress) | |
196 { | |
197 if (pdump_weak_object_chains == NULL) | |
198 pdump_weak_object_chains = Dynarr_new2 (Lisp_Object_ptr_dynarr, Lisp_Object *); | |
199 Dynarr_add (pdump_weak_object_chains, varaddress); | |
200 } | |
201 | |
202 | |
458 | 203 inline static void |
665 | 204 pdump_align_stream (FILE *stream, Bytecount alignment) |
458 | 205 { |
5727
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
5535
diff
changeset
|
206 OFF_T offset = FTELL (stream); |
86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
Jerry James <james@xemacs.org>
parents:
5535
diff
changeset
|
207 OFF_T adjustment = ALIGN_SIZE (offset, alignment) - offset; |
458 | 208 if (adjustment) |
5839
d139eb1fead8
Check return value of fseek.
Marcus Crestani <marcus@crestani.de>
parents:
5838
diff
changeset
|
209 { |
d139eb1fead8
Check return value of fseek.
Marcus Crestani <marcus@crestani.de>
parents:
5838
diff
changeset
|
210 if (FSEEK (stream, adjustment, SEEK_CUR) == -1) |
d139eb1fead8
Check return value of fseek.
Marcus Crestani <marcus@crestani.de>
parents:
5838
diff
changeset
|
211 { |
d139eb1fead8
Check return value of fseek.
Marcus Crestani <marcus@crestani.de>
parents:
5838
diff
changeset
|
212 report_file_error ("Unable to fseek dump file", |
d139eb1fead8
Check return value of fseek.
Marcus Crestani <marcus@crestani.de>
parents:
5838
diff
changeset
|
213 build_ascstring (EMACS_PROGNAME ".dmp")); |
d139eb1fead8
Check return value of fseek.
Marcus Crestani <marcus@crestani.de>
parents:
5838
diff
changeset
|
214 } |
d139eb1fead8
Check return value of fseek.
Marcus Crestani <marcus@crestani.de>
parents:
5838
diff
changeset
|
215 } |
458 | 216 } |
217 | |
218 #define PDUMP_ALIGN_OUTPUT(type) pdump_align_stream (pdump_out, ALIGNOF (type)) | |
219 | |
220 #define PDUMP_WRITE(type, object) \ | |
771 | 221 retry_fwrite (&object, sizeof (object), 1, pdump_out); |
458 | 222 |
223 #define PDUMP_WRITE_ALIGNED(type, object) do { \ | |
224 PDUMP_ALIGN_OUTPUT (type); \ | |
225 PDUMP_WRITE (type, object); \ | |
226 } while (0) | |
227 | |
228 #define PDUMP_READ(ptr, type) \ | |
2367 | 229 (((type *) (ptr = (Rawbyte *) (((type *) ptr) + 1)))[-1]) |
458 | 230 |
231 #define PDUMP_READ_ALIGNED(ptr, type) \ | |
2367 | 232 ((ptr = (Rawbyte *) ALIGN_PTR (ptr, type)), PDUMP_READ (ptr, type)) |
458 | 233 |
234 | |
235 | |
452 | 236 typedef struct |
237 { | |
1204 | 238 const struct memory_description *desc; |
442 | 239 int count; |
240 } pdump_reloc_table; | |
241 | |
2367 | 242 static Rawbyte *pdump_rt_list = 0; |
442 | 243 |
3263 | 244 #ifndef NEW_GC |
442 | 245 void |
246 pdump_objects_unmark (void) | |
247 { | |
248 int i; | |
2367 | 249 Rawbyte *p = pdump_rt_list; |
442 | 250 if (p) |
251 for (;;) | |
252 { | |
253 pdump_reloc_table *rt = (pdump_reloc_table *)p; | |
254 p += sizeof (pdump_reloc_table); | |
255 if (rt->desc) | |
256 { | |
257 for (i=0; i<rt->count; i++) | |
258 { | |
259 struct lrecord_header *lh = * (struct lrecord_header **) p; | |
5059
c8f90d61dcf3
fix memory usage stats to include pdumped objects
Ben Wing <ben@xemacs.org>
parents:
4976
diff
changeset
|
260 #ifdef ALLOC_TYPE_STATS |
c8f90d61dcf3
fix memory usage stats to include pdumped objects
Ben Wing <ben@xemacs.org>
parents:
4976
diff
changeset
|
261 if (C_READONLY_RECORD_HEADER_P (lh)) |
c8f90d61dcf3
fix memory usage stats to include pdumped objects
Ben Wing <ben@xemacs.org>
parents:
4976
diff
changeset
|
262 tick_lrecord_stats (lh, ALLOC_IN_USE); |
c8f90d61dcf3
fix memory usage stats to include pdumped objects
Ben Wing <ben@xemacs.org>
parents:
4976
diff
changeset
|
263 |
c8f90d61dcf3
fix memory usage stats to include pdumped objects
Ben Wing <ben@xemacs.org>
parents:
4976
diff
changeset
|
264 else |
c8f90d61dcf3
fix memory usage stats to include pdumped objects
Ben Wing <ben@xemacs.org>
parents:
4976
diff
changeset
|
265 { |
c8f90d61dcf3
fix memory usage stats to include pdumped objects
Ben Wing <ben@xemacs.org>
parents:
4976
diff
changeset
|
266 tick_lrecord_stats (lh, MARKED_RECORD_HEADER_P (lh) ? |
c8f90d61dcf3
fix memory usage stats to include pdumped objects
Ben Wing <ben@xemacs.org>
parents:
4976
diff
changeset
|
267 ALLOC_IN_USE : ALLOC_ON_FREE_LIST); |
c8f90d61dcf3
fix memory usage stats to include pdumped objects
Ben Wing <ben@xemacs.org>
parents:
4976
diff
changeset
|
268 UNMARK_RECORD_HEADER (lh); |
c8f90d61dcf3
fix memory usage stats to include pdumped objects
Ben Wing <ben@xemacs.org>
parents:
4976
diff
changeset
|
269 } |
c8f90d61dcf3
fix memory usage stats to include pdumped objects
Ben Wing <ben@xemacs.org>
parents:
4976
diff
changeset
|
270 #else /* not ALLOC_TYPE_STATS */ |
442 | 271 if (! C_READONLY_RECORD_HEADER_P (lh)) |
272 UNMARK_RECORD_HEADER (lh); | |
5059
c8f90d61dcf3
fix memory usage stats to include pdumped objects
Ben Wing <ben@xemacs.org>
parents:
4976
diff
changeset
|
273 #endif /* (not) ALLOC_TYPE_STATS */ |
442 | 274 p += sizeof (EMACS_INT); |
275 } | |
276 } else | |
277 break; | |
278 } | |
279 } | |
3263 | 280 #endif /* not NEW_GC */ |
281 | |
282 | |
283 #ifdef NEW_GC | |
2720 | 284 /* The structure of the dump file looks like this: |
285 0 - header | |
286 - dumped objects | |
287 stab_offset - mc allocation table (count, size, address) for individual | |
288 allocation and relocation at load time. | |
289 - nb_cv_data*struct(dest, adr) for in-object externally | |
290 represented data | |
291 - nb_cv_ptr*(adr) for pointed-to externally represented data | |
292 - relocation table | |
293 - nb_root_struct_ptrs*struct(void *, adr) | |
294 for global pointers to structures | |
295 - nb_root_blocks*struct(void *, size, info) for global | |
296 objects to restore | |
297 - root lisp object address/value couples with the count | |
298 preceding the list | |
299 */ | |
3263 | 300 #else /* not NEW_GC */ |
1204 | 301 /* The structure of the dump file looks like this: |
458 | 302 0 - header |
303 - dumped objects | |
2551 | 304 stab_offset - nb_cv_data*struct(dest, adr) for in-object externally |
305 represented data | |
306 - nb_cv_ptr*(adr) for pointed-to externally represented data | |
307 - nb_root_block_ptrs*struct(void *, adr) | |
2367 | 308 for global pointers to heap blocks |
1204 | 309 - nb_root_blocks*struct(void *, size, info) for global |
2367 | 310 data-segment blocks to restore |
458 | 311 - relocation table |
312 - root lisp object address/value couples with the count | |
313 preceding the list | |
442 | 314 */ |
3263 | 315 #endif /* not NEW_GC */ |
442 | 316 |
317 | |
452 | 318 #define PDUMP_SIGNATURE "XEmacsDP" |
319 #define PDUMP_SIGNATURE_LEN (sizeof (PDUMP_SIGNATURE) - 1) | |
442 | 320 |
321 typedef struct | |
322 { | |
452 | 323 char signature[PDUMP_SIGNATURE_LEN]; |
442 | 324 unsigned int id; |
325 EMACS_UINT stab_offset; | |
326 EMACS_UINT reloc_address; | |
2367 | 327 int nb_root_block_ptrs; |
1204 | 328 int nb_root_blocks; |
2551 | 329 int nb_cv_data; |
330 int nb_cv_ptr; | |
452 | 331 } pdump_header; |
442 | 332 |
2367 | 333 Rawbyte *pdump_start; |
334 Rawbyte *pdump_end; | |
665 | 335 static Bytecount pdump_length; |
442 | 336 |
2551 | 337 static pdump_cv_data_dump_info *pdump_loaded_cv_data; |
338 static pdump_cv_ptr_load_info *pdump_loaded_cv_ptr; | |
339 | |
442 | 340 #ifdef WIN32_NATIVE |
452 | 341 /* Handle for the dump file */ |
458 | 342 static HANDLE pdump_hFile = INVALID_HANDLE_VALUE; |
452 | 343 /* Handle for the file mapping object for the dump file */ |
458 | 344 static HANDLE pdump_hMap = INVALID_HANDLE_VALUE; |
442 | 345 #endif |
346 | |
458 | 347 static void (*pdump_free) (void); |
442 | 348 |
460 | 349 static unsigned char pdump_align_table[] = |
442 | 350 { |
460 | 351 64, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, |
352 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, | |
353 32, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, | |
354 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1 | |
442 | 355 }; |
356 | |
647 | 357 static inline int |
665 | 358 pdump_size_to_align (Bytecount size) |
442 | 359 { |
460 | 360 return pdump_align_table[size % countof (pdump_align_table)]; |
361 } | |
362 | |
2367 | 363 /************************************************************************/ |
364 /* Registering memory blocks */ | |
365 /************************************************************************/ | |
366 | |
367 /* "Registering" or recording a heap memory block (which will need to be | |
368 written out, reloaded and relocated, and to which there may be pointers | |
369 from other heap blocks or from the data segment) happens both in a list | |
370 and in a hash table. There is a single hash table covering all | |
371 registered blocks, but different lists for different kinds of blocks. | |
372 There is one list for "opaque data" (stuff identified as | |
373 XD_OPAQUE_DATA_PTR, XD_ASCII_STRING, XD_DOC_STRING), one list for each | |
374 type of Lisp object, and one list for each different memory descriptor. | |
375 This lets similar-sized and aligned objects be grouped together when | |
376 they are written out, to save space. | |
377 | |
378 pdump_block_list is a list keeping track of registered memory blocks. | |
379 pdump_block_list_elt is a single entry through the list, and the list is | |
380 threaded through the NEXT pointer. The information in this list | |
381 associated with a particular block of memory is | |
382 | |
383 -- address of the beginning | |
384 -- number of elements at that address | |
385 -- size of each element | |
386 -- offset to this block in the dumped data | |
387 | |
388 pdump_desc_list is a list keeping track of the various descriptions | |
389 that we've seen. The primary purpose of this is so that memory blocks | |
390 can be grouped depending on the particular memory description | |
391 appropriate for them. The format of the list is different from | |
392 pdump_block_list -- a single array is used. (#### Dynarr should have | |
393 been used!!!). The information in this list associated with a | |
394 description is | |
395 | |
396 -- pointer to the description | |
397 -- a pdump_block_list of blocks using that description | |
398 | |
399 Functions for working with lists of memory blocks: | |
400 | |
401 -- Add a memory block to a list using pdump_add_block() | |
402 | |
403 -- Get a memory block from a pointer to its beginning using | |
404 pdump_get_block(). This uses the hash table, which lists everything. | |
405 | |
406 -- Return the memory-block list (pdump_block_list) associated with a | |
407 descriptor, using pdump_get_block_list(). If no entry found in the | |
408 pdump_desc_list, add a new one. | |
409 | |
410 */ | |
411 | |
412 typedef struct pdump_block_list_elt | |
460 | 413 { |
2367 | 414 struct pdump_block_list_elt *next; |
442 | 415 const void *obj; |
665 | 416 Bytecount size; |
442 | 417 int count; |
418 EMACS_INT save_offset; | |
2367 | 419 } pdump_block_list_elt; |
442 | 420 |
421 typedef struct | |
422 { | |
2367 | 423 pdump_block_list_elt *first; |
442 | 424 int align; |
425 int count; | |
2367 | 426 } pdump_block_list; |
442 | 427 |
2367 | 428 typedef struct pdump_desc_list_elt |
442 | 429 { |
2367 | 430 pdump_block_list list; |
1204 | 431 const struct memory_description *desc; |
2367 | 432 } pdump_desc_list_elt; |
442 | 433 |
434 typedef struct | |
435 { | |
2367 | 436 pdump_desc_list_elt *list; |
442 | 437 int count; |
438 int size; | |
2367 | 439 } pdump_desc_list; |
442 | 440 |
2367 | 441 static pdump_block_list *pdump_object_table; |
442 static pdump_block_list pdump_opaque_data_list; | |
443 static pdump_desc_list pdump_desc_table; | |
442 | 444 |
460 | 445 static int *pdump_alert_undump_object; |
442 | 446 |
447 static unsigned long cur_offset; | |
665 | 448 static Bytecount max_size; |
442 | 449 static int pdump_fd; |
450 static void *pdump_buf; | |
458 | 451 static FILE *pdump_out; |
442 | 452 |
3263 | 453 #ifdef NEW_GC |
2775 | 454 /* PDUMP_HASHSIZE is a large prime. */ |
455 #define PDUMP_HASHSIZE 1000003 | |
456 /* Nothing special about PDUMP_HASH_MULTIPLIER: arbitrary odd integer | |
457 smaller than PDUMP_HASHSIZE. */ | |
458 #define PDUMP_HASH_MULTIPLIER 12347 | |
459 /* Nothing special about PDUMP_HASH_STEP: arbitrary integer for linear | |
460 probing. */ | |
461 #define PDUMP_HASH_STEP 574853 | |
3263 | 462 #else /* not NEW_GC */ |
442 | 463 #define PDUMP_HASHSIZE 200001 |
3263 | 464 #endif /* not NEW_GC */ |
442 | 465 |
2367 | 466 static pdump_block_list_elt **pdump_hash; |
442 | 467 |
3263 | 468 #ifndef NEW_GC |
442 | 469 /* Since most pointers are eight bytes aligned, the >>3 allows for a better hash */ |
3263 | 470 #endif /* not NEW_GC */ |
442 | 471 static int |
472 pdump_make_hash (const void *obj) | |
473 { | |
3263 | 474 #ifdef NEW_GC |
2775 | 475 return ((unsigned long)(obj) * PDUMP_HASH_MULTIPLIER) % PDUMP_HASHSIZE; |
3263 | 476 #else /* not NEW_GC */ |
442 | 477 return ((unsigned long)(obj)>>3) % PDUMP_HASHSIZE; |
3263 | 478 #endif /* not NEW_GC */ |
442 | 479 } |
480 | |
2367 | 481 /* Return the entry for an already-registered memory block at OBJ, |
482 or NULL if none. */ | |
483 | |
484 static pdump_block_list_elt * | |
485 pdump_get_block (const void *obj) | |
442 | 486 { |
487 int pos = pdump_make_hash (obj); | |
2367 | 488 pdump_block_list_elt *e; |
442 | 489 |
490 assert (obj != 0); | |
491 | |
492 while ((e = pdump_hash[pos]) != 0) | |
493 { | |
494 if (e->obj == obj) | |
495 return e; | |
496 | |
497 pos++; | |
498 if (pos == PDUMP_HASHSIZE) | |
499 pos = 0; | |
500 } | |
501 return 0; | |
502 } | |
503 | |
2367 | 504 /* Register a new memory block on Return the entry for an already-registered heap (?) memory block at OBJ, |
505 or NULL if none. */ | |
506 | |
442 | 507 static void |
2367 | 508 pdump_add_block (pdump_block_list *list, const void *obj, Bytecount size, |
458 | 509 int count) |
442 | 510 { |
2367 | 511 pdump_block_list_elt *e; |
442 | 512 int pos = pdump_make_hash (obj); |
513 | |
514 while ((e = pdump_hash[pos]) != 0) | |
515 { | |
516 if (e->obj == obj) | |
517 return; | |
518 | |
519 pos++; | |
520 if (pos == PDUMP_HASHSIZE) | |
521 pos = 0; | |
522 } | |
523 | |
2367 | 524 e = xnew (pdump_block_list_elt); |
442 | 525 |
526 e->next = list->first; | |
527 e->obj = obj; | |
528 e->size = size; | |
529 e->count = count; | |
530 list->first = e; | |
531 | |
532 list->count += count; | |
533 pdump_hash[pos] = e; | |
534 | |
460 | 535 { |
536 int align = pdump_size_to_align (size); | |
442 | 537 |
460 | 538 if (align < list->align) |
539 list->align = align; | |
540 } | |
442 | 541 } |
542 | |
3263 | 543 #ifdef NEW_GC |
2720 | 544 typedef struct mc_addr_elt |
545 { | |
546 const void *obj; | |
547 EMACS_INT addr; | |
548 } mc_addr_elt; | |
549 | |
550 static mc_addr_elt *pdump_mc_hash; | |
551 | |
552 /* Return the entry for an already-registered memory block at OBJ, | |
553 or NULL if none. */ | |
554 static EMACS_INT | |
555 pdump_get_mc_addr (const void *obj) | |
556 { | |
557 int pos = pdump_make_hash (obj); | |
558 mc_addr_elt *mc_addr; | |
559 | |
560 assert (obj != 0); | |
561 | |
2723 | 562 while (((mc_addr = &pdump_mc_hash[pos]) != 0) && (mc_addr->obj != 0)) |
2720 | 563 { |
564 if (mc_addr->obj == obj) | |
565 return mc_addr->addr; | |
566 | |
2775 | 567 pos += PDUMP_HASH_STEP; |
568 if (pos >= PDUMP_HASHSIZE) | |
569 pos -= PDUMP_HASHSIZE; | |
2720 | 570 } |
571 | |
572 /* If this code is reached, an heap address occurred which has not | |
573 been written to the lookup table before. | |
574 This is a bug! */ | |
575 ABORT(); | |
576 return 0; | |
577 } | |
578 | |
579 /* For indirect address lookups, needed for convertibles: Ptr points | |
580 to an address within an object. Indirect gives the offset by how | |
581 many bytes the address of the object has to be adjusted to do a | |
582 lookup in the mc_addr translation table and get the new location of | |
583 the data. */ | |
584 #define pdump_get_indirect_mc_addr(ptr, indirect) \ | |
585 pdump_get_mc_addr ((void *)((ptr) - indirect)) + indirect | |
586 | |
587 static void | |
588 pdump_put_mc_addr (const void *obj, EMACS_INT addr) | |
589 { | |
590 mc_addr_elt *mc_addr; | |
591 int pos = pdump_make_hash (obj); | |
592 | |
2723 | 593 while (((mc_addr = &pdump_mc_hash[pos]) != 0) && (mc_addr->obj != 0)) |
2720 | 594 { |
595 if (mc_addr->obj == obj) | |
596 return; | |
597 | |
2775 | 598 pos += PDUMP_HASH_STEP; |
599 if (pos >= PDUMP_HASHSIZE) | |
600 pos -= PDUMP_HASHSIZE; | |
2720 | 601 } |
602 | |
603 pdump_mc_hash[pos].obj = obj; | |
604 pdump_mc_hash[pos].addr = addr; | |
605 } | |
3263 | 606 #endif /* NEW_GC */ |
2720 | 607 |
2367 | 608 static pdump_block_list * |
609 pdump_get_block_list (const struct memory_description *desc) | |
442 | 610 { |
611 int i; | |
2367 | 612 for (i=0; i<pdump_desc_table.count; i++) |
613 if (pdump_desc_table.list[i].desc == desc) | |
614 return &pdump_desc_table.list[i].list; | |
442 | 615 |
2367 | 616 if (pdump_desc_table.size <= pdump_desc_table.count) |
442 | 617 { |
2367 | 618 if (pdump_desc_table.size == -1) |
619 pdump_desc_table.size = 10; | |
442 | 620 else |
2367 | 621 pdump_desc_table.size = pdump_desc_table.size * 2; |
622 pdump_desc_table.list = (pdump_desc_list_elt *) | |
623 xrealloc (pdump_desc_table.list, | |
624 pdump_desc_table.size * sizeof (pdump_desc_list_elt)); | |
442 | 625 } |
2367 | 626 pdump_desc_table.list[pdump_desc_table.count].list.first = 0; |
627 pdump_desc_table.list[pdump_desc_table.count].list.align = ALIGNOF (max_align_t); | |
628 pdump_desc_table.list[pdump_desc_table.count].list.count = 0; | |
629 pdump_desc_table.list[pdump_desc_table.count].desc = desc; | |
442 | 630 |
2367 | 631 return &pdump_desc_table.list[pdump_desc_table.count++].list; |
442 | 632 } |
633 | |
2551 | 634 static pdump_cv_ptr_info * |
635 pdump_find_in_cv_ptr_dynarr(const void *object) | |
636 { | |
637 int i; | |
638 for (i = 0; i < Dynarr_length (pdump_cv_ptr); i++) | |
639 if (Dynarr_at (pdump_cv_ptr, i).object == object) | |
640 return Dynarr_atp (pdump_cv_ptr, i); | |
641 return 0; | |
642 } | |
643 | |
2698 | 644 #define BACKTRACE_MAX 65536 |
645 | |
442 | 646 static struct |
647 { | |
648 struct lrecord_header *obj; | |
649 int position; | |
650 int offset; | |
2698 | 651 } backtrace[BACKTRACE_MAX]; |
442 | 652 |
1204 | 653 static int pdump_depth; |
442 | 654 |
1204 | 655 void |
452 | 656 pdump_backtrace (void) |
442 | 657 { |
658 int i; | |
659 stderr_out ("pdump backtrace :\n"); | |
1204 | 660 for (i = 0; i < pdump_depth; i++) |
442 | 661 { |
662 if (!backtrace[i].obj) | |
458 | 663 stderr_out (" - ind. (%d, %d)\n", |
664 backtrace[i].position, | |
665 backtrace[i].offset); | |
442 | 666 else |
667 { | |
668 stderr_out (" - %s (%d, %d)\n", | |
1204 | 669 LHEADER_IMPLEMENTATION (backtrace[i].obj)->name, |
670 backtrace[i].position, | |
671 backtrace[i].offset); | |
442 | 672 } |
673 } | |
674 } | |
675 | |
1204 | 676 static void |
1333 | 677 pdump_unsupported_dump_type (enum memory_description_type type, |
678 int do_backtrace) | |
679 { | |
680 stderr_out ("Unsupported dump type : %d\n", type); | |
681 #ifdef WIN32_NATIVE | |
682 stderr_out ("Are you compiling with SUPPORT_EDIT_AND_CONTINUE?\n"); | |
683 stderr_out ("See the PROBLEMS file.\n"); | |
684 #endif | |
685 if (do_backtrace) | |
686 pdump_backtrace (); | |
2500 | 687 ABORT (); |
1333 | 688 } |
689 | |
690 static void | |
1204 | 691 pdump_bump_depth (void) |
692 { | |
693 int me = pdump_depth++; | |
2698 | 694 if (me >= BACKTRACE_MAX) |
1204 | 695 { |
696 stderr_out ("Backtrace overflow, loop ?\n"); | |
2500 | 697 ABORT (); |
1204 | 698 } |
699 backtrace[me].obj = 0; | |
700 backtrace[me].position = 0; | |
701 backtrace[me].offset = 0; | |
702 } | |
703 | |
442 | 704 static void pdump_register_object (Lisp_Object obj); |
3092 | 705 #ifdef NEW_GC |
706 static void pdump_register_object_array (Lisp_Object data, | |
707 Bytecount size, | |
708 const struct memory_description *desc, | |
709 int count); | |
710 #endif /* NEW_GC */ | |
2367 | 711 static void pdump_register_block_contents (const void *data, |
712 Bytecount size, | |
713 const struct memory_description * | |
714 desc, | |
715 int count); | |
716 static void pdump_register_block (const void *data, | |
717 Bytecount size, | |
718 const struct memory_description *desc, | |
719 int count); | |
442 | 720 |
721 static void | |
1204 | 722 pdump_register_sub (const void *data, const struct memory_description *desc) |
442 | 723 { |
724 int pos; | |
1204 | 725 int me = pdump_depth - 1; |
442 | 726 |
727 for (pos = 0; desc[pos].type != XD_END; pos++) | |
728 { | |
1204 | 729 const struct memory_description *desc1 = &desc[pos]; |
730 EMACS_INT offset = lispdesc_indirect_count (desc1->offset, desc, | |
731 data); | |
2367 | 732 const void *rdata = (const Rawbyte *) data + offset; |
442 | 733 |
734 backtrace[me].position = pos; | |
1204 | 735 backtrace[me].offset = offset; |
736 | |
737 union_switcheroo: | |
442 | 738 |
1204 | 739 /* If the flag says don't dump, then don't dump. */ |
740 if ((desc1->flags) & XD_FLAG_NO_PDUMP) | |
741 continue; | |
742 | |
743 switch (desc1->type) | |
442 | 744 { |
665 | 745 case XD_BYTECOUNT: |
746 case XD_ELEMCOUNT: | |
747 case XD_HASHCODE: | |
442 | 748 case XD_INT: |
749 case XD_LONG: | |
750 case XD_INT_RESET: | |
751 case XD_LO_LINK: | |
752 break; | |
753 case XD_OPAQUE_DATA_PTR: | |
754 { | |
1204 | 755 EMACS_INT count = lispdesc_indirect_count (desc1->data1, desc, |
756 data); | |
442 | 757 |
2367 | 758 pdump_add_block (&pdump_opaque_data_list, |
458 | 759 *(void **)rdata, count, 1); |
442 | 760 break; |
761 } | |
2367 | 762 case XD_ASCII_STRING: |
442 | 763 { |
2367 | 764 const Ascbyte *str = * (const Ascbyte **) rdata; |
442 | 765 if (str) |
2367 | 766 pdump_add_block (&pdump_opaque_data_list, str, strlen (str) + 1, |
1204 | 767 1); |
442 | 768 break; |
769 } | |
770 case XD_DOC_STRING: | |
771 { | |
2367 | 772 const Ascbyte *str = * (const Ascbyte **) rdata; |
1204 | 773 if ((EMACS_INT) str > 0) |
2367 | 774 pdump_add_block (&pdump_opaque_data_list, str, strlen (str) + 1, |
1204 | 775 1); |
442 | 776 break; |
777 } | |
778 case XD_LISP_OBJECT: | |
779 { | |
1204 | 780 const Lisp_Object *pobj = (const Lisp_Object *) rdata; |
442 | 781 |
1204 | 782 assert (desc1->data1 == 0); |
442 | 783 |
2367 | 784 backtrace[me].offset = |
785 (const Rawbyte *) pobj - (const Rawbyte *) data; | |
442 | 786 pdump_register_object (*pobj); |
787 break; | |
788 } | |
789 case XD_LISP_OBJECT_ARRAY: | |
790 { | |
791 int i; | |
1204 | 792 EMACS_INT count = lispdesc_indirect_count (desc1->data1, desc, |
793 data); | |
442 | 794 |
795 for (i = 0; i < count; i++) | |
796 { | |
1204 | 797 const Lisp_Object *pobj = ((const Lisp_Object *) rdata) + i; |
442 | 798 Lisp_Object dobj = *pobj; |
799 | |
1204 | 800 backtrace[me].offset = |
2367 | 801 (const Rawbyte *) pobj - (const Rawbyte *) data; |
442 | 802 pdump_register_object (dobj); |
803 } | |
804 break; | |
805 } | |
3092 | 806 #ifdef NEW_GC |
5169
6c6d78781d59
cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents:
5059
diff
changeset
|
807 case XD_INLINE_LISP_OBJECT_BLOCK_PTR: |
3092 | 808 { |
809 EMACS_INT count = lispdesc_indirect_count (desc1->data1, desc, | |
810 data); | |
811 const struct sized_memory_description *sdesc = | |
812 lispdesc_indirect_description (data, desc1->data2.descr); | |
813 const Lisp_Object *pobj = (const Lisp_Object *) rdata; | |
814 if (pobj) | |
815 pdump_register_object_array | |
816 (*pobj, sdesc->size, sdesc->description, count); | |
817 break; | |
818 } | |
819 #endif /* NEW_GC */ | |
2367 | 820 case XD_BLOCK_PTR: |
442 | 821 { |
1204 | 822 EMACS_INT count = lispdesc_indirect_count (desc1->data1, desc, |
823 data); | |
824 const struct sized_memory_description *sdesc = | |
2551 | 825 lispdesc_indirect_description (data, desc1->data2.descr); |
2367 | 826 const Rawbyte *dobj = *(const Rawbyte **)rdata; |
442 | 827 if (dobj) |
2367 | 828 pdump_register_block (dobj, sdesc->size, sdesc->description, |
829 count); | |
442 | 830 break; |
831 } | |
2367 | 832 case XD_BLOCK_ARRAY: |
771 | 833 { |
1204 | 834 EMACS_INT count = lispdesc_indirect_count (desc1->data1, desc, |
835 data); | |
836 const struct sized_memory_description *sdesc = | |
2551 | 837 lispdesc_indirect_description (data, desc1->data2.descr); |
771 | 838 |
2367 | 839 pdump_register_block_contents (rdata, sdesc->size, |
840 sdesc->description, count); | |
771 | 841 break; |
842 } | |
843 case XD_UNION: | |
1204 | 844 case XD_UNION_DYNAMIC_SIZE: |
845 desc1 = lispdesc_process_xd_union (desc1, desc, data); | |
846 if (desc1) | |
847 goto union_switcheroo; | |
848 break; | |
2551 | 849 case XD_OPAQUE_PTR_CONVERTIBLE: |
850 { | |
851 pdump_cv_ptr_info info; | |
852 info.object = *(void **)rdata; | |
853 info.fcts = desc1->data2.funcs; | |
854 if (!pdump_find_in_cv_ptr_dynarr (info.object)) | |
855 { | |
856 info.fcts->convert(info.object, &info.data, &info.size); | |
857 Dynarr_add (pdump_cv_ptr, info); | |
858 } | |
859 break; | |
860 } | |
861 case XD_OPAQUE_DATA_CONVERTIBLE: | |
862 { | |
863 pdump_cv_data_info info; | |
864 info.object = data; | |
865 info.offset = offset; | |
866 info.fcts = desc1->data2.funcs; | |
867 | |
868 info.fcts->convert(rdata, &info.data, &info.size); | |
869 Dynarr_add (pdump_cv_data, info); | |
870 break; | |
871 } | |
771 | 872 |
442 | 873 default: |
1333 | 874 pdump_unsupported_dump_type (desc1->type, 1); |
1204 | 875 } |
442 | 876 } |
877 } | |
878 | |
879 static void | |
880 pdump_register_object (Lisp_Object obj) | |
881 { | |
882 struct lrecord_header *objh; | |
458 | 883 const struct lrecord_implementation *imp; |
442 | 884 |
885 if (!POINTER_TYPE_P (XTYPE (obj))) | |
886 return; | |
887 | |
888 objh = XRECORD_LHEADER (obj); | |
889 if (!objh) | |
890 return; | |
891 | |
2367 | 892 if (pdump_get_block (objh)) |
442 | 893 return; |
894 | |
458 | 895 imp = LHEADER_IMPLEMENTATION (objh); |
896 | |
934 | 897 if (imp->description |
3263 | 898 #ifdef NEW_GC |
899 /* Objects with finalizers cannot be dumped with the new | |
900 allocator's asynchronous finalization strategy. */ | |
901 && !imp->finalizer | |
902 #endif /* not NEW_GC */ | |
1204 | 903 && RECORD_DUMPABLE (objh)) |
442 | 904 { |
1204 | 905 pdump_bump_depth (); |
906 backtrace[pdump_depth - 1].obj = objh; | |
2367 | 907 pdump_add_block (pdump_object_table + objh->type, |
1204 | 908 objh, detagged_lisp_object_size (objh), 1); |
909 pdump_register_sub (objh, imp->description); | |
910 --pdump_depth; | |
442 | 911 } |
912 else | |
913 { | |
914 pdump_alert_undump_object[objh->type]++; | |
458 | 915 stderr_out ("Undumpable object type : %s\n", imp->name); |
442 | 916 pdump_backtrace (); |
917 } | |
918 } | |
919 | |
3092 | 920 #ifdef NEW_GC |
921 static void | |
922 pdump_register_object_array (Lisp_Object obj, | |
923 Bytecount size, | |
924 const struct memory_description *desc, | |
925 int count) | |
926 { | |
927 struct lrecord_header *objh; | |
928 const struct lrecord_implementation *imp; | |
929 | |
930 if (!POINTER_TYPE_P (XTYPE (obj))) | |
931 return; | |
932 | |
933 objh = XRECORD_LHEADER (obj); | |
934 if (!objh) | |
935 return; | |
936 | |
937 if (pdump_get_block (objh)) | |
938 return; | |
939 | |
940 imp = LHEADER_IMPLEMENTATION (objh); | |
941 | |
942 if (imp->description | |
943 && RECORD_DUMPABLE (objh)) | |
944 { | |
945 pdump_bump_depth (); | |
946 backtrace[pdump_depth - 1].obj = objh; | |
947 pdump_add_block (pdump_object_table + objh->type, | |
948 objh, lispdesc_block_size_1 (objh, size, desc), count); | |
949 pdump_register_block_contents (objh, size, desc, count); | |
950 --pdump_depth; | |
951 } | |
952 else | |
953 { | |
954 pdump_alert_undump_object[objh->type]++; | |
955 stderr_out ("Undumpable object type : %s\n", imp->name); | |
956 pdump_backtrace (); | |
957 } | |
958 } | |
959 #endif /* NEW_GC */ | |
960 | |
2367 | 961 /* Register the referenced objects in the array of COUNT blocks located at |
962 DATA; each block is described by SIZE and DESC. "Block" here simply | |
963 means any block of memory. | |
771 | 964 |
965 This does not register the block of memory itself; it may, for | |
966 example, be an array of structures inlined in another memory block | |
2367 | 967 and thus should not be registered. See pdump_register_block(), |
771 | 968 which does register the memory block. */ |
969 | |
970 static void | |
2367 | 971 pdump_register_block_contents (const void *data, |
972 Bytecount size, | |
973 const struct memory_description *desc, | |
974 int count) | |
771 | 975 { |
976 int i; | |
977 Bytecount elsize; | |
978 | |
1204 | 979 pdump_bump_depth (); |
2367 | 980 elsize = lispdesc_block_size_1 (data, size, desc); |
771 | 981 for (i = 0; i < count; i++) |
982 { | |
2367 | 983 pdump_register_sub (((Rawbyte *) data) + elsize * i, desc); |
771 | 984 } |
1204 | 985 --pdump_depth; |
771 | 986 } |
987 | |
2367 | 988 /* Register the array of COUNT blocks located at DATA; each block is |
989 described by SDESC. "Block" here simply means any block of memory, | |
990 which is more accurate and less confusing than terms like `struct' and | |
991 `object'. A `block' need not actually be a C "struct". It could be a | |
992 single integer or Lisp_Object, for example, as long as the description | |
993 is accurate. | |
771 | 994 |
2367 | 995 This is like pdump_register_block_contents() but also registers |
771 | 996 the memory block itself. */ |
997 | |
442 | 998 static void |
2367 | 999 pdump_register_block (const void *data, |
1000 Bytecount size, | |
1001 const struct memory_description *desc, | |
1002 int count) | |
442 | 1003 { |
2367 | 1004 if (data && !pdump_get_block (data)) |
442 | 1005 { |
2367 | 1006 pdump_add_block (pdump_get_block_list (desc), data, |
1007 lispdesc_block_size_1 (data, size, desc), count); | |
1008 pdump_register_block_contents (data, size, desc, count); | |
442 | 1009 } |
1010 } | |
1011 | |
2551 | 1012 |
1204 | 1013 /* Store the already-calculated new pointer offsets for all pointers in the |
1014 COUNT contiguous blocks of memory, each described by DESC and of size | |
1015 SIZE, whose original is located at ORIG_DATA and the modifiable copy at | |
1016 DATA. We examine the description to figure out where the pointers are, | |
2367 | 1017 and then look up the replacement values using pdump_get_block(). |
771 | 1018 |
1204 | 1019 This is done just before writing the modified block of memory to the |
1020 dump file. The new pointer offsets have been carefully calculated so | |
1021 that the data being pointed gets written at that offset in the dump | |
1022 file. That way, the dump file is a correct memory image except perhaps | |
1023 for a constant that needs to be added to all pointers. (#### In fact, we | |
1024 SHOULD be starting up a dumped XEmacs, seeing where the dumped file gets | |
1025 loaded into memory, and then rewriting the dumped file after relocating | |
1026 all the pointers relative to this memory location. That way, if the | |
1027 file gets loaded again at the same location, which will be common, we | |
1028 don't have to do any relocating, which is both faster at startup and | |
771 | 1029 allows the read-only part of the dumped data to be shared read-only |
1030 between different invocations of XEmacs.) | |
1031 | |
1032 #### Do we distinguish between read-only and writable dumped data? | |
1033 Should we? It's tricky because the dumped data, once loaded again, | |
1204 | 1034 cannot really be free()d or garbage collected since it's all stored in |
1035 one contiguous block of data with no malloc() headers, and we don't keep | |
1036 track of the pointers used internally in malloc() and the Lisp allocator | |
1037 to track allocated blocks of memory. */ | |
771 | 1038 |
1039 static void | |
1040 pdump_store_new_pointer_offsets (int count, void *data, const void *orig_data, | |
1204 | 1041 const struct memory_description *desc, |
771 | 1042 int size) |
1043 { | |
1044 int pos, i; | |
1045 /* Process each block one by one */ | |
1046 for (i = 0; i < count; i++) | |
1047 { | |
1048 /* CUR points to the beginning of each block in the new data. */ | |
2367 | 1049 Rawbyte *cur = ((Rawbyte *)data) + i * size; |
771 | 1050 /* Scan each line of the description for relocatable pointers */ |
1051 for (pos = 0; desc[pos].type != XD_END; pos++) | |
1052 { | |
1053 /* RDATA points to the beginning of each element in the new data. */ | |
1204 | 1054 const struct memory_description *desc1 = &desc[pos]; |
1055 /* #### Change ORIG_DATA to DATA. See below. */ | |
1056 void *rdata = cur + lispdesc_indirect_count (desc1->offset, desc, | |
1057 orig_data); | |
1058 union_switcheroo: | |
1059 | |
1060 /* If the flag says don't dump, then don't dump. */ | |
1061 if ((desc1->flags) & XD_FLAG_NO_PDUMP) | |
1062 continue; | |
1063 | |
1064 switch (desc1->type) | |
771 | 1065 { |
1066 case XD_BYTECOUNT: | |
1067 case XD_ELEMCOUNT: | |
1068 case XD_HASHCODE: | |
1069 case XD_INT: | |
1070 case XD_LONG: | |
1071 break; | |
1072 case XD_INT_RESET: | |
1073 { | |
1204 | 1074 EMACS_INT val = lispdesc_indirect_count (desc1->data1, desc, |
1075 orig_data); | |
771 | 1076 * (int *) rdata = val; |
1077 break; | |
1078 } | |
3092 | 1079 #ifdef NEW_GC |
5169
6c6d78781d59
cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents:
5059
diff
changeset
|
1080 case XD_INLINE_LISP_OBJECT_BLOCK_PTR: |
3092 | 1081 #endif /* NEW_GC */ |
771 | 1082 case XD_OPAQUE_DATA_PTR: |
2367 | 1083 case XD_ASCII_STRING: |
1084 case XD_BLOCK_PTR: | |
771 | 1085 { |
1086 void *ptr = * (void **) rdata; | |
1087 if (ptr) | |
2367 | 1088 * (EMACS_INT *) rdata = pdump_get_block (ptr)->save_offset; |
771 | 1089 break; |
1090 } | |
1091 case XD_LO_LINK: | |
1092 { | |
1093 /* As described in lrecord.h, this is a weak link. | |
1094 Thus, we need to link this object not (necessarily) | |
1095 to the object directly pointed to, but to the next | |
1096 referenced object in the chain. None of the | |
1097 intermediate objects will be written out, so we | |
1098 traverse down the chain of objects until we find a | |
1099 referenced one. (The Qnil or Qunbound that ends the | |
1100 chain will always be a referenced object.) */ | |
1101 Lisp_Object obj = * (Lisp_Object *) rdata; | |
2367 | 1102 pdump_block_list_elt *elt1; |
1204 | 1103 /* #### Figure out how to handle indirect offsets here. |
1104 #### In general, when computing indirect counts, do we | |
1105 really need to use the orig_data pointer? Why not just | |
1106 use the new stuff? | |
1107 | |
1108 No, we don't usually need orig_data. We only need it | |
1109 when fetching pointers out of the data, not integers. | |
1110 This currently occurs only with description maps. We | |
1111 should change the other places to DATA to emphasize | |
1112 this. */ | |
1113 assert (!XD_IS_INDIRECT (desc1->offset)); | |
771 | 1114 for (;;) |
1115 { | |
2367 | 1116 elt1 = pdump_get_block (XRECORD_LHEADER (obj)); |
771 | 1117 if (elt1) |
1118 break; | |
1204 | 1119 obj = * (Lisp_Object *) (desc1->offset + |
2367 | 1120 (Rawbyte *) |
1121 (XRECORD_LHEADER (obj))); | |
771 | 1122 } |
1123 * (EMACS_INT *) rdata = elt1->save_offset; | |
1124 break; | |
1125 } | |
1126 case XD_LISP_OBJECT: | |
1127 { | |
1128 Lisp_Object *pobj = (Lisp_Object *) rdata; | |
1129 | |
1204 | 1130 assert (desc1->data1 == 0); |
771 | 1131 |
1132 if (POINTER_TYPE_P (XTYPE (*pobj)) && XRECORD_LHEADER (*pobj)) | |
1133 * (EMACS_INT *) pobj = | |
2367 | 1134 pdump_get_block (XRECORD_LHEADER (*pobj))->save_offset; |
771 | 1135 break; |
1136 } | |
1137 case XD_LISP_OBJECT_ARRAY: | |
1138 { | |
1204 | 1139 EMACS_INT num = lispdesc_indirect_count (desc1->data1, desc, |
1140 orig_data); | |
771 | 1141 int j; |
1142 | |
1143 for (j = 0; j < num; j++) | |
1144 { | |
1145 Lisp_Object *pobj = ((Lisp_Object *) rdata) + j; | |
1146 if (POINTER_TYPE_P (XTYPE (*pobj)) && | |
1147 XRECORD_LHEADER (*pobj)) | |
1148 * (EMACS_INT *) pobj = | |
2367 | 1149 pdump_get_block (XRECORD_LHEADER (*pobj))->save_offset; |
771 | 1150 } |
1151 break; | |
1152 } | |
1153 case XD_DOC_STRING: | |
1154 { | |
1155 EMACS_INT str = *(EMACS_INT *)rdata; | |
1156 if (str > 0) | |
1157 * (EMACS_INT *) rdata = | |
2367 | 1158 pdump_get_block ((void *)str)->save_offset; |
771 | 1159 break; |
1160 } | |
2367 | 1161 case XD_BLOCK_ARRAY: |
771 | 1162 { |
1204 | 1163 EMACS_INT num = lispdesc_indirect_count (desc1->data1, desc, |
1164 orig_data); | |
1165 const struct sized_memory_description *sdesc = | |
2551 | 1166 lispdesc_indirect_description (orig_data, desc1->data2.descr); |
771 | 1167 |
1168 pdump_store_new_pointer_offsets | |
1169 (num, rdata, | |
2367 | 1170 ((Rawbyte *) rdata - (Rawbyte *) data) + |
1171 (Rawbyte *) orig_data, | |
1204 | 1172 sdesc->description, |
2367 | 1173 lispdesc_block_size |
1174 (((Rawbyte *) rdata - (Rawbyte *) data) + | |
1175 (Rawbyte *) orig_data, sdesc)); | |
771 | 1176 break; |
1177 } | |
1178 case XD_UNION: | |
1204 | 1179 case XD_UNION_DYNAMIC_SIZE: |
1180 desc1 = lispdesc_process_xd_union (desc1, desc, orig_data); | |
1181 if (desc1) | |
1182 goto union_switcheroo; | |
1183 break; | |
771 | 1184 |
2551 | 1185 case XD_OPAQUE_PTR_CONVERTIBLE: |
1186 *(EMACS_INT *)rdata = pdump_find_in_cv_ptr_dynarr (*(void **)rdata)->index; | |
1187 break; | |
1188 | |
1189 case XD_OPAQUE_DATA_CONVERTIBLE: | |
1190 /* in-object, nothing to do */ | |
1191 break; | |
1192 | |
771 | 1193 default: |
1333 | 1194 pdump_unsupported_dump_type (desc1->type, 0); |
771 | 1195 } |
1196 } | |
1197 } | |
1198 } | |
1199 | |
1200 /* Write out to global file descriptor PDUMP_OUT the element (one or | |
1201 more contiguous blocks of identical size/description) recorded in | |
1202 ELT and described by DESC. The element is first copied to a buffer | |
1203 and then all pointers (this includes Lisp_Objects other than | |
1204 integer/character) are relocated to the (pre-computed) offset in | |
1205 the dump file. */ | |
1206 | |
442 | 1207 static void |
2367 | 1208 pdump_dump_data (pdump_block_list_elt *elt, |
1204 | 1209 const struct memory_description *desc) |
442 | 1210 { |
665 | 1211 Bytecount size = elt->size; |
460 | 1212 int count = elt->count; |
442 | 1213 if (desc) |
1214 { | |
771 | 1215 /* Copy to temporary buffer */ |
460 | 1216 memcpy (pdump_buf, elt->obj, size*count); |
442 | 1217 |
771 | 1218 /* Store new offsets into all pointers in block */ |
1219 pdump_store_new_pointer_offsets (count, pdump_buf, elt->obj, desc, size); | |
1220 } | |
1221 retry_fwrite (desc ? pdump_buf : elt->obj, size, count, pdump_out); | |
1222 } | |
442 | 1223 |
3263 | 1224 #ifdef NEW_GC |
2720 | 1225 /* To be able to relocate during load time, more information about the |
1226 dumped objects are needed: The count (for array-like data | |
1227 structures), the size of the object, and the location in the dumped | |
1228 data. | |
1229 */ | |
1230 static void | |
1231 pdump_dump_mc_data (pdump_block_list_elt *elt, | |
1232 const struct memory_description *UNUSED(desc)) | |
1233 { | |
1234 EMACS_INT rdata = pdump_get_block (elt->obj)->save_offset; | |
1235 int j; | |
1236 PDUMP_WRITE_ALIGNED (int, elt->count); | |
1237 PDUMP_WRITE_ALIGNED (Bytecount, elt->size); | |
1238 for (j = 0; j < elt->count; j++) | |
1239 { | |
1240 PDUMP_WRITE_ALIGNED (EMACS_INT, rdata); | |
1241 rdata += elt->size; | |
1242 } | |
1243 } | |
1244 | |
1245 static void | |
1246 pdump_scan_lisp_objects_by_alignment (void (*f) | |
1247 (pdump_block_list_elt *, | |
1248 const struct memory_description *)) | |
1249 { | |
1250 int align; | |
1251 | |
1252 for (align = ALIGNOF (max_align_t); align; align>>=1) | |
1253 { | |
1254 int i; | |
1255 pdump_block_list_elt *elt; | |
1256 | |
1257 for (i=0; i<lrecord_type_count; i++) | |
1258 if (pdump_object_table[i].align == align) | |
1259 for (elt = pdump_object_table[i].first; elt; elt = elt->next) | |
1260 { | |
1261 f (elt, lrecord_implementations_table[i]->description); | |
1262 } | |
1263 } | |
1264 } | |
1265 | |
1266 static void | |
1267 pdump_scan_non_lisp_objects_by_alignment (void (*f) | |
1268 (pdump_block_list_elt *, | |
1269 const struct memory_description *)) | |
1270 { | |
1271 int align; | |
1272 | |
1273 for (align = ALIGNOF (max_align_t); align; align>>=1) | |
1274 { | |
1275 int i; | |
1276 pdump_block_list_elt *elt; | |
1277 | |
1278 for (i=0; i<pdump_desc_table.count; i++) | |
1279 { | |
1280 pdump_desc_list_elt list = pdump_desc_table.list[i]; | |
1281 if (list.list.align == align) | |
1282 for (elt = list.list.first; elt; elt = elt->next) | |
1283 f (elt, list.desc); | |
1284 } | |
1285 | |
1286 for (elt = pdump_opaque_data_list.first; elt; elt = elt->next) | |
1287 if (pdump_size_to_align (elt->size) == align) | |
1288 f (elt, 0); | |
1289 } | |
1290 } | |
1291 | |
1292 | |
1293 | |
1294 static void | |
1295 pdump_reloc_one_mc (void *data, const struct memory_description *desc) | |
1296 { | |
1297 int pos; | |
1298 | |
1299 for (pos = 0; desc[pos].type != XD_END; pos++) | |
1300 { | |
1301 const struct memory_description *desc1 = &desc[pos]; | |
1302 void *rdata = | |
1303 (Rawbyte *) data + lispdesc_indirect_count (desc1->offset, | |
1304 desc, data); | |
1305 | |
1306 union_switcheroo: | |
1307 | |
1308 /* If the flag says don't dump, then don't dump. */ | |
1309 if ((desc1->flags) & XD_FLAG_NO_PDUMP) | |
1310 continue; | |
1311 | |
1312 switch (desc1->type) | |
1313 { | |
1314 case XD_BYTECOUNT: | |
1315 case XD_ELEMCOUNT: | |
1316 case XD_HASHCODE: | |
1317 case XD_INT: | |
1318 case XD_LONG: | |
1319 case XD_INT_RESET: | |
1320 break; | |
5169
6c6d78781d59
cleanup of code related to xfree(), better KKCC backtrace capabilities, document XD_INLINE_LISP_OBJECT_BLOCK_PTR, fix some memory leaks, other code cleanup
Ben Wing <ben@xemacs.org>
parents:
5059
diff
changeset
|
1321 case XD_INLINE_LISP_OBJECT_BLOCK_PTR: |
2720 | 1322 case XD_OPAQUE_DATA_PTR: |
1323 case XD_ASCII_STRING: | |
1324 case XD_BLOCK_PTR: | |
1325 case XD_LO_LINK: | |
1326 { | |
1327 EMACS_INT ptr = *(EMACS_INT *) rdata; | |
1328 if (ptr) | |
1329 *(EMACS_INT *) rdata = pdump_get_mc_addr ((void *) ptr); | |
1330 break; | |
1331 } | |
1332 case XD_LISP_OBJECT: | |
1333 { | |
1334 Lisp_Object *pobj = (Lisp_Object *) rdata; | |
1335 | |
1336 assert (desc1->data1 == 0); | |
1337 | |
1338 if (POINTER_TYPE_P (XTYPE (*pobj)) | |
1339 && ! EQ (*pobj, Qnull_pointer)) | |
3092 | 1340 *pobj = wrap_pointer_1 ((Rawbyte *) pdump_get_mc_addr |
2720 | 1341 (XPNTR (*pobj))); |
1342 break; | |
1343 } | |
1344 case XD_LISP_OBJECT_ARRAY: | |
1345 { | |
1346 EMACS_INT num = lispdesc_indirect_count (desc1->data1, desc, | |
1347 data); | |
1348 int j; | |
1349 | |
1350 for (j=0; j<num; j++) | |
1351 { | |
1352 Lisp_Object *pobj = (Lisp_Object *) rdata + j; | |
1353 | |
1354 if (POINTER_TYPE_P (XTYPE (*pobj)) | |
1355 && ! EQ (*pobj, Qnull_pointer)) | |
3092 | 1356 *pobj = wrap_pointer_1 ((Rawbyte *) pdump_get_mc_addr |
2775 | 1357 (XPNTR (*pobj))); |
2720 | 1358 } |
1359 break; | |
1360 } | |
1361 case XD_DOC_STRING: | |
1362 { | |
1363 EMACS_INT str = *(EMACS_INT *) rdata; | |
1364 if (str > 0) | |
1365 *(EMACS_INT *) rdata = pdump_get_mc_addr ((void *) str); | |
1366 break; | |
1367 } | |
1368 case XD_BLOCK_ARRAY: | |
1369 { | |
1370 EMACS_INT num = lispdesc_indirect_count (desc1->data1, desc, | |
1371 data); | |
1372 int j; | |
1373 const struct sized_memory_description *sdesc = | |
1374 lispdesc_indirect_description (data, desc1->data2.descr); | |
1375 Bytecount size = lispdesc_block_size (rdata, sdesc); | |
1376 | |
1377 /* Note: We are recursing over data in the block itself */ | |
1378 for (j = 0; j < num; j++) | |
1379 pdump_reloc_one_mc ((Rawbyte *) rdata + j * size, | |
1380 sdesc->description); | |
1381 | |
1382 break; | |
1383 } | |
1384 case XD_UNION: | |
1385 case XD_UNION_DYNAMIC_SIZE: | |
1386 desc1 = lispdesc_process_xd_union (desc1, desc, data); | |
1387 if (desc1) | |
1388 goto union_switcheroo; | |
1389 break; | |
1390 | |
1391 case XD_OPAQUE_PTR_CONVERTIBLE: | |
1392 { | |
1393 pdump_cv_ptr_load_info *p = pdump_loaded_cv_ptr + *(EMACS_INT *)rdata; | |
1394 if (!p->adr) | |
1395 p->adr = desc1->data2.funcs->deconvert(0, | |
1396 pdump_start + p->save_offset, | |
1397 p->size); | |
1398 *(void **)rdata = p->adr; | |
1399 break; | |
1400 } | |
1401 | |
1402 case XD_OPAQUE_DATA_CONVERTIBLE: | |
1403 { | |
1404 EMACS_INT dest_offset = (EMACS_INT) rdata; | |
1405 EMACS_INT indirect = | |
1406 lispdesc_indirect_count (desc1->offset, desc, data); | |
1407 pdump_cv_data_dump_info *p; | |
1408 | |
1409 for(p = pdump_loaded_cv_data; | |
1410 pdump_get_indirect_mc_addr (p->dest_offset, indirect) | |
1411 != dest_offset; | |
1412 p++); | |
1413 | |
1414 desc1->data2.funcs->deconvert(rdata, pdump_start + p->save_offset, | |
1415 p->size); | |
1416 break; | |
1417 } | |
1418 | |
1419 default: | |
1420 pdump_unsupported_dump_type (desc1->type, 0); | |
1421 } | |
1422 } | |
1423 } | |
3263 | 1424 #else /* not NEW_GC */ |
771 | 1425 /* Relocate a single memory block at DATA, described by DESC, from its |
1204 | 1426 assumed load location to its actual one by adding DELTA to all pointers |
1427 in the block. Does not recursively relocate any other memory blocks | |
1428 pointed to. (We already have a list of all memory blocks in the dump | |
1429 file.) This is used once the dump data has been loaded back in, both | |
2367 | 1430 for blocks sitting in the dumped data (former heap blocks) and in global |
1431 data-sgment blocks whose contents have been restored from the dumped | |
1432 data. */ | |
442 | 1433 |
1434 static void | |
458 | 1435 pdump_reloc_one (void *data, EMACS_INT delta, |
1204 | 1436 const struct memory_description *desc) |
442 | 1437 { |
1438 int pos; | |
1439 | |
1440 for (pos = 0; desc[pos].type != XD_END; pos++) | |
1441 { | |
1204 | 1442 const struct memory_description *desc1 = &desc[pos]; |
2367 | 1443 void *rdata = |
1444 (Rawbyte *) data + lispdesc_indirect_count (desc1->offset, | |
1445 desc, data); | |
1204 | 1446 |
1447 union_switcheroo: | |
1448 | |
1449 /* If the flag says don't dump, then don't dump. */ | |
1450 if ((desc1->flags) & XD_FLAG_NO_PDUMP) | |
1451 continue; | |
1452 | |
1453 switch (desc1->type) | |
442 | 1454 { |
665 | 1455 case XD_BYTECOUNT: |
1456 case XD_ELEMCOUNT: | |
1457 case XD_HASHCODE: | |
442 | 1458 case XD_INT: |
1459 case XD_LONG: | |
1460 case XD_INT_RESET: | |
1461 break; | |
1462 case XD_OPAQUE_DATA_PTR: | |
2367 | 1463 case XD_ASCII_STRING: |
1464 case XD_BLOCK_PTR: | |
442 | 1465 case XD_LO_LINK: |
1466 { | |
1467 EMACS_INT ptr = *(EMACS_INT *)rdata; | |
1468 if (ptr) | |
1469 *(EMACS_INT *)rdata = ptr+delta; | |
1470 break; | |
1471 } | |
1472 case XD_LISP_OBJECT: | |
1473 { | |
1474 Lisp_Object *pobj = (Lisp_Object *) rdata; | |
1475 | |
1204 | 1476 assert (desc1->data1 == 0); |
442 | 1477 |
1478 if (POINTER_TYPE_P (XTYPE (*pobj)) | |
1479 && ! EQ (*pobj, Qnull_pointer)) | |
2367 | 1480 *pobj = wrap_pointer_1 ((Rawbyte *) XPNTR (*pobj) + delta); |
442 | 1481 |
1482 break; | |
1483 } | |
1484 case XD_LISP_OBJECT_ARRAY: | |
1485 { | |
1204 | 1486 EMACS_INT num = lispdesc_indirect_count (desc1->data1, desc, |
1487 data); | |
442 | 1488 int j; |
1489 | |
1490 for (j=0; j<num; j++) | |
1491 { | |
1492 Lisp_Object *pobj = (Lisp_Object *) rdata + j; | |
1493 | |
1494 if (POINTER_TYPE_P (XTYPE (*pobj)) | |
1495 && ! EQ (*pobj, Qnull_pointer)) | |
2367 | 1496 *pobj = wrap_pointer_1 ((Rawbyte *) XPNTR (*pobj) + |
1497 delta); | |
442 | 1498 } |
1499 break; | |
1500 } | |
1501 case XD_DOC_STRING: | |
1502 { | |
1503 EMACS_INT str = *(EMACS_INT *)rdata; | |
1504 if (str > 0) | |
1505 *(EMACS_INT *)rdata = str + delta; | |
1506 break; | |
1507 } | |
2367 | 1508 case XD_BLOCK_ARRAY: |
771 | 1509 { |
1204 | 1510 EMACS_INT num = lispdesc_indirect_count (desc1->data1, desc, |
1511 data); | |
771 | 1512 int j; |
1204 | 1513 const struct sized_memory_description *sdesc = |
2551 | 1514 lispdesc_indirect_description (data, desc1->data2.descr); |
2367 | 1515 Bytecount size = lispdesc_block_size (rdata, sdesc); |
771 | 1516 |
1517 /* Note: We are recursing over data in the block itself */ | |
1518 for (j = 0; j < num; j++) | |
2367 | 1519 pdump_reloc_one ((Rawbyte *) rdata + j * size, delta, |
771 | 1520 sdesc->description); |
1521 | |
1522 break; | |
1523 } | |
1204 | 1524 case XD_UNION: |
1525 case XD_UNION_DYNAMIC_SIZE: | |
1526 desc1 = lispdesc_process_xd_union (desc1, desc, data); | |
1527 if (desc1) | |
1528 goto union_switcheroo; | |
1529 break; | |
771 | 1530 |
2551 | 1531 case XD_OPAQUE_PTR_CONVERTIBLE: |
1532 { | |
1533 pdump_cv_ptr_load_info *p = pdump_loaded_cv_ptr + *(EMACS_INT *)rdata; | |
1534 if (!p->adr) | |
1535 p->adr = desc1->data2.funcs->deconvert(0, pdump_start + | |
1536 p->save_offset, p->size); | |
1537 *(void **)rdata = p->adr; | |
1538 break; | |
1539 } | |
1540 | |
1541 case XD_OPAQUE_DATA_CONVERTIBLE: | |
1542 { | |
1543 EMACS_INT dest_offset = (Rawbyte *)rdata - pdump_start; | |
1544 pdump_cv_data_dump_info *p; | |
1545 | |
1546 for(p = pdump_loaded_cv_data; p->dest_offset != dest_offset; p++); | |
1547 | |
1548 desc1->data2.funcs->deconvert(rdata, pdump_start + p->save_offset, | |
1549 p->size); | |
1550 break; | |
1551 } | |
1552 | |
442 | 1553 default: |
1333 | 1554 pdump_unsupported_dump_type (desc1->type, 0); |
1204 | 1555 } |
442 | 1556 } |
1557 } | |
3263 | 1558 #endif /* not NEW_GC */ |
442 | 1559 |
1560 static void | |
2367 | 1561 pdump_allocate_offset (pdump_block_list_elt *elt, |
2286 | 1562 const struct memory_description *UNUSED (desc)) |
442 | 1563 { |
665 | 1564 Bytecount size = elt->count * elt->size; |
460 | 1565 elt->save_offset = cur_offset; |
2367 | 1566 if (size > max_size) |
442 | 1567 max_size = size; |
1568 cur_offset += size; | |
1569 } | |
1570 | |
2551 | 1571 /* Write out to global file descriptor PDUMP_OUT the result of an |
1572 external element. It's just opaque data. */ | |
1573 | |
1574 static void | |
1575 pdump_dump_cv_data (pdump_cv_data_info *elt) | |
1576 { | |
1577 retry_fwrite (elt->data, elt->size, 1, pdump_out); | |
1578 } | |
1579 | |
1580 static void | |
1581 pdump_dump_cv_ptr (pdump_cv_ptr_info *elt) | |
1582 { | |
1583 retry_fwrite (elt->data, elt->size, 1, pdump_out); | |
1584 } | |
1585 | |
1586 static void | |
1587 pdump_allocate_offset_cv_data (pdump_cv_data_info *elt) | |
1588 { | |
1589 elt->save_offset = cur_offset; | |
1590 if (elt->size>max_size) | |
1591 max_size = elt->size; | |
1592 cur_offset += elt->size; | |
1593 } | |
1594 | |
1595 static void | |
1596 pdump_allocate_offset_cv_ptr (pdump_cv_ptr_info *elt) | |
1597 { | |
1598 elt->save_offset = cur_offset; | |
1599 if (elt->size>max_size) | |
1600 max_size = elt->size; | |
1601 cur_offset += elt->size; | |
1602 } | |
1603 | |
2367 | 1604 /* Traverse through all the heap blocks, once the "register" stage of |
1605 dumping has finished. To compress space as much as possible, we | |
1606 logically sort all blocks by alignment, hitting all blocks with | |
1607 alignment == the maximum (which may be 8 bytes, for doubles), then | |
1608 all blocks with the next lower alignment (4 bytes), etc. | |
1609 | |
1610 Within each alignment we hit | |
1611 | |
1612 -- first the Lisp objects, type-by-type | |
1613 | |
1614 -- then the heap memory blocks that are not Lisp objects, description-by- | |
1615 description -- i.e. all blocks with the same description will be | |
1616 placed together | |
1617 | |
1618 -- then the "opaque" data objects declared as XD_OPAQUE_DATA_PTR, | |
1619 XD_ASCII_STRING and XD_DOC_STRING. | |
1620 | |
1621 The idea is to have as little blank space as possible in the laid-out | |
1622 data. | |
1623 | |
1624 For each item that we have hit, we process it by calling F, the function | |
1625 passed it. In dumper.c, pdump_scan_by_alignment() is called twice with | |
1626 two different functions -- pdump_allocate_offset() in stage 2 to compute | |
1627 the offset to each block, and pdump_dump_data() in stage 3 to | |
1628 successively write each block to disk. | |
1629 | |
1630 It's extremely important that the SAME traversal order gets invoked | |
1631 in both stage 2 and 3. | |
1632 */ | |
1633 | |
442 | 1634 static void |
2367 | 1635 pdump_scan_by_alignment (void (*f)(pdump_block_list_elt *, |
2551 | 1636 const struct memory_description *), |
1637 void (*g)(pdump_cv_data_info *), | |
1638 void (*h)(pdump_cv_ptr_info *)) | |
442 | 1639 { |
460 | 1640 int align; |
1641 | |
1642 for (align = ALIGNOF (max_align_t); align; align>>=1) | |
442 | 1643 { |
460 | 1644 int i; |
2367 | 1645 pdump_block_list_elt *elt; |
460 | 1646 |
442 | 1647 for (i=0; i<lrecord_type_count; i++) |
1648 if (pdump_object_table[i].align == align) | |
460 | 1649 for (elt = pdump_object_table[i].first; elt; elt = elt->next) |
1650 f (elt, lrecord_implementations_table[i]->description); | |
442 | 1651 |
2367 | 1652 for (i=0; i<pdump_desc_table.count; i++) |
460 | 1653 { |
2367 | 1654 pdump_desc_list_elt list = pdump_desc_table.list[i]; |
460 | 1655 if (list.list.align == align) |
1656 for (elt = list.list.first; elt; elt = elt->next) | |
1204 | 1657 f (elt, list.desc); |
460 | 1658 } |
442 | 1659 |
460 | 1660 for (elt = pdump_opaque_data_list.first; elt; elt = elt->next) |
1661 if (pdump_size_to_align (elt->size) == align) | |
1662 f (elt, 0); | |
2551 | 1663 |
1664 for (i=0; i < Dynarr_length (pdump_cv_data); i++) | |
1665 if (pdump_size_to_align (Dynarr_atp (pdump_cv_data, i)->size) == align) | |
1666 g (Dynarr_atp (pdump_cv_data, i)); | |
1667 | |
1668 for (i=0; i < Dynarr_length (pdump_cv_ptr); i++) | |
1669 if (pdump_size_to_align (Dynarr_atp (pdump_cv_ptr, i)->size) == align) | |
1670 h (Dynarr_atp (pdump_cv_ptr, i)); | |
442 | 1671 } |
1672 } | |
1673 | |
2551 | 1674 static void |
1675 pdump_dump_cv_data_info (void) | |
1676 { | |
1677 int i; | |
1678 Elemcount count = Dynarr_length (pdump_cv_data); | |
1679 pdump_cv_data_dump_info *data = alloca_array (pdump_cv_data_dump_info, count); | |
1680 for (i = 0; i < count; i++) | |
1681 { | |
1682 data[i].dest_offset = Dynarr_at (pdump_cv_data, i).dest_offset; | |
1683 data[i].save_offset = Dynarr_at (pdump_cv_data, i).save_offset; | |
1684 data[i].size = Dynarr_at (pdump_cv_data, i).size; | |
1685 } | |
1686 | |
1687 PDUMP_ALIGN_OUTPUT (pdump_cv_data_dump_info); | |
1688 retry_fwrite (data, sizeof (pdump_cv_data_dump_info), count, pdump_out); | |
1689 } | |
1690 | |
442 | 1691 static void |
2551 | 1692 pdump_dump_cv_ptr_info (void) |
1693 { | |
1694 int i; | |
1695 Elemcount count = Dynarr_length (pdump_cv_ptr); | |
1696 pdump_cv_ptr_dump_info *data = alloca_array (pdump_cv_ptr_dump_info, count); | |
1697 for (i = 0; i < count; i++) | |
1698 { | |
1699 data[i].save_offset = Dynarr_at (pdump_cv_ptr, i).save_offset; | |
1700 data[i].size = Dynarr_at (pdump_cv_ptr, i).size; | |
1701 } | |
1702 | |
1703 PDUMP_ALIGN_OUTPUT (pdump_cv_ptr_dump_info); | |
1704 retry_fwrite (data, sizeof (pdump_cv_ptr_dump_info), count, pdump_out); | |
1705 } | |
1706 | |
3103 | 1707 /* Dump out the root block pointers, part of stage 3 (the "WRITE" stage) of |
1708 dumping. For each pointer we dump out a structure containing the | |
1709 location of the pointer and its value, replaced by the appropriate | |
1710 offset into the dumped data. */ | |
1711 | |
2551 | 1712 static void |
2367 | 1713 pdump_dump_root_block_ptrs (void) |
442 | 1714 { |
1715 int i; | |
2367 | 1716 Elemcount count = Dynarr_length (pdump_root_block_ptrs); |
458 | 1717 pdump_static_pointer *data = alloca_array (pdump_static_pointer, count); |
1718 for (i = 0; i < count; i++) | |
442 | 1719 { |
1333 | 1720 data[i].address = |
2367 | 1721 (Rawbyte **) Dynarr_atp (pdump_root_block_ptrs, i)->ptraddress; |
1333 | 1722 data[i].value = |
2367 | 1723 (Rawbyte *) pdump_get_block (* data[i].address)->save_offset; |
442 | 1724 } |
458 | 1725 PDUMP_ALIGN_OUTPUT (pdump_static_pointer); |
771 | 1726 retry_fwrite (data, sizeof (pdump_static_pointer), count, pdump_out); |
442 | 1727 } |
1728 | |
2367 | 1729 /* Dump out the root blocks, part of stage 3 (the "WRITE" stage) of |
1730 dumping. For each block we dump a structure containing info about the | |
1731 block (its location, size and description) and then the block itself, | |
1732 with its pointers replaced with offsets into the dump data. */ | |
1733 | |
442 | 1734 static void |
1204 | 1735 pdump_dump_root_blocks (void) |
442 | 1736 { |
1737 int i; | |
1204 | 1738 for (i = 0; i < Dynarr_length (pdump_root_blocks); i++) |
442 | 1739 { |
2367 | 1740 pdump_root_block info = Dynarr_at (pdump_root_blocks, i); |
1741 PDUMP_WRITE_ALIGNED (pdump_root_block, info); | |
1742 | |
1743 if (info.desc) | |
1744 { | |
1745 /* Copy to temporary buffer */ | |
1746 memcpy (pdump_buf, info.blockaddr, info.size); | |
1747 | |
1748 /* Store new offsets into all pointers in block */ | |
1749 pdump_store_new_pointer_offsets (1, pdump_buf, info.blockaddr, | |
1750 info.desc, info.size); | |
1751 } | |
1752 retry_fwrite (info.desc ? pdump_buf : info.blockaddr, | |
1753 info.size, 1, pdump_out); | |
442 | 1754 } |
1755 } | |
1756 | |
1757 static void | |
1758 pdump_dump_rtables (void) | |
1759 { | |
452 | 1760 int i; |
2367 | 1761 pdump_block_list_elt *elt; |
442 | 1762 pdump_reloc_table rt; |
1763 | |
1764 for (i=0; i<lrecord_type_count; i++) | |
1765 { | |
460 | 1766 elt = pdump_object_table[i].first; |
1767 if (!elt) | |
442 | 1768 continue; |
1769 rt.desc = lrecord_implementations_table[i]->description; | |
1770 rt.count = pdump_object_table[i].count; | |
458 | 1771 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt); |
460 | 1772 while (elt) |
442 | 1773 { |
2367 | 1774 EMACS_INT rdata = pdump_get_block (elt->obj)->save_offset; |
3092 | 1775 #ifdef NEW_GC |
1776 int j; | |
1777 for (j=0; j<elt->count; j++) | |
1778 { | |
1779 PDUMP_WRITE_ALIGNED (EMACS_INT, rdata); | |
1780 rdata += elt->size; | |
1781 } | |
1782 #else /* not NEW_GC */ | |
458 | 1783 PDUMP_WRITE_ALIGNED (EMACS_INT, rdata); |
3092 | 1784 #endif /* not NEW_GC */ |
460 | 1785 elt = elt->next; |
442 | 1786 } |
1787 } | |
1788 | |
1789 rt.desc = 0; | |
1790 rt.count = 0; | |
458 | 1791 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt); |
442 | 1792 |
2367 | 1793 for (i=0; i<pdump_desc_table.count; i++) |
442 | 1794 { |
2367 | 1795 elt = pdump_desc_table.list[i].list.first; |
1796 rt.desc = pdump_desc_table.list[i].desc; | |
1797 rt.count = pdump_desc_table.list[i].list.count; | |
458 | 1798 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt); |
460 | 1799 while (elt) |
442 | 1800 { |
2367 | 1801 EMACS_INT rdata = pdump_get_block (elt->obj)->save_offset; |
452 | 1802 int j; |
460 | 1803 for (j=0; j<elt->count; j++) |
442 | 1804 { |
458 | 1805 PDUMP_WRITE_ALIGNED (EMACS_INT, rdata); |
460 | 1806 rdata += elt->size; |
442 | 1807 } |
460 | 1808 elt = elt->next; |
442 | 1809 } |
1810 } | |
1811 rt.desc = 0; | |
1812 rt.count = 0; | |
458 | 1813 PDUMP_WRITE_ALIGNED (pdump_reloc_table, rt); |
442 | 1814 } |
1815 | |
1816 static void | |
1204 | 1817 pdump_dump_root_lisp_objects (void) |
442 | 1818 { |
1204 | 1819 Elemcount count = (Dynarr_length (pdump_root_lisp_objects) + |
647 | 1820 Dynarr_length (pdump_weak_object_chains)); |
665 | 1821 Elemcount i; |
442 | 1822 |
665 | 1823 PDUMP_WRITE_ALIGNED (Elemcount, count); |
458 | 1824 PDUMP_ALIGN_OUTPUT (pdump_static_Lisp_Object); |
442 | 1825 |
1204 | 1826 for (i = 0; i < Dynarr_length (pdump_root_lisp_objects); i++) |
442 | 1827 { |
458 | 1828 pdump_static_Lisp_Object obj; |
1204 | 1829 obj.address = Dynarr_at (pdump_root_lisp_objects, i); |
458 | 1830 obj.value = * obj.address; |
460 | 1831 |
458 | 1832 if (POINTER_TYPE_P (XTYPE (obj.value))) |
619 | 1833 obj.value = |
2367 | 1834 wrap_pointer_1 ((void *) pdump_get_block (XRECORD_LHEADER |
617 | 1835 (obj.value))->save_offset); |
460 | 1836 |
458 | 1837 PDUMP_WRITE (pdump_static_Lisp_Object, obj); |
442 | 1838 } |
1839 | |
2367 | 1840 for (i = 0; i < Dynarr_length (pdump_weak_object_chains); i++) |
442 | 1841 { |
2367 | 1842 pdump_block_list_elt *elt; |
458 | 1843 pdump_static_Lisp_Object obj; |
442 | 1844 |
458 | 1845 obj.address = Dynarr_at (pdump_weak_object_chains, i); |
1846 obj.value = * obj.address; | |
460 | 1847 |
442 | 1848 for (;;) |
1849 { | |
1204 | 1850 const struct memory_description *desc; |
442 | 1851 int pos; |
2367 | 1852 elt = pdump_get_block (XRECORD_LHEADER (obj.value)); |
460 | 1853 if (elt) |
442 | 1854 break; |
458 | 1855 desc = XRECORD_LHEADER_IMPLEMENTATION (obj.value)->description; |
442 | 1856 for (pos = 0; desc[pos].type != XD_LO_LINK; pos++) |
1857 assert (desc[pos].type != XD_END); | |
1858 | |
1204 | 1859 /* #### Figure out how to handle indirect offsets here. */ |
1860 assert (!XD_IS_INDIRECT (desc[pos].offset)); | |
1861 obj.value = | |
1862 * (Lisp_Object *) (desc[pos].offset + | |
2367 | 1863 (Rawbyte *) (XRECORD_LHEADER (obj.value))); |
442 | 1864 } |
619 | 1865 obj.value = wrap_pointer_1 ((void *) elt->save_offset); |
442 | 1866 |
458 | 1867 PDUMP_WRITE (pdump_static_Lisp_Object, obj); |
442 | 1868 } |
1869 } | |
1870 | |
2367 | 1871 |
1872 /*######################################################################## | |
1873 # Pdump # | |
1874 ######################################################################## | |
1875 | |
1876 [ben] | |
1877 | |
1878 DISCUSSION OF DUMPING: | |
1879 | |
1880 The idea of dumping is to record the state of XEmacs in a file, so that | |
1881 it can be reloaded later. This avoids having to reload all of the basic | |
1882 Lisp code each time XEmacs is run, which is a rather time-consuming | |
1883 process. (Less so on new machines, but still noticeable. As an example | |
1884 of a program with similar issues but which does not have a dumping | |
1885 process and as a result has a slow startup time, consider Adobe Photoshop | |
1886 5.0 or Adobe Photoshop Elements 2.0.) | |
1887 | |
1888 We don't actually record ALL the state of XEmacs (some of it, for example, | |
1889 is dependent on the run-time environment and needs to be initialized | |
1890 whenever XEmacs is run), but whatever state we don't record needs to be | |
1891 reinitialized every time XEmacs is run. | |
1892 | |
1893 The old way of dumping was to make a new executable file with the data | |
1894 segment expanded to contain the heap and written out from memory. This | |
1895 is what the unex* files do. Unfortunately this process is extremely | |
1896 system-specific and breaks easily with OS changes. | |
1897 | |
1898 Another simple, more portable trick, the "static heap" method, involves | |
1899 replacing the allocator with our own allocator which allocates all space | |
1900 out of a very large array declared in our data segment until we run out, | |
1901 then uses the underlying malloc() to start allocating on the heap. If we | |
1902 ensure that the large array is big enough to hold all data allocated | |
1903 during the dump stage, then all of the data we need to save is in the | |
1904 data segment, and it's easy to calculate the location and size of the | |
1905 data segment we want to save (we don't want to record and reinitialize | |
1906 the data segment of library functions) by using appropriately declared | |
1907 variables in the first and last file linked. This method is known as the | |
1908 "static heap" method, and is used by the non-pdump version of the dumper | |
1909 under Cygwin, and was also used under VMS and in Win-Emacs. | |
1910 | |
1911 The "static heap" method works well in practice. Nonetheless, a more | |
1912 complex method of dumping was written by Olivier Galibert, which requires | |
1913 that structural descriptions of all data allocated in the heap be provided | |
1914 and the roots of all pointers into the heap be noted through function calls | |
1915 to the pdump API. This way, all the heap data can be traversed and written | |
1916 out to a file, and then reloaded at run-time and the pointers relocated to | |
1917 point at the new location of the loaded data. This is the "pdump" method | |
1918 used in this file. | |
1919 | |
1920 There are two potential advantages of "pdump" over the "static heap": | |
1921 | |
1922 (1) It doesn't require any tricks to calculate the beginning and end of | |
1923 the data segment, or even that the XEmacs section of the data segment | |
1924 be contiguous. (It's not clear whether this is an issue in practice.) | |
1925 (2) Potentially, it could handle an OS that does not always load the | |
1926 static data segment at a predictable location. The "static heap" | |
1927 method by its nature needs the data segment to stay in the same place | |
1928 from invocation to invocation, since it simply dumps out memory and | |
1929 reloads it, without any pointer relocation. I say "potentially" | |
1930 because as it is currently written pdump does assume that the data | |
1931 segment is never relocated. However, changing pdump to remove this | |
1932 assumption is probably not difficult, as all the mechanism to handle | |
1933 pointer relocation is already present. | |
1934 | |
1935 | |
1936 DISCUSSION OF PDUMP WORKINGS: | |
1937 | |
1938 See man/internals/internals.texi for more information. | |
1939 | |
1940 NOTE that we have two kinds of memory to handle: memory on the heap | |
1941 (i.e. allocated through malloc()) or the like, and static memory in the | |
1942 data segment of the program, i.e. stuff declared as global or static. | |
1943 All heap memory needs to be written out to the dump file and reproduced | |
1944 (i.e. reloaded and any necessary relocations performed). Data-segment | |
1945 memory that is not statically initialized (i.e. through declarations in | |
1946 the C code) needs either to be written out and reloaded, or | |
1947 reinitialized. In addition, any pointers in data-segment memory to heap | |
1948 memory must be written out, reloaded and relocated. | |
1949 | |
1950 NOTE that we currently don't handle relocation of pointers into data- | |
1951 segment memory. (See overview discussion above.) These are treated in | |
1952 the descriptions as opaque data not needing relocation. If this becomes a | |
1953 problem, it can be fixed through new kinds of types in | |
1954 enum memory_description_type. | |
1955 | |
1956 Three basic steps to dumping out: | |
1957 | |
1958 (1) "REGISTER": | |
1959 Starting with all sources of relocatable memory (currently this means | |
1960 all data-segment pointers to heap memory -- see above about pointers | |
1961 to data-segment memory), recursively traverse the tree of pointers | |
1962 and "register" (make a note of) every memory block seen. | |
1963 | |
1964 (2) "LAYOUT": | |
1965 Go through all of the registered blocks and compute the location of | |
1966 each one in the dump data (i.e. the "offset" that will be added to | |
1967 the address corresponding to start of the loaded-in data to get the | |
1968 new pointer referring to this block). The blocks will be laid out | |
1969 sequentially according to the order we traverse them. Also note the | |
1970 maximum-sized block for use in step 3. | |
1971 | |
1972 (3) "WRITE": | |
1973 After writing some header stuff, go through all of the registered | |
1974 blocks and write out each one to the dump file. Note that we are | |
1975 simply writing out the blocks sequentially as we see them, and our | |
1976 traversal path is identical to that in step 2, so blocks will end up | |
1977 at the locations computed for them. In order to write out a block, | |
1978 first copy it to a temporary location (hence the maximum-block-size | |
1979 computation in the previous step), then for each relocatable pointer | |
1980 in the block, write in its place the offset to the heap block in the | |
1981 dump data. When the dump data is loaded, the address of the | |
1982 beginning of the dump data will be added to the offset in each | |
1983 pointer, and thence become accurate. | |
1984 | |
1985 --ben | |
1986 */ | |
1987 | |
442 | 1988 void |
1989 pdump (void) | |
1990 { | |
1991 int i; | |
1992 Lisp_Object t_console, t_device, t_frame; | |
1993 int none; | |
458 | 1994 pdump_header header; |
442 | 1995 |
1204 | 1996 in_pdump = 1; |
1997 | |
2367 | 1998 pdump_object_table = xnew_array (pdump_block_list, lrecord_type_count); |
460 | 1999 pdump_alert_undump_object = xnew_array (int, lrecord_type_count); |
2000 | |
2001 assert (ALIGNOF (max_align_t) <= pdump_align_table[0]); | |
2002 | |
2003 for (i = 0; i < countof (pdump_align_table); i++) | |
2004 if (pdump_align_table[i] > ALIGNOF (max_align_t)) | |
2005 pdump_align_table[i] = ALIGNOF (max_align_t); | |
2006 | |
446 | 2007 flush_all_buffer_local_cache (); |
2008 | |
442 | 2009 /* These appear in a DEFVAR_LISP, which does a staticpro() */ |
452 | 2010 t_console = Vterminal_console; Vterminal_console = Qnil; |
2011 t_frame = Vterminal_frame; Vterminal_frame = Qnil; | |
2012 t_device = Vterminal_device; Vterminal_device = Qnil; | |
442 | 2013 |
452 | 2014 dump_add_opaque (&lrecord_implementations_table, |
1204 | 2015 lrecord_type_count * |
2016 sizeof (lrecord_implementations_table[0])); | |
1676 | 2017 #ifdef USE_KKCC |
2018 dump_add_opaque (&lrecord_memory_descriptions, | |
2019 lrecord_type_count | |
2020 * sizeof (lrecord_memory_descriptions[0])); | |
2021 #else /* not USE_KKCC */ | |
452 | 2022 dump_add_opaque (&lrecord_markers, |
2023 lrecord_type_count * sizeof (lrecord_markers[0])); | |
1676 | 2024 #endif /* not USE_KKCC */ |
442 | 2025 |
2367 | 2026 pdump_hash = xnew_array_and_zero (pdump_block_list_elt *, PDUMP_HASHSIZE); |
442 | 2027 |
2367 | 2028 for (i = 0; i<lrecord_type_count; i++) |
442 | 2029 { |
2030 pdump_object_table[i].first = 0; | |
460 | 2031 pdump_object_table[i].align = ALIGNOF (max_align_t); |
442 | 2032 pdump_object_table[i].count = 0; |
2033 pdump_alert_undump_object[i] = 0; | |
2034 } | |
2367 | 2035 pdump_desc_table.count = 0; |
2036 pdump_desc_table.size = -1; | |
442 | 2037 |
2038 pdump_opaque_data_list.first = 0; | |
460 | 2039 pdump_opaque_data_list.align = ALIGNOF (max_align_t); |
442 | 2040 pdump_opaque_data_list.count = 0; |
1204 | 2041 pdump_depth = 0; |
442 | 2042 |
2551 | 2043 pdump_cv_data = Dynarr_new2 (pdump_cv_data_info_dynarr, pdump_cv_data_info); |
2044 pdump_cv_ptr = Dynarr_new2 (pdump_cv_ptr_info_dynarr, pdump_cv_ptr_info); | |
2045 | |
2367 | 2046 /* (I) The "register" stage: Note all heap memory blocks to be relocated |
2047 */ | |
2048 | |
2049 /* Try various roots of accessibility: */ | |
2050 | |
2051 /* (1) Lisp objects, both those declared using DEFVAR_LISP*() and those | |
2052 staticpro()d. */ | |
1204 | 2053 for (i = 0; i < Dynarr_length (pdump_root_lisp_objects); i++) |
2054 pdump_register_object (* Dynarr_at (pdump_root_lisp_objects, i)); | |
442 | 2055 |
2056 none = 1; | |
2367 | 2057 for (i = 0; i < lrecord_type_count; i++) |
442 | 2058 if (pdump_alert_undump_object[i]) |
2059 { | |
2060 if (none) | |
2367 | 2061 stderr_out ("Undumpable types list :\n"); |
442 | 2062 none = 0; |
2367 | 2063 stderr_out (" - %s (%d)\n", lrecord_implementations_table[i]->name, |
2064 pdump_alert_undump_object[i]); | |
442 | 2065 } |
2066 if (!none) | |
1204 | 2067 { |
2068 in_pdump = 0; | |
2069 return; | |
2070 } | |
442 | 2071 |
2367 | 2072 /* (2) Register out the data-segment pointer variables to heap blocks */ |
2073 for (i = 0; i < Dynarr_length (pdump_root_block_ptrs); i++) | |
452 | 2074 { |
2367 | 2075 pdump_root_block_ptr info = Dynarr_at (pdump_root_block_ptrs, i); |
2076 pdump_register_block (*(info.ptraddress), info.desc->size, | |
2077 info.desc->description, 1); | |
452 | 2078 } |
442 | 2079 |
2367 | 2080 /* (3) Register out the data-segment blocks, maybe with pointers to heap |
2081 blocks */ | |
2082 for (i = 0; i < Dynarr_length (pdump_root_blocks); i++) | |
2083 { | |
2084 pdump_root_block *info = Dynarr_atp (pdump_root_blocks, i); | |
2085 if (info->desc) | |
2086 { | |
2087 /* Size may have been given as 0 meaning "compute later". | |
2088 Compute now and update. If no DESC, size must always be | |
2089 correct as there is no other way of computing it. */ | |
2090 info->size = lispdesc_block_size_1 (info->blockaddr, info->size, | |
2091 info->desc); | |
2092 pdump_register_block_contents (info->blockaddr, info->size, | |
2093 info->desc, 1); | |
2094 } | |
2095 } | |
2096 | |
2097 /* (II) The "layout" stage: Compute the offsets and max-size */ | |
2098 | |
2099 /* (1) Determine header size */ | |
458 | 2100 memcpy (header.signature, PDUMP_SIGNATURE, PDUMP_SIGNATURE_LEN); |
2101 header.id = dump_id; | |
2102 header.reloc_address = 0; | |
2367 | 2103 header.nb_root_block_ptrs = Dynarr_length (pdump_root_block_ptrs); |
1204 | 2104 header.nb_root_blocks = Dynarr_length (pdump_root_blocks); |
2551 | 2105 header.nb_cv_data = Dynarr_length (pdump_cv_data); |
2106 header.nb_cv_ptr = Dynarr_length (pdump_cv_ptr); | |
442 | 2107 |
826 | 2108 cur_offset = MAX_ALIGN_SIZE (sizeof (header)); |
442 | 2109 max_size = 0; |
2110 | |
2367 | 2111 /* (2) Traverse all heap blocks and compute their offsets; keep track |
2112 of maximum block size seen */ | |
2551 | 2113 pdump_scan_by_alignment (pdump_allocate_offset, |
2114 pdump_allocate_offset_cv_data, | |
2115 pdump_allocate_offset_cv_ptr); | |
826 | 2116 cur_offset = MAX_ALIGN_SIZE (cur_offset); |
458 | 2117 header.stab_offset = cur_offset; |
442 | 2118 |
2367 | 2119 /* (3) Update maximum size based on root (data-segment) blocks */ |
2120 for (i = 0; i < Dynarr_length (pdump_root_blocks); i++) | |
2121 { | |
2122 pdump_root_block info = Dynarr_at (pdump_root_blocks, i); | |
2123 | |
2124 /* If no DESC, no relocation needed and we copy directly instead of | |
2125 into a temp buffer. */ | |
2126 if (info.desc) | |
2127 { | |
2128 if (info.size > max_size) | |
2129 max_size = info.size; | |
2130 } | |
2131 } | |
2132 | |
2133 /* (III) The "write "stage: Dump out the data, storing the offsets in | |
2134 place of pointers whenever we write out memory blocks */ | |
2135 | |
442 | 2136 pdump_buf = xmalloc (max_size); |
2367 | 2137 /* EMACS_PROGNAME is entirely ASCII so this should be Mule-safe */ |
442 | 2138 pdump_fd = open (EMACS_PROGNAME ".dmp", |
2139 O_WRONLY | O_CREAT | O_TRUNC | OPEN_BINARY, 0666); | |
771 | 2140 if (pdump_fd < 0) |
2141 report_file_error ("Unable to open dump file", | |
4952
19a72041c5ed
Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents:
4388
diff
changeset
|
2142 build_ascstring (EMACS_PROGNAME ".dmp")); |
458 | 2143 pdump_out = fdopen (pdump_fd, "w"); |
5837
09b5be18ba0e
fdopen returns NULL when it fails.
Marcus Crestani <marcus@crestani.de>
parents:
5727
diff
changeset
|
2144 if (pdump_out == NULL) |
771 | 2145 report_file_error ("Unable to open dump file for writing", |
4952
19a72041c5ed
Mule-izing, various fixes related to char * arguments
Ben Wing <ben@xemacs.org>
parents:
4388
diff
changeset
|
2146 build_ascstring (EMACS_PROGNAME ".dmp")); |
442 | 2147 |
771 | 2148 retry_fwrite (&header, sizeof (header), 1, pdump_out); |
458 | 2149 PDUMP_ALIGN_OUTPUT (max_align_t); |
442 | 2150 |
2551 | 2151 for (i = 0; i < Dynarr_length (pdump_cv_data); i++) |
2152 { | |
2153 pdump_cv_data_info *elt = Dynarr_atp (pdump_cv_data, i); | |
2154 elt->dest_offset = | |
2155 pdump_get_block (elt->object)->save_offset + elt->offset; | |
2156 } | |
2157 | |
2158 for (i = 0; i < Dynarr_length (pdump_cv_ptr); i++) | |
2159 Dynarr_at (pdump_cv_ptr, i).index = i; | |
2160 | |
2161 pdump_scan_by_alignment (pdump_dump_data, pdump_dump_cv_data, pdump_dump_cv_ptr); | |
2162 | |
2163 for (i = 0; i < Dynarr_length (pdump_cv_data); i++) | |
2164 { | |
2165 pdump_cv_data_info *elt = Dynarr_atp (pdump_cv_data, i); | |
2166 if(elt->fcts->convert_free) | |
2167 elt->fcts->convert_free(elt->object, elt->data, elt->size); | |
2168 } | |
2169 | |
2170 for (i = 0; i < Dynarr_length (pdump_cv_ptr); i++) | |
2171 { | |
2172 pdump_cv_ptr_info *elt = Dynarr_atp (pdump_cv_ptr, i); | |
2173 if(elt->fcts->convert_free) | |
2174 elt->fcts->convert_free(elt->object, elt->data, elt->size); | |
2175 } | |
442 | 2176 |
5839
d139eb1fead8
Check return value of fseek.
Marcus Crestani <marcus@crestani.de>
parents:
5838
diff
changeset
|
2177 if (FSEEK (pdump_out, header.stab_offset, SEEK_SET) == -1) |
d139eb1fead8
Check return value of fseek.
Marcus Crestani <marcus@crestani.de>
parents:
5838
diff
changeset
|
2178 { |
d139eb1fead8
Check return value of fseek.
Marcus Crestani <marcus@crestani.de>
parents:
5838
diff
changeset
|
2179 report_file_error ("Unable to fseek dump file", |
d139eb1fead8
Check return value of fseek.
Marcus Crestani <marcus@crestani.de>
parents:
5838
diff
changeset
|
2180 build_ascstring (EMACS_PROGNAME ".dmp")); |
d139eb1fead8
Check return value of fseek.
Marcus Crestani <marcus@crestani.de>
parents:
5838
diff
changeset
|
2181 } |
442 | 2182 |
3263 | 2183 #ifdef NEW_GC |
2720 | 2184 { |
2185 EMACS_INT zero = 0; | |
2186 pdump_scan_lisp_objects_by_alignment (pdump_dump_mc_data); | |
2187 PDUMP_WRITE_ALIGNED (EMACS_INT, zero); | |
2188 pdump_scan_non_lisp_objects_by_alignment (pdump_dump_mc_data); | |
2189 PDUMP_WRITE_ALIGNED (EMACS_INT, zero); | |
2190 } | |
3263 | 2191 #endif /* NEW_GC */ |
2551 | 2192 pdump_dump_cv_data_info (); |
2193 pdump_dump_cv_ptr_info (); | |
3263 | 2194 #ifdef NEW_GC |
2720 | 2195 pdump_dump_rtables (); |
3263 | 2196 #endif /* NEW_GC */ |
2367 | 2197 pdump_dump_root_block_ptrs (); |
1204 | 2198 pdump_dump_root_blocks (); |
3263 | 2199 #ifndef NEW_GC |
442 | 2200 pdump_dump_rtables (); |
3263 | 2201 #endif /* not NEW_GC */ |
1204 | 2202 pdump_dump_root_lisp_objects (); |
442 | 2203 |
771 | 2204 retry_fclose (pdump_out); |
3964 | 2205 /* pdump_fd is already closed by the preceding call to fclose. |
2206 retry_close (pdump_fd); */ | |
458 | 2207 |
442 | 2208 free (pdump_buf); |
2209 | |
2210 free (pdump_hash); | |
2211 | |
2212 Vterminal_console = t_console; | |
2213 Vterminal_frame = t_frame; | |
2214 Vterminal_device = t_device; | |
1204 | 2215 in_pdump = 0; |
442 | 2216 } |
2217 | |
452 | 2218 static int |
2219 pdump_load_check (void) | |
442 | 2220 { |
2367 | 2221 return (!memcmp (((pdump_header *) pdump_start)->signature, |
452 | 2222 PDUMP_SIGNATURE, PDUMP_SIGNATURE_LEN) |
2223 && ((pdump_header *)pdump_start)->id == dump_id); | |
442 | 2224 } |
2225 | |
458 | 2226 /*----------------------------------------------------------------------*/ |
2227 /* Reading the dump file */ | |
2228 /*----------------------------------------------------------------------*/ | |
452 | 2229 static int |
2230 pdump_load_finish (void) | |
442 | 2231 { |
2232 int i; | |
2367 | 2233 Rawbyte *p; |
442 | 2234 EMACS_INT delta; |
2235 EMACS_INT count; | |
1204 | 2236 pdump_header *header = (pdump_header *) pdump_start; |
442 | 2237 |
3092 | 2238 #ifdef NEW_GC |
2239 /* This is a DEFVAR_BOOL and gets dumped, but the actual value was | |
2240 already determined by vdb_install_signal_handler () in | |
2241 vdb-mprotect.c, which could be different from the value in the | |
2242 dump file. So store it here and restore it after loading the dump | |
2243 file. */ | |
2244 int allow_inc_gc = allow_incremental_gc; | |
2245 #endif /* NEW_GC */ | |
442 | 2246 pdump_end = pdump_start + pdump_length; |
2247 | |
1204 | 2248 delta = ((EMACS_INT) pdump_start) - header->reloc_address; |
458 | 2249 p = pdump_start + header->stab_offset; |
442 | 2250 |
3263 | 2251 #ifdef NEW_GC |
2720 | 2252 pdump_mc_hash = xnew_array_and_zero (mc_addr_elt, PDUMP_HASHSIZE); |
2253 | |
2254 /* Allocate space for each object individually. First the | |
2255 Lisp_Objects, then the blocks. */ | |
2256 count = 2; | |
2257 for (;;) | |
2258 { | |
2824 | 2259 EMACS_INT elt_count = PDUMP_READ_ALIGNED (p, EMACS_INT); |
2720 | 2260 if (elt_count) |
2261 { | |
2262 Rawbyte *mc_addr = 0; | |
2263 Bytecount size = PDUMP_READ_ALIGNED (p, Bytecount); | |
2264 for (i = 0; i < elt_count; i++) | |
2265 { | |
2266 EMACS_INT rdata = PDUMP_READ_ALIGNED (p, EMACS_INT); | |
2267 | |
2268 if (i == 0) | |
2269 { | |
2270 Bytecount real_size = size * elt_count; | |
2271 if (count == 2) | |
2775 | 2272 { |
3092 | 2273 if (elt_count <= 1) |
2274 mc_addr = (Rawbyte *) mc_alloc (real_size); | |
2275 else | |
2276 mc_addr = (Rawbyte *) mc_alloc_array (size, elt_count); | |
2994 | 2277 #ifdef ALLOC_TYPE_STATS |
2775 | 2278 inc_lrecord_stats (real_size, |
2279 (const struct lrecord_header *) | |
3092 | 2280 ((Rawbyte *) rdata + delta)); |
2994 | 2281 #endif /* ALLOC_TYPE_STATS */ |
2775 | 2282 } |
2720 | 2283 else |
2284 mc_addr = (Rawbyte *) xmalloc_and_zero (real_size); | |
2285 } | |
2286 else | |
2287 mc_addr += size; | |
2288 | |
2289 pdump_put_mc_addr ((void *) rdata, (EMACS_INT) mc_addr); | |
3092 | 2290 memcpy (mc_addr, (Rawbyte *) rdata + delta, size); |
2720 | 2291 } |
2292 } | |
2293 else if (!(--count)) | |
2294 break; | |
2295 } | |
3263 | 2296 #endif /* NEW_GC */ |
2720 | 2297 |
2551 | 2298 /* Get the cv_data array */ |
2553 | 2299 p = (Rawbyte *) ALIGN_PTR (p, pdump_cv_data_dump_info); |
2551 | 2300 pdump_loaded_cv_data = (pdump_cv_data_dump_info *)p; |
2301 p += header->nb_cv_data*sizeof(pdump_cv_data_dump_info); | |
2302 | |
2303 /* Build the cv_ptr array */ | |
2553 | 2304 p = (Rawbyte *) ALIGN_PTR (p, pdump_cv_ptr_dump_info); |
2551 | 2305 pdump_loaded_cv_ptr = |
2306 alloca_array (pdump_cv_ptr_load_info, header->nb_cv_ptr); | |
2307 for (i = 0; i < header->nb_cv_ptr; i++) | |
2308 { | |
2309 pdump_cv_ptr_dump_info info = PDUMP_READ (p, pdump_cv_ptr_dump_info); | |
2310 pdump_loaded_cv_ptr[i].save_offset = info.save_offset; | |
2311 pdump_loaded_cv_ptr[i].size = info.size; | |
2312 pdump_loaded_cv_ptr[i].adr = 0; | |
2313 } | |
2314 | |
3263 | 2315 #ifdef NEW_GC |
2720 | 2316 /* Relocate the heap objects */ |
2317 pdump_rt_list = p; | |
2318 count = 2; | |
2319 for (;;) | |
2320 { | |
2321 pdump_reloc_table rt = PDUMP_READ_ALIGNED (p, pdump_reloc_table); | |
2322 p = (Rawbyte *) ALIGN_PTR (p, Rawbyte *); | |
2323 if (rt.desc) | |
2324 { | |
3092 | 2325 Rawbyte **reloc = (Rawbyte **) p; |
2720 | 2326 for (i = 0; i < rt.count; i++) |
2327 { | |
3092 | 2328 reloc[i] = (Rawbyte *) pdump_get_mc_addr (reloc[i]); |
2720 | 2329 pdump_reloc_one_mc (reloc[i], rt.desc); |
2330 } | |
3092 | 2331 p += rt.count * sizeof (Rawbyte *); |
2720 | 2332 } |
2333 else if (!(--count)) | |
2334 break; | |
2335 } | |
3263 | 2336 #endif /* NEW_GC */ |
2720 | 2337 |
2367 | 2338 /* Put back the pdump_root_block_ptrs */ |
2339 p = (Rawbyte *) ALIGN_PTR (p, pdump_static_pointer); | |
2340 for (i = 0; i < header->nb_root_block_ptrs; i++) | |
442 | 2341 { |
458 | 2342 pdump_static_pointer ptr = PDUMP_READ (p, pdump_static_pointer); |
3263 | 2343 #ifdef NEW_GC |
2720 | 2344 (* ptr.address) = (Rawbyte *) pdump_get_mc_addr (ptr.value); |
3263 | 2345 #else /* not NEW_GC */ |
458 | 2346 (* ptr.address) = ptr.value + delta; |
3263 | 2347 #endif /* not NEW_GC */ |
442 | 2348 } |
2349 | |
1204 | 2350 /* Put back the pdump_root_blocks and relocate */ |
2351 for (i = 0; i < header->nb_root_blocks; i++) | |
442 | 2352 { |
1204 | 2353 pdump_root_block info = PDUMP_READ_ALIGNED (p, pdump_root_block); |
2367 | 2354 memcpy ((void *) info.blockaddr, p, info.size); |
1204 | 2355 if (info.desc) |
3263 | 2356 #ifdef NEW_GC |
2720 | 2357 pdump_reloc_one_mc ((void *) info.blockaddr, info.desc); |
3263 | 2358 #else /* not NEW_GC */ |
2367 | 2359 pdump_reloc_one ((void *) info.blockaddr, delta, info.desc); |
3263 | 2360 #endif /* not NEW_GC */ |
452 | 2361 p += info.size; |
442 | 2362 } |
2363 | |
3263 | 2364 #ifndef NEW_GC |
1204 | 2365 /* Relocate the heap objects */ |
442 | 2366 pdump_rt_list = p; |
2367 count = 2; | |
2368 for (;;) | |
2369 { | |
458 | 2370 pdump_reloc_table rt = PDUMP_READ_ALIGNED (p, pdump_reloc_table); |
2367 | 2371 p = (Rawbyte *) ALIGN_PTR (p, Rawbyte *); |
442 | 2372 if (rt.desc) |
2373 { | |
2367 | 2374 Rawbyte **reloc = (Rawbyte **) p; |
1204 | 2375 for (i = 0; i < rt.count; i++) |
442 | 2376 { |
458 | 2377 reloc[i] += delta; |
2378 pdump_reloc_one (reloc[i], delta, rt.desc); | |
442 | 2379 } |
2367 | 2380 p += rt.count * sizeof (Rawbyte *); |
1204 | 2381 } |
2382 else if (!(--count)) | |
2383 break; | |
442 | 2384 } |
3263 | 2385 #endif /* not NEW_GC */ |
442 | 2386 |
1204 | 2387 /* Put the pdump_root_lisp_objects variables in place */ |
665 | 2388 i = PDUMP_READ_ALIGNED (p, Elemcount); |
2367 | 2389 p = (Rawbyte *) ALIGN_PTR (p, pdump_static_Lisp_Object); |
458 | 2390 while (i--) |
442 | 2391 { |
458 | 2392 pdump_static_Lisp_Object obj = PDUMP_READ (p, pdump_static_Lisp_Object); |
442 | 2393 |
458 | 2394 if (POINTER_TYPE_P (XTYPE (obj.value))) |
3263 | 2395 #ifdef NEW_GC |
2720 | 2396 obj.value = wrap_pointer_1 ((Rawbyte *) pdump_get_mc_addr |
2397 (XPNTR (obj.value))); | |
3263 | 2398 #else /* not NEW_GC */ |
2720 | 2399 obj.value = wrap_pointer_1 ((Rawbyte *) XPNTR (obj.value) + delta); |
3263 | 2400 #endif /* not NEW_GC */ |
442 | 2401 |
458 | 2402 (* obj.address) = obj.value; |
442 | 2403 } |
2404 | |
2405 /* Final cleanups */ | |
2406 /* reorganize hash tables */ | |
2407 p = pdump_rt_list; | |
2408 for (;;) | |
2409 { | |
458 | 2410 pdump_reloc_table rt = PDUMP_READ_ALIGNED (p, pdump_reloc_table); |
2367 | 2411 p = (Rawbyte *) ALIGN_PTR (p, Lisp_Object); |
442 | 2412 if (!rt.desc) |
2413 break; | |
2414 if (rt.desc == hash_table_description) | |
2415 { | |
1204 | 2416 for (i = 0; i < rt.count; i++) |
442 | 2417 pdump_reorganize_hash_table (PDUMP_READ (p, Lisp_Object)); |
2418 break; | |
1204 | 2419 } |
2420 else | |
2421 p += sizeof (Lisp_Object) * rt.count; | |
442 | 2422 } |
2423 | |
3263 | 2424 #ifdef NEW_GC |
4976
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
4952
diff
changeset
|
2425 xfree (pdump_mc_hash); |
3263 | 2426 #endif /* NEW_GC */ |
2720 | 2427 |
3092 | 2428 #ifdef NEW_GC |
2429 allow_incremental_gc = allow_inc_gc; | |
2430 #endif /* NEW_GC */ | |
2431 | |
442 | 2432 return 1; |
2433 } | |
2434 | |
2435 #ifdef WIN32_NATIVE | |
2436 /* Free the mapped file if we decide we don't want it after all */ | |
452 | 2437 static void |
2438 pdump_file_unmap (void) | |
442 | 2439 { |
2440 UnmapViewOfFile (pdump_start); | |
2441 CloseHandle (pdump_hFile); | |
2442 CloseHandle (pdump_hMap); | |
2443 } | |
2444 | |
452 | 2445 static int |
2367 | 2446 pdump_file_get (const Wexttext *wpath) |
442 | 2447 { |
2367 | 2448 Extbyte *path; |
2449 if (XEUNICODE_P) | |
2450 path = (Extbyte *) wpath; | |
2451 else | |
2452 path = WEXTTEXT_TO_MULTIBYTE (wpath); | |
442 | 2453 |
2367 | 2454 pdump_hFile = |
2455 qxeCreateFile (path, | |
2456 GENERIC_READ + GENERIC_WRITE, /* Required for copy on | |
2457 write */ | |
2458 0, /* Not shared */ | |
2459 NULL, /* Not inheritable */ | |
2460 OPEN_EXISTING, | |
2461 FILE_ATTRIBUTE_NORMAL, | |
2462 NULL); /* No template file */ | |
442 | 2463 if (pdump_hFile == INVALID_HANDLE_VALUE) |
2464 return 0; | |
2465 | |
2466 pdump_length = GetFileSize (pdump_hFile, NULL); | |
2367 | 2467 pdump_hMap = |
2468 qxeCreateFileMapping (pdump_hFile, | |
2469 NULL, /* No security attributes */ | |
2470 PAGE_WRITECOPY, /* Copy on write */ | |
2471 0, /* Max size, high half */ | |
2472 0, /* Max size, low half */ | |
2473 NULL); /* Unnamed */ | |
442 | 2474 if (pdump_hMap == INVALID_HANDLE_VALUE) |
2475 return 0; | |
2476 | |
2367 | 2477 pdump_start = |
2478 (Rawbyte *) MapViewOfFile (pdump_hMap, | |
2479 FILE_MAP_COPY, /* Copy on write */ | |
2480 0, /* Start at zero */ | |
2481 0, | |
2482 0); /* Map all of it */ | |
442 | 2483 pdump_free = pdump_file_unmap; |
2484 return 1; | |
2485 } | |
2486 | |
2487 /* pdump_resource_free is called (via the pdump_free pointer) to release | |
2488 any resources allocated by pdump_resource_get. Since the Windows API | |
2489 specs specifically state that you don't need to (and shouldn't) free the | |
2490 resources allocated by FindResource, LoadResource, and LockResource this | |
2491 routine does nothing. */ | |
452 | 2492 static void |
2493 pdump_resource_free (void) | |
442 | 2494 { |
2495 } | |
2496 | |
452 | 2497 static int |
2498 pdump_resource_get (void) | |
442 | 2499 { |
452 | 2500 HRSRC hRes; /* Handle to dump resource */ |
2501 HRSRC hResLoad; /* Handle to loaded dump resource */ | |
442 | 2502 |
2503 /* See Q126630 which describes how Windows NT and 95 trap writes to | |
2504 resource sections and duplicate the page to allow the write to proceed. | |
2505 It also describes how to make the resource section read/write (and hence | |
2506 private to each process). Doing this avoids the exceptions and related | |
2507 overhead, but causes the resource section to be private to each process | |
2508 that is running XEmacs. Since the resource section contains little | |
2509 other than the dumped data, which should be private to each process, we | |
2510 make the whole resource section read/write so we don't have to copy it. */ | |
2511 | |
800 | 2512 hRes = FindResourceA (NULL, MAKEINTRESOURCE (101), "DUMP"); |
442 | 2513 if (hRes == NULL) |
2514 return 0; | |
2515 | |
2516 /* Found it, use the data in the resource */ | |
1204 | 2517 hResLoad = (HRSRC) LoadResource (NULL, hRes); |
442 | 2518 if (hResLoad == NULL) |
2519 return 0; | |
2520 | |
2367 | 2521 pdump_start = (Rawbyte *) LockResource (hResLoad); |
442 | 2522 if (pdump_start == NULL) |
2523 return 0; | |
2524 | |
2525 pdump_free = pdump_resource_free; | |
2526 pdump_length = SizeofResource (NULL, hRes); | |
665 | 2527 if (pdump_length <= (Bytecount) sizeof (pdump_header)) |
442 | 2528 { |
2529 pdump_start = 0; | |
2530 return 0; | |
2531 } | |
2532 | |
2533 return 1; | |
2534 } | |
2535 | |
2536 #else /* !WIN32_NATIVE */ | |
2537 | |
452 | 2538 static void |
2539 pdump_file_free (void) | |
442 | 2540 { |
4976
16112448d484
Rename xfree(FOO, TYPE) -> xfree(FOO)
Ben Wing <ben@xemacs.org>
parents:
4952
diff
changeset
|
2541 xfree (pdump_start); |
442 | 2542 } |
2543 | |
2544 #ifdef HAVE_MMAP | |
452 | 2545 static void |
2546 pdump_file_unmap (void) | |
442 | 2547 { |
2548 munmap (pdump_start, pdump_length); | |
2549 } | |
2550 #endif | |
2551 | |
452 | 2552 static int |
2367 | 2553 pdump_file_get (const Wexttext *path) |
442 | 2554 { |
2367 | 2555 int fd = wext_retry_open (path, O_RDONLY | OPEN_BINARY); |
2556 if (fd < 0) | |
442 | 2557 return 0; |
2558 | |
2559 pdump_length = lseek (fd, 0, SEEK_END); | |
665 | 2560 if (pdump_length < (Bytecount) sizeof (pdump_header)) |
442 | 2561 { |
771 | 2562 retry_close (fd); |
442 | 2563 return 0; |
2564 } | |
2565 | |
5838
b1500f1ec617
Check return value of lseek.
Marcus Crestani <marcus@crestani.de>
parents:
5837
diff
changeset
|
2566 if (lseek (fd, 0, SEEK_SET) == -1) |
b1500f1ec617
Check return value of lseek.
Marcus Crestani <marcus@crestani.de>
parents:
5837
diff
changeset
|
2567 { |
b1500f1ec617
Check return value of lseek.
Marcus Crestani <marcus@crestani.de>
parents:
5837
diff
changeset
|
2568 retry_close (fd); |
b1500f1ec617
Check return value of lseek.
Marcus Crestani <marcus@crestani.de>
parents:
5837
diff
changeset
|
2569 return 0; |
b1500f1ec617
Check return value of lseek.
Marcus Crestani <marcus@crestani.de>
parents:
5837
diff
changeset
|
2570 } |
442 | 2571 |
2572 #ifdef HAVE_MMAP | |
456 | 2573 /* Unix 98 requires that sys/mman.h define MAP_FAILED, |
2574 but many earlier implementations don't. */ | |
2575 # ifndef MAP_FAILED | |
2576 # define MAP_FAILED ((void *) -1L) | |
2577 # endif | |
2367 | 2578 pdump_start = |
2579 (Rawbyte *) mmap (0, pdump_length, PROT_READ|PROT_WRITE, MAP_PRIVATE, | |
2580 fd, 0); | |
2581 if (pdump_start != (Rawbyte *) MAP_FAILED) | |
442 | 2582 { |
2583 pdump_free = pdump_file_unmap; | |
771 | 2584 retry_close (fd); |
442 | 2585 return 1; |
2586 } | |
456 | 2587 #endif /* HAVE_MMAP */ |
442 | 2588 |
2367 | 2589 pdump_start = xnew_array (Rawbyte, pdump_length); |
442 | 2590 pdump_free = pdump_file_free; |
771 | 2591 retry_read (fd, pdump_start, pdump_length); |
442 | 2592 |
771 | 2593 retry_close (fd); |
442 | 2594 return 1; |
2595 } | |
2015 | 2596 |
2720 | 2597 #ifdef DUMP_IN_EXEC |
2015 | 2598 static int |
2599 pdump_ram_try (void) | |
2600 { | |
2367 | 2601 pdump_start = dumped_data_get (); |
2602 pdump_length = dumped_data_size (); | |
2015 | 2603 |
2367 | 2604 return pdump_load_check (); |
2015 | 2605 } |
2720 | 2606 #endif |
2015 | 2607 |
442 | 2608 #endif /* !WIN32_NATIVE */ |
2609 | |
5498
eb4eeec50f25
Remove static qualifier from pdump_file_try.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5402
diff
changeset
|
2610 /* This used to be static, but there seems to be a bug in the GCC 4.1.2 |
eb4eeec50f25
Remove static qualifier from pdump_file_try.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5402
diff
changeset
|
2611 optimizer that clobbers exe_path. */ |
5535
25325da1d1a8
Suppress the "no prototype" warning for pdump_file_try.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5498
diff
changeset
|
2612 int pdump_file_try (Wexttext*); |
5498
eb4eeec50f25
Remove static qualifier from pdump_file_try.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
5402
diff
changeset
|
2613 int |
2367 | 2614 pdump_file_try (Wexttext *exe_path) |
442 | 2615 { |
2367 | 2616 Wexttext *w = exe_path + wext_strlen (exe_path); |
442 | 2617 |
2563 | 2618 /* We look for various names, including those with the version and dump ID, |
2619 those with just the dump ID, and those without either. We first try | |
2620 adding directly to the executable name, then lopping off any extension | |
2621 (e.g. .exe) or version name in the executable (xemacs-21.5.18). */ | |
442 | 2622 do |
2623 { | |
2367 | 2624 wext_sprintf (w, WEXTSTRING ("-%s-%08x.dmp"), WEXTSTRING (EMACS_VERSION), |
2625 dump_id); | |
442 | 2626 if (pdump_file_get (exe_path)) |
2627 { | |
2628 if (pdump_load_check ()) | |
2629 return 1; | |
452 | 2630 pdump_free (); |
442 | 2631 } |
2632 | |
2367 | 2633 wext_sprintf (w, WEXTSTRING ("-%08x.dmp"), dump_id); |
442 | 2634 if (pdump_file_get (exe_path)) |
2635 { | |
2636 if (pdump_load_check ()) | |
2637 return 1; | |
452 | 2638 pdump_free (); |
442 | 2639 } |
2640 | |
2367 | 2641 wext_sprintf (w, WEXTSTRING (".dmp")); |
442 | 2642 if (pdump_file_get (exe_path)) |
2643 { | |
2644 if (pdump_load_check ()) | |
2645 return 1; | |
452 | 2646 pdump_free (); |
442 | 2647 } |
2648 | |
2649 do | |
2650 w--; | |
2367 | 2651 /* !!#### See comment below about how this is unsafe. */ |
2652 while (w > exe_path && !IS_DIRECTORY_SEP (*w) && (*w != '-') && | |
2653 (*w != '.')); | |
442 | 2654 } |
2367 | 2655 while (w > exe_path && !IS_DIRECTORY_SEP (*w)); |
442 | 2656 return 0; |
2657 } | |
2658 | |
4388
1a14c304cb8e
Don't use PATH_MAX_EXTERNAL, non-Win32.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4137
diff
changeset
|
2659 #define DUMP_SLACK 100 /* Enough to include dump ID, version name, .DMP */ |
1a14c304cb8e
Don't use PATH_MAX_EXTERNAL, non-Win32.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4137
diff
changeset
|
2660 |
452 | 2661 int |
2367 | 2662 pdump_load (const Wexttext *argv0) |
442 | 2663 { |
2664 #ifdef WIN32_NATIVE | |
2421 | 2665 Wexttext *exe_path = NULL; |
2666 int bufsize = 4096; | |
2667 int cchpathsize; | |
2668 | |
2669 /* Copied from mswindows_get_module_file_name (). Not clear if it's | |
2670 kosher to malloc() yet. */ | |
2671 while (1) | |
2672 { | |
2673 exe_path = alloca_array (Wexttext, bufsize); | |
2674 cchpathsize = qxeGetModuleFileName (NULL, (Extbyte *) exe_path, | |
2675 bufsize); | |
2676 if (!cchpathsize) | |
2677 goto fail; | |
2563 | 2678 if (cchpathsize + DUMP_SLACK <= bufsize) |
2421 | 2679 break; |
2680 bufsize *= 2; | |
2681 } | |
2682 | |
2367 | 2683 if (!XEUNICODE_P) |
2684 { | |
2685 Wexttext *wexe = MULTIBYTE_TO_WEXTTEXT ((Extbyte *) exe_path); | |
2686 wext_strcpy (exe_path, wexe); | |
2687 } | |
442 | 2688 #else /* !WIN32_NATIVE */ |
4388
1a14c304cb8e
Don't use PATH_MAX_EXTERNAL, non-Win32.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4137
diff
changeset
|
2689 Wexttext *exe_path; |
2367 | 2690 Wexttext *w; |
2691 const Wexttext *dir, *p; | |
442 | 2692 |
2720 | 2693 #ifdef DUMP_IN_EXEC |
2367 | 2694 if (pdump_ram_try ()) |
2695 { | |
2696 pdump_load_finish (); | |
2697 in_pdump = 0; | |
2698 return 1; | |
2699 } | |
2720 | 2700 #endif |
2015 | 2701 |
1204 | 2702 in_pdump = 1; |
442 | 2703 dir = argv0; |
2704 if (dir[0] == '-') | |
2705 { | |
2706 /* XEmacs as a login shell, oh goody! */ | |
2367 | 2707 dir = wext_getenv ("SHELL"); /* not egetenv -- not yet initialized and we |
2708 want external-format data */ | |
442 | 2709 } |
2710 | |
2367 | 2711 p = dir + wext_strlen (dir); |
2712 /* !!#### This is bad as it may fail with certain non-ASCII-compatible | |
2713 external formats such as JIS. Maybe we should be using the mb*() | |
2714 routines in libc? But can we reliably trust them on all Unix | |
2715 platforms? (We can't convert to internal since those conversion | |
2716 routines aren't yet initialized) */ | |
2717 while (p != dir && !IS_ANY_SEP (p[-1])) | |
2718 p--; | |
442 | 2719 |
2720 if (p != dir) | |
2721 { | |
2722 /* invocation-name includes a directory component -- presumably it | |
4137 | 2723 is relative to cwd, not $PATH. */ |
4388
1a14c304cb8e
Don't use PATH_MAX_EXTERNAL, non-Win32.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4137
diff
changeset
|
2724 exe_path = alloca_array (Wexttext, 1 + wext_strlen (dir) + DUMP_SLACK); |
2367 | 2725 wext_strcpy (exe_path, dir); |
442 | 2726 } |
2727 else | |
2728 { | |
2367 | 2729 const Wexttext *path = wext_getenv ("PATH"); /* not egetenv -- |
4388
1a14c304cb8e
Don't use PATH_MAX_EXTERNAL, non-Win32.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4137
diff
changeset
|
2730 not yet init. */ |
2367 | 2731 const Wexttext *name = p; |
4388
1a14c304cb8e
Don't use PATH_MAX_EXTERNAL, non-Win32.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4137
diff
changeset
|
2732 exe_path = alloca_array (Wexttext, |
1a14c304cb8e
Don't use PATH_MAX_EXTERNAL, non-Win32.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4137
diff
changeset
|
2733 1 + DUMP_SLACK + max (wext_strlen (name), |
1a14c304cb8e
Don't use PATH_MAX_EXTERNAL, non-Win32.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4137
diff
changeset
|
2734 wext_strlen (path))); |
442 | 2735 for (;;) |
2736 { | |
2737 p = path; | |
2738 while (*p && *p != SEPCHAR) | |
2739 p++; | |
2740 if (p == path) | |
2741 { | |
2742 exe_path[0] = '.'; | |
2743 w = exe_path + 1; | |
2744 } | |
2745 else | |
2746 { | |
2367 | 2747 memcpy (exe_path, path, (p - path) * sizeof (Wexttext)); |
442 | 2748 w = exe_path + (p - path); |
2749 } | |
2750 if (!IS_DIRECTORY_SEP (w[-1])) | |
2367 | 2751 *w++ = '/'; |
2752 wext_strcpy (w, name); | |
1466 | 2753 |
2754 { | |
2755 struct stat statbuf; | |
2367 | 2756 if (wext_access (exe_path, X_OK) == 0 |
2757 && wext_stat (exe_path, &statbuf) == 0 | |
1466 | 2758 && ! S_ISDIR (statbuf.st_mode)) |
2759 break; | |
2760 } | |
2761 | |
442 | 2762 if (!*p) |
2763 { | |
2764 /* Oh well, let's have some kind of default */ | |
2367 | 2765 wext_sprintf (exe_path, "./%s", name); |
442 | 2766 break; |
2767 } | |
2421 | 2768 path = p + 1; |
442 | 2769 } |
2770 } | |
2771 #endif /* WIN32_NATIVE */ | |
2772 | |
2773 if (pdump_file_try (exe_path)) | |
2774 { | |
2775 pdump_load_finish (); | |
1204 | 2776 in_pdump = 0; |
3263 | 2777 #ifdef NEW_GC |
2720 | 2778 pdump_free (); |
3263 | 2779 #endif /* NEW_GC */ |
442 | 2780 return 1; |
2781 } | |
2782 | |
2783 #ifdef WIN32_NATIVE | |
2784 if (pdump_resource_get ()) | |
2785 { | |
2786 if (pdump_load_check ()) | |
2787 { | |
2788 pdump_load_finish (); | |
1204 | 2789 in_pdump = 0; |
3263 | 2790 #ifdef NEW_GC |
2720 | 2791 pdump_free (); |
3263 | 2792 #endif /* NEW_GC */ |
442 | 2793 return 1; |
2794 } | |
2795 pdump_free (); | |
2796 } | |
2421 | 2797 |
2798 fail: | |
442 | 2799 #endif |
2800 | |
1204 | 2801 in_pdump = 0; |
442 | 2802 return 0; |
2803 } |