mapping.lisp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
  2. ;;;
  3. ;;; mapping.lisp --- An example for mapping Lisp objects to ints.
  4. ;;;
  5. ;;; Copyright (C) 2007, Luis Oliveira <loliveira@common-lisp.net>
  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. ;;; This is an example on how to tackle the problem of passing Lisp
  28. ;;; object identifiers to foreign code. It is not a great example,
  29. ;;; but might be useful nevertheless.
  30. ;;;
  31. ;;; Requires trivial-garbage: <http://cliki.net/trivial-garbage>
  32. (defpackage #:cffi-mapping-test
  33. (:use #:common-lisp #:cffi #:trivial-garbage)
  34. (:export #:run))
  35. (in-package #:cffi-mapping-test)
  36. (define-foreign-type lisp-object-type ()
  37. ((weakp :initarg :weakp))
  38. (:actual-type :unsigned-int))
  39. (define-parse-method lisp-object (&key weak-mapping)
  40. (make-instance 'lisp-object-type :weakp weak-mapping))
  41. (defvar *regular-hashtable* (make-hash-table))
  42. (defvar *weak-hashtable* (make-weak-hash-table :weakness :value))
  43. (defvar *regular-counter* 0)
  44. (defvar *weak-counter* 0)
  45. (defun increment-counter (value)
  46. (mod (1+ value) (expt 2 (* 8 (foreign-type-size :unsigned-int)))))
  47. (define-modify-macro incf-counter () increment-counter)
  48. (defmethod translate-to-foreign (value (type lisp-object-type))
  49. (with-slots (weakp) type
  50. (let ((id (if weakp
  51. (incf-counter *weak-counter*)
  52. (incf-counter *regular-counter*)))
  53. (ht (if weakp *weak-hashtable* *regular-hashtable*)))
  54. (setf (gethash id ht) value)
  55. id)))
  56. (defmethod translate-from-foreign (int (type lisp-object-type))
  57. (with-slots (weakp) type
  58. (gethash int (if weakp *weak-hashtable* *regular-hashtable*))))
  59. ;;;; Silly example.
  60. (defctype weak-mapping (lisp-object :weak-mapping t))
  61. ;;; (run) => #<FUNCTION (LAMBDA (X)) {11AB46F5}>
  62. (defun run ()
  63. (foreign-funcall "abs" weak-mapping (lambda (x) x) weak-mapping))