Hack around a defect in `define-wrapped-pointer-type'.
[bpt/guile.git] / module / system / foreign.scm
1 ;;;; Copyright (C) 2010, 2011 Free Software Foundation, Inc.
2 ;;;;
3 ;;;; This library is free software; you can redistribute it and/or
4 ;;;; modify it under the terms of the GNU Lesser General Public
5 ;;;; License as published by the Free Software Foundation; either
6 ;;;; version 2.1 of the License, or (at your option) any later version.
7 ;;;;
8 ;;;; This library is distributed in the hope that it will be useful,
9 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;;;; Lesser General Public License for more details.
12 ;;;;
13 ;;;; You should have received a copy of the GNU Lesser General Public
14 ;;;; License along with this library; if not, write to the Free Software
15 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 ;;;;
17 \f
18
19 (define-module (system foreign)
20 #:use-module (rnrs bytevectors)
21 #:use-module (srfi srfi-1)
22 #:use-module (srfi srfi-9)
23 #:use-module (srfi srfi-9 gnu)
24 #:export (void
25 float double
26 short
27 unsigned-short
28 int unsigned-int long unsigned-long size_t
29 int8 uint8
30 uint16 int16
31 uint32 int32
32 uint64 int64
33
34 sizeof alignof
35
36 %null-pointer
37 null-pointer?
38 pointer?
39 make-pointer
40 pointer-address
41
42 pointer->bytevector
43 bytevector->pointer
44 set-pointer-finalizer!
45
46 dereference-pointer
47 string->pointer
48 pointer->string
49
50 pointer->procedure
51 ;; procedure->pointer (see below)
52 make-c-struct parse-c-struct
53
54 define-wrapped-pointer-type))
55
56 (eval-when (load eval compile)
57 (load-extension (string-append "libguile-" (effective-version))
58 "scm_init_foreign"))
59
60 \f
61 ;;;
62 ;;; Pointers.
63 ;;;
64
65 (define (null-pointer? pointer)
66 "Return true if POINTER is the null pointer."
67 (= (pointer-address pointer) 0))
68
69 (if (defined? 'procedure->pointer)
70 (export procedure->pointer))
71
72 \f
73 ;;;
74 ;;; Structures.
75 ;;;
76
77 (define bytevector-pointer-ref
78 (case (sizeof '*)
79 ((8) (lambda (bv offset)
80 (make-pointer (bytevector-u64-native-ref bv offset))))
81 ((4) (lambda (bv offset)
82 (make-pointer (bytevector-u32-native-ref bv offset))))
83 (else (error "what machine is this?"))))
84
85 (define bytevector-pointer-set!
86 (case (sizeof '*)
87 ((8) (lambda (bv offset ptr)
88 (bytevector-u64-native-set! bv offset (pointer-address ptr))))
89 ((4) (lambda (bv offset ptr)
90 (bytevector-u32-native-set! bv offset (pointer-address ptr))))
91 (else (error "what machine is this?"))))
92
93 (define *writers*
94 `((,float . ,bytevector-ieee-single-native-set!)
95 (,double . ,bytevector-ieee-double-native-set!)
96 (,int8 . ,bytevector-s8-set!)
97 (,uint8 . ,bytevector-u8-set!)
98 (,int16 . ,bytevector-s16-native-set!)
99 (,uint16 . ,bytevector-u16-native-set!)
100 (,int32 . ,bytevector-s32-native-set!)
101 (,uint32 . ,bytevector-u32-native-set!)
102 (,int64 . ,bytevector-s64-native-set!)
103 (,uint64 . ,bytevector-u64-native-set!)
104 (* . ,bytevector-pointer-set!)))
105
106 (define *readers*
107 `((,float . ,bytevector-ieee-single-native-ref)
108 (,double . ,bytevector-ieee-double-native-ref)
109 (,int8 . ,bytevector-s8-ref)
110 (,uint8 . ,bytevector-u8-ref)
111 (,int16 . ,bytevector-s16-native-ref)
112 (,uint16 . ,bytevector-u16-native-ref)
113 (,int32 . ,bytevector-s32-native-ref)
114 (,uint32 . ,bytevector-u32-native-ref)
115 (,int64 . ,bytevector-s64-native-ref)
116 (,uint64 . ,bytevector-u64-native-ref)
117 (* . ,bytevector-pointer-ref)))
118
119
120 (define (align off alignment)
121 (1+ (logior (1- off) (1- alignment))))
122
123 (define (write-c-struct bv offset types vals)
124 (let lp ((offset offset) (types types) (vals vals))
125 (cond
126 ((not (pair? types))
127 (or (null? vals)
128 (error "too many values" vals)))
129 ((not (pair? vals))
130 (error "too few values" types))
131 (else
132 ;; alignof will error-check
133 (let* ((type (car types))
134 (offset (align offset (alignof type))))
135 (if (pair? type)
136 (write-c-struct bv offset (car types) (car vals))
137 ((assv-ref *writers* type) bv offset (car vals)))
138 (lp (+ offset (sizeof type)) (cdr types) (cdr vals)))))))
139
140 (define (read-c-struct bv offset types)
141 (let lp ((offset offset) (types types) (vals '()))
142 (cond
143 ((not (pair? types))
144 (reverse vals))
145 (else
146 ;; alignof will error-check
147 (let* ((type (car types))
148 (offset (align offset (alignof type))))
149 (lp (+ offset (sizeof type)) (cdr types)
150 (cons (if (pair? type)
151 (read-c-struct bv offset (car types))
152 ((assv-ref *readers* type) bv offset))
153 vals)))))))
154
155 (define (make-c-struct types vals)
156 (let ((bv (make-bytevector (sizeof types) 0)))
157 (write-c-struct bv 0 types vals)
158 (bytevector->pointer bv)))
159
160 (define (parse-c-struct foreign types)
161 (let ((size (fold (lambda (type total)
162 (+ (sizeof type)
163 (align total (alignof type))))
164 0
165 types)))
166 (read-c-struct (pointer->bytevector foreign size) 0 types)))
167
168 \f
169 ;;;
170 ;;; Wrapped pointer types.
171 ;;;
172
173 (define-syntax define-wrapped-pointer-type
174 (lambda (stx)
175 "Define helper procedures to wrap pointer objects into Scheme
176 objects with a disjoint type. Specifically, this macro defines PRED, a
177 predicate for the new Scheme type, WRAP, a procedure that takes a
178 pointer object and returns an object that satisfies PRED, and UNWRAP
179 which does the reverse. PRINT must name a user-defined object printer."
180 (syntax-case stx ()
181 ((_ pred wrap unwrap print)
182 (and (symbol? (syntax->datum #'pred))
183 (symbol? (syntax->datum #'wrap))
184 (symbol? (syntax->datum #'unwrap)))
185
186 ;; Choose TYPE-NAME deterministically to help separate
187 ;; compilation. It could be an arg of the macro, but that would
188 ;; expose an implementation detail.
189 (with-syntax ((type-name (datum->syntax
190 #'pred
191 (symbol-append '%%
192 (syntax->datum #'pred)
193 '-type-name)))
194 (%wrap (datum->syntax #'wrap (gensym "wrap"))))
195 #'(begin
196 (define-record-type type-name
197 (%wrap pointer)
198 pred
199 (pointer unwrap))
200 (define wrap
201 ;; Use a weak hash table to preserve pointer identity, i.e.,
202 ;; PTR1 == PTR2 <-> (eq? (wrap PTR1) (wrap PTR2)).
203 (let ((ptr->obj (make-weak-value-hash-table 3000)))
204 (lambda (ptr)
205 (let ((key+value (hash-create-handle! ptr->obj ptr #f)))
206 (or (cdr key+value)
207 (let ((o (%wrap ptr)))
208 (set-cdr! key+value o)
209 o))))))
210 (set-record-type-printer! type-name print)))))))