[cffi-devel] Passing address of pointer

Frank Goenninger frgo at me.com
Fri Aug 26 09:08:12 UTC 2011


Hello Luís,

Am 26.08.2011 um 01:20 schrieb Luís Oliveira:

> Hello Frank,
> 
> On Thu, Aug 25, 2011 at 6:19 PM, Frank Goenninger <frgo at me.com> wrote:
>> In the sample code from Apple the C function is called like this:
>> 
>> typedef struct MyPrivateData {
>>    io_object_t            notification;
>>    IOUSBDeviceInterface   **deviceInterface;
>>    CFStringRef            deviceName;
>>    UInt32                 locationID;
>> } MyPrivateData;
> 
> Why is deviceInterface a pointer to a pointer?

Because Apple designed the interface to calls like GetLocationID in IOKit like this:
        
kr = (*privateDataRef->deviceInterface)->GetLocationID(privateDataRef->deviceInterface, &locationID);

Here I need to pass the address of a pointer to IOUSBDeviceInterface struct ...

This:

IOReturn (*GetLocationID)(void *self, UInt32 *locationID);

is part of

typedef struct IOUSBDeviceStruct {
...
} IOUSBDeviceInterface;

So, it's actually the address of a pointer to a IOUSBDeviceInterface struct.

I have defined this as:

(defcstruct IOUSBDeviceStruct
    
  (_reserved      :pointer)
  (QueryInterface :pointer)
  (AddRef         :pointer)
  (Release        :pointer)
  
  ;; Function Pointers
  
  (CreateDeviceAsyncEventSource     :pointer)
  (GetDeviceAsyncEventSource        :pointer)
  
  (CreateDeviceAsyncPort            :pointer)
  (GetDeviceAsyncPort               :pointer)
  
  (USBDeviceOpen                    :pointer)
  (USBDeviceClose                   :pointer)
  
  (GetDeviceClass                   :pointer)
  (GetDeviceSubClass                :pointer)
  (GetDeviceProtocol                :pointer)
  (GetDeviceVendor                  :pointer)
  (GetDeviceProduct                 :pointer)
  (GetDeviceReleaseNumber           :pointer)
  (GetDeviceAddress                 :pointer)
  (GetDeviceBusPowerAvailable       :pointer)
  (GetDeviceSpeed                   :pointer)
  (GetNumberOfConfigurations        :pointer)
  (GetLocationID                    :pointer)
  (GetConfigurationDescriptionPtr   :pointer)
  (GetConfiguration                 :pointer)
  (SetConfiguration                 :pointer)
  (GetBusFrameNumber                :pointer)
  (ResetDevice                      :pointer)
  (DeviceRequest                    :pointer)
  (DeviceRequestAsync               :pointer)
  (CreateInterfaceIterator          :pointer))

(defctype IOUSBDeviceInterface IOUSBDeviceStruct)

So, IOKit lib allocates the memory for this struct and the pointer passed in via the pointer address is then pointing to this memory block. Or that's at least how I understood it ...

Thanks!

  Frank





More information about the cffi-devel mailing list