[racket-dev] cstruct and properties

From: Tobias Hammer (tobias.hammer at dlr.de)
Date: Wed Nov 7 14:00:51 EST 2012

Hi,

i am currently playing around with properties attached to cstructs and ran  
into some problems.

Normally, with racket structs, i can access a property without an instance  
of the struct via the struct type descriptor (struct:S). There seems to be  
nothing similar for cstructs, so the only way i found to check for or  
access a property is to create a dummy instance solely for this purpose.
Would it be possible to add a similar type descriptor for cstructs? To  
account for the incompatibility with the one for structs it might make  
sense to name it different, maybe cstruct:S or similar.

If such a descriptor would exists it should be included in the transformer  
bindings for cstructs. Another info i am really missing there is the  
super-type field. As far as i can see, it would be a valid semantic to set  
it to the identifier if the cstruct was created by type and to set it #f  
if it got its super-type by cpointer.

But one more thing on properties. A source of really nasty bugs is if you  
try to use the ctype of a cstruct inside the property:

(define-values (p p? p-ref) (make-struct-type-property 'my-p))
(define-cstruct _S ([a (_array _byte 23)])
   #:property p _S)
(define s (ptr-ref (malloc _S) _S)) ; dummy instance

Getting the value results in:
(p-ref s)
> #<ctype>
(ctype-sizeof (p-ref s))
> 4

Checking ffi/unsafe.rkt shows that i get a temporary cpointer as ctype.  
The real type already exists at the time the wrapper-struct is created but  
it is not yet accessible by the name _S.
The following small patch should solve this by locally introducing the  
correct name during the struct creation.

diff --git a/collects/ffi/unsafe.rkt b/collects/ffi/unsafe.rkt
index 37ffc7c..4842e87 100644
--- a/collects/ffi/unsafe.rkt
+++ b/collects/ffi/unsafe.rkt
@@ -1384,7 +1384,7 @@
                                                 #'(begin)
                                                 (with-syntax ([(prop ...)  
property-stxes])
                                                   #'(define make-wrap-TYPE
-                                                     (let ()
+                                                     (let ([_TYPE _TYPE*])
                                                         (struct  
cpointer:TYPE (ptr)
                                                           #:property  
prop:cpointer 0
                                                           prop ...)




-- 
---------------------------------------------------------
Tobias Hammer
DLR / Institute of Robotics and Mechatronics
Muenchner Str. 20, D-82234 Wessling
Tel.: 08153/28-1487
Mail: tobias.hammer at dlr.de

Posted on the dev mailing list.