Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

pack.H

Go to the documentation of this file.
00001 /*
00002  * Copyright 1997, Brown University, Providence, RI.
00003  * 
00004  *                         All Rights Reserved
00005  * 
00006  * Permission to use, copy, modify, and distribute this software and its
00007  * documentation for any purpose other than its incorporation into a
00008  * commercial product is hereby granted without fee, provided that the
00009  * above copyright notice appear in all copies and that both that
00010  * copyright notice and this permission notice appear in supporting
00011  * documentation, and that the name of Brown University not be used in
00012  * advertising or publicity pertaining to distribution of the software
00013  * without specific, written prior permission.
00014  * 
00015  * BROWN UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
00016  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY
00017  * PARTICULAR PURPOSE.  IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE FOR
00018  * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00019  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00020  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00021  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00022  */
00023 #ifndef PACK_H
00024 #define PACK_H
00025 
00026 /*
00027  * These routines safely pack and unpack double, words and byte
00028  * strings. bcopy is not used for efficiency reasons, even though
00029  * code size slightly increases. Have to be careful with alignment,
00030  * that is why byte by byte copy is used. 
00031  * Assumes: 8-byte doubles
00032  *          4-byte words
00033  * Note that if across multiple architectures of different word size
00034  * then must agree on largest word size and change the macros
00035  * appropriately, else will not be able to communicate between 
00036  * architectures.
00037  */
00038 
00039 #if defined(i386) || defined(WIN32)
00040 #define UGA_LITTLE_ENDIAN
00041 #endif
00042 
00043 /* UGA packing routines for words, doubles and bytes */
00044 
00045 #ifndef UGA_LITTLE_ENDIAN
00046 #define UGA_PACK_DOUBLE(QUAN, BUF, COUNT)           \
00047         do { char *_buf_ = BUF, *_ptr_ = (char *)&(QUAN);       \
00048         *((_buf_)++) = *((_ptr_)++);                \
00049         *((_buf_)++) = *((_ptr_)++);                \
00050         *((_buf_)++) = *((_ptr_)++);                \
00051         *((_buf_)++) = *((_ptr_)++);                \
00052         *((_buf_)++) = *((_ptr_)++);                \
00053         *((_buf_)++) = *((_ptr_)++);                \
00054         *((_buf_)++) = *((_ptr_)++);                \
00055         *((_buf_)++) = *((_ptr_)++);                \
00056         (BUF) = _buf_;                      \
00057         (COUNT) += 8; } while (0)
00058 #else
00059 #define UGA_PACK_DOUBLE(QUAN, BUF, COUNT)           \
00060         do { char *_buf_ = BUF, *_ptr_ = (char *)&(QUAN)+7;     \
00061         *((_buf_)++) = *((_ptr_)--);                \
00062         *((_buf_)++) = *((_ptr_)--);                \
00063         *((_buf_)++) = *((_ptr_)--);                \
00064         *((_buf_)++) = *((_ptr_)--);                \
00065         *((_buf_)++) = *((_ptr_)--);                \
00066         *((_buf_)++) = *((_ptr_)--);                \
00067         *((_buf_)++) = *((_ptr_)--);                \
00068         *((_buf_)++) = *((_ptr_)--);                \
00069         (BUF) = _buf_;                      \
00070         (COUNT) += 8; } while (0)
00071 #endif
00072 
00073 #define UGA_PACK_NDOUBLE(QUAN, BUF, NUM, COUNT)             \
00074         do { int _i;                        \
00075           for (_i=0; _i<(int)NUM; _i++)             \
00076                UGA_PACK_DOUBLE(QUAN[_i], BUF, COUNT); } while (0)
00077 
00078 #define UGA_PACK_WORD(QUAN, BUF, COUNT)             \
00079         do { u_long _long_ = htonl((u_long)QUAN);       \
00080         char *_buf_ = BUF, *_ptr_;              \
00081         _ptr_ = (char *)&_long_;                \
00082         *((_buf_)++) = *((_ptr_)++);                \
00083         *((_buf_)++) = *((_ptr_)++);                \
00084         *((_buf_)++) = *((_ptr_)++);                \
00085         *((_buf_)++) = *((_ptr_)++);                \
00086         (BUF) = _buf_;                      \
00087         (COUNT) += 4; } while (0)
00088 
00089 #define UGA_PACK_NWORD(QUAN, BUF, NUM, COUNT)           \
00090         do { int _i;                        \
00091           for (_i=0; _i<(int)NUM; _i++)             \
00092                 UGA_PACK_WORD(QUAN[_i], BUF, COUNT); } while (0)
00093 
00094 #define UGA_PACK_BYTES(PTR, BUF, SIZE, COUNT)           \
00095         ((void)bcopy((char *)(PTR),(char *)(BUF),(int)(SIZE)),  \
00096          BUF += (SIZE),(COUNT) += (SIZE))
00097 
00098 /* UGA unpacking routines for words, doubles and bytes */
00099 
00100 #ifndef UGA_LITTLE_ENDIAN
00101 #define UGA_UNPACK_DOUBLE(QUAN, BUF, COUNT)         \
00102         do { char *_buf_ = BUF, *_ptr_ = (char *)&(QUAN);   \
00103         *((_ptr_)++) = *((_buf_)++);                \
00104         *((_ptr_)++) = *((_buf_)++);                \
00105         *((_ptr_)++) = *((_buf_)++);                \
00106         *((_ptr_)++) = *((_buf_)++);                \
00107         *((_ptr_)++) = *((_buf_)++);                \
00108         *((_ptr_)++) = *((_buf_)++);                \
00109         *((_ptr_)++) = *((_buf_)++);                \
00110         *((_ptr_)++) = *((_buf_)++);                \
00111         (BUF) = _buf_;                      \
00112         (COUNT) += 8; } while (0)
00113 #else
00114 #define UGA_UNPACK_DOUBLE(QUAN, BUF, COUNT)         \
00115         do { char *_buf_ = BUF, *_ptr_ = (char *)&(QUAN)+7; \
00116         *((_ptr_)--) = *((_buf_)++);                \
00117         *((_ptr_)--) = *((_buf_)++);                \
00118         *((_ptr_)--) = *((_buf_)++);                \
00119         *((_ptr_)--) = *((_buf_)++);                \
00120         *((_ptr_)--) = *((_buf_)++);                \
00121         *((_ptr_)--) = *((_buf_)++);                \
00122         *((_ptr_)--) = *((_buf_)++);                \
00123         *((_ptr_)--) = *((_buf_)++);                \
00124         (BUF) = _buf_;                      \
00125         (COUNT) += 8; } while (0)
00126 #endif
00127 
00128 #define UGA_UNPACK_NDOUBLE(QUAN, BUF, NUM, COUNT)       \
00129         do { int _i;                        \
00130           for (_i=0; _i<(int)NUM; _i++)             \
00131                UGA_UNPACK_DOUBLE(QUAN[_i], BUF, COUNT); } while (0)
00132 
00133 #define UGA_UNPACK_WORD(QUAN, BUF, COUNT, TYPE)         \
00134         do { char *_buf_ = BUF, *_ptr_ = (char *)&(QUAN);   \
00135         *((_ptr_)++) = *((_buf_)++);                \
00136         *((_ptr_)++) = *((_buf_)++);                \
00137         *((_ptr_)++) = *((_buf_)++);                \
00138         *((_ptr_)++) = *((_buf_)++);                \
00139         (BUF) = _buf_;                      \
00140         (QUAN) = (TYPE)ntohl((u_long)QUAN);         \
00141         (COUNT) += 4; } while (0)
00142 
00143 #define UGA_UNPACK_NWORD(QUAN, BUF, NUM, COUNT, TYPE)       \
00144         do { int _i;                        \
00145           for (_i=0; _i<(int)NUM; _i++)             \
00146                 UGA_UNPACK_WORD(QUAN, BUF, COUNT, TYPE); } while (0)
00147 
00148 #define UGA_UNPACK_BYTES(PTR, BUF, SIZE, COUNT)         \
00149         ((void)bcopy((char *)(BUF), (char *)(PTR), (int)(SIZE)),\
00150          BUF += (SIZE),(COUNT) += (SIZE))
00151 
00152 /*
00153  * Additional types that are currently based on the above macros, but
00154  * could change, depending on architecture.
00155  * long = word
00156  * int  = word
00157  * enum = word
00158  * ptr  = word
00159  */
00160 
00161 #define UGA_PACK_LONG(Q,B,C)        UGA_PACK_WORD(Q,B,C)
00162 #define UGA_PACK_NLONG(Q,B,N,C)     UGA_PACK_NWORD(Q,B,N,C)
00163 #define UGA_PACK_INTEGER(Q,B,C)     UGA_PACK_WORD(Q,B,C)
00164 #define UGA_PACK_NINTEGER(Q,B,N,C)  UGA_PACK_NWORD(Q,B,N,C)
00165 #define UGA_PACK_ENUM(Q,B,C)        UGA_PACK_WORD(Q,B,C)
00166 #define UGA_PACK_NENUM(Q,B,N,C)     UGA_PACK_NWORD(Q,B,N,C)
00167 #define UGA_PACK_POINTER(Q,B,C)     UGA_PACK_WORD(Q,B,C)
00168 #define UGA_PACK_NPOINTER(Q,B,N,C)  UGA_PACK_NWORD(Q,B,N,C)
00169 
00170 #define UGA_UNPACK_LONG(Q,B,C)      UGA_UNPACK_WORD(Q,B,C,long)
00171 #define UGA_UNPACK_NLONG(Q,B,N,C)   UGA_UNPACK_NWORD(Q,B,N,C,long)
00172 #define UGA_UNPACK_INTEGER(Q,B,C)   UGA_UNPACK_WORD(Q,B,C,int)
00173 #define UGA_UNPACK_NINTEGER(Q,B,N,C)    UGA_UNPACK_NWORD(Q,B,N,C,int)
00174 #define UGA_UNPACK_ENUM(Q,B,C,T)    UGA_UNPACK_WORD(Q,B,C,T)
00175 #define UGA_UNPACK_NENUM(Q,B,N,C,T) UGA_UNPACK_NWORD(Q,B,N,C,T)
00176 #define UGA_UNPACK_POINTER(Q,B,C,T) UGA_UNPACK_WORD(Q,B,C,T)
00177 #define UGA_UNPACK_NPOINTER(Q,B,N,C,T)  UGA_UNPACK_NWORD(Q,B,N,C,T)
00178 
00179 #define OPSYS_ALIGN_CHAR    sizeof(char)
00180 #define OPSYS_ALIGN_INT     sizeof(int)
00181 #define OPSYS_ALIGN_SHORT   sizeof(short)
00182 #define OPSYS_ALIGN_LONG    sizeof(long)
00183 #define OPSYS_ALIGN_FLOAT   sizeof(float)
00184 #define OPSYS_ALIGN_DOUBLE  sizeof(double)
00185 #define OPSYS_ALIGN_GENERIC (sizeof(double)>sizeof(long)?sizeof(double):sizeof(long))
00186 
00187 #endif

Generated on Mon Sep 15 16:25:57 2003 for gluebase by doxygen1.2.18