Re: Finalization function for GC per object in LispWorks.

Martin Simmons <[email protected]>
Newsgroups gmane.lisp.lispworks.general
Message-ID <[email protected]>
This could also be done with the :free-function argument to weak hash tables,
e.g. something like this:

(defun callfun (object function)
  (funcall function object))

(defvar *finalizer-table*
  (make-hash-table
   :test #'eq
   :weak-kind :key
   :free-function 'callfun))

(defun add-finalizer (thing function)
  (setf (gethash thing *finalizer-table*)
        function)
  thing)

(defun remove-finalizer (thing)
  (remhash thing *finalizer-table*))

-- 
Martin Simmons
LispWorks Ltd
http://www.lispworks.com/



>>>>> On Fri, 7 Nov 2025 17:49:53 +0000, Tim Bradshaw (as tfb at tfeb dot org) said:
> 
> It's quite easy to implement a per-object finalizer model in terms of LW's:
> 
> (define-functions (finalize finalizer-table)
>   ;; This is my define-functions hack, the aim being to avoid a
>   ;; special-variable lookup.  Unclear whether it is needed.
>   (let ((finalization-actions
>          (make-hash-table
>           :test #'eq
>           :weak-kind :key)))
>     (values
>      (lambda (o)
>        (funcall (gethash o finalization-actions #'identity) o))
>      (lambda ()
>        finalization-actions))))
> 
> (defun add-finalizer (thing function)
>   (setf (gethash thing (finalizer-table))
>         function)
>   (flag-special-free-action thing)
>   thing)
> 
> (defun remove-finalizer (thing)
>   (flag-not-special-free-action thing)
>   (remhash thing (finalizer-table)))
> 
> (add-special-free-action 'finalize)
> 
> Note this adds a hash lookup to anything which has a special free action. I assume that such objects are rather rare so the performance impact will not be horrible.  Empirically they do seem to be.
> 
> --tim
>   
> 
> > On 7 Nov 2025, at 15:55, Uģis Lācis (as ugis dot lacis at equa dot se) <[email protected]> wrote:
> > 
> > Dear LW users and developers,
> >  Continuing my work on our old codebase originally ran in AllegroCL 3.0, I have stumbled upon the following problem. Apparently, ACL (and also SBCL, if I understand correctly) allows defining special clean-up functions for objects:
> > ACL: (add-finalization my-stream #'close)
> > SBCL: (sb-ext:finalize object (lambda () (release-handle handle))) SBCL 2.5.10 User Manual
> > 
> > Looking for analogous option in LispWorks, closest thing I have found is (hcl:add-special-free-action), however it is global and requires tagging the objects for special clean-up.
> >  I this the only option we have in LispWorks, or is there something that is closer to ACL/SBCL that I have not managed find?
> >  Best wishes,
> > Ugis
> 
> 
> 
> _______________________________________________
> Lisp Hug - the mailing list for LispWorks users
> [email protected]
> http://www.lispworks.com/support/lisp-hug.html
> 

_______________________________________________
Lisp Hug - the mailing list for LispWorks users
[email protected]
http://www.lispworks.com/support/lisp-hug.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.