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)
23 int unsigned-int long unsigned-long size_t
31 foreign-ref foreign-set!
32 foreign->bytevector bytevector->foreign
33 foreign-set-finalizer!
35 make-c-struct parse-c-struct))
37 (load-extension (string-append "libguile-" (effective-version))
41 `((,float . ,bytevector-ieee-single-native-set!)
42 (,double . ,bytevector-ieee-double-native-set!)
43 (,int8 . ,bytevector-s8-set!)
44 (,uint8 . ,bytevector-u8-set!)
45 (,int16 . ,bytevector-s16-native-set!)
46 (,uint16 . ,bytevector-u16-native-set!)
47 (,int32 . ,bytevector-s32-native-set!)
48 (,uint32 . ,bytevector-u32-native-set!)
49 (,int64 . ,bytevector-s64-native-set!)
50 (,uint64 . ,bytevector-u64-native-set!)))
53 `((,float . ,bytevector-ieee-single-native-ref)
54 (,double . ,bytevector-ieee-double-native-ref)
55 (,int8 . ,bytevector-s8-ref)
56 (,uint8 . ,bytevector-u8-ref)
57 (,int16 . ,bytevector-s16-native-ref)
58 (,uint16 . ,bytevector-u16-native-ref)
59 (,int32 . ,bytevector-s32-native-ref)
60 (,uint32 . ,bytevector-u32-native-ref)
61 (,int64 . ,bytevector-s64-native-ref)
62 (,uint64 . ,bytevector-u64-native-ref)))
64 (define (align off alignment)
65 (1+ (logior (1- off) (1- alignment))))
67 (define (write-c-struct bv offset types vals)
68 (let lp ((offset offset) (types types) (vals vals))
72 (error "too many values" vals)))
74 (error "too few values" types))
76 ;; alignof will error-check
77 (let* ((type (car types))
78 (offset (align offset (alignof type))))
80 (write-c-struct bv offset (car types) (car vals))
81 ((assv-ref *writers* type) bv offset (car vals)))
82 (lp (+ offset (sizeof type)) (cdr types) (cdr vals)))))))
84 (define (read-c-struct bv offset types)
85 (let lp ((offset offset) (types types) (vals '()))
90 ;; alignof will error-check
91 (let* ((type (car types))
92 (offset (align offset (alignof type))))
93 (lp (+ offset (sizeof type)) (cdr types)
94 (cons (if (pair? type)
95 (read-c-struct bv offset (car types))
96 ((assv-ref *readers* type) bv offset))
99 (define (make-c-struct types vals)
100 (let ((bv (make-bytevector (sizeof types) 0)))
101 (write-c-struct bv 0 types vals)
102 (bytevector->foreign bv)))
104 (define (parse-c-struct foreign types)
105 (read-c-struct (foreign->bytevector foreign) 0 types))