1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
3 ;;; cffi-clasp.lisp --- CFFI-SYS implementation for Clasp.
5 ;;; Copyright (C) 2017 Frank Goenninger <frank.goenninger@goenninger.net>
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:
15 ;;; The above copyright notice and this permission notice shall be
16 ;;; included in all copies or substantial portions of the Software.
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.
28 (in-package #:cffi-sys
)
32 (pushnew 'flat-namespace cl
:*features
*)
36 (defun canonicalize-symbol-name-case (name)
37 (declare (string name
))
42 (defun %foreign-alloc
(size)
43 "Allocate SIZE bytes of foreign-addressable memory."
44 (clasp-ffi:%foreign-alloc size
))
46 (defun foreign-free (ptr)
47 "Free a pointer PTR allocated by FOREIGN-ALLOC."
48 (clasp-ffi:%foreign-free ptr
))
50 (defmacro with-foreign-pointer
((var size
&optional size-var
) &body body
)
51 "Bind VAR to SIZE bytes of foreign memory during BODY. The
52 pointer in VAR is invalid beyond the dynamic extent of BODY, and
53 may be stack-allocated if supported by the implementation. If
54 SIZE-VAR is supplied, it will be bound to SIZE during BODY."
56 (setf size-var
(gensym "SIZE")))
57 `(let* ((,size-var
,size
)
58 (,var
(%foreign-alloc
,size-var
)))
61 (foreign-free ,var
))))
63 ;;;# Misc. Pointer Operations
65 (deftype foreign-pointer
()
66 'clasp-ffi
:foreign-data
)
68 (defun null-pointer-p (ptr)
69 "Test if PTR is a null pointer."
70 (clasp-ffi:%null-pointer-p ptr
))
72 (defun null-pointer ()
73 "Construct and return a null pointer."
74 (clasp-ffi:%make-nullpointer
))
76 (defun make-pointer (address)
77 "Return a pointer pointing to ADDRESS."
78 (clasp-ffi:%make-pointer address
))
80 (defun inc-pointer (ptr offset
)
81 "Return a pointer OFFSET bytes past PTR."
82 (clasp-ffi:%inc-pointer ptr offset
))
84 (defun pointer-address (ptr)
85 "Return the address pointed to by PTR."
86 (clasp-ffi:%foreign-data-address ptr
))
89 "Return true if PTR is a foreign pointer."
90 (typep ptr
'clasp-ffi
:foreign-data
))
92 (defun pointer-eq (ptr1 ptr2
)
93 "Return true if PTR1 and PTR2 point to the same address."
94 (check-type ptr1 clasp-ffi
:foreign-data
)
95 (check-type ptr2 clasp-ffi
:foreign-data
)
96 (eql (pointer-address ptr1
) (pointer-address ptr2
)))
99 ;;;# Shareable Vectors
101 ;;; This interface is very experimental. WITH-POINTER-TO-VECTOR-DATA
102 ;;; should be defined to perform a copy-in/copy-out if the Lisp
103 ;;; implementation can't do this.
105 (defun make-shareable-byte-vector (size)
106 "Create a Lisp vector of SIZE bytes that can passed to
107 WITH-POINTER-TO-VECTOR-DATA."
108 (make-array size
:element-type
'(unsigned-byte 8)))
110 (defmacro with-pointer-to-vector-data
((ptr-var vector
) &body body
)
111 "Bind PTR-VAR to a foreign pointer to the data in VECTOR."
112 `(let ((,ptr-var
(ext:array-pointer
,vector
)))
115 (defun %foreign-type-size
(type-keyword)
116 "Return the size in bytes of a foreign type."
117 (clasp-ffi:%foreign-type-size type-keyword
))
119 (defun %foreign-type-alignment
(type-keyword)
120 "Return the alignment in bytes of a foreign type."
121 (clasp-ffi:%foreign-type-alignment type-keyword
))
125 (defun %mem-ref
(ptr type
&optional
(offset 0))
126 "Dereference an object of TYPE at OFFSET bytes from PTR."
127 (clasp-ffi:%mem-ref ptr type offset
))
129 (defun %mem-set
(value ptr type
&optional
(offset 0))
130 "Set an object of TYPE at OFFSET bytes from PTR."
131 (clasp-ffi:%mem-set ptr type value offset
))
133 (defmacro %foreign-funcall
(name args
&key library convention
)
134 "Call a foreign function."
135 (declare (ignore library convention
))
136 `(clasp-ffi:%foreign-funcall
,name
,@args
))
138 (defmacro %foreign-funcall-pointer
(ptr args
&key convention
)
139 "Funcall a pointer to a foreign function."
140 (declare (ignore convention
))
141 `(clasp-ffi:%foreign-funcall-pointer
,ptr
,@args
))
143 ;;;# Foreign Libraries
145 (defun %load-foreign-library
(name path
)
146 "Load a foreign library."
147 (clasp-ffi:%load-foreign-library name path
))
149 (defun %close-foreign-library
(handle)
150 "Close a foreign library."
151 (clasp-ffi:%close-foreign-library handle
))
153 (defun %foreign-symbol-pointer
(name library
)
154 "Returns a pointer to a foreign symbol NAME."
155 (clasp-ffi:%foreign-symbol-pointer name library
))
157 (defun native-namestring (pathname)
158 (namestring pathname
))
162 (defmacro %defcallback
(name rettype arg-names arg-types body
164 `(clasp-ffi:%defcallback
(,name
,@(when convention
`(:convention
,convention
)))
165 ,rettype
,arg-names
,arg-types
,body
))
167 (defun %callback
(name)
168 (clasp-ffi:%get-callback name
))