Menu

[r760]: / trunk / lispbuilder-sdl / trivial-garbage / trivial-garbage.lisp  Maximize  Restore  History

Download this file

284 lines (256 with data), 10.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
;;;
;;; trivial-garbage.lisp --- Trivial Garbage!
;;;
;;; This software is placed in the public domain by Luis Oliveira
;;; <loliveira@common-lisp.net> and is provided with absolutely no
;;; warranty.
(defpackage #:trivial-garbage
(:use #:cl)
(:shadow #:make-hash-table)
(:nicknames #:tg)
(:export #:gc
#:make-weak-pointer
#:weak-pointer-value
#:weak-pointer-p
#:make-weak-hash-table
#:hash-table-weakness
#:finalize
#:cancel-finalization))
(in-package #:trivial-garbage)
;;;; GC
(defun gc (&key full verbose)
"Initiates a garbage collection."
(declare (ignorable verbose full))
#+:cmu (ext:gc :verbose verbose :full full)
#+:sbcl (sb-ext:gc :full full)
#+:allegro (excl:gc (not (null full)))
#+:clisp (ext:gc)
#+:openmcl (ccl:gc)
#+:corman (ccl:gc (if full 3 0))
#+:lispworks (hcl:mark-and-sweep (if full 3 0)))
;;;; Weak Pointers
#+:openmcl
(defvar *weak-pointers* (cl:make-hash-table :test 'eq :weak :value)
"Weak value hash-table mapping between pseudo weak pointers and its values.")
#+(or :allegro :openmcl :lispworks)
(defstruct (weak-pointer (:constructor %make-weak-pointer))
#-:openmcl pointer)
(defun make-weak-pointer (object)
"Creates a new weak pointer which points to OBJECT. For
portability reasons, OBJECT most not be NIL."
(assert (not (null object)))
#+:sbcl (sb-ext:make-weak-pointer object)
#+:cmu (ext:make-weak-pointer object)
#+:clisp (ext:make-weak-pointer object)
#+:allegro
(let ((wv (excl:weak-vector 1)))
(setf (svref wv 0) object)
(%make-weak-pointer :pointer wv))
#+:openmcl
(let ((wp (%make-weak-pointer)))
(setf (gethash wp *weak-pointers*) object)
wp)
#+:corman (ccl:make-weak-pointer object)
#+:lispworks
(let ((array (make-array 1)))
(hcl:set-array-weak array t)
(setf (svref array 0) object)
(%make-weak-pointer :pointer array)))
#-(or :allegro :openmcl :lispworks)
(defun weak-pointer-p (object)
"Returns true if OBJECT is a weak pointer and NIL otherwise."
#+:sbcl (sb-ext:weak-pointer-p object)
#+:cmu (ext:weak-pointer-p object)
#+:clisp (ext:weak-pointer-p object)
#+:corman (ccl:weak-pointer-p object))
(defun weak-pointer-value (weak-pointer)
"If WEAK-POINTER is valid, returns its value. Otherwise, returns NIL."
#+:sbcl (values (sb-ext:weak-pointer-value weak-pointer))
#+:cmu (values (ext:weak-pointer-value weak-pointer))
#+:clisp (values (ext:weak-pointer-value weak-pointer))
#+:allegro (svref (weak-pointer-pointer weak-pointer) 0)
#+:openmcl (values (gethash weak-pointer *weak-pointers*))
#+:corman (ccl:weak-pointer-obj weak-pointer)
#+:lispworks (svref (weak-pointer-pointer weak-pointer) 0))
;;;; Weak Hash-tables
;;; Allegro can apparently create weak hash-tables with both weak keys
;;; and weak values but it's not obvious whether it's an OR or an AND
;;; relation. TODO: figure that out.
(defun weakness-keyword-arg (weakness)
(declare (ignorable weakness))
#+:sbcl :weakness
#+(or :clisp :openmcl) :weak
#+:lispworks :weak-kind
#+:allegro (case weakness (:key :weak-keys) (:value :values))
#+:cmu :weak-p)
(defun weakness-keyword-opt (weakness)
(ecase weakness
(:key
#+(or :lispworks :sbcl :clisp :openmcl) :key
#+(or :allegro :cmu) t
#-(or :lispworks :sbcl :clisp :openmcl :allegro :cmu)
(error "Your Lisp does not support weak key hash-tables."))
(:value
#+:allegro :weak
#+(or :clisp :openmcl :sbcl :lispworks) :value
#-(or :allegro :clisp :openmcl :sbcl :lispworks)
(error "Your Lisp does not support weak value hash-tables."))
(:key-or-value
#+(or :clisp :sbcl) :key-or-value
#-(or :clisp :sbcl)
(error "Your Lisp does not support weak key-or-value hash-tables."))
(:key-and-value
#+(or :clisp :sbcl) :key-and-value
#-(or :clisp :sbcl)
(error "Your Lisp does not support weak key-and-value hash-tables."))))
(defun make-weak-hash-table (&rest args &key weakness &allow-other-keys)
"Returns a new weak hash table. In addition to the standard arguments
accepted by CL:MAKE-HASH-TABLE, this function an extra keyword :WEAKNESS
that determines the kind of weak table it should create. WEAKNESS can be
one of :KEY, :VALUE, :KEY-OR-VALUE, :KEY-AND-VALUE.
TG::MAKE-HASH-TABLE is available as an alias for this function should you
wish to import it into your package and shadow CL:MAKE-HASH-TABLE."
(remf args :weakness)
(if weakness
(apply #'cl:make-hash-table
(weakness-keyword-arg weakness)
(weakness-keyword-opt weakness)
args)
(apply #'cl:make-hash-table args)))
;;; If you want to use this function to override CL:MAKE-HASH-TABLE,
;;; it's necessary to shadow-import it. For example:
;;;
;;; (defpackage #:foo
;;; (:use #:common-lisp #:trivial-garbage)
;;; (:shadowing-import-from #:trivial-garbage #:make-hash-table))
;;;
(defun make-hash-table (&rest args)
(apply #'make-weak-hash-table args))
(defun hash-table-weakness (ht)
"Returns one of NIL, :KEY, :VALUE, :KEY-OR-VALUE or :KEY-AND-VALUE."
#-(or :allegro :sbcl :clisp :cmu :openmcl :lispworks)
(declare (ignore ht))
;; keep this first if any of the other lisps bugously insert a NIL
;; for the returned (values) even when *read-suppress* is NIL (e.g. clisp)
#.(if (find :sbcl *features*)
(if (find-symbol "HASH-TABLE-WEAKNESS" "SB-EXT")
(read-from-string "(sb-ext:hash-table-weakness ht)")
nil)
(values))
#+:allegro (cond ((excl:hash-table-weak-keys ht) :key)
((eq (excl:hash-table-values ht) :weak) :value))
#+:clisp (ext:hash-table-weak-p ht)
#+:cmu (if (lisp::hash-table-weak-p ht) :key nil)
#+:openmcl (ccl::hash-table-weak-p ht)
#+:lispworks (system::hash-table-weak-kind ht))
;;;; Finalizers
;;; The fact that SBCL/CMUCL throw away the object *before* running
;;; the finalizer is somewhat unfortunate...
#+(or :allegro :clisp :lispworks :openmcl)
(defvar *finalizers*
(cl:make-hash-table :test 'eq
#+:allegro :weak-keys #+:allegro t
#+(or :clisp :openmcl) :weak
#+:lispworks :weak-kind
#+(or :clisp :openmcl :lispworks) :key)
"Weak hashtable that holds registered finalizers.")
#+:corman
(progn
(defvar *finalizers* '()
"Weak alist that holds registered finalizers.")
(defvar *finalizers-cs* (threads:allocate-critical-section)))
#+:lispworks
(progn
(hcl:add-special-free-action 'free-action)
(defun free-action (object)
(let ((finalizers (gethash object *finalizers*)))
(unless (null finalizers)
(mapc #'funcall finalizers)))))
(defun finalize (object function)
"Pushes a new FUNCTION to the OBJECT's list of
finalizers. FUNCTION should take no arguments. Returns OBJECT.
For portability reasons, FUNCTION should not attempt to look
at OBJECT by closing over it because, in some lisps, OBJECT
will already have been garbage collected and is therefore not
accessible when FUNCTION is invoked."
#+:cmu (ext:finalize object function)
#+:sbcl (sb-ext:finalize object function)
#+:allegro
(progn
(push (excl:schedule-finalization
object (lambda (obj) (declare (ignore obj)) (funcall function)))
(gethash object *finalizers*))
object)
#+:clisp
;; The CLISP code used to be a bit simpler but we had to workaround
;; a bug regarding the interaction between GC and weak hashtables.
;; See <http://article.gmane.org/gmane.lisp.clisp.general/11028>
;; and <http://article.gmane.org/gmane.lisp.cffi.devel/994>.
(multiple-value-bind (finalizers presentp)
(gethash object *finalizers* (cons 'finalizers nil))
(unless presentp
(setf (gethash object *finalizers*) finalizers)
(ext:finalize object (lambda (obj)
(declare (ignore obj))
(mapc #'funcall (cdr finalizers)))))
(push function (cdr finalizers))
object)
#+:openmcl
(progn
(ccl:terminate-when-unreachable
object (lambda (obj) (declare (ignore obj)) (funcall function)))
;; store number of finalizers
(if (gethash object *finalizers*)
(incf (gethash object *finalizers*))
(setf (gethash object *finalizers*) 1))
object)
#+:corman
(flet ((get-finalizers (obj)
(assoc obj *finalizers* :test #'eq :key #'ccl:weak-pointer-obj)))
(threads:with-synchronization *finalizers-cs*
(let ((pair (get-finalizers object)))
(if (null pair)
(push (list (ccl:make-weak-pointer object) function) *finalizers*)
(push function (cdr pair)))))
(ccl:register-finalization
object (lambda (obj)
(threads:with-synchronization *finalizers-cs*
(mapc #'funcall (cdr (get-finalizers obj)))
(setq *finalizers*
(delete obj *finalizers*
:test #'eq :key #'ccl:weak-pointer-obj)))))
object)
#+:lispworks
(progn
(push function (gethash object *finalizers*))
(hcl:flag-special-free-action object)
object))
(defun cancel-finalization (object)
"Cancels all of OBJECT's finalizers, if any."
#+:cmu (ext:cancel-finalization object)
#+:sbcl (sb-ext:cancel-finalization object)
#+:allegro
(progn
(mapc #'excl:unschedule-finalization
(gethash object *finalizers*))
(remhash object *finalizers*))
#+:clisp
(multiple-value-bind (finalizers present-p)
(gethash object *finalizers*)
(when present-p
(setf (cdr finalizers) nil))
(remhash object *finalizers*))
#+:openmcl
(let ((count (gethash object *finalizers*)))
(unless (null count)
(dotimes (i count)
(ccl:cancel-terminate-when-unreachable object))))
#+:corman
(threads:with-synchronization *finalizers-cs*
(setq *finalizers*
(delete object *finalizers* :test #'eq :key #'ccl:weak-pointer-obj)))
#+:lispworks
(progn
(remhash object *finalizers*)
(hcl:flag-not-special-free-action object)))
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.