utils.lisp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #+CCL (eval-when (:compile-toplevel :load-toplevel :execute)
  5. (defvar *standard-optimize-settings* '(optimize (speed 3) (safety 0) (space 0) (debug 0))))
  6. (defparameter *break-on-warn-user* nil "set to T if you'd like to stop in warn-user")
  7. (defun warn-user (format-string &rest args)
  8. "Print a warning error to *ERROR-OUTPUT* and continue"
  9. (when *break-on-warn-user* (break "Breaking in WARN-USER"))
  10. (format *error-output* "~&********************************************************************************~%")
  11. #+CCL (format *error-output* "~&WARNING in ~a:: " (ccl::%last-fn-on-stack 1))
  12. (apply #'format *error-output* format-string args)
  13. (format *error-output* "~&**********************************************************************************~%"))
  14. (defparameter *max-raw-bytes-print-len* 10 "Max number of octets to print from an array")
  15. (defun printable-array (array &optional (max-len *max-raw-bytes-print-len*))
  16. "Given an array, return a string of the first *MAX-RAW-BYTES-PRINT-LEN* bytes"
  17. (declare #.utils:*standard-optimize-settings*)
  18. (let* ((len (length array))
  19. (print-len (min len max-len))
  20. (printable-array (make-array print-len :displaced-to array)))
  21. (format nil "[~:d of ~:d bytes] <~x>" print-len len printable-array)))
  22. (defun upto-null (string)
  23. "Trim STRING to end at first NULL found"
  24. (declare #.utils:*standard-optimize-settings*)
  25. (subseq string 0 (position #\Null string)))
  26. (defun dump-data (file-name data)
  27. (with-open-file (f file-name :direction :output :if-exists :supersede :element-type '(unsigned-byte 8))
  28. (write-sequence data f)))
  29. (defmacro redirect (filename &rest body)
  30. "Temporarily set *STANDARD-OUTPUT* to FILENAME and execute BODY."
  31. `(let ((*standard-output* (open ,filename :direction :output :if-does-not-exist :create :if-exists :supersede)))
  32. ,@body
  33. (finish-output *standard-output*)))
  34. (defun get-bitmask(start width)
  35. "Create a bit mask that begins at bit START (31 is MSB) and is WIDTH bits wide.
  36. Example: (get-bitmask 31 11) -->> #xffe00000"
  37. (declare #.utils:*standard-optimize-settings*)
  38. (ash (- (ash 1 width) 1) (- (1+ start) width)))
  39. (defmacro get-bitfield (int start width)
  40. "Extract WIDTH bits from INT starting at START
  41. Example: (get-bitfield #xFFFBB240 31 11) -->> #x7ff.
  42. The above will expand to (ash (logand #xFFFBB240 #xFFE00000) -21) at COMPILE time."
  43. `(ash (logand ,int ,(utils::get-bitmask start width)) ,(- ( - start width -1))))
  44. ;;;;;;;;;;;;;;;;;;;; convenience macros ;;;;;;;;;;;;;;;;;;;;
  45. (defmacro with-gensyms (syms &body body)
  46. `(let ,(mapcar #'(lambda (s)
  47. `(,s (gensym)))
  48. syms)
  49. ,@body))
  50. (defun make-keyword (name)
  51. (intern (string name) :keyword))
  52. (defmacro while (test &body body)
  53. `(do ()
  54. ((not ,test))
  55. ,@body))
  56. (defmacro aif (test-form then-form &optional else-form)
  57. `(let ((it ,test-form))
  58. (if it ,then-form ,else-form)))
  59. (defmacro awhen (test-form &body body)
  60. `(aif ,test-form
  61. (progn ,@body)))
  62. #+INSTRUMENT-MEMOIZED (progn
  63. (defstruct memoized-funcs
  64. table
  65. calls
  66. finds
  67. news)
  68. (defvar *memoized-funcs* nil))
  69. (defun mk-memoize (func)
  70. "Takes a normal function object and returns a memoized one"
  71. (let* ((hash-table (make-hash-table :test 'equal))
  72. #+INSTRUMENT-MEMOIZED (s (make-memoized-funcs :table hash-table :calls 0 :finds 0 :news 0))
  73. )
  74. #+INSTRUMENT-MEMOIZED (push s *memoized-funcs*)
  75. #'(lambda (arg)
  76. (multiple-value-bind (value foundp) (gethash arg hash-table)
  77. #+INSTRUMENT-MEMOIZED (incf (memoized-funcs-calls s))
  78. (if foundp
  79. (progn
  80. #+INSTRUMENT-MEMOIZED (incf (memoized-funcs-finds s))
  81. value)
  82. (progn
  83. #+INSTRUMENT-MEMOIZED (incf (memoized-funcs-news s))
  84. (setf (gethash arg hash-table) (funcall func arg))))))))
  85. (defmacro memoize (func-name)
  86. "Memoize function associated with Function-Name. Simplified version"
  87. `(setf (symbol-function ,func-name) (utils::mk-memoize (symbol-function ,func-name))))