[MacRuby-devel] MacRuby equivalent to C sizeof(struct Foo)

kyossi kyossi212 at gmail.com
Fri Dec 24 12:19:17 PST 2010


Hi,

I'm trying to write some app in MacRuby with CoreAudio framework.

I've notices some framework's C-function require pointer manipulation,
and for some cases, I need calculate size of C-struct, but don't know
how to get sizeof(struct Foo).

Is there any way to calculate size of C-struct in MacRuby?

For example,  to obtain available formats of audio stream,I have to
call AudioStreamGetPropertyInfo() at first.

extern OSStatus AudioStreamGetPropertyInfo(
    AudioStreamID inStream,
    UInt32 inChannel,
    AudioDevicePropertyID inPropertyID,
    UInt32*outSize,
    Boolean*outWritable);

if I specify kAudioStreamPropertyAvailableVirtualFormats as 3rd
argument(inPropertyID),
outSize gives me total required byte size for [an array of
AudioStreamRangedDescription struct] which describe available formats.

Then, AudioStreamGetProperty() will fill [an array of
AudioStreamRangedDescription struct] with available formats

extern OSStatus AudioStreamGetProperty(
    AudioStreamID inStream,
    UInt32 inChannel,
    AudioDevicePropertyID inPropertyID, //again use
kAudioStreamPropertyAvailableVirtualFormats
    UInt32*ioPropertyDataSize,
    void*outPropertyData);                      //filled by API

http://developer.apple.com/library/mac/#documentation/MusicAudio/Reference/CACoreAudioReference/AudioHardware/CompositePage.html

So, I need to write some code like

framework "CoreAudio"
def getAvailableFormats(stream, channel)

  pSize = Pointer.new("I")
  ret = AudioStreamGetPropertyInfo( stream , channel,
KAudioStreamPropertyAvailableVirtualFormats, pRequiredSize, nil)
  requiredSize = pSize[0]

  howmany = ??? # how to get reqired length of array?  in C:  howmany
= requiredSize / sizeof(  AudioStreamRangedDescription )
  pAudioStreamRangedDescription =
Pointer.new("{AudioStreamRangedDescription={AudioStreamBasicDescription=dIIIIIIII}{AudioValueRange=dd}}",
howmany)


  ret = AudioStreamGetProperty(stream, channel,
KAudioStreamPropertyAvailableVirtualFormats, pRequiredSize,
pAudioStreamRangedDescription)
  ...
end


 Of course I could calculate sizeof( AudioStreamRangedDescription)
manually but it's annoying.
Is there any convenient way?


More information about the MacRuby-devel mailing list