[Openmcl-devel] HOWTO: Have a foreign structure field point to parent structure

Gary Byers gb at clozure.com
Sun Sep 29 20:53:16 UTC 2002



On Sun, 29 Sep 2002, Kevin Rosenberg wrote:

> With John Desoi's excellent work adding openmcl support to UFFI, I
> nearly have all of CLSQL working on OpenMCL. There is one more thing
> that I need:
>
> I need to have a way to have a create a field in a foreign type
> structure that contains a field that is of a type that points to the
> parent structure. Such as
>
> struct astruct {
>   int data;
>   struct astruct* next;
> }
>
> In UFFI syntax, this would be:
> (uffi:def-struct astruct
>   (data :int)
>   (next :pointer-self))
>
> So, I need to add code to UFFI to convert the :pointer-self construct
> to a valid openmcl construct.

There isn't any special syntax for "pointer to self" or for "pointer to
as-yet-undefined-structure-type" in general:


Welcome to OpenMCL Version (Beta: Darwin) 0.13!
? (ccl::def-foreign-type nil (:struct :astruct
				   (:data :int)
				   (:next (:* (:struct :astruct)))))
NIL
? (ccl::parse-foreign-type '(:struct :astruct))
#<FOREIGN-RECORD-TYPE (CCL::STRUCT :ASTRUCT (:DATA (CCL::SIGNED 32)) (:NEXT (* (CCL::STRUCT :ASTRUCT)))) #x522944E>
? (describe *)
#<FOREIGN-RECORD-TYPE (CCL::STRUCT :ASTRUCT (:DATA (CCL::SIGNED 32))
                       (:NEXT (* (CCL::STRUCT :ASTRUCT)))) #x522944E>
Type: CCL::FOREIGN-RECORD-TYPE
Class: #<STRUCTURE-CLASS CCL::FOREIGN-RECORD-TYPE>
CLASS: CCL::RECORD
BITS: 64
ALIGNMENT: 32
KIND: :STRUCT
NAME: :ASTRUCT
FIELDS: (#<FOREIGN-RECORD-FIELD #<FOREIGN-INTEGER-TYPE (CCL::SIGNED
                                                        32) #x5130DE6> :DATA>
         #<FOREIGN-RECORD-FIELD #<FOREIGN-POINTER-TYPE (*
                                                        (CCL::STRUCT
                                                         :ASTRUCT
                                                         (:DATA
                                                          (CCL::SIGNED 32))
                                                         (:NEXT
                                                          (*
                                                           (CCL::STRUCT
                                                            :ASTRUCT))))) #x5229506> :NEXT>)
ALT-ALIGN: NIL
?
;;; Equivalently, we can define the structure :CSTRUCT as containing
;;; a pointer to an (as yet undefined) structure type :BSTRUCT :

? (ccl::def-foreign-type nil (:struct :cstruct
				   (:data :int)
				   (:b (:* (:struct :bstruct)))))
NIL
?
;;; At this point, we can instantiate a :CSTRUCT (via RLET or MAKE-RECORD),
;;; but the :B field of such a structure is just a generic pointer.

;;; What we can't do (and could, if CCL::DEF-FOREIGN-TYPE was a little
;;; lazier) is define a structure type that contains instances of (as
;;; opposed to pointers to) an undefined structure type.  That requires
;;; a little bit of care (to detect and avoid circularity: a structure
;;; shouldnt transitively contain an instance of itself), but could
;;; sometimes be useful.

> I browsed through the HTML/foreign-types.html document, but I didn't
> see what I needed.
>
> Thanks in advance for any help.
>
> --
>        Kevin Rosenberg        |  .''`.  ** Debian GNU/Linux **
>   http://b9.com/debian.html   | : :' :      The  universal
>   GPG signed and encrypted    | `. `'      Operating System
>      messages accepted.       |   `-    http://www.debian.org/
>
> _______________________________________________
> Openmcl-devel mailing list
> Openmcl-devel at clozure.com
> http://clozure.com/cgi-bin/mailman/listinfo/openmcl-devel
>
>


_______________________________________________
Openmcl-devel mailing list
Openmcl-devel at clozure.com
http://clozure.com/cgi-bin/mailman/listinfo/openmcl-devel



More information about the Openmcl-devel mailing list