2 * This software is part of the SBCL system. See the README file for
5 * This software is derived from the CMU CL system, which was
6 * written at Carnegie Mellon University and released into the
7 * public domain. The software is in the public domain and is
8 * provided with absolutely no warranty. See the COPYING and CREDITS
9 * files for more information.
31 write_bytes(FILE *file, char *addr, long bytes)
33 long count, here, data;
35 bytes = (bytes+CORE_PAGESIZE-1)&~(CORE_PAGESIZE-1);
40 data = (ftell(file)+CORE_PAGESIZE-1)&~(CORE_PAGESIZE-1);
44 count = fwrite(addr, 1, bytes, file);
50 perror("error writing to save file");
56 return data/CORE_PAGESIZE - 1;
60 output_space(FILE *file, int id, lispobj *addr, lispobj *end)
62 int words, bytes, data;
63 static char *names[] = {NULL, "dynamic", "static", "read-only"};
69 bytes = words * sizeof(lispobj);
71 printf("writing %d bytes from the %s space at 0x%08lx\n",
72 bytes, names[id], (unsigned long)addr);
74 data = write_bytes(file, (char *)addr, bytes);
77 putw((long)addr / CORE_PAGESIZE, file);
78 putw((bytes + CORE_PAGESIZE - 1) / CORE_PAGESIZE, file);
82 save(char *filename, lispobj init_function)
86 volatile lispobj*func_ptr = &init_function;
88 strcpy(sbuf,filename);
90 /* Get rid of remnant stuff. This is a MUST so that
91 * the memory manager can get started correctly when
92 * we restart after this save. Purify is going to
93 * maybe move the args so we need to consider them volatile,
94 * especially if the gcc optimizer is working!!
98 init_function = *func_ptr;
99 /* Set dynamic space pointer to base value so we don't write out
100 * MBs of just cleared heap.
102 if(SymbolValue(X86_CGC_ACTIVE_P) != NIL)
103 SetSymbolValue(ALLOCATION_POINTER, DYNAMIC_SPACE_START);
107 file = fopen(filename, "w");
112 printf("[undoing binding stack... ");
114 unbind_to_here((lispobj *)BINDING_STACK_START);
115 SetSymbolValue(CURRENT_CATCH_BLOCK, 0);
116 SetSymbolValue(CURRENT_UNWIND_PROTECT_BLOCK, 0);
117 SetSymbolValue(EVAL_STACK_TOP, 0);
119 #if defined WANT_CGC && defined X86_CGC_ACTIVE_P
120 SetSymbolValue(X86_CGC_ACTIVE_P, T);
122 printf("[saving current Lisp image into %s:\n", filename);
124 putw(CORE_MAGIC, file);
126 putw(CORE_VERSION, file);
128 putw(SBCL_CORE_VERSION_INTEGER, file);
130 putw(CORE_NDIRECTORY, file);
133 output_space(file, READ_ONLY_SPACE_ID, (lispobj *)READ_ONLY_SPACE_START,
134 (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
135 output_space(file, STATIC_SPACE_ID, (lispobj *)STATIC_SPACE_START,
136 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER));
138 output_space(file, DYNAMIC_SPACE_ID, (lispobj *)DYNAMIC_SPACE_START,
139 dynamic_space_free_pointer);
142 /* Flush the current_region updating the tables. */
143 gc_alloc_update_page_tables(0,&boxed_region);
144 gc_alloc_update_page_tables(1,&unboxed_region);
145 update_x86_dynamic_space_free_pointer();
147 output_space(file, DYNAMIC_SPACE_ID, (lispobj *)DYNAMIC_SPACE_START,
148 (lispobj *)SymbolValue(ALLOCATION_POINTER));
151 putw(CORE_INITIAL_FUNCTION, file);
153 putw(init_function, file);
155 putw(CORE_END, file);