473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

stdarg definitions

Can somebody explain the following code segment, from stdargs.h (from
linux 0.01)

#define __va_rounded_size(TYPE) \
(((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof
(int))va_rounded_size

#define va_start(AP, LASTARG) \
(__builtin_saveregs (), \
AP = ((char *) &(LASTARG) + __va_rounded_size (LASTARG)))

#define va_arg(AP, TYPE) \
(AP += __va_rounded_size (TYPE), \
*((TYPE *) (AP - __va_rounded_size (TYPE))))

What are the above macros trying to accomplish?

Tilak

Mar 20 '07 #1
3 5093
"cman" <ti****@gmail.comwrites:
Can somebody explain the following code segment, from stdargs.h (from
linux 0.01)

#define __va_rounded_size(TYPE) \
(((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof
(int))va_rounded_size
(X + K-1)/K*K rounds X up to a whole multiple of K - so if
sizeof(TYPE) == 6 and sizeof(int) == 4, this becomes 8.
The final "va_rounded_size" looks like a typo.

The following code appears to assume that the arguments are put after
each other in a contiguous memory area, at addresses (address of first
argument + some multiple of sizeof(int)). So...
#define va_start(AP, LASTARG) \
(__builtin_saveregs (), \
AP = ((char *) &(LASTARG) + __va_rounded_size (LASTARG)))
Presumably __builtin_saveregs() does some magic to prepare for use of
stdarg. The AP is set to point to the argument following LASTARG.
#define va_arg(AP, TYPE) \
(AP += __va_rounded_size (TYPE), \
*((TYPE *) (AP - __va_rounded_size (TYPE))))
AP points at the next argument to be fetched. That argument is returned
after stepping AP up to to point to the next argument.

--
Regards,
Hallvard
Mar 20 '07 #2
In article <11**********************@n76g2000hsh.googlegroups .com>,
cman <ti****@gmail.comwrote:
>Can somebody explain the following code segment, from stdargs.h (from
linux 0.01)

#define __va_rounded_size(TYPE) \
(((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof
(int))va_rounded_size

#define va_start(AP, LASTARG) \
(__builtin_saveregs (), \
AP = ((char *) &(LASTARG) + __va_rounded_size (LASTARG)))

#define va_arg(AP, TYPE) \
(AP += __va_rounded_size (TYPE), \
*((TYPE *) (AP - __va_rounded_size (TYPE))))

What are the above macros trying to accomplish?
They're implementing the compiler magic that va_start and va_arg need
to know where to find the arguments you ask them for.
(In general, you shouldn't care how they accomplish this; to write code
using them, you only need to know what they do, not how they do it.
Using knowledge of how they work underneath is more likely to get you
into trouble than to help you.)

In this case, it looks like arguments (at least the last non-variable
argument and the variable arguments to a variadic function) are
stored as if they were memcpy'd into an array of bytes with alignment
of sizeof(int). (This alignment makes sense, since every type smaller
than int (unless pointers are smaller than int, which I don't believe
they are on most implementations) is promoted to int when it gets passed
as a variable argument.)
So va_start takes the location of the last non-variable argument and
uses it to calculate the location of the first variable argument, and
va_arg takes the location of the next variable argument (stored in the
va_list), updates the va_list to reflect the fact that an argument has
been consumed, and returns the next variable argument (by treating the
collection of bytes in the right place as an object of the reqested type).
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
If parachutes were maintained to the same standards as the typical car
you'd have parachutists dropping out of the skies like flies.
--Bram in the scary devil monastery
Mar 20 '07 #3
>In article <11**********************@n76g2000hsh.googlegroups .com>
>cman <ti****@gmail.comwrote:
>>Can somebody explain the following code segment, from stdargs.h ...
[snip lots of machine-dependent stuff]

In article <et**********@rumours.uwaterloo.ca>,
Dave Vandervies <dj******@caffeine.csclub.uwaterloo.cawrote:
>They're implementing the compiler magic that va_start and va_arg need
to know where to find the arguments you ask them for.
(In general, you shouldn't care how they accomplish this; to write code
using them, you only need to know what they do, not how they do it.
Using knowledge of how they work underneath is more likely to get you
into trouble than to help you.)
Right.
>In this case, it looks like arguments (at least the last non-variable
argument and the variable arguments to a variadic function) are
stored as if they were memcpy'd into an array of bytes with alignment
of sizeof(int). (This alignment makes sense, since every type smaller
than int (unless pointers are smaller than int, which I don't believe
they are on most implementations) is promoted to int when it gets passed
as a variable argument.)
Yes, but there is a bug, on this particular implementation
(fixed in later versions of the <stdarg.hmacros and compiler).
>So va_start takes the location of the last non-variable argument and
uses it to calculate the location of the first variable argument, and
va_arg takes the location of the next variable argument (stored in the
va_list), updates the va_list to reflect the fact that an argument has
been consumed, and returns the next variable argument (by treating the
collection of bytes in the right place as an object of the reqested type).
All of this works fine for char, short, and int arguments and
for double and long double arguments. The bug occurs when the
"last fixed argument" has type "float":

#include <stdarg.h>

void foo(int nargs, float firstfloat, ...) {
va_list ap;

va_start(ap, foo);
vfoo(nargs, firstfloat, ap);
va_end(ap);
}

void vfoo(int nargs, float firstfloat, va_list ap) {
float cur;

for (cur = firstfloat;;) {
operate(cur); /* replace this with real code */

if (--nargs <= 0)
break;

/*
* Note that we cannot use va_arg(ap, float) here,
* since floats that correspond to the ", ..." part
* were promoted to "double" by the caller.
*
* Some versions of <stdarg.hattempt to catch
* the use of "float" as the "type" and automatically
* convert it to "double", but technically we, as
* C programmers, are supposed to just use "double".
*/
cur = va_arg(ap, double);
}
}

The above is (assuming I have not done something silly) good,
solid, conforming Standard C code. We might call it with, e.g.:

extern void foo(int, float, ...);

foo(3, 1.5, 15.0, 150.0);

The implementation that ti****@gmail.com asked about, however, will
break, because the C compiler handles the va_start() in foo()
incorrectly: it takes "sizeof(float)" as the number of bytes to
"skip over" in order to find the first only-as-precise-as-float-
but-promoted-to-double argument -- i.e., 15.0 -- but that particular
implementation (which I am sure about; I see it in my crystal
ball :-) ) actually *also* promotes the fixed argument ("firstfloat")
to double. The implementation-specific macros (__va_rounded_size
and so forth) fail to take this into account.

Newer versions of the compiler, with their newer <stdarg.h>, tend
to dump more of the problem on the compiler. Instead of just one
__builtin, they use a whole series.

The "correct" (or ideal, anyway) solution is of course to dump the
*entire* problem on the compiler: have a __builtin_va_list type
(which the compiler allocates and knows about), have a __builtin_va_start
(the compiler knows how to save and/or locate the variable arguments),
and have a __builtin_va_arg (the compiler knows how to take a type
name and use that to extract a variable argument). Of course, if
one does this, one arrives at:

typedef __builtin_va_list va_list;
#define va_start(ap, X) __builtin_va_start(ap)
#define va_arg(ap, type) __builtin_va_arg(ap, type)
#define va_end(ap) __builtin_va_end(ap)

Note that this sequence of four source-code lines is itself completely
machine-independent (even though each __builtin_* is completely
machine-dependent): EVERY C compiler on EVERY system could have
used EXACTLY those four lines, provided that every C compiler were
to provide those four __builtin_* names. Note further that the
second argument to __va_start is unused, and therefore need not
ever have been required. The original C89 standard should have
used the original pre-C89 <varargs.hsyntax (which omitted the
second argument to va_start()). The whole problem I describe above
(with promoted float arguments) would never have arisen.

Providing <stdarg.has a header is still a good idea. On
compilers where no "setup" or "teardown" is required to make
the arguments accessible, a C compiler could have used this:

typedef __builtin_va_list va_list; /* or typedef char *, etc */
#define va_start(ap) ((ap) = &...)
#define va_end(ap) /*nothing*/
#define va_arg(ap, type) __builtin_va_arg(ap, type)

In other words, such a C compiler (e.g., gcc 1.x for x86) could
simply have made the "..." pseudo-variable addressable. On
more complicated systems (SPARC, PowerPC) that pass arguments
in registers, va_start would still need to use some __builtin,
but could still be defined fairly simply:

typedef __builtin_va_list va_list; /* compiler provides size of save area */
#define va_start(ap) (__builtin_va_start(ap))
#define va_end(ap) /*nothing*/
#define va_arg(ap, type) __builtin_va_arg(ap, type)

But instead we have these old, muddle-headed, slightly broken
implementations that attempted to use a slightly broken idea embedded
in the Standard, instead of ignoring the broken idea to start with
and thus avoiding the problem. :-)

(For anyone who actually read this far: if you want to avoid
encountering the bug, make sure your "last fixed argument" to any
variable-argument function never has a type that undergoes promotion.
It is *supposed* to work, but at least some cases -- floats -- fail
on at least some implementations. Other implementations may even
get char and short wrong as well.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Mar 25 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Charles L | last post by:
Can someone explain to me what the following means? "C permits multiple definitions of a variable in any given namespace, provided the definitions are the same and it generates only a single...
8
by: Klaus Schneider | last post by:
Hi all! I'm having trouble with a template function with variable arguments when I parse an enum type as variable argument. Example: template <class T> bool test(int num, ...) { va_list ap;...
5
by: Francesco Bochicchio | last post by:
Hi all, anybody knows if there is a (standard, portable) way to dinamically build a list of parameters to call a C function? Something like va_start & co, but to be used on the calling side? ...
6
by: Clint Olsen | last post by:
I had this crazy idea to use stdarg macros to copy data in a generic fashion, regardless of type. However, I'm not sure it will work in part due to the ANSI C default argument promotions and the...
9
by: Mac A. Cody | last post by:
Hello, I'm encountering a problem with using stdarg that I cannot figure out. I'm trying to develop a function for a linux driver module that takes a variable-length sequence of u8-type...
36
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same...
13
by: nariknahom | last post by:
Hi, Can someone tell me what is wrong with the below program? ------------------------------------------- int main() { f1( 25 50,"testing stdarg. ", "it is working", "if it is working." );...
9
by: tonybalinski | last post by:
I'd like to be able to scan a va_list twice in a v... function, but can't see how to do it. For example char *strdupcat(const char *first, ...) { char *result, pos; va_list arg; size_t len;...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.