Page 1 of 1

Using va_list's

Posted: Sat Nov 23, 2002 1:34 am
by PlayOS
Hi,

I am ready to create a proper printing function that will accept variable amounts of parameters, however I need to learn how to create, manage and use a va_list.

I would be grateful for code examples from someones OS, but what I would really like is some kind of tutorial, that is if anyone knows where one is, I need to understand the va_list rather than just implement it from someone elses code.

I have searched for info, but I can only find stuff about how to use one in an existing system, obviously I need to create it from scratch.

thanks.

Re:Using va_list's

Posted: Sat Nov 23, 2002 4:43 am
by grey wolf
the only thing i can suggest is looking at the GNU LibC source. i don't know of anything else that would help you understand it.

all i have is a C++ programmer's reference, and it only lightly touches on the subject.

Re:Using va_list's

Posted: Sat Nov 23, 2002 3:46 pm
by Avery
Check out the oskit. They have a basic implementation. Here's my slight modification:

#ifndef stdarg_hxx
#define stdarg_hxx

typedef void * va_list;

#define __va_size( type ) \
( ( sizeof( type ) + 3 ) & ~0x3 )

#define va_start( va_l, last ) \
( ( va_l ) = ( void * )&( last ) + __va_size( last ) )

#define va_end( va_l )

#define va_arg( va_l, type ) \
( ( va_l ) += __va_size( type ), \
*( ( type * )( ( va_l ) - __va_size( type ) ) ) )

#endif /* stdarg_hxx */

Re:Using va_list's

Posted: Sat Nov 23, 2002 4:00 pm
by PlayOS
OK Thanks,

Just a couple of questions:

1) How does this va_start() create a pointer to the stack?
2) What does va_end() do?

Thanks for your help.

Re:Using va_list's

Posted: Sat Nov 23, 2002 5:19 pm
by Avery
PlayOS wrote: OK Thanks,

Just a couple of questions:

1) How does this va_start() create a pointer to the stack?
2) What does va_end() do?

Thanks for your help.
You pass it the last known parameter on the stack. So, if printf is

int printf(const char *format, ...);

then you would have

int printf(const char *format, ...)
{
va_list args;
va_start(args, format);
/* etc */
}

This technique of taking the last parameter and looking beyond it works on x86, but I'm not sure about other platforms. Also, va_end() I believe isn't needed on x86 but is needed on other platforms so it's there to maintain api compatibility.

Re:Using va_list's

Posted: Sat Nov 23, 2002 5:52 pm
by PlayOS
Thankyou, I thought that va_end() wouldn't be needed because it does nothing. :)

Thanks again.

Re:Using va_list's

Posted: Sat Nov 23, 2002 7:49 pm
by Tom
Hello....

you can use my printf sources for examples...commented good....(except for the va_..stuff )

I tested and....at least my code won't work without the va_end...