[Openmcl-devel] Garbage collector - ccl:terminate (memory stuff)

Robert Munyer 2420506348 at munyer.com
Mon May 20 02:09:08 PDT 2024


While I was waking up, I had a strange thought (possibly left
over from a dream) about a way to implement a counter that lives
in "unreachable" heapspace, and is repeatedly destroyed by the
garbage collector, but continues counting anyway.

Code:

  (defclass garbage-counter () ((n :initarg :n)))

  (defmethod initialize-instance :after
      ((x garbage-counter) &rest initargs)
    (declare (ignore initargs))
    (ccl:terminate-when-unreachable x))

  (let ((s *terminal-io*))
    (defmethod ccl:terminate ((x garbage-counter))
      (let ((n (1+ (slot-value x 'n))))
        (make-instance 'garbage-counter :n n)
        (format s "~&garbage-counter: ~s~%" n)
        (finish-output s))))

Usage example:

  ? (progn (make-instance 'garbage-counter :n 0) t)
  T
  ? (ccl:gc)
  NIL
  ?
  garbage-counter: 1
  (ccl:gc)
  NIL
  ?
  garbage-counter: 2

Greg, I had no difficulty getting this to work in FriCAS's "BOOT"
package; it worked on the first try.  I'll attach a transcript,
in case seeing exactly what I did might be useful to you.

-------------- next part --------------
$ fricas
Checking for foreign routines
FRICAS="/usr/local/lib/fricas/target/x86_64-linux-gnu"
spad-lib="/usr/local/lib/fricas/target/x86_64-linux-gnu/lib/libspad.so"
foreign routines found
openServer result 0
                       FriCAS Computer Algebra System
              Version: FriCAS 1.3.10 built with openmcl 1.12.2
                 Timestamp: Mon 20 May 2024 02:34:03 AM UTC
-----------------------------------------------------------------------------
   Issue )copyright to view copyright notices.
   Issue )summary for a summary of useful system commands.
   Issue )quit to leave FriCAS and return to shell.
-----------------------------------------------------------------------------

(1) -> )lisp *package*
Value = #<Package "BOOT">
(1) -> )lisp (defclass garbage-counter () ((n :initarg :n)))
Value = #<STANDARD-CLASS GARBAGE-COUNTER>
(1) -> )fin
Clozure Common Lisp Version 1.12.2  LinuxX8664

For more information about CCL, please see http://ccl.clozure.com.

CCL is free software.  It is distributed under the terms of the Apache
Licence, Version 2.0.
? *package*
#<Package "BOOT">
?
(defmethod initialize-instance :after
    ((x garbage-counter) &rest initargs)
  (declare (ignore initargs))
  (ccl:terminate-when-unreachable x))
#<STANDARD-METHOD INITIALIZE-INSTANCE :AFTER (GARBAGE-COUNTER)>
?
(let ((s *terminal-io*))
  (defmethod ccl:terminate ((x garbage-counter))
    (let ((n (1+ (slot-value x 'n))))
      (make-instance 'garbage-counter :n n)
      (format s "~&garbage-counter: ~s~%" n)
      (finish-output s))))
#<STANDARD-METHOD CCL:TERMINATE (GARBAGE-COUNTER)>
? (|spad|)
(1) -> )lisp (progn (make-instance 'garbage-counter :n 0) t)
Value = T
(1) -> )lisp (ccl:gc)
Value = NIL
(1) ->
garbage-counter: 1
)lisp (ccl:gc)
Value = NIL
(1) ->
garbage-counter: 2


More information about the Openmcl-devel mailing list