1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: gentils.lisp
6 ;;;; Purpose: Main general utility functions for KMRCL package
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Apr 2000
10 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
12 ;;;; KMRCL users are granted the rights to distribute and use this software
13 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
14 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
15 ;;;; *************************************************************************
19 (defmacro let-when ((var test-form) &body body)
20 `(let ((,var ,test-form))
23 (defmacro let-if ((var test-form) if-true &optional if-false)
24 `(let ((,var ,test-form))
25 (if ,var ,if-true ,if-false)))
29 (defmacro aif (test then &optional else)
33 (defmacro awhen (test-form &body body)
37 (defmacro awhile (expr &body body)
38 `(do ((it ,expr ,expr))
42 (defmacro aand (&rest args)
44 ((null (cdr args)) (car args))
45 (t `(aif ,(car args) (aand ,@(cdr args))))))
47 (defmacro acond (&rest clauses)
50 (let ((cl1 (car clauses))
52 `(let ((,sym ,(car cl1)))
54 (let ((it ,sym)) ,@(cdr cl1))
55 (acond ,@(cdr clauses)))))))
57 (defmacro alambda (parms &body body)
58 `(labels ((self ,parms ,@body))
61 (defmacro aif2 (test &optional then else)
63 `(multiple-value-bind (it ,win) ,test
64 (if (or it ,win) ,then ,else))))
66 (defmacro awhen2 (test &body body)
70 (defmacro awhile2 (test &body body)
71 (let ((flag (gensym)))
78 (defmacro acond2 (&rest clauses)
81 (let ((cl1 (car clauses))
84 `(multiple-value-bind (,val ,win) ,(car cl1)
86 (let ((it ,val)) ,@(cdr cl1))
87 (acond2 ,@(cdr clauses)))))))
91 `(pprint (macroexpand-1 ',expr)))
93 (defmacro print-form-and-results (form)
94 `(format t "~&~A --> ~S~%" (write-to-string ',form) ,form))
99 (defmacro until (test &body body)
104 (defmacro while (test &body body)
109 (defmacro for ((var start stop) &body body)
110 (let ((gstop (gensym)))
111 `(do ((,var ,start (1+ ,var))
116 (defmacro with-each-stream-line ((var stream) &body body)
120 `(let ((,strm ,stream)
122 (do ((,var (read-line ,strm nil ,eof) (read-line ,strm nil ,eof)))
126 (defmacro with-each-file-line ((var file) &body body)
127 (let ((stream (gensym)))
128 `(with-open-file (,stream ,file :direction :input)
129 (with-each-stream-line (,var ,stream)
133 (defmacro in (obj &rest choices)
134 (let ((insym (gensym)))
135 `(let ((,insym ,obj))
136 (or ,@(mapcar #'(lambda (c) `(eql ,insym ,c))
139 (defmacro mean (&rest args)
140 `(/ (+ ,@args) ,(length args)))
142 (defmacro with-gensyms (syms &body body)
143 `(let ,(mapcar #'(lambda (s) `(,s (gensym)))
148 (defmacro time-seconds (&body body)
150 `(let ((,t1 (get-internal-real-time)))
153 (coerce (/ (- (get-internal-real-time) ,t1)
154 internal-time-units-per-second)
157 (defmacro time-iterations (n &body body)
162 (format t "~&Test with ~d iterations: ~W" ,count (quote ,body))
163 (let ((t1 (get-internal-real-time)))
166 (let* ((t2 (get-internal-real-time))
167 (secs (coerce (/ (- t2 t1)
168 internal-time-units-per-second)
170 (format t "~&Total time: ")
172 (format t ", time per iteration: ")
173 (print-seconds (coerce (/ secs ,n) 'double-float))))))))
175 (defmacro mv-bind (vars form &body body)
176 `(multiple-value-bind ,vars ,form
180 (defmacro deflex (var val &optional (doc nil docp))
181 "Defines a top level (global) lexical VAR with initial value VAL,
182 which is assigned unconditionally as with DEFPARAMETER. If a DOC
183 string is provided, it is attached to both the name |VAR| and the
184 name *STORAGE-FOR-DEFLEX-VAR-|VAR|* as a documentation string of
185 kind 'VARIABLE. The new VAR will have lexical scope and thus may
186 be shadowed by LET bindings without affecting its global value."
187 (let* ((s0 (load-time-value (symbol-name '#:*storage-for-deflex-var-)))
188 (s1 (symbol-name var))
189 (p1 (symbol-package var))
190 (s2 (load-time-value (symbol-name '#:*)))
191 (backing-var (intern (concatenate 'string s0 s1 s2) p1)))
193 (defparameter ,backing-var ,val ,@(when docp `(,doc)))
195 `((setf (documentation ',var 'variable) ,doc)))
196 (define-symbol-macro ,var ,backing-var))))
198 (defmacro def-cached-vector (name element-type)
199 (let ((get-name (concat-symbol "get-" name "-vector"))
200 (release-name (concat-symbol "release-" name "-vector"))
201 (table-name (concat-symbol "*cached-" name "-table*"))
202 (lock-name (concat-symbol "*cached-" name "-lock*")))
203 `(eval-when (:compile-toplevel :load-toplevel :execute)
204 (defvar ,table-name (make-hash-table :test 'equal))
205 (defvar ,lock-name (kmrcl::make-lock ,name))
207 (defun ,get-name (size)
208 (kmrcl::with-lock-held (,lock-name)
209 (let ((buffers (gethash (cons size ,element-type) ,table-name)))
211 (let ((buffer (pop buffers)))
212 (setf (gethash (cons size ,element-type) ,table-name) buffers)
214 (make-array size :element-type ,element-type)))))
216 (defun ,release-name (buffer)
217 (kmrcl::with-lock-held (,lock-name)
218 (let ((buffers (gethash (cons (array-total-size buffer)
221 (setf (gethash (cons (array-total-size buffer)
222 ,element-type) ,table-name)
223 (cons buffer buffers))))))))
225 (defmacro def-cached-instance (name)
226 (let* ((new-name (concat-symbol "new-" name "-instance"))
227 (release-name (concat-symbol "release-" name "-instance"))
228 (cache-name (concat-symbol "*cached-" name "-instance-table*"))
229 (lock-name (concat-symbol "*cached-" name "-instance-lock*")))
230 `(eval-when (:compile-toplevel :load-toplevel :execute)
231 (defvar ,cache-name nil)
232 (defvar ,lock-name (kmrcl::make-lock ',name))
235 (kmrcl::with-lock-held (,lock-name)
238 (make-instance ',name))))
240 (defun ,release-name (instance)
241 (kmrcl::with-lock-held (,lock-name)
242 (push instance ,cache-name))))))
244 (defmacro with-ignore-errors (&rest forms)
247 (lambda (x) (list 'ignore-errors x))
250 (defmacro ppmx (form)
251 "Pretty prints the macro expansion of FORM."
252 `(let* ((exp1 (macroexpand-1 ',form))
253 (exp (macroexpand exp1))
254 (*print-circle* nil))
255 (cond ((equal exp exp1)
256 (format t "~&Macro expansion:")
258 (t (format t "~&First step of expansion:")
260 (format t "~%~%Final expansion:")
265 (defmacro defconstant* (sym value &optional doc)
266 "Ensure VALUE is evaluated only once."
267 `(defconstant ,sym (if (boundp ',sym)
270 ,@(when doc (list doc))))
272 (defmacro defvar-unbound (sym &optional (doc ""))
273 "defvar with a documentation string."
276 (setf (documentation ',sym 'variable) ,doc)))