Using va_list's

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
PlayOS

Using va_list's

Post 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.
grey wolf

Re:Using va_list's

Post 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.
Avery

Re:Using va_list's

Post 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 */
PlayOS

Re:Using va_list's

Post 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.
Avery

Re:Using va_list's

Post 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.
PlayOS

Re:Using va_list's

Post by PlayOS »

Thankyou, I thought that va_end() wouldn't be needed because it does nothing. :)

Thanks again.
Tom

Re:Using va_list's

Post 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...
Post Reply