6.4 The Resource Facility
;; Make a simple, empty resource of 10-element arrays. > (setq simp-array-resource (make-resource "10 element arrays" :constructor #'(lambda () (make-array 10)))) #<Resource 10 element arrays 0 objects>;; Obtain an element from the resource. > (resource-allocate simp-array-resource) #<Simple-Vector T 10 B3789B>
;; Now put this object back. > (resource-deallocate simp-array-resource) #<Resource 10 element arrays 104CC7F6>
;; When you allocate from the resource, you get the same object ;; back. > (resource-allocate simp-array-resource) #<Simple-Vector T 10 B3789B>
;; If you allocate again, you get a different one. > (resource-allocate simp-array-resource) #<Simple-Vector T 10 B39773>
;; Put them both back, so there are two objects remembered. > (resource-deallocate simp-array-resource **) #<Resource 10 element arrays 104CC7F6>
> simp-array-resource #<Resource 10 element arrays 104CC7F6>
;; Now clear all the objects, so there are none. > (clear-resource simp-array-resource) 0
> simp-array-resource #<Resource 10 element arrays 104CC7F6>
;; Make a complicated resource. It accepts a parameter and gives ;; you an array with 10 elements initialized to the parameter ;; value. When you return an array, the elements are set to NIL; ;; otherwise, they might point to some object and keep it from ;; being garbage collected. The resource is created with three ;; arrays preallocated and initialized to contain 0. > (setq comp-array-resource (make-resource "10 element initialized arrays" :constructor #'(lambda (init) (make-array 10 :initial-element init)) :initial-copies 3 :initial-args '(0) :initialization-function #'(lambda (obj init) (fill obj init)) :cleanup-function #'(lambda (obj) (fill obj nil)))) #<Resource 10 element initialized arrays 104CCDDE
;; Temporarily allocate an array with elements set to 'X. > (using-resource (x comp-array-resource 'x) (let ((*print-array* t)) (format t "~S" x))) #(X X X X X X X X X X) NIL
;; This resource has a new cons cell as its initial element. When ;; the array is deallocated, the cons cell is eligible for garbage ;; collection. > (using-resource (x comp-array-resource (cons 'a 'b)) (let ((*print-array* t)) (format t "~S" x))) #((A . B) (A . B) (A . B) (A . B) (A . B) (A . B) (A . B) (A . B) (A . B) (A . B)) NIL
Generated with Harlequin WebMaker