1 ;;;; Copyright (C) 2010 Free Software Foundation, Inc.
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.
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.
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
19 (define-module (system foreign)
20 #:use-module (rnrs bytevector)
30 foreign-ref foreign-set!
31 foreign->bytevector bytevector->foreign
32 foreign-set-finalizer!
34 make-c-struct parse-c-struct))
36 (load-extension (string-append "libguile-" (effective-version))
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!)))
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)))
63 (define (align off alignment)
64 (1+ (logior (1- off) (1- alignment))))
66 (define (write-c-struct bv offset types vals)
67 (let lp ((offset offset) (types types) (vals vals))
71 (error "too many values" vals)))
73 (error "too few values" types))
75 ;; alignof will error-check
76 (let* ((type (car types))
77 (offset (align offset (alignof 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)))))))
83 (define (read-c-struct bv offset types)
84 (let lp ((offset offset) (types types) (vals '()))
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))
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)))
103 (define (parse-c-struct foreign types)
104 (read-c-struct (foreign->bytevector foreign) 0 types))