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