unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* make-c-struct type support
@ 2010-10-26 15:49 Tristan Colgate
  2010-11-04  0:01 ` Ludovic Courtès
  0 siblings, 1 reply; 3+ messages in thread
From: Tristan Colgate @ 2010-10-26 15:49 UTC (permalink / raw)
  To: guile-user

Hi all,

  Does make-c-struct support poitners, long-integer and integer types?
A brief grok of the code suggests not. How would one go about
supporting it in a 32/64 bit agnostic manner? I'm trying to use the
ffi to access a fairly heavy duty struct (from net-snmp).

  Cheers,

-- 
Tristan Colgate-McFarlane
----
  "You can get all your daily vitamins from 52 pints of guiness, and a
glass of milk"



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: make-c-struct type support
  2010-10-26 15:49 make-c-struct type support Tristan Colgate
@ 2010-11-04  0:01 ` Ludovic Courtès
  2010-11-11 15:47   ` Ludovic Courtès
  0 siblings, 1 reply; 3+ messages in thread
From: Ludovic Courtès @ 2010-11-04  0:01 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 477 bytes --]

Hello,

Tristan Colgate <tcolgate@gmail.com> writes:

>   Does make-c-struct support poitners, long-integer and integer types?
> A brief grok of the code suggests not. How would one go about
> supporting it in a 32/64 bit agnostic manner?

The following patch (not committed yet) adds support for pointers (and
changes ‘parse-c-struct’ to honor alignment requirements);
‘long-integer’, ‘size_t’, etc., can be added similarly.

What do you think?

Thanks,
Ludo’.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: the patch --]
[-- Type: text/x-patch, Size: 2589 bytes --]

diff --git a/module/system/foreign.scm b/module/system/foreign.scm
index 84d1a03..1651d75 100644
--- a/module/system/foreign.scm
+++ b/module/system/foreign.scm
@@ -76,7 +76,15 @@
     (,int32 . ,bytevector-s32-native-set!)
     (,uint32 . ,bytevector-u32-native-set!)
     (,int64 . ,bytevector-s64-native-set!)
-    (,uint64 . ,bytevector-u64-native-set!)))
+    (,uint64 . ,bytevector-u64-native-set!)
+    (*       . ,(let ((write-addr!
+                       (case (sizeof '*)
+                         ((4) bytevector-u32-native-set!)
+                         ((8) bytevector-u64-native-set!)
+                         (else (error "unsupported pointer type")
+                               (sizeof '*)))))
+                  (lambda (bv offset ptr)
+                    (write-addr! bv offset (pointer-address ptr)))))))
 
 (define *readers*
   `((,float . ,bytevector-ieee-single-native-ref)
@@ -88,7 +96,15 @@
     (,int32 . ,bytevector-s32-native-ref)
     (,uint32 . ,bytevector-u32-native-ref)
     (,int64 . ,bytevector-s64-native-ref)
-    (,uint64 . ,bytevector-u64-native-ref)))
+    (,uint64 . ,bytevector-u64-native-ref)
+    (*       . ,(let ((read-addr
+                       (case (sizeof '*)
+                         ((4) bytevector-u32-native-ref)
+                         ((8) bytevector-u64-native-ref)
+                         (else (error "unsupported pointer type")
+                               (sizeof '*)))))
+                  (lambda (bv offset)
+                    (make-pointer (read-addr bv offset)))))))
 
 (define (align off alignment)
   (1+ (logior (1- off) (1- alignment))))
@@ -132,7 +148,8 @@
 
 (define (parse-c-struct foreign types)
   (let ((size (fold (lambda (type total)
-                      (+ (sizeof type) total))
+                      (+ (sizeof type)
+                         (align total (alignof type))))
                     0
                     types)))
     (read-c-struct (pointer->bytevector foreign size) 0 types)))
diff --git a/test-suite/tests/foreign.test b/test-suite/tests/foreign.test
index db92eca..8f7fd0a 100644
--- a/test-suite/tests/foreign.test
+++ b/test-suite/tests/foreign.test
@@ -182,4 +182,11 @@
           (data   (list -300 43)))
       (equal? (parse-c-struct (make-c-struct layout data)
                               layout)
+              data)))
+
+  (pass-if "with pointers"
+    (let ((layout (list uint8 '*))
+          (data   (list 222 (make-pointer 7777))))
+      (equal? (parse-c-struct (make-c-struct layout data)
+                              layout)
               data))))

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: make-c-struct type support
  2010-11-04  0:01 ` Ludovic Courtès
@ 2010-11-11 15:47   ` Ludovic Courtès
  0 siblings, 0 replies; 3+ messages in thread
From: Ludovic Courtès @ 2010-11-11 15:47 UTC (permalink / raw)
  To: guile-user

Hello!

ludo@gnu.org (Ludovic Courtès) writes:

> Tristan Colgate <tcolgate@gmail.com> writes:
>
>>   Does make-c-struct support poitners, long-integer and integer types?
>> A brief grok of the code suggests not. How would one go about
>> supporting it in a 32/64 bit agnostic manner?
>
> The following patch (not committed yet) adds support for pointers (and
> changes ‘parse-c-struct’ to honor alignment requirements);
> ‘long-integer’, ‘size_t’, etc., can be added similarly.

Committed:

  http://git.sv.gnu.org/cgit/guile.git/commit/?id=fb636a1cce4444928ab313574fa150a06baae54b
  http://git.sv.gnu.org/cgit/guile.git/commit/?id=42f7c01e0a1d1c139ec8b835429a80ab15ac4007

Thanks,
Ludo’.




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-11-11 15:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-10-26 15:49 make-c-struct type support Tristan Colgate
2010-11-04  0:01 ` Ludovic Courtès
2010-11-11 15:47   ` Ludovic Courtès

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).