varsizemsg.h

Go to the documentation of this file.
00001 
00007 #ifndef VARSIZEMSG_H
00008 #define VARSIZEMSG_H
00009 
00010 /* Macros for automatically allocating, packing and unpacking varsize messages.
00011 
00012 Usage:
00013 
00014 class MyMsg : public CMessage_MyMsg {
00015 public:
00016 
00017   int myscalar;
00018   int *myarray;
00019   double myscalar2;
00020   double *myarray2;
00021 
00022   VARSIZE_DECL(MyMsg);
00023 };
00024 
00025 VARSIZE_MSG(MyMsg,
00026   VARSIZE_ARRAY(myarray);  // array size passed in via new
00027   VARSIZE_ARRAY(myarray2);  // array size passed in via new
00028 )
00029 
00030 */
00031 
00032 #define VARSIZE_DECL(MSGTYPE) \
00033   static void *alloc(int, int, int *, int); \
00034   static void *pack(MSGTYPE *); \
00035   static MSGTYPE* unpack(void *)
00036 
00037 #define VARSIZE_MSG(MSGTYPE,MSGDATA) \
00038 void *MSGTYPE::alloc(int varsizemsg_msgnum, int varsizemsg_size, \
00039                    int *varsizemsg_array, int varsizemsg_priobits) { \
00040   varsizemsg_size = ALIGN8(varsizemsg_size); \
00041   MSGTYPE *varsizemsg_msg = 0; \
00042   { \
00043     const int varsizemsg_pass = 0; \
00044     int varsizemsg_totalsize = varsizemsg_size; \
00045     int varsizemsg_arraycount = 0; \
00046     MSGDATA \
00047     varsizemsg_msg = (MSGTYPE *) CkAllocMsg( \
00048         varsizemsg_msgnum, varsizemsg_totalsize, varsizemsg_priobits); \
00049   } \
00050   { \
00051     const int varsizemsg_pass = 1; \
00052     int varsizemsg_totalsize = varsizemsg_size; \
00053     int varsizemsg_arraycount = 0; \
00054     MSGDATA \
00055   } \
00056   return (void *) varsizemsg_msg; \
00057 } \
00058  \
00059 void *MSGTYPE::pack(MSGTYPE *varsizemsg_msg) { \
00060   int *varsizemsg_array=0, varsizemsg_arraycount=0, varsizemsg_totalsize=0; \
00061   { \
00062     const int varsizemsg_pass = 2; \
00063     MSGDATA \
00064   } \
00065   return (void *) varsizemsg_msg; \
00066 } \
00067  \
00068 MSGTYPE *MSGTYPE::unpack(void *varsizemsg_buf) { \
00069   int *varsizemsg_array=0, varsizemsg_arraycount=0, varsizemsg_totalsize=0; \
00070   MSGTYPE *varsizemsg_msg = (MSGTYPE *) varsizemsg_buf; \
00071   { \
00072     const int varsizemsg_pass = 3; \
00073     MSGDATA \
00074   } \
00075   return varsizemsg_msg; \
00076 }
00077 
00078 template<class T> inline T* cast_array(T*, char *a) { return (T*) a; }
00079 template<class T> inline T* cast_size(T*, size_t a) { return (T*) a; }
00080 
00081 #define VARSIZE_ARRAY(ARRAY) { \
00082   int varsizemsg_arraysize; \
00083   switch ( varsizemsg_pass ) { \
00084   case 0: \
00085     varsizemsg_arraysize = sizeof(*(varsizemsg_msg->ARRAY)) * \
00086                 varsizemsg_array[varsizemsg_arraycount]; \
00087     varsizemsg_totalsize += ALIGN8(varsizemsg_arraysize); \
00088     varsizemsg_arraycount++; \
00089     break; \
00090   case 1: \
00091     varsizemsg_msg->ARRAY = cast_array(varsizemsg_msg->ARRAY, \
00092         (char *) varsizemsg_msg + varsizemsg_totalsize); \
00093     varsizemsg_arraysize = sizeof(*(varsizemsg_msg->ARRAY)) * \
00094                 varsizemsg_array[varsizemsg_arraycount]; \
00095     varsizemsg_totalsize += ALIGN8(varsizemsg_arraysize); \
00096     varsizemsg_arraycount++; \
00097     break; \
00098   case 2: \
00099     varsizemsg_msg->ARRAY = cast_size(varsizemsg_msg->ARRAY, \
00100       (char *) (varsizemsg_msg->ARRAY) - (char *) &(varsizemsg_msg->ARRAY) ); \
00101     break; \
00102   case 3: \
00103     varsizemsg_msg->ARRAY = cast_array(varsizemsg_msg->ARRAY, \
00104       (char *) &(varsizemsg_msg->ARRAY) + (size_t) (varsizemsg_msg->ARRAY) ); \
00105     break; \
00106   default: \
00107     break; \
00108   } \
00109 }
00110 
00111 #endif
00112 

Generated on Sat Sep 23 01:17:16 2017 for NAMD by  doxygen 1.4.7