home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / include / varargs.h < prev    next >
C/C++ Source or Header  |  1998-06-16  |  5KB  |  164 lines

  1. /***
  2. *varargs.h - XENIX style macros for variable argument functions
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       This file defines XENIX style macros for accessing arguments of a
  8. *       function which takes a variable number of arguments.
  9. *       [System V]
  10. *
  11. *       [Public]
  12. *
  13. ****/
  14.  
  15. #if     _MSC_VER > 1000
  16. #pragma once
  17. #endif
  18.  
  19. #ifndef _INC_VARARGS
  20. #define _INC_VARARGS
  21.  
  22. #if     !defined(_WIN32) && !defined(_MAC)
  23. #error ERROR: Only Mac or Win32 targets supported!
  24. #endif
  25.  
  26.  
  27. #ifdef  _MSC_VER
  28. /*
  29.  * Currently, all MS C compilers for Win32 platforms default to 8 byte
  30.  * alignment.
  31.  */
  32. #pragma pack(push,8)
  33. #endif  /* _MSC_VER */
  34.  
  35. #ifdef  __cplusplus
  36. extern "C" {
  37. #endif
  38.  
  39. #if     __STDC__
  40. #error varargs.h incompatible with ANSI (use stdarg.h)
  41. #endif
  42.  
  43.  
  44. #ifndef _VA_LIST_DEFINED
  45.  
  46. #ifdef  _M_ALPHA
  47. typedef struct {
  48.     char *a0;           /* pointer to first homed integer argument */
  49.     int offset;         /* byte offset of next parameter */
  50. } va_list;
  51. #else
  52. typedef char *va_list;
  53. #endif
  54.  
  55. #define _VA_LIST_DEFINED
  56. #endif
  57.  
  58.  
  59. #if     defined(_M_IX86)
  60.  
  61. /*
  62.  * define a macro to compute the size of a type, variable or expression,
  63.  * rounded up to the nearest multiple of sizeof(int). This number is its
  64.  * size as function argument (Intel architecture). Note that the macro
  65.  * depends on sizeof(int) being a power of 2!
  66.  */
  67. #define _INTSIZEOF(n)    ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
  68.  
  69. #define va_dcl va_list va_alist;
  70. #define va_start(ap) ap = (va_list)&va_alist
  71. #define va_arg(ap,t)    ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
  72. #define va_end(ap) ap = (va_list)0
  73.  
  74.  
  75. #elif   defined(_M_MRX000)      /* _MIPS_ */
  76.  
  77.  
  78. #define va_dcl int va_alist;
  79. #define va_start(list) list = (char *) &va_alist
  80. #define va_end(list)
  81. #define va_arg(list, mode) ((mode *)(list =\
  82.  (char *) ((((int)list + (__builtin_alignof(mode)<=4?3:7)) &\
  83.  (__builtin_alignof(mode)<=4?-4:-8))+sizeof(mode))))[-1]
  84. /*  +++++++++++++++++++++++++++++++++++++++++++
  85.     Because of parameter passing conventions in C:
  86.     use mode=int for char, and short types
  87.     use mode=double for float types
  88.     use a pointer for array types
  89.     +++++++++++++++++++++++++++++++++++++++++++ */
  90.  
  91.  
  92. #elif   defined(_M_ALPHA)
  93.  
  94. /*
  95.  * The Alpha compiler supports two builtin functions that are used to
  96.  * implement stdarg/varargs.  The __builtin_va_start function is used
  97.  * by va_start to initialize the data structure that locates the next
  98.  * argument.  The __builtin_isfloat function is used by va_arg to pick
  99.  * which part of the home area a given register argument is stored in.
  100.  * The home area is where up to six integer and/or six floating point
  101.  * register arguments are stored down (so they can also be referenced
  102.  * by a pointer like any arguments passed on the stack).
  103.  */
  104. extern void * __builtin_va_start(va_list, ...);
  105.  
  106. #define va_dcl long va_alist;
  107. #define va_start(list) __builtin_va_start(list, va_alist, 0)
  108. #define va_end(list)
  109. #define va_arg(list, mode) \
  110.     ( *(        ((list).offset += ((int)sizeof(mode) + 7) & -8) , \
  111.         (mode *)((list).a0 + (list).offset - \
  112.                     ((__builtin_isfloat(mode) && (list).offset <= (6 * 8)) ? \
  113.                         (6 * 8) + 8 : ((int)sizeof(mode) + 7) & -8) \
  114.                 ) \
  115.        ) \
  116.     )
  117.  
  118.  
  119. #elif   defined(_M_PPC)
  120.  
  121. /*
  122.  * define a macro to compute the size of a type, variable or expression,
  123.  * rounded up to the nearest multiple of sizeof(int). This number is its
  124.  * size as function argument (PPC architecture). Note that the macro
  125.  * depends on sizeof(int) being a power of 2!
  126.  */
  127. /* this is for LITTLE-ENDIAN PowerPC */
  128.  
  129. /* bytes that a type occupies in the argument list */
  130. #define _INTSIZEOF(n)   ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
  131. /* return 'ap' adjusted for type 't' in arglist */
  132. #define _ALIGNIT(ap,t) \
  133.         ((((int)(ap))+(sizeof(t)<8?3:7)) & (sizeof(t)<8?~3:~7))
  134.  
  135. #define va_dcl va_list va_alist;
  136. #define va_start(ap) ap = (va_list)&va_alist
  137. #define va_arg(ap,t)    ( *(t *)((ap = (char *) (_ALIGNIT(ap, t) + _INTSIZEOF(t))) - _INTSIZEOF(t)) )
  138. #define va_end(ap) ap = (va_list)0
  139.  
  140. #else
  141.  
  142. /* A guess at the proper definitions for other platforms */
  143.  
  144. #define _INTSIZEOF(n)    ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
  145.  
  146. #define va_dcl va_list va_alist;
  147. #define va_start(ap) ap = (va_list)&va_alist
  148. #define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
  149. #define va_end(ap) ap = (va_list)0
  150.  
  151.  
  152. #endif
  153.  
  154.  
  155. #ifdef  __cplusplus
  156. }
  157. #endif
  158.  
  159. #ifdef  _MSC_VER
  160. #pragma pack(pop)
  161. #endif  /* _MSC_VER */
  162.  
  163. #endif  /* _INC_VARARGS */
  164.