Use "pointer" instead of "foreign" when dealing with wrapped pointers.
[bpt/guile.git] / module / system / foreign.scm
1 ;;;; Copyright (C) 2010 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 #:export (void
22 float double
23 int unsigned-int long unsigned-long size_t
24 int8 uint8
25 uint16 int16
26 uint32 int32
27 uint64 int64
28
29 sizeof alignof
30
31 %null-pointer
32 null-pointer?
33 make-pointer
34 pointer-address
35 dereference-pointer
36
37 pointer->bytevector
38 bytevector->pointer
39 set-pointer-finalizer!
40
41 make-foreign-function
42 make-c-struct parse-c-struct))
43
44 (load-extension (string-append "libguile-" (effective-version))
45 "scm_init_foreign")
46
47 \f
48 ;;;
49 ;;; Pointers.
50 ;;;
51
52 (define (null-pointer? pointer)
53 (= (pointer-address pointer) 0))
54
55
56 \f
57 ;;;
58 ;;; Structures.
59 ;;;
60
61 (define *writers*
62 `((,float . ,bytevector-ieee-single-native-set!)
63 (,double . ,bytevector-ieee-double-native-set!)
64 (,int8 . ,bytevector-s8-set!)
65 (,uint8 . ,bytevector-u8-set!)
66 (,int16 . ,bytevector-s16-native-set!)
67 (,uint16 . ,bytevector-u16-native-set!)
68 (,int32 . ,bytevector-s32-native-set!)
69 (,uint32 . ,bytevector-u32-native-set!)
70 (,int64 . ,bytevector-s64-native-set!)
71 (,uint64 . ,bytevector-u64-native-set!)))
72
73 (define *readers*
74 `((,float . ,bytevector-ieee-single-native-ref)
75 (,double . ,bytevector-ieee-double-native-ref)
76 (,int8 . ,bytevector-s8-ref)
77 (,uint8 . ,bytevector-u8-ref)
78 (,int16 . ,bytevector-s16-native-ref)
79 (,uint16 . ,bytevector-u16-native-ref)
80 (,int32 . ,bytevector-s32-native-ref)
81 (,uint32 . ,bytevector-u32-native-ref)
82 (,int64 . ,bytevector-s64-native-ref)
83 (,uint64 . ,bytevector-u64-native-ref)))
84
85 (define (align off alignment)
86 (1+ (logior (1- off) (1- alignment))))
87
88 (define (write-c-struct bv offset types vals)
89 (let lp ((offset offset) (types types) (vals vals))
90 (cond
91 ((not (pair? types))
92 (or (null? vals)
93 (error "too many values" vals)))
94 ((not (pair? vals))
95 (error "too few values" types))
96 (else
97 ;; alignof will error-check
98 (let* ((type (car types))
99 (offset (align offset (alignof type))))
100 (if (pair? type)
101 (write-c-struct bv offset (car types) (car vals))
102 ((assv-ref *writers* type) bv offset (car vals)))
103 (lp (+ offset (sizeof type)) (cdr types) (cdr vals)))))))
104
105 (define (read-c-struct bv offset types)
106 (let lp ((offset offset) (types types) (vals '()))
107 (cond
108 ((not (pair? types))
109 (reverse vals))
110 (else
111 ;; alignof will error-check
112 (let* ((type (car types))
113 (offset (align offset (alignof type))))
114 (lp (+ offset (sizeof type)) (cdr types)
115 (cons (if (pair? type)
116 (read-c-struct bv offset (car types))
117 ((assv-ref *readers* type) bv offset))
118 vals)))))))
119
120 (define (make-c-struct types vals)
121 (let ((bv (make-bytevector (sizeof types) 0)))
122 (write-c-struct bv 0 types vals)
123 (bytevector->pointer bv)))
124
125 (define (parse-c-struct foreign types)
126 (read-c-struct (pointer->bytevector foreign) 0 types))