nix-archive-1(type directoryentry(namesharenode(type directoryentry(name common-lispnode(type directoryentry(namesourcenode(type directoryentry(namecl-trivial-garbagenode(type directoryentry(name .gitignorenode(typeregularcontents0*~ doc/.atdoc.xml doc/header.gif doc/index.html ))entry(name .travis.ymlnode(typeregularcontentsŪlanguage: lisp env: matrix: - LISP=abcl - LISP=allegro - LISP=sbcl - LISP=sbcl32 - LISP=ccl - LISP=ccl32 - LISP=clisp - LISP=clisp32 # - LISP=cmucl - LISP=ecl # matrix: # allow_failures: # - env: LISP=ecl install: - curl -L https://github.com/luismbo/cl-travis/raw/master/install.sh | sh script: - cl -e '(ql:quickload :trivial-garbage/tests) (unless (trivial-garbage-tests:run) (uiop:quit 1))' ))entry(name README.mdnode(typeregularcontentså[![Build Status](https://travis-ci.org/trivial-garbage/trivial-garbage.svg?branch=master)](https://travis-ci.org/trivial-garbage/trivial-garbage) trivial-garbage provides a portable API to finalizers, weak hash-tables and weak pointers on all major implementations of the Common Lisp programming language. Documentation is available at the [project's website][1]. It is placed in the public domain with absolutely no warranty. [1]: http://common-lisp.net/project/trivial-garbage/ ))entry(name build.xcvbnode(typeregularcontentsO#+xcvb (module (:fullname "/trivial-garbage" :depends-on ("trivial-garbage"))) ))entry(namedocnode(type directoryentry(name index.cssnode(typeregularcontentsā /* based on atdoc's orange-sans.css */ div.sidebar { float: right; min-width: 15%; padding: 0pt 5pt 5pt 5pt; font-family: verdana, arial; } a { text-decoration: none; color: #ee8500; /* border-bottom: 1px dotted black; border-top: 1px solid white; border-left: 1px solid white; border-right: 1px solid white; padding-top: 1px; padding-bottom: 1px; */ } .nonlink { border-bottom: 1px solid white; border-top: 1px solid white; border-left: 1px solid white; border-right: 1px solid white; padding-top: 1px; padding-bottom: 1px; } .sidebar a { border-top: 1px solid #eeeeee; border-left: 1px solid #eeeeee; border-right: 1px solid #eeeeee; } /* a:hover { color: #000000; border: 1px solid black; } */ #headerlink { border: none; } #headerlink:hover { border: none; } body { color: #000000; background-color: #ffffff; margin-top: 2em; margin-right: 10em; margin-left: 10em; font-family: helvetica, verdana, arial; } .main { margin-top: 20px; margin-left: 40px; } .padded { padding-left: 30px; } .padded h1,h2 { margin-left: -30px; } h1 { color: #ee8500; font-size: 22pt; } h3 { color: #000000; border-bottom: 2px solid #ff9500; margin-left: -3px; padding-left: 3px; padding-top: 2em; } h4 { } .grau { padding-top: 1em; } .code { border: solid 1px #d0d0d0; padding: 1em; margin-right: 10%; } .indent { /*margin-left: 20px; padding-bottom: 1em;*/ } .def { padding: 1px 1px 1px 1px; margin-bottom: 1px; font-weight: bold; margin-right: 40px; } .nomargin { margin-bottom: 0; margin-top: 0; } .noindent { margin-left: -30px; padding-bottom: 1em; } #header { margin-right: 22px; /*border-bottom: 1px solid black;*/ font-family: verdana, arial; font-size: 18pt; padding-bottom: 1px; } pre { background-color: #eeeeee; border: solid 1px #d0d0d0; padding: 1em; margin-right: 10%; } #sp-package-list { /* ... */ } #sp-about-packages { /* ... */ } .sp-lambda-list { background-color: #f4f4f4; padding: 3px 3px 3px 3px; } .sp-definition { border: 1px solid #cccccc; padding: 3px 3px 3px 3px; } .sp-definition-body { padding-left: 2em; padding-right: 2em; padding-top: 0.5em; padding-bottom: 1em; } .sp-definition-body ul { margin-top: 0; margin-bottom: 0; } .sp-return { } .sph3 { padding-top: 1em; font-weight: bold; } #about-package-legend { display: none } #contents { display: none } .function-details-legend { display: none } #heading2 { display: none } ))))entry(name gendocs.lispnode(typeregularcontents (in-package :cl-user) (asdf:load-system :atdoc) (asdf:load-system :trivial-garbage) (let* ((base (asdf:component-pathname (asdf:find-system :trivial-garbage))) (output-directory (merge-pathnames "doc/" base)) (css-file (merge-pathnames "doc/index.css" base))) (ensure-directories-exist output-directory) (atdoc:generate-html-documentation '(:trivial-garbage) output-directory :single-page-p t :heading "Trivial Garbage" :index-title "Trivial Garbage" :css css-file)) ))entry(name release.lispnode(typeregular executablecontents“&#!/usr/bin/env clisp ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*- (defpackage :release-script (:use #:cl #:regexp)) (in-package :release-script) ;;;; Configuration ------------------------------------------------------------ (defparameter *project-name* "trivial-garbage") (defparameter *asdf-file* (format nil "~A.asd" *project-name*)) (defparameter *host* "common-lisp.net") (defparameter *release-dir* (format nil "/project/~A/public_html/releases" *project-name*)) (defparameter *version-file* "VERSION") (defparameter *version-file-dir* (format nil "/project/~A/public_html" *project-name*)) ;;;; -------------------------------------------------------------------------- ;;;; Utilities (defun ensure-list (x) (if (listp x) x (list x))) (defmacro string-case (expression &body clauses) `(let ((it ,expression)) ; yes, anaphoric, deal with it. (cond ,@(loop for clause in clauses collect `((or ,@(loop for alternative in (ensure-list (first clause)) collect (or (eq t alternative) `(string= it ,alternative)))) ,@(rest clause)))))) (defparameter *development-mode* nil) (defun die (format-control &rest format-args) (format *error-output* "~?" format-control format-args) (if *development-mode* (cerror "continue" "die") (ext:quit 1))) (defun numeric-split (string) (if (digit-char-p (char string 0)) (multiple-value-bind (number next-position) (parse-integer string :junk-allowed t) (cons number (when (< next-position (length string)) (numeric-split (subseq string next-position))))) (let ((next-digit-position (position-if #'digit-char-p string))) (if next-digit-position (cons (subseq string 0 next-digit-position) (numeric-split (subseq string next-digit-position))) (list string))))) (defun natural-string-< (s1 s2) (labels ((aux< (l1 l2) (cond ((null l1) (not (null l2))) ((null l2) nil) (t (destructuring-bind (x . xs) l1 (destructuring-bind (y . ys) l2 (cond ((and (numberp x) (stringp y)) t) ((and (numberp y) (stringp x)) nil) ((and (numberp x) (numberp y)) (or (< x y) (and (= x y) (aux< xs ys)))) (t (or (string-lessp x y) (and (string-equal x y) (aux< xs ys))))))))))) (aux< (numeric-split s1) (numeric-split s2)))) ;;;; Running commands (defparameter *dry-run* nil) (defun cmd? (format-control &rest format-args) (let ((cmd (format nil "~?" format-control format-args))) (with-open-stream (s1 (ext:run-shell-command cmd :output :stream)) (loop for line = (read-line s1 nil nil) while line collect line)))) ;; XXX: quote arguments. (defun cmd (format-control &rest format-args) (when *development-mode* (format *debug-io* "CMD: ~?~%" format-control format-args)) (let ((ret (ext:run-shell-command (format nil "~?" format-control format-args)))) (or (null ret) (zerop ret)))) (defun cmd! (format-control &rest format-args) (or (apply #'cmd format-control format-args) (die "cmd '~?' failed." format-control format-args))) (defun maybe-cmd! (format-control &rest format-args) (if *dry-run* (format t "SUPPRESSING: ~?~%" format-control format-args) (apply #'cmd! format-control format-args))) ;;;; (defun find-current-version () (subseq (reduce (lambda (x y) (if (natural-string-< x y) y x)) (or (cmd? "git tag -l v\\*") (die "no version tags found. Please specify initial version."))) 1)) (defun parse-version (string) (mapcar (lambda (x) (parse-integer x :junk-allowed t)) (loop repeat 3 ; XXX: parameterize for el in (regexp-split "\\." (find-current-version)) collect el))) (defun check-for-unrecorded-changes (&optional force) (unless (cmd "git diff --exit-code") (write-line "Unrecorded changes.") (if force (write-line "Continuing anyway.") (die "Aborting.~@ Use -f or --force if you want to make a release anyway.")))) (defun new-version-number-candidates (current-version) (let ((current-version (parse-version current-version))) (labels ((alternatives (before after) (when after (cons (append before (list (1+ (first after))) (mapcar (constantly 0) (rest after))) (alternatives (append before (list (first after))) (rest after)))))) (loop for alt in (alternatives nil current-version) collect (reduce (lambda (acc next) (format nil "~a.~a" acc next)) alt))))) (defun ask-user-for-version (current-version next-versions) (format *query-io* "Current version is ~A. Which will be the next one?~%" current-version) (loop for i from 1 and version in next-versions do (format *query-io* "~T~A) ~A~%" i version)) (format *query-io* "? ") (finish-output *query-io*) (nth (1- (parse-integer (read-line) :junk-allowed t)) next-versions)) (defun git-tag-tree (version) (write-line "Tagging the tree...") (maybe-cmd! "git tag \"v~A\"" version)) (defun add-version-to-system-file (version path-in path-out) (let ((defsystem-line (format nil "(defsystem ~A" *project-name*))) (with-open-file (in path-in :direction :input) (with-open-file (out path-out :direction :output) (loop for line = (read-line in nil nil) while line do (write-line line out) when (string= defsystem-line line) do (format out " :version ~s~%" version)))))) (defun create-dist (version distname) (write-line "Creating distribution...") (cmd! "mkdir \"~a\"" distname) (cmd! "git archive master | tar xC \"~A\"" distname) (format t "Updating ~A with new version: ~A~%" *asdf-file* version) (let* ((asdf-file-path (format nil "~A/~A" distname *asdf-file*)) (tmp-asdf-file-path (format nil "~a.tmp" asdf-file-path))) (add-version-to-system-file version asdf-file-path tmp-asdf-file-path) (cmd! "mv \"~a\" \"~a\"" tmp-asdf-file-path asdf-file-path))) (defun tar-and-sign (distname tarball) (write-line "Creating and signing tarball...") (cmd! "tar czf \"~a\" \"~a\"" tarball distname) (cmd! "gpg -b -a \"~a\"" tarball)) (defparameter *remote-directory* (format nil "~A:~A" *host* *release-dir*)) (defun upload-tarball (tarball signature remote-directory) (write-line "Copying tarball to web server...") (maybe-cmd! "scp \"~A\" \"~A\" \"~A\"" tarball signature remote-directory) (format t "Uploaded ~A and ~A.~%" tarball signature)) (defun update-remote-links (tarball signature host release-dir project-name) (format t "Updating ~A_latest links...~%" project-name) (maybe-cmd! "ssh \"~A\" ln -sf \"~A\" \"~A/~A_latest.tar.gz\"" host tarball release-dir project-name) (maybe-cmd! "ssh \"~A\" ln -sf \"~A\" \"~A/~A_latest.tar.gz.asc\"" host signature release-dir project-name)) (defun upload-version-file (version version-file host version-file-dir) (format t "Uploading ~A...~%" version-file) (with-open-file (out version-file :direction :output) (write-string version out)) (maybe-cmd! "scp \"~A\" \"~A\":\"~A\"" version-file host version-file-dir) (maybe-cmd! "rm \"~A\"" version-file)) (defun maybe-clean-things-up (tarball signature) (when (y-or-n-p "Clean local tarball and signature?") (cmd! "rm \"~A\" \"~A\"" tarball signature))) (defun run (force version) (check-for-unrecorded-changes force) ;; figure out what version we'll be preparing. (unless version (let* ((current-version (find-current-version)) (next-versions (new-version-number-candidates current-version))) (setf version (or (ask-user-for-version current-version next-versions) (die "invalid selection."))))) (git-tag-tree version) (let* ((distname (format nil "~A_~A" *project-name* version)) (tarball (format nil "~A.tar.gz" distname)) (signature (format nil "~A.asc" tarball))) ;; package things up. (create-dist version distname) (tar-and-sign distname tarball) ;; upload. (upload-tarball tarball signature *remote-directory*) (update-remote-links tarball signature *host* *release-dir* *project-name*) (when *version-file* (upload-version-file version *version-file* *host* *version-file-dir*)) ;; clean up. (maybe-clean-things-up tarball signature) ;; documentation. ;; (write-line "Building and uploading documentation...") ;; (maybe-cmd! "make -C doc upload-docs") ;; push tags and any outstanding changes. (write-line "Pushing tags and changes...") (maybe-cmd! "git push --tags origin master"))) ;;;; Do it to it (let ((force nil) (version nil) (args ext:*args*)) (loop while args do (string-case (pop args) (("-h" "--help") (write-line "No help, sorry. Read the source.") (ext:quit 0)) (("-f" "--force") (setf force t)) (("-v" "--version") (setf version (pop args))) (("-n" "--dry-run") (setf *dry-run* t)) (t (die "Unrecognized argument '~a'" it)))) (run force version)) ))entry(name tests.lispnode(typeregularcontents;;;; -*- Mode: LISP; Syntax: ANSI-Common-lisp; Base: 10 -*- ;;;; The above modeline is required for Genera. Do not change. ;;; ;;; tests.lisp --- trivial-garbage tests. ;;; ;;; This software is placed in the public domain by Luis Oliveira ;;; and is provided with absolutely no ;;; warranty. (defpackage #:trivial-garbage-tests (:use #:cl #:trivial-garbage #:regression-test) (:nicknames #:tg-tests) (:export #:run)) (in-package #:trivial-garbage-tests) (defun run () (let ((*package* (find-package :trivial-garbage-tests))) (do-tests) (null (set-difference (regression-test:pending-tests) rtest::*expected-failures*)))) ;;;; Weak Pointers (deftest pointers.1 (weak-pointer-p (make-weak-pointer 42)) t) (deftest pointers.2 (weak-pointer-value (make-weak-pointer 42)) 42) ;;;; Weak Hashtables (eval-when (:compile-toplevel :load-toplevel :execute) (defun sbcl-without-weak-hash-tables-p () (if (and (find :sbcl *features*) (not (find-symbol "HASH-TABLE-WEAKNESS" "SB-EXT"))) '(:and) '(:or)))) #+(or corman scl #.(tg-tests::sbcl-without-weak-hash-tables-p)) (progn (pushnew 'hashtables.weak-key.1 rt::*expected-failures*) (pushnew 'hashtables.weak-key.2 rt::*expected-failures*) (pushnew 'hashtables.weak-value.1 rt::*expected-failures*)) #+clasp (progn (pushnew 'pointers.1 rt::*expected-failures*) (pushnew 'pointers.2 rt::*expected-failures*) (pushnew 'hashtables.weak-value.1 rt::*expected-failures*)) #+genera (progn (pushnew 'hashtables.weak-key.1 rt::*expected-failures*) (pushnew 'hashtables.weak-key.2 rt::*expected-failures*)) (deftest hashtables.weak-key.1 (let ((ht (make-weak-hash-table :weakness :key))) (values (hash-table-p ht) (hash-table-weakness ht))) t :key) (deftest hashtables.weak-key.2 (let ((ht (make-weak-hash-table :weakness :key :test 'eq))) (values (hash-table-p ht) (hash-table-weakness ht))) t :key) (deftest hashtables.weak-value.1 (let ((ht (make-weak-hash-table :weakness :value))) (values (hash-table-p ht) (hash-table-weakness ht))) t :value) (deftest hashtables.not-weak.1 (hash-table-weakness (make-hash-table)) nil) ;;;; Finalizers ;;; ;;; These tests are, of course, not very reliable. (defun dummy (x) (declare (ignore x)) nil) (defun test-finalizers-aux (count extra-action) (let* ((cons (list 0)) ;; lbd should not be defined in a lexical scope where obj is ;; present to prevent closing over the variable on compilers ;; which does not optimize away unused lexenv variables (i.e ;; ecl's bytecmp). (lbd (lambda () (incf (car cons)))) (obj (string (gensym)))) (dotimes (i count) (finalize obj lbd)) (when extra-action (cancel-finalization obj) (when (eq extra-action :add-again) (dotimes (i count) (finalize obj lbd)))) (setq obj (gensym)) (setq obj (dummy obj)) cons)) (defvar *result*) #+genera (progn (pushnew 'finalizers.1 rt::*expected-failures*) (pushnew 'finalizers.2 rt::*expected-failures*) (pushnew 'finalizers.3 rt::*expected-failures*) (pushnew 'finalizers.4 rt::*expected-failures*) (pushnew 'finalizers.5 rt::*expected-failures*)) ;;; I don't really understand this, but it seems to work, and stems ;;; from the observation that typing the code in sequence at the REPL ;;; achieves the desired result. Superstition at its best. (defmacro voodoo (string) `(funcall (compile nil `(lambda () (eval (let ((*package* (find-package :tg-tests))) (read-from-string ,,string))))))) (defun test-finalizers (count &optional remove) (gc :full t) (voodoo (format nil "(setq *result* (test-finalizers-aux ~S ~S))" count remove)) (voodoo "(gc :full t)") ;; Normally done by a background thread every 0.3 sec: #+openmcl (ccl::drain-termination-queue) ;; (an alternative is to sleep a bit) (voodoo "(car *result*)")) (deftest finalizers.1 (test-finalizers 1) 1) (deftest finalizers.2 (test-finalizers 1 t) 0) (deftest finalizers.3 (test-finalizers 5) 5) (deftest finalizers.4 (test-finalizers 5 t) 0) (deftest finalizers.5 (test-finalizers 5 :add-again) 5) ))entry(nametrivial-garbage.asdnode(typeregularcontents_;;;; -*- Mode: LISP; Syntax: ANSI-Common-lisp; Base: 10; Package: ASDF -*- ;;;; The above modeline is required for Genera. Do not change. ;;; ;;; trivial-garbage.asd --- ASDF system definition for trivial-garbage. ;;; ;;; This software is placed in the public domain by Luis Oliveira ;;; and is provided with absolutely no ;;; warranty. #-(or cmu scl sbcl allegro clisp openmcl corman lispworks ecl abcl clasp mezzano genera) (error "Sorry, your Lisp is not supported by trivial-garbage.") (defsystem trivial-garbage :description "Portable finalizers, weak hash-tables and weak pointers." :author "Luis Oliveira " :in-order-to ((test-op (test-op "trivial-garbage/tests"))) :licence "Public Domain" :components ((:file "trivial-garbage"))) (defsystem trivial-garbage/tests :description "Unit tests for TRIVIAL-GARBAGE." :depends-on (trivial-garbage rt) :components ((:file "tests"))) (defmethod perform ((op test-op) (sys (eql (find-system "trivial-garbage/tests")))) (funcall (find-symbol (string '#:do-tests) '#:rtest))) ))entry(nametrivial-garbage.lispnode(typeregularcontents]I;;;; -*- Mode: LISP; Syntax: ANSI-Common-lisp; Base: 10 -*- ;;;; The above modeline is required for Genera. Do not change. ;; ;;; trivial-garbage.lisp --- Trivial Garbage! ;;; ;;; This software is placed in the public domain by Luis Oliveira ;;; and is provided with absolutely no ;;; warranty. #+xcvb (module ()) (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) (:documentation "@a[http://common-lisp.net/project/trivial-garbage]{trivial-garbage} provides a portable API to finalizers, weak hash-tables and weak pointers on all major implementations of the Common Lisp programming language. For a good introduction to these data-structures, have a look at @a[http://www.haible.de/bruno/papers/cs/weak/WeakDatastructures-writeup.html]{Weak References: Data Types and Implementation} by Bruno Haible. Source code is available at @a[https://github.com/trivial-garbage/trivial-garbage]{github}, which you are welcome to use for submitting patches and/or @a[https://github.com/trivial-garbage/trivial-garbage/issues]{bug reports}. Discussion takes place on @a[http://lists.common-lisp.net/cgi-bin/mailman/listinfo/trivial-garbage-devel]{trivial-garbage-devel at common-lisp.net}. @a[http://common-lisp.net/project/trivial-garbage/releases/]{Tarball releases} are available, but the easiest way to install this library is via @a[http://www.quicklisp.org/]{Quicklisp}: @code{(ql:quickload :trivial-garbage)}. @begin[Weak Pointers]{section} A @em{weak pointer} holds an object in a way that does not prevent it from being reclaimed by the garbage collector. An object referenced only by weak pointers is considered unreachable (or \"weakly reachable\") and so may be collected at any time. When that happens, the weak pointer's value becomes @code{nil}. @aboutfun{make-weak-pointer} @aboutfun{weak-pointer-value} @aboutfun{weak-pointer-p} @end{section} @begin[Weak Hash-Tables]{section} A @em{weak hash-table} is one that weakly references its keys and/or values. When both key and value are unreachable (or weakly reachable) that pair is reclaimed by the garbage collector. @aboutfun{make-weak-hash-table} @aboutfun{hash-table-weakness} @end{section} @begin[Finalizers]{section} A @em{finalizer} is a hook that is executed after a given object has been reclaimed by the garbage collector. @aboutfun{finalize} @aboutfun{cancel-finalization} @end{section}")) (in-package #:trivial-garbage) ;;;; GC (defun gc (&key full verbose) "Initiates a garbage collection. @code{full} forces the collection of all generations, when applicable. When @code{verbose} is @em{true}, diagnostic information about the collection is printed if possible." (declare (ignorable verbose full)) #+(or cmu scl) (ext:gc :verbose verbose :full full) #+sbcl (sb-ext:gc :full full) #+allegro (excl:gc (not (null full))) #+(or abcl clisp) (ext:gc) #+ecl (si:gc t) #+openmcl (ccl:gc) #+corman (ccl:gc (if full 3 0)) #+lispworks (hcl:gc-generation (if full t 0)) #+clasp (gctools:garbage-collect) #+mezzano (mezzano.extensions:gc :full full) #+genera (scl:let-globally ((si:gc-report-stream *standard-output*) (si:gc-reports-enable verbose) (si:gc-ephemeral-reports-enable verbose) (si:gc-warnings-enable verbose)) (if full (sys:gc-immediately t) (si:ephemeral-gc-flip)))) ;;;; 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.") #+genera (defvar *weak-pointers* (scl:make-hash-table :test 'eq :gc-protect-values nil) "Weak value hash-table mapping between pseudo weak pointers and its values.") #+(or allegro openmcl lispworks genera) (defstruct (weak-pointer (:constructor %make-weak-pointer)) #-(or openmcl genera) pointer) (defun make-weak-pointer (object) "Creates a new weak pointer which points to @code{object}. For portability reasons, @code{object} must not be @code{nil}." (assert (not (null object))) #+sbcl (sb-ext:make-weak-pointer object) #+(or cmu scl) (ext:make-weak-pointer object) #+clisp (ext:make-weak-pointer object) #+abcl (ext:make-weak-reference object) #+ecl (ext:make-weak-pointer object) #+allegro (let ((wv (excl:weak-vector 1))) (setf (svref wv 0) object) (%make-weak-pointer :pointer wv)) #+(or openmcl genera) (let ((wp (%make-weak-pointer))) (setf (gethash wp *weak-pointers*) object) wp) #+corman (ccl:make-weak-pointer object) #+lispworks (let ((array (make-array 1 :weak t))) (setf (svref array 0) object) (%make-weak-pointer :pointer array)) #+clasp (core:make-weak-pointer object) #+mezzano (mezzano.extensions:make-weak-pointer object)) #-(or allegro openmcl lispworks genera) (defun weak-pointer-p (object) "Returns @em{true} if @code{object} is a weak pointer and @code{nil} otherwise." #+sbcl (sb-ext:weak-pointer-p object) #+(or cmu scl) (ext:weak-pointer-p object) #+clisp (ext:weak-pointer-p object) #+abcl (typep object 'ext:weak-reference) #+ecl (typep object 'ext:weak-pointer) #+corman (ccl:weak-pointer-p object) #+clasp (core:weak-pointer-valid object) #+mezzano (mezzano.extensions:weak-pointer-p object)) (defun weak-pointer-value (weak-pointer) "If @code{weak-pointer} is valid, returns its value. Otherwise, returns @code{nil}." #+sbcl (values (sb-ext:weak-pointer-value weak-pointer)) #+(or cmu scl) (values (ext:weak-pointer-value weak-pointer)) #+clisp (values (ext:weak-pointer-value weak-pointer)) #+abcl (values (ext:weak-reference-value weak-pointer)) #+ecl (values (ext:weak-pointer-value weak-pointer)) #+allegro (svref (weak-pointer-pointer weak-pointer) 0) #+(or openmcl genera) (values (gethash weak-pointer *weak-pointers*)) #+corman (ccl:weak-pointer-obj weak-pointer) #+lispworks (svref (weak-pointer-pointer weak-pointer) 0) #+clasp (core:weak-pointer-value weak-pointer) #+mezzano (values (mezzano.extensions:weak-pointer-value object))) ;;;; 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)) #+(or sbcl abcl clasp ecl-weak-hash mezzano) :weakness #+(or clisp openmcl) :weak #+lispworks :weak-kind #+allegro (case weakness (:key :weak-keys) (:value :values)) #+cmu :weak-p #+genera :gc-protect-values) (defvar *weakness-warnings* '() "List of weaknesses that have already been warned about this session. Used by `weakness-missing'.") (defun weakness-missing (weakness errorp) "Signal an error or warning, depending on ERRORP, about lack of Lisp support for WEAKNESS." (cond (errorp (error "Your Lisp does not support weak ~(~A~) hash-tables." weakness)) ((member weakness *weakness-warnings*) nil) (t (push weakness *weakness-warnings*) (warn "Your Lisp does not support weak ~(~A~) hash-tables." weakness)))) (defun weakness-keyword-opt (weakness errorp) (declare (ignorable errorp)) (ecase weakness (:key #+(or lispworks sbcl abcl clasp clisp openmcl ecl-weak-hash mezzano) :key #+(or allegro cmu) t #-(or lispworks sbcl abcl clisp openmcl allegro cmu ecl-weak-hash clasp mezzano) (weakness-missing weakness errorp)) (:value #+allegro :weak #+(or clisp openmcl sbcl abcl lispworks cmu ecl-weak-hash mezzano) :value #+genera nil #-(or allegro clisp openmcl sbcl abcl lispworks cmu ecl-weak-hash mezzano genera) (weakness-missing weakness errorp)) (:key-or-value #+(or clisp sbcl abcl cmu mezzano) :key-or-value #+lispworks :either #-(or clisp sbcl abcl lispworks cmu mezzano) (weakness-missing weakness errorp)) (:key-and-value #+(or clisp abcl sbcl cmu ecl-weak-hash mezzano) :key-and-value #+lispworks :both #-(or clisp sbcl abcl lispworks cmu ecl-weak-hash mezzano) (weakness-missing weakness errorp)))) (defun make-weak-hash-table (&rest args &key weakness (weakness-matters t) #+openmcl (test #'eql) &allow-other-keys) "Returns a new weak hash table. In addition to the standard arguments accepted by @code{cl:make-hash-table}, this function adds extra keywords: @code{:weakness} being the kind of weak table it should create, and @code{:weakness-matters} being whether an error should be signalled when that weakness isn't available (the default is to signal an error). @code{weakness} can be one of @code{:key}, @code{:value}, @code{:key-or-value}, @code{:key-and-value}. If @code{weakness} is @code{:key} or @code{:value}, an entry is kept as long as its key or value is reachable, respectively. If @code{weakness} is @code{:key-or-value} or @code{:key-and-value}, an entry is kept if either or both of its key and value are reachable, respectively. @code{tg::make-hash-table} is available as an alias for this function should you wish to import it into your package and shadow @code{cl:make-hash-table}." (remf args :weakness) (remf args :weakness-matters) (if weakness (let ((arg (weakness-keyword-arg weakness)) (opt (weakness-keyword-opt weakness weakness-matters))) (apply #-genera #'cl:make-hash-table #+genera #'scl:make-hash-table #+openmcl :test #+openmcl (if (eq opt :key) #'eq test) #+clasp :test #+clasp #'eq (if arg (list* arg opt args) 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 @code{nil}, @code{:key}, @code{:value}, @code{:key-or-value} or @code{:key-and-value}." #-(or allegro sbcl abcl clisp cmu openmcl lispworks ecl-weak-hash clasp mezzano genera) (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)) #+abcl (sys:hash-table-weakness ht) #+ecl-weak-hash (ext:hash-table-weakness ht) #+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 (let ((weakness (lisp::hash-table-weak-p ht))) (if (eq t weakness) :key weakness)) #+openmcl (ccl::hash-table-weak-p ht) #+lispworks (system::hash-table-weak-kind ht) #+clasp (core:hash-table-weakness ht) #+mezzano (mezzano.extensions:hash-table-weakness ht) #+genera (if (null (getf (cli::basic-table-options ht) :gc-protect-values t)) :value nil)) ;;;; Finalizers ;;; Note: Lispworks can't finalize gensyms. #+(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))))) #+mezzano (progn (defvar *finalizers* (cl:make-hash-table :test 'eq :weakness :key) "Weak hashtable that holds registered finalizers.") (defvar *finalizers-lock* (mezzano.supervisor:make-mutex '*finalizers-lock*))) ;;; Note: ECL bytecmp does not perform escape analysis and unused ;;; variables are not optimized away from its lexenv. That leads to ;;; closing over whole definition lexenv. That's why we define ;;; EXTEND-FINALIZER-FN which defines lambda outside the lexical scope ;;; of FINALIZE (which inludes object) - to prevent closing over ;;; finalized object. This problem does not apply to C compiler. #+ecl (defun extend-finalizer-fn (old-fn new-fn) (if (null old-fn) (lambda (obj) (declare (ignore obj)) (funcall new-fn)) (lambda (obj) (declare (ignore obj)) (funcall new-fn) (funcall old-fn nil)))) (defun finalize (object function) "Pushes a new @code{function} to the @code{object}'s list of finalizers. @code{function} should take no arguments. Returns @code{object}. @b{Note:} @code{function} should not attempt to look at @code{object} by closing over it because that will prevent it from being garbage collected." #+genera (declare (ignore object function)) #+(or cmu scl) (ext:finalize object function) #+sbcl (sb-ext:finalize object function :dont-save t) #+abcl (ext:finalize object function) #+ecl (let* ((old-fn (ext:get-finalizer object)) (new-fn (extend-finalizer-fn old-fn function))) (ext:set-finalizer object new-fn) object) #+allegro (progn (push (excl:schedule-finalization object (lambda (obj) (declare (ignore obj)) (funcall function))) (gethash object *finalizers*)) object) #+clasp (progn (gctools:finalize object (lambda (obj) (declare (ignore obj)) (funcall function))) 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 ;; and . (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 (incf (gethash object *finalizers* 0)) 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 (let ((finalizers (gethash object *finalizers*))) (unless finalizers (hcl:flag-special-free-action object)) (setf (gethash object *finalizers*) (cons function finalizers))) object) #+mezzano (mezzano.supervisor:with-mutex (*finalizers-lock*) (let ((finalizer-key (gethash object *finalizers*))) (unless finalizer-key (setf finalizer-key (mezzano.extensions:make-weak-pointer object :finalizer (lambda () (let ((finalizers (mezzano.supervisor:with-mutex (*finalizers-lock*) (prog1 (gethash finalizer-key *finalizers*) (remhash finalizer-key *finalizers*))))) (mapc #'funcall finalizers))))) (setf (gethash object *finalizers*) finalizer-key)) (push function (gethash finalizer-key *finalizers*))) ;; Make sure the object doesn't actually get captured by the finalizer lambda. (prog1 object (setf object nil))) #+genera (error "Finalizers are not available in Genera.")) (defun cancel-finalization (object) "Cancels all of @code{object}'s finalizers, if any." #+genera (declare (ignore object)) #+cmu (ext:cancel-finalization object) #+scl (ext:cancel-finalization object nil) #+sbcl (sb-ext:cancel-finalization object) #+abcl (ext:cancel-finalization object) #+ecl (ext:set-finalizer object nil) #+allegro (progn (mapc #'excl:unschedule-finalization (gethash object *finalizers*)) (remhash object *finalizers*)) #+clasp (gctools:definalize object) #+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)) #+mezzano (mezzano.supervisor:with-mutex (*finalizers-lock*) (let ((finalizer-key (gethash object *finalizers*))) (when finalizer-key (setf (gethash finalizer-key *finalizers*) '())))) #+genera (error "Finalizers are not available in Genera.")) ))))))entry(namesystemsnode(type directoryentry(nametrivial-garbage.asdnode(typesymlinktarget/gnu/store/00898qhfpa6j9z51r7xm7a7v8a38p8gg-cl-trivial-garbage-0.21-0.3474f64/share/common-lisp/source/cl-trivial-garbage/trivial-garbage.asd)))))))))