utils.lisp 7.2 KB

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