Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

pack.H

Go to the documentation of this file.
00001 #ifndef PACK_H
00002 #define PACK_H
00003 
00004 /*
00005  * These routines safely pack and unpack double, words and byte
00006  * strings. bcopy is not used for efficiency reasons, even though
00007  * code size slightly increases. Have to be careful with alignment,
00008  * that is why byte by byte copy is used. 
00009  * Assumes: 8-byte doubles
00010  *          4-byte words
00011  * Note that if across multiple architectures of different word size
00012  * then must agree on largest word size and change the macros
00013  * appropriately, else will not be able to communicate between 
00014  * architectures.
00015  */
00016 
00017 #if defined(i386) || defined(WIN32)
00018 #define UGA_LITTLE_ENDIAN
00019 #endif
00020 
00021 /* UGA packing routines for words, doubles and bytes */
00022 
00023 #ifndef UGA_LITTLE_ENDIAN
00024 #define UGA_PACK_DOUBLE(QUAN, BUF, COUNT)       \
00025         do { char *_buf_ = BUF, *_ptr_ = (char *)&(QUAN);       \
00026         *((_buf_)++) = *((_ptr_)++);            \
00027         *((_buf_)++) = *((_ptr_)++);            \
00028         *((_buf_)++) = *((_ptr_)++);            \
00029         *((_buf_)++) = *((_ptr_)++);            \
00030         *((_buf_)++) = *((_ptr_)++);            \
00031         *((_buf_)++) = *((_ptr_)++);            \
00032         *((_buf_)++) = *((_ptr_)++);            \
00033         *((_buf_)++) = *((_ptr_)++);            \
00034         (BUF) = _buf_;                 \
00035         (COUNT) += 8; } while (0)
00036 #else
00037 #define UGA_PACK_DOUBLE(QUAN, BUF, COUNT)       \
00038         do { char *_buf_ = BUF, *_ptr_ = (char *)&(QUAN)+7;     \
00039         *((_buf_)++) = *((_ptr_)--);            \
00040         *((_buf_)++) = *((_ptr_)--);            \
00041         *((_buf_)++) = *((_ptr_)--);            \
00042         *((_buf_)++) = *((_ptr_)--);            \
00043         *((_buf_)++) = *((_ptr_)--);            \
00044         *((_buf_)++) = *((_ptr_)--);            \
00045         *((_buf_)++) = *((_ptr_)--);            \
00046         *((_buf_)++) = *((_ptr_)--);            \
00047         (BUF) = _buf_;                 \
00048         (COUNT) += 8; } while (0)
00049 #endif
00050 
00051 #define UGA_PACK_NDOUBLE(QUAN, BUF, NUM, COUNT)            \
00052         do { int _i;                \
00053           for (_i=0; _i<(int)NUM; _i++)            \
00054                UGA_PACK_DOUBLE(QUAN[_i], BUF, COUNT); } while (0)
00055 
00056 #define UGA_PACK_WORD(QUAN, BUF, COUNT)            \
00057         do { u_long _long_ = htonl((u_long)QUAN);     \
00058         char *_buf_ = BUF, *_ptr_;           \
00059         _ptr_ = (char *)&_long_;          \
00060         *((_buf_)++) = *((_ptr_)++);            \
00061         *((_buf_)++) = *((_ptr_)++);            \
00062         *((_buf_)++) = *((_ptr_)++);            \
00063         *((_buf_)++) = *((_ptr_)++);            \
00064         (BUF) = _buf_;                 \
00065         (COUNT) += 4; } while (0)
00066 
00067 #define UGA_PACK_NWORD(QUAN, BUF, NUM, COUNT)         \
00068         do { int _i;                \
00069           for (_i=0; _i<(int)NUM; _i++)            \
00070                 UGA_PACK_WORD(QUAN[_i], BUF, COUNT); } while (0)
00071 
00072 #define UGA_PACK_BYTES(PTR, BUF, SIZE, COUNT)         \
00073         ((void)bcopy((char *)(PTR),(char *)(BUF),(int)(SIZE)),  \
00074          BUF += (SIZE),(COUNT) += (SIZE))
00075 
00076 /* UGA unpacking routines for words, doubles and bytes */
00077 
00078 #ifndef UGA_LITTLE_ENDIAN
00079 #define UGA_UNPACK_DOUBLE(QUAN, BUF, COUNT)        \
00080         do { char *_buf_ = BUF, *_ptr_ = (char *)&(QUAN);   \
00081         *((_ptr_)++) = *((_buf_)++);            \
00082         *((_ptr_)++) = *((_buf_)++);            \
00083         *((_ptr_)++) = *((_buf_)++);            \
00084         *((_ptr_)++) = *((_buf_)++);            \
00085         *((_ptr_)++) = *((_buf_)++);            \
00086         *((_ptr_)++) = *((_buf_)++);            \
00087         *((_ptr_)++) = *((_buf_)++);            \
00088         *((_ptr_)++) = *((_buf_)++);            \
00089         (BUF) = _buf_;                 \
00090         (COUNT) += 8; } while (0)
00091 #else
00092 #define UGA_UNPACK_DOUBLE(QUAN, BUF, COUNT)        \
00093         do { char *_buf_ = BUF, *_ptr_ = (char *)&(QUAN)+7; \
00094         *((_ptr_)--) = *((_buf_)++);            \
00095         *((_ptr_)--) = *((_buf_)++);            \
00096         *((_ptr_)--) = *((_buf_)++);            \
00097         *((_ptr_)--) = *((_buf_)++);            \
00098         *((_ptr_)--) = *((_buf_)++);            \
00099         *((_ptr_)--) = *((_buf_)++);            \
00100         *((_ptr_)--) = *((_buf_)++);            \
00101         *((_ptr_)--) = *((_buf_)++);            \
00102         (BUF) = _buf_;                 \
00103         (COUNT) += 8; } while (0)
00104 #endif
00105 
00106 #define UGA_UNPACK_NDOUBLE(QUAN, BUF, NUM, COUNT)     \
00107         do { int _i;                \
00108           for (_i=0; _i<(int)NUM; _i++)            \
00109                UGA_UNPACK_DOUBLE(QUAN[_i], BUF, COUNT); } while (0)
00110 
00111 #define UGA_UNPACK_WORD(QUAN, BUF, COUNT, TYPE)       \
00112         do { char *_buf_ = BUF, *_ptr_ = (char *)&(QUAN);   \
00113         *((_ptr_)++) = *((_buf_)++);            \
00114         *((_ptr_)++) = *((_buf_)++);            \
00115         *((_ptr_)++) = *((_buf_)++);            \
00116         *((_ptr_)++) = *((_buf_)++);            \
00117         (BUF) = _buf_;                 \
00118         (QUAN) = (TYPE)ntohl((u_long)QUAN);        \
00119         (COUNT) += 4; } while (0)
00120 
00121 #define UGA_UNPACK_NWORD(QUAN, BUF, NUM, COUNT, TYPE)    \
00122         do { int _i;                \
00123           for (_i=0; _i<(int)NUM; _i++)            \
00124                 UGA_UNPACK_WORD(QUAN, BUF, COUNT, TYPE); } while (0)
00125 
00126 #define UGA_UNPACK_BYTES(PTR, BUF, SIZE, COUNT)       \
00127         ((void)bcopy((char *)(BUF), (char *)(PTR), (int)(SIZE)),\
00128          BUF += (SIZE),(COUNT) += (SIZE))
00129 
00130 /*
00131  * Additional types that are currently based on the above macros, but
00132  * could change, depending on architecture.
00133  * long = word
00134  * int  = word
00135  * enum = word
00136  * ptr  = word
00137  */
00138 
00139 #define UGA_PACK_LONG(Q,B,C)     UGA_PACK_WORD(Q,B,C)
00140 #define UGA_PACK_NLONG(Q,B,N,C)  UGA_PACK_NWORD(Q,B,N,C)
00141 #define UGA_PACK_INTEGER(Q,B,C)  UGA_PACK_WORD(Q,B,C)
00142 #define UGA_PACK_NINTEGER(Q,B,N,C)  UGA_PACK_NWORD(Q,B,N,C)
00143 #define UGA_PACK_ENUM(Q,B,C)     UGA_PACK_WORD(Q,B,C)
00144 #define UGA_PACK_NENUM(Q,B,N,C)     UGA_PACK_NWORD(Q,B,N,C)
00145 #define UGA_PACK_POINTER(Q,B,C)     UGA_PACK_WORD(Q,B,C)
00146 #define UGA_PACK_NPOINTER(Q,B,N,C)  UGA_PACK_NWORD(Q,B,N,C)
00147 
00148 #define UGA_UNPACK_LONG(Q,B,C)      UGA_UNPACK_WORD(Q,B,C,long)
00149 #define UGA_UNPACK_NLONG(Q,B,N,C)   UGA_UNPACK_NWORD(Q,B,N,C,long)
00150 #define UGA_UNPACK_INTEGER(Q,B,C)   UGA_UNPACK_WORD(Q,B,C,int)
00151 #define UGA_UNPACK_NINTEGER(Q,B,N,C)   UGA_UNPACK_NWORD(Q,B,N,C,int)
00152 #define UGA_UNPACK_ENUM(Q,B,C,T) UGA_UNPACK_WORD(Q,B,C,T)
00153 #define UGA_UNPACK_NENUM(Q,B,N,C,T) UGA_UNPACK_NWORD(Q,B,N,C,T)
00154 #define UGA_UNPACK_POINTER(Q,B,C,T) UGA_UNPACK_WORD(Q,B,C,T)
00155 #define UGA_UNPACK_NPOINTER(Q,B,N,C,T) UGA_UNPACK_NWORD(Q,B,N,C,T)
00156 
00157 #define OPSYS_ALIGN_CHAR    sizeof(char)
00158 #define OPSYS_ALIGN_INT     sizeof(int)
00159 #define OPSYS_ALIGN_SHORT   sizeof(short)
00160 #define OPSYS_ALIGN_LONG    sizeof(long)
00161 #define OPSYS_ALIGN_FLOAT   sizeof(float)
00162 #define OPSYS_ALIGN_DOUBLE  sizeof(double)
00163 #define OPSYS_ALIGN_GENERIC (sizeof(double)>sizeof(long)?sizeof(double):sizeof(long))
00164 
00165 #endif

Generated on Mon Sep 18 11:39:32 2006 for jot by  doxygen 1.4.4