[Openmcl-devel] def-fore-type with :unsigned-doubleword

Gary Byers gb at clozure.com
Tue Dec 3 15:53:40 PST 2013


A little C program:
----------------------------------------------------------------------------
#include <stddef.h>
#include <stdio.h>

typedef unsigned long long MIDITimeStamp;

struct MIDIPacket {
    MIDITimeStamp  timeStamp;
    unsigned short         length;
    unsigned char           data[256];
};

int
main()
{
   fprintf(stderr, "timestamp @ %d, length @ %d, data @ %d, total size = %d\n",
           (int) offsetof(struct MIDIPacket, timeStamp),
           (int) offsetof(struct MIDIPacket, length),
           (int) offsetof(struct MIDIPacket, data),
           (int) sizeof(struct MIDIPacket));


}
----------------------------------------------------------------------------
when compiled with

$ cc example.c -o example

and run via:

$ ./example

generates output like:

timestamp @ 0, length @ 8, data @ 10, total size = 272

for me.  (In other words, it agrees with CCL about the
size of the structure and about the offsets of its fields when
the structure is defined in CCL via

(ccl:def-foreign-type :midipacket
   (:struct :midipacket
   (time-stamp :unsigned-doubleword)
   (length :unsigned-short)
   (data (:array :unsigned-char 256))))

).

I don't know if the structure is actually defined in the relevant
header file or why you got different results for sizeof() in a C
program.  I googled for "MIDIPacket" and found something that
seemed to match what you define below, but eventually realized
that I was looking at iOS documentation.

If the "timeStamp" field was actually defined to be (for instance)
two 32-bit integers (rather than a single 64-bit integer), that
would explain the different sizeof() result (since it would
weaken alignment constraints) and that might explain the
problem you're having.

I don't know if that's the problem, but DEF-FOREIGN-TYPE seems
to be doing what it's told to do in this case.

On Wed, 4 Dec 2013, Park SungMin wrote:

> I working with CoreMIDI in ccl.(1.10-dev-r15972M-trunk ?(DarwinX8664))
> 
> in CoreMIDI have type "structure MIDIPacket"
> 
> typedef UInt64 MIDITimeStamp;
> 
> struct MIDIPacket {
>    MIDITimeStamp  timeStamp;
>    UInt16         length;
>    Byte           data[256];
> };
> 
> so I wrapper it?
> 
> (ccl:def-foreign-type :midipacket
> ? ? (:struct :midipacket?
> ? ? (time-stamp :unsigned-doubleword)
> ? ? (length :unsigned-short)
> ? ? (data (:array :unsigned-char 256))))
> 
> ?in C?printf(?size: %d\n?, sizeof(struct MIDIPacket)) is 268,
> but (/ (ccl::foreign-size :midipacket) 8) is 272.
> 
> This type used by this callback function for MidiEvent..
> (ccl:defcallback midi-read-proc (:address packet-list
> :address read-proc-ref-con
> :address src-conn-ref-con
> :void)
> ? (let ((pkt (ccl:pref packet-list :midi-packet-list.packet)))
> ? ? (format t "midipkt's length: ~a~%" (ccl:pref pkt :midi-packet.length))))
> 
> but it does not work correctly? It seems wrong read bytes??
> 
> so I changed it...
> 
> (ccl:def-foreign-type :midi-packet
> ? (:struct :midi-packet
> ? (time-stamp :unsigned-int)
> ? (time-stamp2 :unsigned-int)
> ? (length :unsigned-short)
> ? (data (:array :unsigned-char 256))))
> 
> then..(/ (ccl::foreign-size :midi-packet) 8) is 268, and callback is work
> correctly!
> 
> 
> I was search google, so look
> it?http://clozure.com/pipermail/openmcl-devel/2002-September/004407.html
> This article is related to my problem?? or just bug??
> 
> sorry. my ugly English??but I wish you understand me?:-<
> 
> 
> 
> 
> 
> 
> 
> 
>



More information about the Openmcl-devel mailing list