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 bytevectors)
23 int unsigned-int long unsigned-long size_t
32 foreign-ref foreign-set!
33 foreign->bytevector bytevector->foreign
34 foreign-set-finalizer!
36 make-c-struct parse-c-struct))
38 (load-extension (string-append "libguile-" (effective-version))
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!)))
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)))
65 (define (align off alignment)
66 (1+ (logior (1- off) (1- alignment))))
68 (define (write-c-struct bv offset types vals)
69 (let lp ((offset offset) (types types) (vals vals))
73 (error "too many values" vals)))
75 (error "too few values" types))
77 ;; alignof will error-check
78 (let* ((type (car types))
79 (offset (align offset (alignof 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)))))))
85 (define (read-c-struct bv offset types)
86 (let lp ((offset offset) (types types) (vals '()))
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))
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)))
105 (define (parse-c-struct foreign types)
106 (read-c-struct (foreign->bytevector foreign) 0 types))