From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,8b691ded59393771 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-08-23 01:30:03 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!kibo.news.demon.net!demon!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!fr.clara.net!heighliner.fr.clara.net!oleane.net!oleane!nnrp.oleane.net!not-for-mail From: Thierry Lelegard Newsgroups: comp.lang.ada Subject: Re: Wrapping / Interfacing to C macros Date: Fri, 23 Aug 2002 10:26:57 +0200 Organization: CANAL+ TECHNOLOGIES Message-ID: <3D65F1D1.7945ADA@canal-plus.fr> References: NNTP-Posting-Host: host227.canal-plus.fr Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: s1.read.news.oleane.net 1030091220 23061 194.2.208.227 (23 Aug 2002 08:27:00 GMT) X-Complaints-To: abuse@oleane.net NNTP-Posting-Date: Fri, 23 Aug 2002 08:27:00 +0000 (UTC) X-Mailer: Mozilla 4.78 [fr]C-CCK-MCD C+ (WinNT; U) X-Accept-Language: fr,en,zh-CN,zh-TW Xref: archiver1.google.com comp.lang.ada:28334 Date: 2002-08-23T10:26:57+02:00 List-Id: > I'm hoping someone has seen a dialect or pattern for interfacing to C > macros. I need to use the ioctl() mechanisms of VxWorks to control > some low level features of a couple of serial cards. I've never seen > any Ada applications wrapping up linguistic attributes such as the > following from Tornado/target/h/sys/ioctl.h : > > #define _IO(x,y) (IOC_VOID|((x)<<8)|y) > #define _IOR(x,y,t) (IOC_OUT|((sizeof(t)&IOCPARM_MASK)<<16)|((x)<<8)|y) > #define _IOW(x,y,t) (IOC_IN|((sizeof(t)&IOCPARM_MASK)<<16)|((x)<<8)|y) > /* this should be _IORW, but stdio got there first */ > #define _IOWR(x,y,t) (IOC_INOUT|((sizeof(t)&IOCPARM_MASK)<<16)|((x)<<8)|y) > > #define SIOCSHIWAT _IOW('s', 0, int) /* set high watermark */ > #define SIOCGHIWAT _IOR('s', 1, int) /* get high watermark */ > #define SIOCSLOWAT _IOW('s', 2, int) /* set low watermark */ > #define SIOCGLOWAT _IOR('s', 3, int) /* get low watermark */ > #define SIOCATMARK _IOR('s', 7, int) /* at oob mark? */ > > I have one alternative in mind - create a C function to provide all of > these macros through a parameterized selection. > > Any other ideas? Well, there is a tradeoff between theoretical-intellectual exercise and practical-maintainabiliy-portability issue. In the first case, you can mimic the macro definitions using plain Ada features. However, the API specification is C-oriented and only defines the name of the C macro, not the way it is implemented (using nested macros like your examples, plain values, etc). We have noticed that different flavors of UNIX implement ioctl codes, for instance, in many different ways. So, your plain Ada code which mimics the C macros will not be portable (even between different versions of the same OS). You will have the same problem with structures (like the stat/fstat structure). The UNIX API (POSIX, XPG, etc) defines the name of the structure, the names of _some_ fields in the structure and that's all. On different UNIXen, the number of fields is different, contains different fields in addition to the "standard" ones. Even the order and exact type (int, unsigned, short, etc) of the standard fields are different. So, to interface the C UNIX API in a portable, maintainable manner, we use C jacket routines for each system routine which uses non basic types as parameters or with parameter values which depend on C macros (like the ioctl function code). Thus, we have: - Portable C jacket routines (they respect the standard C API for system calls). - Portable interface between the C jacket routines and Ada (provided that we only use types which have a direct mapping in Interfaces.C in the interface of the jacket routine) - Portable Ada code. As a rule of thumb: - If the information in C UNIX API standard (or man page) is sufficient to write an Ada interface using types from Interfaces.C, we write a direct Ada interface. - If we need to look at the content of some .h file to write an Ada interface, then we don't and we write a C jacket routine. -Thierry Lelegard