[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Restoring objects



>Neither CLtL2 nor AMOP deal with what happens to objects that are dumped (as
>in SAVE-APPLICATION) and restored. Not surprising since SAVE-APPLICATION is
>not part of the standard.
>Is there (or will there be in 2.1) a MOP extension in MCL to deal with this?
>I see the need for a way to wipe out some slots before they are saved (e.g. if
>they point to the Mac heap) and reinitialize them on restore.
>This is obviously related to MAKE-LOAD-FORM. Is SAVE-APPLICATION using MAKE-
>LOAD-FORM?
>     Daniel.

MCL's *save-exit-functions* & *restore-lisp-functions* should give you
all you need. Here's an (untested) example of a possible way to use it.

-----------------------------------------------------------------------

(defvar *cleanup-mixin-instances* nil)

(defclass cleanup-mixin () ())

(defmethod initialize-instance :after ((object cleanup-mixin) &key)
  (pushnew object *cleanup-mixin-instances*))

(defmethod cleanup ((instance cleanup-mixin))
  nil)

(defun cleanup-objects-for-dumping ()
  (dolist (instance *cleanup-mixin-instances*)
    (cleanup instance)))

(defmethod reinit ((instance cleanup-mixin))
  nil)

(defun reinit-objects-at-startup ()
  (dolist (instance *cleanup-mixin-instances*)
    (reinit instance)))
    

(pushnew 'cleanup-objects-for-dumping *save-exit-functions*)
(pushnew 'reinit-objects-at-startup *restore-lisp-functions*)


-----------------------------------------------------------------------

The only drawback with this is that no instance of cleanup-mixin
will be gc'able. You can solve this by using weak hash tables
(undocumented, but present in both 2.0b1 and 2.0 final).

-----------------------------------------------------------------------


(defvar *cleanup-mixin-instances*
  (make-hash-table :test 'eq :weak :key))

(defclass cleanup-mixin () ())

(defmethod initialize-instance :after ((object cleanup-mixin) &key)
  (setf (gethash object *cleanup-mixin-instances*) object))

(defmethod cleanup ((instance cleanup-mixin))
  nil)

(defun cleanup-objects-for-dumping ()
  (maphash #'(lambda (instance ignore)
               (declare (ignore ignore))
               (cleanup instance))
           *cleanup-mixin-instances*))

(defmethod reinit ((instance cleanup-mixin))
  nil)

(defun reinit-objects-at-startup ()
  (maphash #'(lambda (instance ignore)
               (declare (ignore ignore))
               (reinit instance))
           *cleanup-mixin-instances*))

(pushnew 'cleanup-objects-for-dumping *save-exit-functions*)
(pushnew 'reinit-objects-at-startup *restore-lisp-functions*)