473,795 Members | 3,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stdarg definitions

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

#define __va_rounded_si ze(TYPE) \
(((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof
(int))va_rounde d_size

#define va_start(AP, LASTARG) \
(__builtin_save regs (), \
AP = ((char *) &(LASTARG) + __va_rounded_si ze (LASTARG)))

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

What are the above macros trying to accomplish?

Tilak

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

#define __va_rounded_si ze(TYPE) \
(((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof
(int))va_rounde d_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_siz e" 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_save regs (), \
AP = ((char *) &(LASTARG) + __va_rounded_si ze (LASTARG)))
Presumably __builtin_saver egs() 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_si ze (TYPE), \
*((TYPE *) (AP - __va_rounded_si ze (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************ **********@n76g 2000hsh.googleg roups.com>,
cman <ti****@gmail.c omwrote:
>Can somebody explain the following code segment, from stdargs.h (from
linux 0.01)

#define __va_rounded_si ze(TYPE) \
(((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof
(int))va_round ed_size

#define va_start(AP, LASTARG) \
(__builtin_save regs (), \
AP = ((char *) &(LASTARG) + __va_rounded_si ze (LASTARG)))

#define va_arg(AP, TYPE) \
(AP += __va_rounded_si ze (TYPE), \
*((TYPE *) (AP - __va_rounded_si ze (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************ **********@n76g 2000hsh.googleg roups.com>
>cman <ti****@gmail.c omwrote:
>>Can somebody explain the following code segment, from stdargs.h ...
[snip lots of machine-dependent stuff]

In article <et**********@r umours.uwaterlo o.ca>,
Dave Vandervies <dj******@caffe ine.csclub.uwat erloo.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.hmacr os 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.hattemp t 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.co m asked about, however, will
break, because the C compiler handles the va_start() in foo()
incorrectly: it takes "sizeof(flo at)" 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_s ize
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_li st type
(which the compiler allocates and knows about), have a __builtin_va_st art
(the compiler knows how to save and/or locate the variable arguments),
and have a __builtin_va_ar g (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_li st va_list;
#define va_start(ap, X) __builtin_va_st art(ap)
#define va_arg(ap, type) __builtin_va_ar g(ap, type)
#define va_end(ap) __builtin_va_en d(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.hsynta x (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_li st va_list; /* or typedef char *, etc */
#define va_start(ap) ((ap) = &...)
#define va_end(ap) /*nothing*/
#define va_arg(ap, type) __builtin_va_ar g(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_li st va_list; /* compiler provides size of save area */
#define va_start(ap) (__builtin_va_s tart(ap))
#define va_end(ap) /*nothing*/
#define va_arg(ap, type) __builtin_va_ar g(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
2477
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 variable for the multiple definitions. C++, however, does not permit redefinition of a variable or any other entity for a very definite reason that we will discuss later." Chapter 1. C++ Tutorial. Coronado Enterprises Charles L
8
3455
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; int ind;
5
3037
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? In other words, suppose that I have a generic pointer : void * f_ptr; I know that this pointer points to a fuction. I also know the function
6
3850
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 fact that va_arg requires a type to decide how far to advance to subsequent arguments. So, essentially what I want in the variable argument function is something that just assigns to a character type, and then copy a predefined number of bytes to...
9
1829
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 values. Below is the function: #include <stdarg.h> .. ..
36
3859
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 program will result in a linker error complaining about multiple definitions. So this kind of definition should be avoided as much as possible. But as we know, the definition of a class is always in a header file. And we can use "#ifndef" to eliminate...
13
2144
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." ); return 0; }
9
5467
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; const char *next;
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10435
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6779
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.