utils.lisp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ;;; -*- Mode: Lisp; show-trailing-whitespace: t; Base: 10; indent-tabs: nil; Syntax: ANSI-Common-Lisp; Package: UTILS; -*-
  2. ;;; Copyright (c) 2013, Mark VandenBrink. All rights reserved.
  3. (in-package #:utils)
  4. (eval-when (:compile-toplevel :load-toplevel :execute)
  5. #+DBG (defvar *standard-optimize-settings* '(optimize (debug 3)))
  6. #-DBG (defvar *standard-optimize-settings* '(optimize (speed 3) (safety 0) (space 0) (debug 0)))
  7. )
  8. (defparameter *break-on-warn-user* nil "set to T if you'd like to stop in warn-user")
  9. (defun warn-user (format-string &rest args)
  10. "Print a warning error to *ERROR-OUTPUT* and continue"
  11. (declare #.utils:*standard-optimize-settings*)
  12. (when *break-on-warn-user*
  13. (break "Breaking in WARN-USER"))
  14. (format *error-output* "~&********************************************************************************~%")
  15. #+CCL (format *error-output* "~&WARNING in ~a:: " (ccl::%last-fn-on-stack 1))
  16. (apply #'format *error-output* format-string args)
  17. (format *error-output* "~&**********************************************************************************~%"))
  18. (defparameter *max-raw-bytes-print-len* 10 "Max number of octets to print from an array")
  19. (defun printable-array (array &optional (max-len *max-raw-bytes-print-len*))
  20. "Given an array, return a string of the first *MAX-RAW-BYTES-PRINT-LEN* bytes"
  21. (declare #.utils:*standard-optimize-settings*)
  22. (let* ((len (length array))
  23. (print-len (min len max-len))
  24. (printable-array (make-array print-len :displaced-to array)))
  25. (declare (fixnum max-len len)
  26. (type (array (unsigned-byte 8) 1) array))
  27. (format nil "[~:d of ~:d bytes] <~x>" print-len len printable-array)))
  28. (declaim (inline upto-null))
  29. (defun upto-null (string)
  30. "Trim STRING to end at first NULL found"
  31. (declare #.utils:*standard-optimize-settings*)
  32. (subseq string 0 (position #\Null string)))
  33. (defun dump-data (file-name data)
  34. (declare #.utils:*standard-optimize-settings*)
  35. (with-open-file (f file-name :direction :output :if-exists :supersede :element-type '(unsigned-byte 8))
  36. (write-sequence data f)))
  37. (defmacro redirect (filename &rest body)
  38. "Temporarily set *STANDARD-OUTPUT* to FILENAME and execute BODY."
  39. `(let ((*standard-output* (open ,filename :direction :output :if-does-not-exist :create :if-exists :supersede)))
  40. ,@body
  41. (finish-output *standard-output*)))
  42. (defun get-bitmask(start width)
  43. "Create a bit mask that begins at bit START (31 is MSB) and is WIDTH bits wide.
  44. Example: (get-bitmask 31 11) -->> #xffe00000"
  45. (declare #.utils:*standard-optimize-settings*)
  46. (ash (- (ash 1 width) 1) (- (1+ start) width)))
  47. (defmacro get-bitfield (int start width)
  48. "Extract WIDTH bits from INT starting at START
  49. Example: (get-bitfield #xFFFBB240 31 11) -->> #x7ff.
  50. The above will expand to (ash (logand #xFFFBB240 #xFFE00000) -21) at COMPILE time."
  51. `(ash (logand ,int ,(utils::get-bitmask start width)) ,(- ( - start width -1))))
  52. ;;;;;;;;;;;;;;;;;;;; convenience macros ;;;;;;;;;;;;;;;;;;;;
  53. (defmacro with-gensyms (syms &body body)
  54. `(let ,(mapcar #'(lambda (s)
  55. `(,s (gensym)))
  56. syms)
  57. ,@body))
  58. (defun make-keyword (name)
  59. (intern (string name) :keyword))
  60. (defmacro while (test &body body)
  61. `(do ()
  62. ((not ,test))
  63. ,@body))
  64. (defmacro aif (test-form then-form &optional else-form)
  65. `(let ((it ,test-form))
  66. (if it ,then-form ,else-form)))
  67. (defmacro awhen (test-form &body body)
  68. `(aif ,test-form
  69. (progn ,@body)))
  70. ;;; in multi-thread mode, need to protect insertions into hash-table
  71. ;;; Note: CCL hash-tables are thread-safe, but some other implementations
  72. ;;; don't appear to be...
  73. (defstruct locked-hash-table lock hash-table)
  74. #+(or :ccl :sbcl :abcl)
  75. (defmacro with-lock ((l) &body body)
  76. `(bt:with-lock-held (,l)
  77. ,@body))
  78. #-(or :ccl :sbcl :abcl)
  79. (defmacro with-lock ((l) &body body)
  80. (declare (ignore l))
  81. `(progn
  82. ,@body))
  83. (defun mk-memoize (func-name)
  84. "Takes a normal function object and returns a memoized one"
  85. (declare #.utils:*standard-optimize-settings*)
  86. (let* ((func (symbol-function func-name))
  87. (the-hash-table (make-locked-hash-table
  88. :lock #+ENABLE-MP (bt:make-lock) #-ENABLE-MP nil
  89. :hash-table (make-hash-table :test 'equal))))
  90. (with-slots (lock hash-table) the-hash-table
  91. #'(lambda (arg)
  92. (multiple-value-bind (value foundp) (gethash arg hash-table)
  93. (if foundp
  94. value
  95. (with-lock (lock)
  96. (setf (gethash arg hash-table) (funcall func arg)))))))))
  97. (defmacro memoize (func-name)
  98. "Memoize function associated with FUNC-NAME. Simplified version"
  99. `(setf (symbol-function ,func-name) (utils::mk-memoize ,func-name)))
  100. (defun timings (function)
  101. (declare #.utils:*standard-optimize-settings*)
  102. (let ((real-base (get-internal-real-time)))
  103. (funcall function)
  104. (float (/ (- (get-internal-real-time) real-base) internal-time-units-per-second))))
  105. ;;; Taken from ASDF
  106. (defmacro DBG (tag &rest exprs)
  107. "debug macro for print-debugging:
  108. TAG is typically a constant string or keyword to identify who is printing,
  109. but can be an arbitrary expression returning a tag to be princ'ed first;
  110. if the expression returns NIL, nothing is printed.
  111. EXPRS are expressions, which when the TAG was not NIL are evaluated in order,
  112. with their source code then their return values being printed each time.
  113. The last expression is *always* evaluated and its multiple values are returned,
  114. but its source and return values are only printed if TAG was not NIL;
  115. previous expressions are not evaluated at all if TAG returned NIL.
  116. The macro expansion has relatively low overhead in space or time."
  117. (let* ((last-expr (car (last exprs)))
  118. (other-exprs (butlast exprs))
  119. (tag-var (gensym "TAG"))
  120. (thunk-var (gensym "THUNK")))
  121. `(let ((,tag-var ,tag))
  122. (flet ,(when exprs `((,thunk-var () ,last-expr)))
  123. (if ,tag-var
  124. (DBG-helper ,tag-var
  125. (list ,@(loop :for x :in other-exprs :collect
  126. `(cons ',x #'(lambda () ,x))))
  127. ',last-expr ,(if exprs `#',thunk-var nil))
  128. ,(if exprs `(,thunk-var) '(values)))))))
  129. (defun DBG-helper (tag expressions-thunks last-expression last-thunk)
  130. ;; Helper for the above debugging macro
  131. (declare #.utils:*standard-optimize-settings*)
  132. (labels
  133. ((f (stream fmt &rest args)
  134. (with-standard-io-syntax
  135. (let ((*print-readably* nil)
  136. (*package* (find-package :cl)))
  137. (apply 'format stream fmt args)
  138. (finish-output stream))))
  139. (z (stream)
  140. (f stream "~&"))
  141. (e (fmt arg)
  142. (f *error-output* fmt arg))
  143. (x (expression thunk)
  144. (e "~& ~S => " expression)
  145. (let ((results (multiple-value-list (funcall thunk))))
  146. (e "~{~S~^ ~}~%" results)
  147. (apply 'values results))))
  148. (map () #'z (list *standard-output* *error-output* *trace-output*))
  149. (e "~A~%" tag)
  150. (loop :for (expression . thunk) :in expressions-thunks
  151. :do (x expression thunk))
  152. (if last-thunk
  153. (x last-expression last-thunk)
  154. (values))))