rename (rnrs bytevector) to (rnrs bytevectors)
[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 %null-pointer
29
30 sizeof alignof
31
32 foreign-ref foreign-set!
33 foreign->bytevector bytevector->foreign
34 foreign-set-finalizer!
35 make-foreign-function
36 make-c-struct parse-c-struct))
37
38 (load-extension (string-append "libguile-" (effective-version))
39 "scm_init_foreign")
40
41 (define *writers*
42 `((,float . ,bytevector-ieee-single-native-set!)
43 (,double . ,bytevector-ieee-double-native-set!)
44 (,int8 . ,bytevector-s8-set!)
45 (,uint8 . ,bytevector-u8-set!)
46 (,int16 . ,bytevector-s16-native-set!)
47 (,uint16 . ,bytevector-u16-native-set!)
48 (,int32 . ,bytevector-s32-native-set!)
49 (,uint32 . ,bytevector-u32-native-set!)
50 (,int64 . ,bytevector-s64-native-set!)
51 (,uint64 . ,bytevector-u64-native-set!)))
52
53 (define *readers*
54 `((,float . ,bytevector-ieee-single-native-ref)
55 (,double . ,bytevector-ieee-double-native-ref)
56 (,int8 . ,bytevector-s8-ref)
57 (,uint8 . ,bytevector-u8-ref)
58 (,int16 . ,bytevector-s16-native-ref)
59 (,uint16 . ,bytevector-u16-native-ref)
60 (,int32 . ,bytevector-s32-native-ref)
61 (,uint32 . ,bytevector-u32-native-ref)
62 (,int64 . ,bytevector-s64-native-ref)
63 (,uint64 . ,bytevector-u64-native-ref)))
64
65 (define (align off alignment)
66 (1+ (logior (1- off) (1- alignment))))
67
68 (define (write-c-struct bv offset types vals)
69 (let lp ((offset offset) (types types) (vals vals))
70 (cond
71 ((not (pair? types))
72 (or (null? vals)
73 (error "too many values" vals)))
74 ((not (pair? vals))
75 (error "too few values" types))
76 (else
77 ;; alignof will error-check
78 (let* ((type (car types))
79 (offset (align offset (alignof type))))
80 (if (pair? type)
81 (write-c-struct bv offset (car types) (car vals))
82 ((assv-ref *writers* type) bv offset (car vals)))
83 (lp (+ offset (sizeof type)) (cdr types) (cdr vals)))))))
84
85 (define (read-c-struct bv offset types)
86 (let lp ((offset offset) (types types) (vals '()))
87 (cond
88 ((not (pair? types))
89 (reverse vals))
90 (else
91 ;; alignof will error-check
92 (let* ((type (car types))
93 (offset (align offset (alignof type))))
94 (lp (+ offset (sizeof type)) (cdr types)
95 (cons (if (pair? type)
96 (read-c-struct bv offset (car types))
97 ((assv-ref *readers* type) bv offset))
98 vals)))))))
99
100 (define (make-c-struct types vals)
101 (let ((bv (make-bytevector (sizeof types) 0)))
102 (write-c-struct bv 0 types vals)
103 (bytevector->foreign bv)))
104
105 (define (parse-c-struct foreign types)
106 (read-c-struct (foreign->bytevector foreign) 0 types))