climber.cui@[EMAIL PROTECTED]
wrote:
> Hi All,
> I am trying to dissect some source code, but the C code there
> really confuses me. The problem I had is the declaration of the macros
> and the way they use them.
> While I would like to find out how DEF_MARCEL_POSIX is defined, I
> found the following in the source:
>
> # define DEF_ALIAS_POSIX_OF_MARCEL(rtype, name, proto, args)
>
> # define DEF_ALIAS_POSIX(rtype, name, proto, args)
>
> #define DEF_ALIAS_MARCEL(rtype, name, proto, args) \
> TBX_FUN_ALIAS(rtype, MARCEL_NAME(name), \
> LOCAL_MARCEL_NAME(name), proto, args); // why there is
> a semi colon at the end?
>
> #define DEF_MARCEL_POSIX(rtype, name, proto, args) \
> DEF_ALIAS_MARCEL(rtype, name, proto, args) \
> DEF_ALIAS_POSIX(rtype, name, proto, args) \
> DEF_ALIAS_POSIX_OF_MARCEL(rtype, name, proto, args) \
> rtype LOCAL_MARCEL_NAME(name) proto
>
>
-------------------------------------------------------------------------------------------------------------------------------------------
> And in another file from the source, several occurrences of
> DEF_MARCEL_POSIX, like this:
>
> DEF_MARCEL_POSIX(int, attr_setstacksize, (marcel_attr_t *attr, size_t
> stack), (attr, stack))
> {
> if (stack > THREAD_SLOT_SIZE) {
> errno = EINVAL;
> return 1;
> }
> attr->__stacksize = stack;
> return 0;
> }
> ...................
> ..................
> DEF_MARCEL_POSIX(int, attr_getstacksize, (__const marcel_attr_t *
> __restrict attr, size_t * __restrict stack), (attr, stack))
> {
> *stack = attr->__stacksize;
> return 0;
> }
>
-------------------------------------------------------------------------------------------------------------------------------------------
> I got completely lost here. What is DEF_MARCEL_POSIX defined to be?
> Why it is declared as functions multiple times in the other file?
I've seen similar stuff - not identical, not least because Marcel was
completely absent - which dates back to the era (early to mid 90s) when
some C compilers sup****ted prototypes and many did not. The macros I
saw in a header - declarations - looked like:
#if defined(__STDC__) || defined(__cplusplus) || defined(PCDOS) ||
defined(_WIN32) || defined(_WINDOWS)
#define XX_PROTO(type, func, args) type XX_PASCAL func args
#define XX_PTR_PROTO(type, name, args) type (XX_PASCAL *name) args
#else
#define XX_PROTO(type, func, args) type func()
#define XX_PTR_PROTO(type, name, args) type (*name)()
#endif
XX_PROTO(int XX_EX****T, _xx_ext_fseek, (xx_e_fileptr_t stream, long
offset, int origin));
There are Windows-isms in there too (XX_EX****T).
For Unix, that translates to:
int _xx_ext_fseek(xx_e_fileptr_t stream, long offset, int origin);
The corresponding code used for defining a function was:
#if defined(__STDC__) || defined(PCDOS) || defined(NT)
#define SEP ,
#define XX_FUNC(type, func, args, typargs) type func(typargs)
#define XX_FUNC_NOARG(type, func) type func(void)
#define DOTS , ...
#define CONST const
#else
#define DOTS
#define SEP ;
#define XX_FUNC(type, func, args, typargs) type func args typargs;
#define XX_FUNC_NOARG(type, func) type func()
#define CONST
#endif
An example function definition:
XX_FUNC(static void, get_dl_name, (fp, directory, fname, size),
xx_e_fileptr_t fp SEP char * directory SEP char ** fname SEP int * size)
Under Standard C, that translates to:
static void get_dl_name(xx_e_fileptr_t fp, char *directory, char
**fname, int *size);
Under K&R, that translates to:
static void get_dl_name(fp, directory, fname, size)
xx_e_fileptr_t fp;
char *directory;
char **fname;
int * size;
The stuff you show - which isn't complete since you've not shown
TBX_FUN_ALIAS and LOCAL_MARCEL_NAME - doesn't self-evidently handle the
semi-colons vs commas which the code I showed does. Nevertheless, I
think it is probably doing something somewhat similar.
Recommendation:
* Examine the output from the C preprocessor.
* Replace the macros with plain function declarations.
The rats nest of macros is not nice!
--
Jonathan Leffler #include <disclaimer.h>
Email: jleffler@[EMAIL PROTECTED]
jleffler@[EMAIL PROTECTED]
of DBD::Informix v2008.0229 -- http://dbi.perl.org/
publictimestamp.org/ptb/PTB-2806 tiger 2008-03-21 06:00:06
20EB83446392CFE04280D1D0321A5AC30224F1B018309EC1
--
comp.lang.c.moderated - moderation address: clcm@[EMAIL PROTECTED]
-- you must
have an appropriate newsgroups line in your header for your mail to be
seen,
or the newsgroup name in square brackets in the subject line. Sorry.


|