]> pd.if.org Git - pdclib/blob - includes/stdarg.h
Re-import from Subversion.
[pdclib] / includes / stdarg.h
1 /* ----------------------------------------------------------------------------
2  * $Id$
3  * ----------------------------------------------------------------------------
4  * Public Domain C Library - http://pdclib.sourceforge.net
5  * This code is Public Domain. Use, modify, and redistribute at will.
6  * ----------------------------------------------------------------------------
7  * Variable arguments
8  * ----------------------------------------------------------------------------
9  * This header is part of a freestanding implementation
10  * --------------------------------------------------------------------------*/
11
12 #ifndef _STDARG_H
13 #define _STDARG_H _STDARG_H
14
15 /* TODO: This code was contributed by Michael Moody, who added:
16  * "As always with my code it's not guaranteed bug-free (In fact I know for a
17  * fact it won't work properly on certain archs)."
18  * Code assumes that:
19  * - arguments are passed on the stack;
20  * - arguments are alligned to pointer size.
21  */
22
23 /* ----------------------------------------------------------------------------
24  * TYPEDEFS
25  * --------------------------------------------------------------------------*/
26
27 typedef char * va_list;
28
29 /* ----------------------------------------------------------------------------
30  * MACROS
31  * --------------------------------------------------------------------------*/
32
33 /** Returns the size of 'type' rounded up to the nearest multiple of the size
34  * of a pointer. This macro is just here for clarity.
35  */
36 #define __va_round(type)                                                      \
37    (                                                                          \
38       (sizeof(type) + sizeof(void *) - 1) & ~(sizeof(void *) - 1)             \
39    )
40
41 /** Initialises ap for use by va_arg by setting ap to point at the first
42  * argument in the ellipsis. parmN is the last known parameter in the function
43  * definition (ie the one before the ellipsis).
44  */
45 #define va_start(ap, parmN)                                                   \
46    (                                                                          \
47       (ap) = (char *)&parmN + (__va_round(parmN))                               \
48       ,                                                                       \
49       (void)0                                                                 \
50    )
51
52 /** Returns the next argument, assumed variable type is 'type', in the
53  * ellipsis.
54  */
55 #define va_arg(ap, type)                                                      \
56    (                                                                          \
57       (ap) += (__va_round(type))                                              \
58       ,                                                                       \
59       (*(type*)((ap) - (__va_round(type))))                                   \
60    )
61
62 /** Cleans up ap.
63  */
64 #define va_end(ap)                                                            \
65    (                                                                          \
66       (ap) = (void *)0                                                        \
67       ,                                                                       \
68       (void)0                                                                 \
69    )
70
71 /** Makes the va_list dest be a copy of the va_list src.
72  */
73 #define va_copy(dest, src)                                                    \
74    (                                                                          \
75       (dest) = (src)                                                          \
76       ,                                                                       \
77       (void)0                                                                 \
78    )
79
80 #endif /* _STDARG_H */