0.9.8.7:
[sbcl.git] / src / runtime / save.c
index 450d3a4..f939eec 100644 (file)
@@ -43,6 +43,13 @@ write_bytes(FILE *file, char *addr, long bytes)
 
     bytes = (bytes+os_vm_page_size-1)&~(os_vm_page_size-1);
 
+#ifdef LISP_FEATURE_WIN32
+    /* touch every single page in the space to force it to be mapped. */
+    for (count = 0; count < bytes; count += 0x1000) {
+        volatile int temp = addr[count];
+    }
+#endif
+
     fflush(file);
     here = ftell(file);
     fseek(file, 0, 2);
@@ -87,21 +94,20 @@ output_space(FILE *file, int id, lispobj *addr, lispobj *end)
     write_lispobj((bytes + os_vm_page_size - 1) / os_vm_page_size, file);
 }
 
-boolean
-save(char *filename, lispobj init_function)
+FILE *
+open_core_for_saving(char *filename)
 {
-    FILE *file;
-    struct thread *th;
-
     /* Open the output file. We don't actually need the file yet, but
      * the fopen() might fail for some reason, and we want to detect
      * that and back out before we do anything irreversible. */
     unlink(filename);
-    file = fopen(filename, "w");
-    if (!file) {
-        perror(filename);
-        return 1;
-    }
+    return fopen(filename, "wb");
+}
+
+boolean
+save_to_filehandle(FILE *file, char *filename, lispobj init_function)
+{
+    struct thread *th;
 
     /* Smash the enclosing state. (Once we do this, there's no good
      * way to go back, which is a sufficient reason that this ends up
@@ -131,10 +137,10 @@ save(char *filename, lispobj init_function)
     write_lispobj(/* (We're writing the word count of the entry here, and the 2
           * term is one word for the leading BUILD_ID_CORE_ENTRY_TYPE_CODE
           * word and one word where we store the count itself.) */
-         2 + strlen(build_id),
+         2 + strlen((const char *)build_id),
          file);
     {
-        char *p;
+        unsigned char *p;
         for (p = build_id; *p; ++p)
             write_lispobj(*p, file);
     }
@@ -160,7 +166,7 @@ save(char *filename, lispobj init_function)
 #ifdef LISP_FEATURE_GENCGC
     /* Flush the current_region, updating the tables. */
     gc_alloc_update_all_page_tables();
-    update_x86_dynamic_space_free_pointer();
+    update_dynamic_space_free_pointer();
 #endif
     output_space(file,
                  DYNAMIC_CORE_SPACE_ID,
@@ -172,6 +178,26 @@ save(char *filename, lispobj init_function)
     write_lispobj(3, file);
     write_lispobj(init_function, file);
 
+#ifdef LISP_FEATURE_GENCGC
+    {
+        size_t size = (last_free_page*sizeof(long)+os_vm_page_size-1)
+            &~(os_vm_page_size-1);
+        long *data = calloc(size, 1);
+        if (data) {
+            long offset;
+            int i;
+            for (i = 0; i < last_free_page; i++) {
+                data[i] = page_table[i].first_object_offset;
+            }
+            write_lispobj(PAGE_TABLE_CORE_ENTRY_TYPE_CODE, file);
+            write_lispobj(4, file);
+            write_lispobj(size, file);
+            offset = write_bytes(file, (char *) data, size);
+            write_lispobj(offset, file);
+        }
+    }
+#endif
+
     write_lispobj(END_CORE_ENTRY_TYPE_CODE, file);
 
     fclose(file);
@@ -179,3 +205,16 @@ save(char *filename, lispobj init_function)
 
     exit(0);
 }
+
+boolean
+save(char *filename, lispobj init_function)
+{
+    FILE *file = open_core_for_saving(filename);
+
+    if (!file) {
+        perror(filename);
+        return 1;
+    }
+
+    return save_to_filehandle(file, filename, init_function);
+}