Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ParN02/include/MarshaledObj.h
Go to the documentation of this file.
1  /**********************************************************************
2  * Include file with Base Class of Marshalgen *
3  **********************************************************************/
4 
5 #ifndef MARSHALEDOBJ_H
6 #define MARSHALEDOBJ_H
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <assert.h>
12 
13 #define MSH_ASSERT(X) {assert(X);}
14 
15 #define MSH_HEADER_SIZE (sizeof(int)*2)
16 // the first field (first sizeof(int) bytes) contains the $TYPE_CHOICE
17 // the second field contains the size including the header
18 #define MSH_TOTALSIZE_OFFSET (sizeof(int))
19 #define MSH_TYPECHOICE_OFFSET 0
20 
21 #define MSH_SET_TYPECHOICE(X) { memcpy(msh_buffer+MSH_TYPECHOICE_OFFSET,&(X),sizeof(int));}
22 #define MSH_SET_TOTALSIZE(X) { memcpy(msh_buffer+MSH_TOTALSIZE_OFFSET,&(X),sizeof(int));}
23 #define MSH_GET_TYPECHOICE(X,BUF) { memcpy(&(X), ((char*)BUF)+MSH_TYPECHOICE_OFFSET,sizeof(int));}
24 #define MSH_GET_TOTALSIZE(X,BUF) { memcpy(&(X), ((char*)BUF)+MSH_TOTALSIZE_OFFSET,sizeof(int));}
25 
26 
27 class MarshaledObj {
28  private:
29  // Make sure all marshaled objects are word aligned.
30  static const int WORD_SIZE = sizeof(long);
31  public:
32  static int ROUND_UP( int x ){
33  return (((x)+(WORD_SIZE-1)) / WORD_SIZE) * WORD_SIZE;
34  }
35 
36  public:
37  // Constructs an empty MarshaledObj,
39  msh_extent = 128;
41  msh_isUnmarshalDone = false;
42 
43  msh_buffer = (char *)malloc(msh_extent);
45 
48 
49  msh_typechoice = 0;
50  int totalsize = msh_cursor-msh_buffer;
51 
53  MSH_SET_TOTALSIZE(totalsize);
54  }
55 
56  //MarshaledObj(void *buf);
57  // This constructs a MarshledObj from a buffer (of type char*) for unmarshaling.
58  // buf is obtain from an already marshaled object.
59  // The first field of buf must be an int that contains the size of the buf
60  // NOT including itself.
61  // isUnmarshaling must be 'u' (for unmarshaling) .
62  MarshaledObj(void *buf, char chIsUnmarshaling) {
63  msh_isUnmarshalDone = false;
64 
65  if(chIsUnmarshaling != 'u') {
66  printf("MarshaledObj(void*, char): wrong argument\n");
67  return;
68  }
69 
70  //msh_extent = ROUND_UP(*(int *)buf + sizeof(int));
72 
75 
76  msh_buffer = (char *)malloc(msh_extent);
78 
79  memcpy(msh_buffer, (char *)buf, msh_extent);
82 
83  //MSH_SET_TYPECHOICE(msh_typechoice);
84 
85  }
86 
88  if ( ! isUnmarshaling() )
90  }
91 
92  inline bool isUnmarshaling() {
93  return (msh_extent <= 0);
94  }
95 
96  private:
97  // Dont use copy constructor
98  const MarshaledObj& operator=(const MarshaledObj& right);
99 
100  protected:
101  int msh_typechoice; // alias of $TYPE_CHOICE
102 
103  // points to the buffer (header+body)
104  char *msh_buffer;
105 
106  // msh_field_begin points to the size of the current field being marshaled
108 
109  // msh_size contains the total size of msh_buffer. i.e.,
110  size_t msh_size;
111 
112  // msh_cursor points to the next field to be marshaled.
113  char *msh_cursor;
114 
115  // msh_extent is the total allocated space for msh_buffer.
116  // msh_extent is always >= msh_size
117  size_t msh_extent;
118 
119  bool msh_isUnmarshalDone; //Is unmarshaling done yet?
120 
121  public:
122  inline void EXTEND_BUFFER(int size){
123  msh_size += size;
124  if(msh_size > msh_extent){
126  }
127  }
128 
129  void resizeBuffer(size_t new_size ) {
130  int msh_displacement = msh_cursor - msh_buffer;
131  int field_displacement = msh_field_begin - msh_buffer;
132 
133  while(new_size > msh_extent)
134  msh_extent *= 2;
135 
136  msh_buffer = (char *)realloc( msh_buffer, msh_extent);
137  MSH_ASSERT(msh_buffer);
138 
139  msh_cursor = msh_buffer + msh_displacement;
140  msh_field_begin = msh_buffer + field_displacement;
141  }
142 
143  public:
144  // Returns the total size of buffer
145  inline int getBufferSize() {
146  return msh_size;
147  }
148 
149  inline char *getBuffer() {
150  return msh_buffer;
151  }
152 
153  /* p: pointer to the data field, size: size of that primitive data field */
154  void marshalPrimitive(void* p, int size) {
155  int msh_currentSize;
156  if (isUnmarshaling())
157  throw "Tried to marshal in object marked isUnmarshaling = true";
158  msh_currentSize = size;
159  EXTEND_BUFFER(msh_currentSize + sizeof(int));
160 
161  // *(int *)msh_cursor = msh_currentSize;
162  memcpy(msh_cursor, &msh_currentSize, sizeof(int));
163  msh_cursor += sizeof(int);
164  memcpy(msh_cursor, p, size);
165  msh_cursor += msh_currentSize;
167 
169  }
170 
171  void unmarshalPrimitive(void* p, int size) {
172  int msh_currentSize;
173  //memcpy(&msh_currentSize, msh_cursor, sizeof(int));
174  /* in case *msh_cursor is invalid, use "size" not to crash the memory */
175  msh_currentSize = size;
176  msh_cursor += sizeof(int);
177  memcpy(p, msh_cursor, msh_currentSize);
178  msh_cursor += msh_currentSize;
179  //msh_size = msh_cursor - msh_buffer;
180  }
181 };
182 
183 /* Used for distinguish the class types of the template parameter
184  vietha 2003.05.01 */
185 template <class T,class>
187 {
188 public:
189  enum {Is = 0};
190 };
191 
192 template<class T>
193 class MSH_IsSameClass<T,T>
194 {
195 public:
196  enum {Is = 1};
197 };
198 
199 #endif
#define MSH_GET_TOTALSIZE(X, BUF)
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
void free(void *__ptr)
Definition: hjmalloc.cc:140
void unmarshalPrimitive(void *p, int size)
const char * p
Definition: xmltok.h:285
MarshaledObj(void *buf, char chIsUnmarshaling)
static int ROUND_UP(int x)
#define MSH_SET_TYPECHOICE(X)
void resizeBuffer(size_t new_size)
#define MSH_ASSERT(X)
#define MSH_GET_TYPECHOICE(X, BUF)
void EXTEND_BUFFER(int size)
void marshalPrimitive(void *p, int size)
void * realloc(void *__ptr, size_t __size)
Definition: hjmalloc.cc:103
void * malloc(size_t __size)
Definition: hjmalloc.cc:30
#define MSH_HEADER_SIZE
#define MSH_SET_TOTALSIZE(X)