utils.lisp 4.5 KB

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