gettimeofday.lisp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
  2. ;;;
  3. ;;; gettimeofday.lisp --- Example CFFI binding to gettimeofday(2)
  4. ;;;
  5. ;;; Copyright (C) 2005-2006, James Bielman <jamesjb@jamesjb.com>
  6. ;;;
  7. ;;; Permission is hereby granted, free of charge, to any person
  8. ;;; obtaining a copy of this software and associated documentation
  9. ;;; files (the "Software"), to deal in the Software without
  10. ;;; restriction, including without limitation the rights to use, copy,
  11. ;;; modify, merge, publish, distribute, sublicense, and/or sell copies
  12. ;;; of the Software, and to permit persons to whom the Software is
  13. ;;; furnished to do so, subject to the following conditions:
  14. ;;;
  15. ;;; The above copyright notice and this permission notice shall be
  16. ;;; included in all copies or substantial portions of the Software.
  17. ;;;
  18. ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. ;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. ;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  22. ;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  23. ;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. ;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  25. ;;; DEALINGS IN THE SOFTWARE.
  26. ;;;
  27. ;;;# CFFI Example: gettimeofday binding
  28. ;;;
  29. ;;; This example illustrates the use of foreign structures, typedefs,
  30. ;;; and using type translators to do checking of input and output
  31. ;;; arguments to a foreign function.
  32. (defpackage #:cffi-example-gettimeofday
  33. (:use #:common-lisp #:cffi)
  34. (:export #:gettimeofday))
  35. (in-package #:cffi-example-gettimeofday)
  36. ;;; Define the TIMEVAL structure used by 'gettimeofday'. This assumes
  37. ;;; that 'time_t' is a 'long' --- it would be nice if CFFI could
  38. ;;; provide a proper :TIME-T type to help make this portable.
  39. (defcstruct timeval
  40. (tv-sec :long)
  41. (tv-usec :long))
  42. ;;; A NULL-POINTER is a foreign :POINTER that must always be NULL.
  43. ;;; Both a NULL pointer and NIL are legal values---any others will
  44. ;;; result in a runtime error.
  45. (define-foreign-type null-pointer-type ()
  46. ()
  47. (:actual-type :pointer)
  48. (:simple-parser null-pointer))
  49. ;;; This type translator is used to ensure that a NULL-POINTER has a
  50. ;;; null value. It also converts NIL to a null pointer.
  51. (defmethod translate-to-foreign (value (type null-pointer-type))
  52. (cond
  53. ((null value) (null-pointer))
  54. ((null-pointer-p value) value)
  55. (t (error "~A is not a null pointer." value))))
  56. ;;; The SYSCALL-RESULT type is an integer type used for the return
  57. ;;; value of C functions that return -1 and set errno on errors.
  58. ;;; Someday when CFFI has a portable interface for dealing with
  59. ;;; 'errno', this error reporting can be more useful.
  60. (define-foreign-type syscall-result-type ()
  61. ()
  62. (:actual-type :int)
  63. (:simple-parser syscall-result))
  64. ;;; Type translator to check a SYSCALL-RESULT and signal a Lisp error
  65. ;;; if the value is negative.
  66. (defmethod translate-from-foreign (value (type syscall-result-type))
  67. (if (minusp value)
  68. (error "System call failed with return value ~D." value)
  69. value))
  70. ;;; Define the Lisp function %GETTIMEOFDAY to call the C function
  71. ;;; 'gettimeofday', passing a pointer to the TIMEVAL structure to fill
  72. ;;; in. The TZP parameter is deprecated and should be NULL --- we can
  73. ;;; enforce this by using our NULL-POINTER type defined above.
  74. (defcfun ("gettimeofday" %gettimeofday) syscall-result
  75. (tp :pointer)
  76. (tzp null-pointer))
  77. ;;; Define a Lispy interface to 'gettimeofday' that returns the
  78. ;;; seconds and microseconds as multiple values.
  79. (defun gettimeofday ()
  80. (with-foreign-object (tv 'timeval)
  81. (%gettimeofday tv nil)
  82. (with-foreign-slots ((tv-sec tv-usec) tv timeval)
  83. (values tv-sec tv-usec))))