utils.lisp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #+ENABLE-MP (defmacro with-lock ((l) &body body)
  75. `(bt:with-lock-held (,l)
  76. ,@body))
  77. #-ENABLE-MP (defmacro with-lock ((l) &body body)
  78. (declare (ignore l))
  79. `(progn
  80. ,@body))
  81. (defun mk-memoize (func-name)
  82. "Takes a normal function object and returns a memoized one"
  83. (declare #.utils:*standard-optimize-settings*)
  84. (let* ((func (symbol-function func-name))
  85. (the-hash-table (make-locked-hash-table
  86. :lock #+ENABLE-MP (bt:make-lock) #-ENABLE-MP nil
  87. :hash-table (make-hash-table :test 'equal))))
  88. (with-slots (lock hash-table) the-hash-table
  89. #'(lambda (arg)
  90. (multiple-value-bind (value foundp) (gethash arg hash-table)
  91. (if foundp
  92. value
  93. (with-lock (lock)
  94. (setf (gethash arg hash-table) (funcall func arg)))))))))
  95. (defmacro memoize (func-name)
  96. "Memoize function associated with FUNC-NAME. Simplified version"
  97. `(setf (symbol-function ,func-name) (utils::mk-memoize ,func-name)))
  98. (defun timings (function)
  99. (declare #.utils:*standard-optimize-settings*)
  100. (let ((real-base (get-internal-real-time)))
  101. (funcall function)
  102. (float (/ (- (get-internal-real-time) real-base) internal-time-units-per-second))))
  103. ;;; Taken from ASDF
  104. (defmacro DBG (tag &rest exprs)
  105. "debug macro for print-debugging:
  106. TAG is typically a constant string or keyword to identify who is printing,
  107. but can be an arbitrary expression returning a tag to be princ'ed first;
  108. if the expression returns NIL, nothing is printed.
  109. EXPRS are expressions, which when the TAG was not NIL are evaluated in order,
  110. with their source code then their return values being printed each time.
  111. The last expression is *always* evaluated and its multiple values are returned,
  112. but its source and return values are only printed if TAG was not NIL;
  113. previous expressions are not evaluated at all if TAG returned NIL.
  114. The macro expansion has relatively low overhead in space or time."
  115. (let* ((last-expr (car (last exprs)))
  116. (other-exprs (butlast exprs))
  117. (tag-var (gensym "TAG"))
  118. (thunk-var (gensym "THUNK")))
  119. `(let ((,tag-var ,tag))
  120. (flet ,(when exprs `((,thunk-var () ,last-expr)))
  121. (if ,tag-var
  122. (DBG-helper ,tag-var
  123. (list ,@(loop :for x :in other-exprs :collect
  124. `(cons ',x #'(lambda () ,x))))
  125. ',last-expr ,(if exprs `#',thunk-var nil))
  126. ,(if exprs `(,thunk-var) '(values)))))))
  127. (defun DBG-helper (tag expressions-thunks last-expression last-thunk)
  128. ;; Helper for the above debugging macro
  129. (declare #.utils:*standard-optimize-settings*)
  130. (labels
  131. ((f (stream fmt &rest args)
  132. (with-standard-io-syntax
  133. (let ((*print-readably* nil)
  134. (*package* (find-package :cl)))
  135. (apply 'format stream fmt args)
  136. (finish-output stream))))
  137. (z (stream)
  138. (f stream "~&"))
  139. (e (fmt arg)
  140. (f *error-output* fmt arg))
  141. (x (expression thunk)
  142. (e "~& ~S => " expression)
  143. (let ((results (multiple-value-list (funcall thunk))))
  144. (e "~{~S~^ ~}~%" results)
  145. (apply 'values results))))
  146. (map () #'z (list *standard-output* *error-output* *trace-output*))
  147. (e "~A~%" tag)
  148. (loop :for (expression . thunk) :in expressions-thunks
  149. :do (x expression thunk))
  150. (if last-thunk
  151. (x last-expression last-thunk)
  152. (values))))