473,385 Members | 1,641 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,385 software developers and data experts.

dynamic construction of va_list

I would like to support stdio functions for an extremely primitive
type system, as shown in the attached sample program, using dynamic
creation of a va_list.

I've tested it on successfully a couple platforms, but what I don't
know is whether it's actually a legal program.

The function of interest is usnprintf, "union sprintf". (psnprintf is
a dummy wrapper for vsnprintf, useful for debugging.)
The signature is

int usnprintf (char *buffer, size_t size, const char *fmt, const
type_union *arg);

where the last argument is a flag-terminated array of type_union, as
defined below.

================Code begins =========================
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>

typedef struct type_union type_union;

typedef enum type_val {
T_END = 0, /* terminator flag for variable-length array */
T_INT,
T_FLOAT,
T_CHAR,
T_STRING,
} type_val;

struct type_union
{
type_val t;
union {
float f;
int i;
char c;
const char *s;
};
};

/* peek into va_list offsets */
int psnprintf(char *buf, size_t s, const char *fmt, va_list va)
{
int i;
int r;
va_list vb = va;
for(i = 0; i < 2; i++) {
double t = va_arg(va, double);
}
r = vsnprintf(buf, s, fmt, vb);
return r;
}
/*
* Treat ac_list, ac_arg as lvalues.
*/

#define va_start_assign(list, var) ((list) = ((char*)&var))

#define va_assign(lvalp, type, rval) \
*((type*)lvalp) = rval; \
va_arg(lvalp, type);

#define VA_FLOAT double
#define VA_CHAR int

/*
* snprintf for typed unions. (Does not validate the parse.)
* Requires flag-terminated array of values.
* TODO: validate with parse_printf_format and eliminate terminator.
*/
int usnprintf (char *buffer, size_t size, const char *fmt, const
type_union *arg)
{
int i;
int ret;
va_list out;
double aligned[100]; /* aligned buffer of ridiculously large size */
va_start_assign(out, aligned[0]);
for(i = 0; arg[i].t != T_END; i++) {
switch(arg[i].t) {
case T_INT:
va_assign(out, int, arg[i].i);
break;
case T_FLOAT:
va_assign(out, VA_FLOAT, arg[i].f);
break;
case T_CHAR:
va_assign(out, VA_CHAR, arg[i].c);
break;
case T_STRING:
va_assign(out, const char *, arg[i].s);
break;
default:
assert(0);
va_end(out);
return -1;
}
}

va_end(out);
va_start_assign(out, aligned[0]);
ret = psnprintf(buffer, size, fmt, out);
va_end(out);
return ret;
}

#define TYPE_FUN(v2u, tv, T, v) \
type_union v2u(T v) { \
type_union r; \
r.v = v; \
r.t = tv; \
return r; \
}

TYPE_FUN(i2u, T_INT, int, i)
TYPE_FUN(c2u, T_CHAR, char, c)
TYPE_FUN(f2u, T_FLOAT, float, f)
TYPE_FUN(s2u, T_STRING, const char *, s)

static const type_union END = { T_END };
int main() {
char buffer[30];
type_union x[] = { f2u(2.5), f2u(3.5), i2u(5), c2u('a'), s2u("xyz
\n"), END };
usnprintf(buffer, 30, "%.1f; %.1f; %d; %c; %s", x);
printf("%s", buffer);
}

Apr 28 '07 #1
1 9040
(I am not sure why no one ever replied to this earlier, but I have
been saving it and have finally gotten around to replying myself.)

In article <11**********************@o5g2000hsb.googlegroups. com>
pete m <pm*****@yahoo.comwrote:
>I would like to support stdio functions for an extremely primitive
type system, as shown in the attached sample program, using dynamic
creation of a va_list.
This cannot be done portably.

(It *can* be done *non*-portably, once for each target machine.
In some cases the technique may require compiler support and/or
sneaky assembly code. To get the "right" sizes, rather than using
a large array and simply hoping it is big enough -- as you did in
some code I snipped below -- may require a two-pass approach.)

[much snippage -- retaining only the va_assign related code]
>#define va_start_assign(list, var) ((list) = ((char*)&var))

#define va_assign(lvalp, type, rval) \
*((type*)lvalp) = rval; \
va_arg(lvalp, type);

#define VA_FLOAT double
#define VA_CHAR int
...
va_list out;
double aligned[100]; /* aligned buffer of ridiculously large size */
va_start_assign(out, aligned[0]);
for(i = 0; arg[i].t != T_END; i++) {
switch(arg[i].t) {
case T_INT:
va_assign(out, int, arg[i].i);
break;
case T_FLOAT:
va_assign(out, VA_FLOAT, arg[i].f);
break;
case T_CHAR:
va_assign(out, VA_CHAR, arg[i].c);
break;
case T_STRING:
va_assign(out, const char *, arg[i].s);
break;
default:
assert(0);
va_end(out);
return -1;
}
}

va_end(out);
va_start_assign(out, aligned[0]);
("out" is then used as if it were an ordinary "va_list" parameter
created by va_start().)

This is likely to work on a typical stack-based system, such as
normally used on x86 implementations. It may even work on some
systems that pass arguments in registers. It will, however, fail
on some other systems that pass arguments in registers. For
instance, on V9 SPARC, "out" needs to point to a structure
object, where the structure contains two counters and three
pointers. The two counters indicate how many integral and
floating-point parameters remain unprocessed in the "save areas"
to which the first two pointers point. The final pointer points
to the "overflow area" for any additional parameters.

Hence, to "va_assign" (as it were) a VA_FLOAT, you would (at least
nominally) want to copy the "double" to the floating-point save
area and increment the floating-point count. In practice, you
could "cheat" and simply set the two counters to zero, then dump
all the remaining arguments into the "pseudo-stack" as you do above:

#define va_start_assign(ptr, save_area) \
(((struct __va_args *)&(save_area))->__va_ni = 0, \
((struct __va_args *)&(save_area))->__va_nf = 0, \
((struct __va_args *)&(save_area))->__va_rest = \
(void *)((struct va_args *)&(save_area) + 1), \
(ptr) = (struct __va_args *)&(save_area))

This method is of course quite specific to the SPARC-V9 ABI (and
makes some assumptions about the compiler's internal names for
various pieces involved in <stdarg.h>; and I may not recall them
quite correctly, and/or they may depend on specific compiler
revisions).
--
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.
Jun 4 '07 #2

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

Similar topics

6
by: Peter | last post by:
-------------------------------------------------------------------------------- Hi, I was wondering if we can create a va_list by adding objects in the va_list instead of passing in the...
3
by: Douwe | last post by:
I try to build my own version of printf which just passes all arguments to the original printf. As long as I keep it with the single argument version everything is fine. But their is also a version...
2
by: Joerg Schoen | last post by:
Hi folks! I have a function that gets a 'va_list'. I am passing the 'va_list' two times to a function like 'vprintf' to print it out. I thought that this was portable until I came across a...
9
by: Gibby Koldenhof | last post by:
Hiya, Terrible subject but I haven't got a better term at the moment. I've been building up my own library of functionality (all nice conforming ISO C) for over 6 years and decided to adopt a...
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
1
by: skillzero | last post by:
Is there a portable way to pass a va_list as a parameter to another function taking a variable argument list? I have a function that takes a printf-like format string and I'd like to use...
5
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
On Jun 3, 3:23 am, Jesse Ziser <d...@spam.diewrote: The relevant paragraph from the Standard is: ---- Begin Quote ---- The type declared is va_list which is an object type suitable for...
18
by: squaretriangle | last post by:
Is it possible through some trickery, perhaps using the methods of stdarg, to pass a dynamic (unknown at compile time) number of arguments to a function? For example, I want to enumerate an array...
1
by: Chuck Chopp | last post by:
I have some code that is being built on the following: Windows Server 2003, both 32-bit & 64-bit editions Windows Vista, both 32-bit & 64-bit editions Windows Server 2008, both 32-bit & 64-bit...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.