utils.lisp 6.5 KB

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