Click here to Skip to main content
15,920,383 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I'm understanding the va_start, but I can't understand this code:

typedef int s32;
typedef s32 acpi_native_int;
#define  _AUPBND                (sizeof (acpi_native_int) - 1)
#define  _ADNBND                (sizeof (acpi_native_int) - 1)

// Variable argument list macro definitions
#define _bnd(X, bnd)            (((sizeof (X)) + (bnd)) & (~(bnd)))
#define va_arg(ap, T)           (*(T *)(((ap) += (_bnd (T, _AUPBND))) - (_bnd (T,_ADNBND))))
#define va_end(ap)              (void) 0
#define va_start(ap, A)         (void) ((ap) = (((char *) &(A)) + (_bnd (A,_AUPBND))))


Please tell me the feature of the _bnd,
thank you
Posted
Updated 27-Dec-10 20:10pm
v3
Comments
Dalek Dave 28-Dec-10 2:10am    
Edited for Grammar and Readability.

I think the feature of _bnd(X, bnd) is set length of X(buffer or you could it array) to multiple of int

take a exmple
1
struct 
{
char c1 ;
char c2 ;
char c3 ;
} A ; //length of A is 3 Bytes = 24 bit

(_bnd (A,_AUPBND)

2
typedef int s32; //assume int is 2 Bytes
typedef s32 acpi_native_int;
 
//value of _AUPBND should be 1. it is "0000 0001" in memory 
#define  _AUPBND   (sizeof (acpi_native_int) - 1)

3 (_bnd (A,_AUPBND) should be (_bnd (A,1))
#define _bnd(X, bnd) (((sizeof (X)) + (bnd)) &(~(bnd)))
//_bnd (A,1)
//sizeof(A) = 3  "0000 0011" in memory
//3 + 1 = 4      "0000 0100" in memory
//(~(1)) =       "1111 1110" in memory
 
//final step
//4 & (~(1)) = 4 "0000 0100" in memory
//and you will found the lowest-1bits always has been clean to zero.
//because this will insure result of _bnd is multiple of int

//first time A is 3 Bytes , now _bnd return 4 (Bytes) is multiple of int

I hope I said right
 
Share this answer
 
v4
Comments
Richard MacCutchan 28-Dec-10 7:11am    
Yes, you said it right; good answer.
tanakahitori 28-Dec-10 10:57am    
sorry. I made a mistake that wrote sizeof() return "Bits"
lxlenovostar 28-Dec-10 22:21pm    
thank you very much
As far as I can see this is a macro to determine the size of the parameters in the arg array in order to calculate the offset from va_start to the next argument. Remember that some arguments may be pointers (32 or 64 bits) while some may be values (8, 16, 32, 64 ...).
 
Share this answer
 
Comments
lxlenovostar 28-Dec-10 22:22pm    
thank you very much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900