/home/runner/work/charm/charm/src/util/pup.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | Pack/UnPack Library for UIUC Parallel Programming Lab |
3 | | Orion Sky Lawlor, olawlor@uiuc.edu, 4/5/2000 |
4 | | |
5 | | This library allows you to easily pack an array, structure, |
6 | | or object into a memory buffer or disk file, and then read |
7 | | the object back later. The library can also handle translating |
8 | | between different machine representations for integers and floats. |
9 | | |
10 | | Originally, you had to write separate functions for buffer |
11 | | sizing, pack to memory, unpack from memory, pack to disk, and |
12 | | unpack from disk. These functions all perform the exact same function-- |
13 | | namely, they list the members of the array, struct, or object. |
14 | | Further, all the functions must agree, or the unpacked data will |
15 | | be garbage. This library allows the user to write *one* function, |
16 | | pup, which will perform all needed packing/unpacking. |
17 | | |
18 | | A simple example is: |
19 | | class foo { |
20 | | private: |
21 | | bool isBar; |
22 | | int x; |
23 | | char y; |
24 | | unsigned long z; |
25 | | CkVec<double> q; |
26 | | public: |
27 | | ... |
28 | | void pup(PUP::er &p) { |
29 | | PUPn(isBar); |
30 | | PUPn(x);PUPn(y);PUPn(z); |
31 | | PUPn(q); |
32 | | } |
33 | | }; |
34 | | |
35 | | A more complex example is: |
36 | | class bar { |
37 | | private: |
38 | | foo f; |
39 | | int nArr;//Length of array below |
40 | | double *arr;//Heap-allocated array |
41 | | public: |
42 | | ... |
43 | | |
44 | | void pup(PUP::er &p) { |
45 | | PUPn(f); // <- automatically calls foo::pup |
46 | | PUPn(nArr); |
47 | | if (p.isUnpacking()) // <- must allocate array on other side. |
48 | | { |
49 | | arr=new double[nArr]; |
50 | | _MEMCHECK(arr); |
51 | | } |
52 | | PUPv(arr,nArr); // <- special syntax for arrays of simple types |
53 | | } |
54 | | }; |
55 | | */ |
56 | | |
57 | | #ifndef __CK_PUP_H |
58 | | #define __CK_PUP_H |
59 | | |
60 | | #include "conv-autoconfig.h" /* guarded; supplies feature macros (e.g. |
61 | | CMK_SIGNEDCHAR_DIFF_CHAR) on reconverse builds, whose converse.h does not |
62 | | pull the classic conv-config chain */ |
63 | | #include <stdio.h> /*<- for "FILE *" */ |
64 | | #include <type_traits> |
65 | | #include <utility> |
66 | | #include <functional> |
67 | | |
68 | | #ifndef __cplusplus |
69 | | #error "Use pup_c.h for C programs-- pup.h is for C++ programs" |
70 | | #endif |
71 | | |
72 | | #ifdef STANDALONE_PUP |
73 | | #define CmiAbort(x) { printf(x); abort(); } |
74 | | #else |
75 | | #ifndef CHARM_H |
76 | | # include "converse.h" // <- for CMK_* defines |
77 | | #endif |
78 | | #endif |
79 | | |
80 | | //We need CkMigrateMessage only to distinguish the migration |
81 | | // constructor from all other constructors-- the type |
82 | | // itself has no meaningful fields. |
83 | | typedef struct {int is_only_a_name;} CkMigrateMessage; |
84 | | |
85 | | namespace PUP { |
86 | | class er; // Forward declare for all sorts of things |
87 | | |
88 | | /** |
89 | | * A utility to let classes avoid default construction when they're |
90 | | * about to be unpacked, by defining a constructor that takes a |
91 | | * value of type PUP::reconstruct |
92 | | */ |
93 | | struct reconstruct {}; |
94 | | namespace detail { |
95 | | template <typename T, bool b = std::is_constructible<T, reconstruct>::value> |
96 | | struct TemporaryObjectHolder { }; |
97 | | template <typename T> |
98 | | struct TemporaryObjectHolder<T, true> |
99 | | { |
100 | | typename std::remove_cv<typename std::remove_reference<T>::type>::type t{reconstruct()}; |
101 | | }; |
102 | | template <typename T> |
103 | | struct TemporaryObjectHolder<T, false> |
104 | | { |
105 | | typename std::remove_cv<typename std::remove_reference<T>::type>::type t{}; |
106 | | }; |
107 | | template <typename T> |
108 | | void operator|(er &p, TemporaryObjectHolder<T> &t) { |
109 | | p | t.t; |
110 | | } |
111 | | } |
112 | | |
113 | | #if CMK_LONG_LONG_DEFINED |
114 | 0 | #define CMK_PUP_LONG_LONG long long |
115 | | #elif CMK___int64_DEFINED |
116 | | #define CMK_PUP_LONG_LONG __int64 |
117 | | #endif |
118 | | |
119 | | |
120 | | //Item data types-- these are used to do byte swapping, etc. |
121 | | typedef enum { |
122 | | //(this list must exactly match that in PUPer_xlate) |
123 | | Tchar=0,Tshort, Tint, Tlong, Tlonglong, |
124 | | Tuchar,Tushort,Tuint,Tulong, Tulonglong, |
125 | | #if CMK_HAS_INT16 |
126 | | Tint128, Tuint128, |
127 | | #endif |
128 | | Tfloat,Tdouble,Tlongdouble, |
129 | | Tbool, |
130 | | Tbyte, |
131 | | Tsync, |
132 | | Tpointer, |
133 | | dataType_last //<- for setting table lengths, etc. |
134 | | } dataType; |
135 | | |
136 | | /// Which side of the machine a pupped buffer lives on. DEVICE means `p` is a |
137 | | /// device pointer, so the bytes travel through the migration message's device |
138 | | /// region rather than its host region. PUP::ers that have no device region |
139 | | /// ignore DEVICE buffers entirely -- checkpointing a chare does not capture |
140 | | /// its device state. |
141 | | enum class PUPMode { |
142 | | HOST, |
143 | | DEVICE |
144 | | }; |
145 | | |
146 | | /// Layout rule for the device side of a migration stream. Every DEVICE-mode |
147 | | /// buffer starts at an offset that is a multiple of this, measured from the |
148 | | /// start of the device region -- relative, not absolute, because the region's |
149 | | /// own base carries no alignment guarantee. The sizer and both memory walkers |
150 | | /// apply the same rule, which is what lets the receiver reconstruct the |
151 | | /// sender's layout from its own pup calls alone, with nothing on the wire |
152 | | /// describing it. |
153 | | constexpr size_t DEVICE_PUP_ALIGN = 256; |
154 | 0 | static inline size_t alignDeviceOffset(size_t offset) { |
155 | 0 | return (offset + (DEVICE_PUP_ALIGN - 1)) & ~(DEVICE_PUP_ALIGN - 1); |
156 | 0 | } Unexecuted instantiation: ckhashtable.C:_ZN3PUPL17alignDeviceOffsetEm Unexecuted instantiation: conv-lists.C:_ZN3PUPL17alignDeviceOffsetEm Unexecuted instantiation: pup_c.C:_ZN3PUPL17alignDeviceOffsetEm Unexecuted instantiation: pup_toNetwork.C:_ZN3PUPL17alignDeviceOffsetEm Unexecuted instantiation: pup_util.C:_ZN3PUPL17alignDeviceOffsetEm Unexecuted instantiation: pup_xlater.C:_ZN3PUPL17alignDeviceOffsetEm |
157 | | |
158 | 0 | static inline dataType getXlateDataType(signed char *a) { return Tchar; }Unexecuted instantiation: ckhashtable.C:_ZN3PUPL16getXlateDataTypeEPa Unexecuted instantiation: conv-lists.C:_ZN3PUPL16getXlateDataTypeEPa Unexecuted instantiation: pup_c.C:_ZN3PUPL16getXlateDataTypeEPa Unexecuted instantiation: pup_toNetwork.C:_ZN3PUPL16getXlateDataTypeEPa Unexecuted instantiation: pup_util.C:_ZN3PUPL16getXlateDataTypeEPa Unexecuted instantiation: pup_xlater.C:_ZN3PUPL16getXlateDataTypeEPa |
159 | | #if CMK_SIGNEDCHAR_DIFF_CHAR |
160 | 0 | static inline dataType getXlateDataType(char *a) { return Tchar; }Unexecuted instantiation: ckhashtable.C:_ZN3PUPL16getXlateDataTypeEPc Unexecuted instantiation: conv-lists.C:_ZN3PUPL16getXlateDataTypeEPc Unexecuted instantiation: pup_c.C:_ZN3PUPL16getXlateDataTypeEPc Unexecuted instantiation: pup_toNetwork.C:_ZN3PUPL16getXlateDataTypeEPc Unexecuted instantiation: pup_util.C:_ZN3PUPL16getXlateDataTypeEPc Unexecuted instantiation: pup_xlater.C:_ZN3PUPL16getXlateDataTypeEPc |
161 | | #endif |
162 | 0 | static inline dataType getXlateDataType(short *a) { return Tshort; }Unexecuted instantiation: ckhashtable.C:_ZN3PUPL16getXlateDataTypeEPs Unexecuted instantiation: conv-lists.C:_ZN3PUPL16getXlateDataTypeEPs Unexecuted instantiation: pup_c.C:_ZN3PUPL16getXlateDataTypeEPs Unexecuted instantiation: pup_toNetwork.C:_ZN3PUPL16getXlateDataTypeEPs Unexecuted instantiation: pup_util.C:_ZN3PUPL16getXlateDataTypeEPs Unexecuted instantiation: pup_xlater.C:_ZN3PUPL16getXlateDataTypeEPs |
163 | 0 | static inline dataType getXlateDataType(int *a) { return Tint; }Unexecuted instantiation: ckhashtable.C:_ZN3PUPL16getXlateDataTypeEPi Unexecuted instantiation: conv-lists.C:_ZN3PUPL16getXlateDataTypeEPi Unexecuted instantiation: pup_c.C:_ZN3PUPL16getXlateDataTypeEPi Unexecuted instantiation: pup_toNetwork.C:_ZN3PUPL16getXlateDataTypeEPi Unexecuted instantiation: pup_util.C:_ZN3PUPL16getXlateDataTypeEPi Unexecuted instantiation: pup_xlater.C:_ZN3PUPL16getXlateDataTypeEPi |
164 | 0 | static inline dataType getXlateDataType(long *a) { return Tlong; }Unexecuted instantiation: ckhashtable.C:_ZN3PUPL16getXlateDataTypeEPl Unexecuted instantiation: conv-lists.C:_ZN3PUPL16getXlateDataTypeEPl Unexecuted instantiation: pup_c.C:_ZN3PUPL16getXlateDataTypeEPl Unexecuted instantiation: pup_toNetwork.C:_ZN3PUPL16getXlateDataTypeEPl Unexecuted instantiation: pup_util.C:_ZN3PUPL16getXlateDataTypeEPl Unexecuted instantiation: pup_xlater.C:_ZN3PUPL16getXlateDataTypeEPl |
165 | 0 | static inline dataType getXlateDataType(unsigned char *a) { return Tuchar; }Unexecuted instantiation: ckhashtable.C:_ZN3PUPL16getXlateDataTypeEPh Unexecuted instantiation: conv-lists.C:_ZN3PUPL16getXlateDataTypeEPh Unexecuted instantiation: pup_c.C:_ZN3PUPL16getXlateDataTypeEPh Unexecuted instantiation: pup_toNetwork.C:_ZN3PUPL16getXlateDataTypeEPh Unexecuted instantiation: pup_util.C:_ZN3PUPL16getXlateDataTypeEPh Unexecuted instantiation: pup_xlater.C:_ZN3PUPL16getXlateDataTypeEPh |
166 | 0 | static inline dataType getXlateDataType(unsigned short *a) { return Tushort; }Unexecuted instantiation: ckhashtable.C:_ZN3PUPL16getXlateDataTypeEPt Unexecuted instantiation: conv-lists.C:_ZN3PUPL16getXlateDataTypeEPt Unexecuted instantiation: pup_c.C:_ZN3PUPL16getXlateDataTypeEPt Unexecuted instantiation: pup_toNetwork.C:_ZN3PUPL16getXlateDataTypeEPt Unexecuted instantiation: pup_util.C:_ZN3PUPL16getXlateDataTypeEPt Unexecuted instantiation: pup_xlater.C:_ZN3PUPL16getXlateDataTypeEPt |
167 | 0 | static inline dataType getXlateDataType(unsigned int *a) { return Tuint; }Unexecuted instantiation: ckhashtable.C:_ZN3PUPL16getXlateDataTypeEPj Unexecuted instantiation: conv-lists.C:_ZN3PUPL16getXlateDataTypeEPj Unexecuted instantiation: pup_c.C:_ZN3PUPL16getXlateDataTypeEPj Unexecuted instantiation: pup_toNetwork.C:_ZN3PUPL16getXlateDataTypeEPj Unexecuted instantiation: pup_util.C:_ZN3PUPL16getXlateDataTypeEPj Unexecuted instantiation: pup_xlater.C:_ZN3PUPL16getXlateDataTypeEPj |
168 | 0 | static inline dataType getXlateDataType(unsigned long *a) { return Tulong; }Unexecuted instantiation: ckhashtable.C:_ZN3PUPL16getXlateDataTypeEPm Unexecuted instantiation: conv-lists.C:_ZN3PUPL16getXlateDataTypeEPm Unexecuted instantiation: pup_c.C:_ZN3PUPL16getXlateDataTypeEPm Unexecuted instantiation: pup_toNetwork.C:_ZN3PUPL16getXlateDataTypeEPm Unexecuted instantiation: pup_util.C:_ZN3PUPL16getXlateDataTypeEPm Unexecuted instantiation: pup_xlater.C:_ZN3PUPL16getXlateDataTypeEPm |
169 | 0 | static inline dataType getXlateDataType(float *a) { return Tfloat; }Unexecuted instantiation: ckhashtable.C:_ZN3PUPL16getXlateDataTypeEPf Unexecuted instantiation: conv-lists.C:_ZN3PUPL16getXlateDataTypeEPf Unexecuted instantiation: pup_c.C:_ZN3PUPL16getXlateDataTypeEPf Unexecuted instantiation: pup_toNetwork.C:_ZN3PUPL16getXlateDataTypeEPf Unexecuted instantiation: pup_util.C:_ZN3PUPL16getXlateDataTypeEPf Unexecuted instantiation: pup_xlater.C:_ZN3PUPL16getXlateDataTypeEPf |
170 | 0 | static inline dataType getXlateDataType(double *a) { return Tdouble; }Unexecuted instantiation: ckhashtable.C:_ZN3PUPL16getXlateDataTypeEPd Unexecuted instantiation: conv-lists.C:_ZN3PUPL16getXlateDataTypeEPd Unexecuted instantiation: pup_c.C:_ZN3PUPL16getXlateDataTypeEPd Unexecuted instantiation: pup_toNetwork.C:_ZN3PUPL16getXlateDataTypeEPd Unexecuted instantiation: pup_util.C:_ZN3PUPL16getXlateDataTypeEPd Unexecuted instantiation: pup_xlater.C:_ZN3PUPL16getXlateDataTypeEPd |
171 | | #if CMK_LONG_DOUBLE_DEFINED |
172 | 0 | static inline dataType getXlateDataType(long double *a) { return Tlongdouble; }Unexecuted instantiation: ckhashtable.C:_ZN3PUPL16getXlateDataTypeEPe Unexecuted instantiation: conv-lists.C:_ZN3PUPL16getXlateDataTypeEPe Unexecuted instantiation: pup_c.C:_ZN3PUPL16getXlateDataTypeEPe Unexecuted instantiation: pup_toNetwork.C:_ZN3PUPL16getXlateDataTypeEPe Unexecuted instantiation: pup_util.C:_ZN3PUPL16getXlateDataTypeEPe Unexecuted instantiation: pup_xlater.C:_ZN3PUPL16getXlateDataTypeEPe |
173 | | #endif |
174 | 0 | static inline dataType getXlateDataType(bool *a) { return Tbool; }Unexecuted instantiation: ckhashtable.C:_ZN3PUPL16getXlateDataTypeEPb Unexecuted instantiation: conv-lists.C:_ZN3PUPL16getXlateDataTypeEPb Unexecuted instantiation: pup_c.C:_ZN3PUPL16getXlateDataTypeEPb Unexecuted instantiation: pup_toNetwork.C:_ZN3PUPL16getXlateDataTypeEPb Unexecuted instantiation: pup_util.C:_ZN3PUPL16getXlateDataTypeEPb Unexecuted instantiation: pup_xlater.C:_ZN3PUPL16getXlateDataTypeEPb |
175 | | #ifdef CMK_PUP_LONG_LONG |
176 | 0 | static inline dataType getXlateDataType(CMK_PUP_LONG_LONG *a) { return Tlonglong; }Unexecuted instantiation: ckhashtable.C:_ZN3PUPL16getXlateDataTypeEPx Unexecuted instantiation: conv-lists.C:_ZN3PUPL16getXlateDataTypeEPx Unexecuted instantiation: pup_c.C:_ZN3PUPL16getXlateDataTypeEPx Unexecuted instantiation: pup_toNetwork.C:_ZN3PUPL16getXlateDataTypeEPx Unexecuted instantiation: pup_util.C:_ZN3PUPL16getXlateDataTypeEPx Unexecuted instantiation: pup_xlater.C:_ZN3PUPL16getXlateDataTypeEPx |
177 | 0 | static inline dataType getXlateDataType(unsigned CMK_PUP_LONG_LONG *a) { return Tulonglong; }Unexecuted instantiation: ckhashtable.C:_ZN3PUPL16getXlateDataTypeEPy Unexecuted instantiation: conv-lists.C:_ZN3PUPL16getXlateDataTypeEPy Unexecuted instantiation: pup_c.C:_ZN3PUPL16getXlateDataTypeEPy Unexecuted instantiation: pup_toNetwork.C:_ZN3PUPL16getXlateDataTypeEPy Unexecuted instantiation: pup_util.C:_ZN3PUPL16getXlateDataTypeEPy Unexecuted instantiation: pup_xlater.C:_ZN3PUPL16getXlateDataTypeEPy |
178 | | #endif |
179 | | #if CMK_HAS_INT16 |
180 | 0 | static inline dataType getXlateDataType(CmiInt16 *a) { return Tint128; }Unexecuted instantiation: ckhashtable.C:_ZN3PUPL16getXlateDataTypeEPn Unexecuted instantiation: conv-lists.C:_ZN3PUPL16getXlateDataTypeEPn Unexecuted instantiation: pup_c.C:_ZN3PUPL16getXlateDataTypeEPn Unexecuted instantiation: pup_toNetwork.C:_ZN3PUPL16getXlateDataTypeEPn Unexecuted instantiation: pup_util.C:_ZN3PUPL16getXlateDataTypeEPn Unexecuted instantiation: pup_xlater.C:_ZN3PUPL16getXlateDataTypeEPn |
181 | 0 | static inline dataType getXlateDataType(CmiUInt16 *a) { return Tuint128; }Unexecuted instantiation: ckhashtable.C:_ZN3PUPL16getXlateDataTypeEPo Unexecuted instantiation: conv-lists.C:_ZN3PUPL16getXlateDataTypeEPo Unexecuted instantiation: pup_c.C:_ZN3PUPL16getXlateDataTypeEPo Unexecuted instantiation: pup_toNetwork.C:_ZN3PUPL16getXlateDataTypeEPo Unexecuted instantiation: pup_util.C:_ZN3PUPL16getXlateDataTypeEPo Unexecuted instantiation: pup_xlater.C:_ZN3PUPL16getXlateDataTypeEPo |
182 | | #endif |
183 | | |
184 | | |
185 | | //This should be a 1-byte unsigned type |
186 | | typedef unsigned char myByte; |
187 | | |
188 | | //Forward declarations |
189 | | class er; |
190 | | class able; |
191 | | |
192 | | //Used for out-of-order unpacking |
193 | | class seekBlock { |
194 | | enum {maxSections=3}; |
195 | | int secTab[maxSections+1];//The start of each seek section |
196 | | int nSec;//Number of sections; current section # |
197 | | int secTabOff;//Start of the section table, relative to the seek block |
198 | | er &p; |
199 | | bool hasEnded; |
200 | | public: |
201 | | //Constructor |
202 | | seekBlock(er &Np,int nSections); |
203 | | //Destructor |
204 | | ~seekBlock(); |
205 | | |
206 | | //Seek to the given section number (0-based, less than nSections) |
207 | | void seek(int toSection); |
208 | | //Finish with this seeker (must be called) |
209 | | void endBlock(void); |
210 | | |
211 | | //An evil hack to avoid inheritance and virtual functions among seekers-- |
212 | | // stores the PUP::er specific block start information. |
213 | | union { |
214 | | size_t off; |
215 | | long loff; |
216 | | const myByte *cptr; |
217 | | myByte *ptr; |
218 | | void *vptr; |
219 | | } data; |
220 | | }; |
221 | | |
222 | | //The abstract base class: PUP::er. |
223 | | class er { |
224 | | private: |
225 | | er(const er &p);//You don't want to copy PUP::er's. |
226 | | protected: |
227 | | unsigned int PUP_er_state; |
228 | | // You don't want to create raw PUP::er's. |
229 | 33.8M | explicit er(unsigned int inType) : PUP_er_state(inType) {} |
230 | | |
231 | | /// These state bits describe the PUP::er's direction. |
232 | | enum |
233 | | { |
234 | | IS_SIZING = 0x0100, |
235 | | IS_PACKING = 0x0200, |
236 | | IS_UNPACKING = 0x0400, |
237 | | TYPE_MASK = 0xFF00 |
238 | | }; |
239 | | public: |
240 | | virtual ~er();//<- does nothing, but might be needed by some child |
241 | | |
242 | | // These state bits describe various user-settable properties. This needs to be public |
243 | | // because it used when PUPers are created from the checkpointing and migration code. |
244 | | enum |
245 | | { |
246 | | IS_USERLEVEL = 0x0004, // If set, this is not a migration pup - it's something else. |
247 | | IS_DELETING = 0x0008, // If set, C & f90 objects should delete themselves after pup |
248 | | IS_COMMENTS = 0x0010, // If set, this PUP::er wants comments and sync codes. |
249 | | IS_CHECKPOINT = 0x0020, // If set, it is creating or restarting from a checkpoint |
250 | | IS_MIGRATION = 0x0040 // If set, it is migrating between PEs (e.g. in LB) |
251 | | }; |
252 | | |
253 | | //State queries (exactly one of these will be true) |
254 | 1.20k | bool isSizing(void) const {return (PUP_er_state&IS_SIZING)!=0?true:false;} |
255 | 2.35M | bool isPacking(void) const {return (PUP_er_state&IS_PACKING)!=0?true:false;} |
256 | 21.5M | bool isUnpacking(void) const {return (PUP_er_state&IS_UNPACKING)!=0?true:false;} |
257 | | const char * typeString() const; |
258 | 16 | unsigned int getStateFlags(void) const {return PUP_er_state;} |
259 | | |
260 | | //This indicates that the pup routine should free memory during packing. |
261 | | void becomeDeleting(void) {PUP_er_state|=IS_DELETING;} |
262 | 80 | bool isDeleting(void) const {return (PUP_er_state&IS_DELETING)!=0?true:false;} |
263 | | |
264 | | //This indicates that the pup routine should not call system objects' pups. |
265 | | void becomeUserlevel(void) {PUP_er_state|=IS_USERLEVEL;} |
266 | 0 | bool isUserlevel(void) const {return (PUP_er_state&IS_USERLEVEL)!=0?true:false;} |
267 | | |
268 | | bool isCheckpoint(void) const |
269 | 0 | { |
270 | 0 | return (PUP_er_state & IS_CHECKPOINT) != 0; |
271 | 0 | } |
272 | | |
273 | | bool isMigration(void) const |
274 | 175k | { |
275 | 175k | return (PUP_er_state & IS_MIGRATION) != 0; |
276 | 175k | } |
277 | | |
278 | 52.2M | bool hasComments(void) const {return (PUP_er_state&IS_COMMENTS)!=0?true:false;} |
279 | | |
280 | | //For single elements, pretend it's an array containing one element |
281 | | template<class T> |
282 | 150M | void operator()(T &v) {(*this)(&v,1);}Line | Count | Source | 282 | 2.93k | void operator()(T &v) {(*this)(&v,1);} |
Line | Count | Source | 282 | 21.6k | void operator()(T &v) {(*this)(&v,1);} |
Line | Count | Source | 282 | 2.94k | void operator()(T &v) {(*this)(&v,1);} |
Line | Count | Source | 282 | 1.00M | void operator()(T &v) {(*this)(&v,1);} |
Line | Count | Source | 282 | 7.01k | void operator()(T &v) {(*this)(&v,1);} |
Line | Count | Source | 282 | 90.2M | void operator()(T &v) {(*this)(&v,1);} |
Line | Count | Source | 282 | 9.40M | void operator()(T &v) {(*this)(&v,1);} |
Line | Count | Source | 282 | 1.10M | void operator()(T &v) {(*this)(&v,1);} |
Line | Count | Source | 282 | 25.3M | void operator()(T &v) {(*this)(&v,1);} |
Line | Count | Source | 282 | 44.6k | void operator()(T &v) {(*this)(&v,1);} |
Line | Count | Source | 282 | 23.6M | void operator()(T &v) {(*this)(&v,1);} |
|
283 | | |
284 | 300 | void operator()(void* &v,void* sig) {(*this)(&v,1,sig);} |
285 | | |
286 | | //For arrays: |
287 | | template<class T> |
288 | 187M | void operator()(T *a,size_t nItems) { |
289 | 187M | bytes((void *)a,nItems, sizeof(T), getXlateDataType(a)); |
290 | 187M | } Line | Count | Source | 288 | 2.93k | void operator()(T *a,size_t nItems) { | 289 | 2.93k | bytes((void *)a,nItems, sizeof(T), getXlateDataType(a)); | 290 | 2.93k | } |
Line | Count | Source | 288 | 21.6k | void operator()(T *a,size_t nItems) { | 289 | 21.6k | bytes((void *)a,nItems, sizeof(T), getXlateDataType(a)); | 290 | 21.6k | } |
Line | Count | Source | 288 | 4.02k | void operator()(T *a,size_t nItems) { | 289 | 4.02k | bytes((void *)a,nItems, sizeof(T), getXlateDataType(a)); | 290 | 4.02k | } |
Line | Count | Source | 288 | 1.00M | void operator()(T *a,size_t nItems) { | 289 | 1.00M | bytes((void *)a,nItems, sizeof(T), getXlateDataType(a)); | 290 | 1.00M | } |
Line | Count | Source | 288 | 34.7M | void operator()(T *a,size_t nItems) { | 289 | 34.7M | bytes((void *)a,nItems, sizeof(T), getXlateDataType(a)); | 290 | 34.7M | } |
Line | Count | Source | 288 | 92.4M | void operator()(T *a,size_t nItems) { | 289 | 92.4M | bytes((void *)a,nItems, sizeof(T), getXlateDataType(a)); | 290 | 92.4M | } |
Line | Count | Source | 288 | 9.45M | void operator()(T *a,size_t nItems) { | 289 | 9.45M | bytes((void *)a,nItems, sizeof(T), getXlateDataType(a)); | 290 | 9.45M | } |
Line | Count | Source | 288 | 1.10M | void operator()(T *a,size_t nItems) { | 289 | 1.10M | bytes((void *)a,nItems, sizeof(T), getXlateDataType(a)); | 290 | 1.10M | } |
Line | Count | Source | 288 | 25.4M | void operator()(T *a,size_t nItems) { | 289 | 25.4M | bytes((void *)a,nItems, sizeof(T), getXlateDataType(a)); | 290 | 25.4M | } |
Line | Count | Source | 288 | 80.8k | void operator()(T *a,size_t nItems) { | 289 | 80.8k | bytes((void *)a,nItems, sizeof(T), getXlateDataType(a)); | 290 | 80.8k | } |
Line | Count | Source | 288 | 23.6M | void operator()(T *a,size_t nItems) { | 289 | 23.6M | bytes((void *)a,nItems, sizeof(T), getXlateDataType(a)); | 290 | 23.6M | } |
|
291 | | |
292 | | /// For a buffer that lives on the device. `a` must already point at |
293 | | /// device memory in BOTH directions -- unlike pup_buffer, this does not |
294 | | /// allocate, so an unpacking chare allocates its device buffer first and |
295 | | /// then pups into it. |
296 | | template<class T> |
297 | | void operator()(T *a,size_t nItems,PUPMode mode) { |
298 | | if (mode == PUPMode::DEVICE) |
299 | | bytes_device((void *)a,nItems, sizeof(T), getXlateDataType(a)); |
300 | | else |
301 | | bytes((void *)a,nItems, sizeof(T), getXlateDataType(a)); |
302 | | } |
303 | | |
304 | | // Standard pup_buffer API that calls malloc for allocation on isUnpacking and free for deallocation on isPacking |
305 | | template<class T> |
306 | | void pup_buffer(T *&a, size_t nItems) { |
307 | | pup_buffer((void *&)a, nItems, sizeof(T), getXlateDataType(a)); |
308 | | } |
309 | | |
310 | | // Custom pup_buffer API that calls user provided 'allocate' function for allocation on isUnpacking and |
311 | | // user provided 'deallocate' function for deallocation on isPacking |
312 | | // Custom pup_buffer behaves same as the standard pup_buffer except for calling custom allocator and deallocator methods |
313 | | template<class T> |
314 | | void pup_buffer(T *&a, size_t nItems, std::function<void *(size_t)> allocate, std::function<void (void *)> deallocate) { |
315 | | pup_buffer((void *&)a, nItems, sizeof(T), getXlateDataType(a), allocate, deallocate); |
316 | | } |
317 | | |
318 | | //For pointers: the last parameter is to make it more difficult to call |
319 | | //(should not be used in normal code as pointers may loose meaning across processor) |
320 | 300 | void operator()(void **a,size_t nItems,void *pointerSignature) { |
321 | 300 | (void)pointerSignature; |
322 | 300 | bytes((void *)a,nItems,sizeof(void *),Tpointer); |
323 | 300 | } |
324 | | |
325 | | //For raw memory (n gives number of bytes) |
326 | | /* |
327 | | // pup void * is error-prune, let's avoid it - Gengbin |
328 | | void operator()(void *a,size_t nBytes) |
329 | | {bytes((void *)a,nBytes,1,Tbyte);} |
330 | | */ |
331 | | |
332 | | //For allocatable objects (system will new/delete object and call pup routine) |
333 | | void operator()(able** a) |
334 | | {object(a);} |
335 | | //For pre- or stack-allocated PUP::able objects-- just call object's pup |
336 | | void operator()(able& a); |
337 | | |
338 | | /// A descriptive (but entirely optional) human-readable comment field |
339 | | virtual void comment(const char *message); |
340 | | |
341 | | /// A 32-bit "synchronization marker" (not human readable). |
342 | | /// Some standard codes are listed under PUP::sync_.... |
343 | | virtual void synchronize(unsigned int sync); |
344 | | |
345 | | /// Insert a synchronization marker and comment into the stream. |
346 | | /// Only applies if this PUP::er wants comments. |
347 | 52.3M | inline void syncComment(unsigned int sync,const char *message=0) { |
348 | 52.3M | #if CMK_ERROR_CHECKING |
349 | 52.3M | if (hasComments()) { |
350 | 0 | synchronize(sync); |
351 | 0 | if (message) comment(message); |
352 | 0 | } |
353 | | #else |
354 | | /* empty, to avoid expensive virtual function calls */ |
355 | | #endif |
356 | 52.3M | } |
357 | | |
358 | | //Generic bottleneck: pack/unpack n items of size itemSize |
359 | | // and data type t from p. Desc describes the data item |
360 | | virtual void bytes(void *p,size_t n,size_t itemSize,dataType t) =0; |
361 | | /// Device-mode bottleneck. Deliberately a distinct name rather than an |
362 | | /// overload of bytes(): as an overload it would be hidden in every subclass |
363 | | /// that overrides the host bottleneck alone, which is all but three of them, |
364 | | /// and every compiler that warns about a partially overridden virtual would |
365 | | /// say so in user code that merely includes this header. |
366 | | /// |
367 | | /// Defaults to dropping the buffer, which is what every PUP::er without a |
368 | | /// device region should do: sizing for a checkpoint, writing to disk and |
369 | | /// converting to text all operate on host memory and cannot dereference `p` |
370 | | /// at all. Only the migration walkers override it. |
371 | 0 | virtual void bytes_device(void *p,size_t n,size_t itemSize,dataType t) {} |
372 | | virtual void object(able** a); |
373 | | |
374 | | virtual void pup_buffer(void *&p, size_t n, size_t itemSize, dataType t) = 0; |
375 | | virtual void pup_buffer(void *&p, size_t n, size_t itemSize, dataType t, std::function<void *(size_t)> allocate, std::function<void (void *)> deallocate) = 0; |
376 | | |
377 | 0 | virtual size_t size(void) const { return 0; } |
378 | | |
379 | | //For seeking (pack/unpack in different orders) |
380 | | virtual void impl_startSeek(seekBlock &s); /*Begin a seeking block*/ |
381 | | virtual size_t impl_tell(seekBlock &s); /*Give the current offset*/ |
382 | | virtual void impl_seek(seekBlock &s,size_t off); /*Seek to the given offset*/ |
383 | | virtual void impl_endSeek(seekBlock &s);/*End a seeking block*/ |
384 | | |
385 | | //See more documentation before PUP_cmiAllocSizer in pup_cmialloc.h |
386 | | //Must be a CmiAlloced buf while packing |
387 | 0 | virtual void pupCmiAllocBuf(void **msg) { |
388 | 0 | (void)msg; |
389 | 0 | CmiAbort("Undefined PUPer:Did you use PUP_toMem or PUP_fromMem?\n"); |
390 | 0 | } |
391 | | |
392 | | //In case source is not CmiAlloced the size can be passed and any |
393 | | //user buf can be converted into a cmialloc'ed buf |
394 | 0 | virtual void pupCmiAllocBuf(void **msg, size_t size) { |
395 | 0 | (void)msg; |
396 | 0 | (void)size; |
397 | 0 | CmiAbort("Undefined PUPer:Did you use PUP_toMem or PUP_fromMem?\n"); |
398 | 0 | } |
399 | | }; |
400 | | |
401 | | /** |
402 | | "Sync" codes are an extra channel to encode data in a pup stream, |
403 | | to indicate higher-order structures in the pup'd objects. |
404 | | Sync codes must follow this grammar: |
405 | | <obj> -> <obj> <obj> | <array> | <list> |
406 | | <obj> -> begin (<obj> system) <obj> end |
407 | | <array> -> begin <obj> (item <obj>)* end |
408 | | <list> -> begin <obj> (index <obj> item <obj>)* end |
409 | | This hack is used, e.g., by the debugger. |
410 | | */ |
411 | | enum { |
412 | | sync_builtin=0x70000000, // Built-in, standard sync codes begin here |
413 | | sync_begin=sync_builtin+0x01000000, // Sync code at start of collection |
414 | | sync_end=sync_builtin+0x02000000, // Sync code at end of collection |
415 | | sync_last_system=sync_builtin+0x09000000, // Sync code at end of "system" portion of object |
416 | | sync_array_m=0x00100000, // Linear-indexed (0..n) array-- use item to separate |
417 | | sync_list_m=0x00200000, // Some other collection-- use index and item |
418 | | sync_object_m=0x00300000, // Sync mask for general object |
419 | | |
420 | | sync_begin_array=sync_begin+sync_array_m, |
421 | | sync_begin_list=sync_begin+sync_list_m, |
422 | | sync_begin_object=sync_begin+sync_object_m, |
423 | | |
424 | | sync_end_array=sync_end+sync_array_m, |
425 | | sync_end_list=sync_end+sync_list_m, |
426 | | sync_end_object=sync_end+sync_object_m, |
427 | | |
428 | | sync_item=sync_builtin+0x00110000, // Sync code for a list or array item |
429 | | sync_index=sync_builtin+0x00120000, // Sync code for index of item in a list |
430 | | |
431 | | sync_last |
432 | | }; |
433 | | |
434 | | /************** PUP::er -- Sizer ******************/ |
435 | | //For finding the number of bytes to pack (e.g., to preallocate a memory buffer) |
436 | | class sizer : public er { |
437 | | protected: |
438 | | size_t nBytes; |
439 | | size_t gpuBytes; |
440 | | //Generic bottleneck: n items of size itemSize |
441 | | virtual void bytes(void *p,size_t n,size_t itemSize,dataType t); |
442 | | virtual void bytes_device(void *p,size_t n,size_t itemSize,dataType t); |
443 | | |
444 | | virtual void pup_buffer(void *&p, size_t n, size_t itemSize, dataType t); |
445 | | virtual void pup_buffer(void *&p, size_t n, size_t itemSize, dataType t, std::function<void *(size_t)> allocate, std::function<void (void *)> deallocate); |
446 | | |
447 | | public: |
448 | | //Write data to the given buffer |
449 | 11.4M | sizer(const unsigned int purpose = 0) : er(IS_SIZING | purpose), nBytes(0), |
450 | 11.4M | gpuBytes(0) |
451 | 11.4M | { |
452 | 11.4M | CmiAssert((purpose & TYPE_MASK) == 0); |
453 | 11.4M | } |
454 | | |
455 | | //Return the current number of bytes to be packed |
456 | 11.4M | size_t size(void) const {return nBytes;} |
457 | | |
458 | | //Return the number of DEVICE bytes to be packed, including the padding |
459 | | //DEVICE_PUP_ALIGN inserts between buffers, so it is directly comparable |
460 | | //with mem::gpu_size(). |
461 | | size_t gpu_size(void) const {return gpuBytes;} |
462 | | }; |
463 | | |
464 | | template <class T> |
465 | | inline size_t size(T &t) { |
466 | | PUP::sizer p; p|t; return p.size(); |
467 | | } |
468 | | |
469 | | /********** PUP::er -- Binary memory buffer pack/unpack *********/ |
470 | | class mem : public er { //Memory-buffer packers and unpackers |
471 | | protected: |
472 | | myByte *origBuf;//Start of memory buffer |
473 | | myByte *buf;//Memory buffer (stuff gets packed into/out of here) |
474 | | //Device-side counterparts, NULL unless this walker was given a device |
475 | | //region. A DEVICE-mode buffer pupped through a walker with no device region |
476 | | //is dropped, the same as for every other PUP::er. |
477 | | myByte *gpuOrigBuf; |
478 | | myByte *gpuBuf; |
479 | | mem(const unsigned int type, myByte* Nbuf, myByte* gpuNbuf, |
480 | | const unsigned int purpose = 0) |
481 | 22.5M | : er(type | purpose), origBuf(Nbuf), buf(Nbuf), |
482 | 22.5M | gpuOrigBuf(gpuNbuf), gpuBuf(gpuNbuf) |
483 | 22.5M | { |
484 | 22.5M | CmiAssert((purpose & TYPE_MASK) == 0); |
485 | 22.5M | } |
486 | | mem(const mem &p); //You don't want to copy |
487 | | void operator=(const mem &p); // You don't want to copy |
488 | | |
489 | | //For seeking (pack/unpack in different orders) |
490 | | virtual void impl_startSeek(seekBlock &s); /*Begin a seeking block*/ |
491 | | virtual size_t impl_tell(seekBlock &s); /*Give the current offset*/ |
492 | | virtual void impl_seek(seekBlock &s,size_t off); /*Seek to the given offset*/ |
493 | | public: |
494 | | //Return the current number of buffer bytes used |
495 | 16.5M | size_t size(void) const {return buf-origBuf;} |
496 | | |
497 | | //Device bytes consumed so far, including inter-buffer alignment padding, |
498 | | //so it is directly comparable with sizer::gpu_size(). |
499 | | size_t gpu_size(void) const { |
500 | | return (gpuOrigBuf == nullptr) ? 0 : (size_t)(gpuBuf - gpuOrigBuf); |
501 | | } |
502 | | |
503 | | inline char* get_current_pointer() const { |
504 | | return reinterpret_cast<char*>(buf); |
505 | | } |
506 | | |
507 | | inline char* get_orig_pointer() const { |
508 | | return reinterpret_cast<char*>(origBuf); |
509 | | } |
510 | | |
511 | | inline void reset() { |
512 | | buf = origBuf; |
513 | | } |
514 | | |
515 | | inline void advance(size_t const offset) { |
516 | | buf += offset; |
517 | | } |
518 | | }; |
519 | | |
520 | | //For packing into a preallocated, presized memory buffer |
521 | | class toMem : public mem { |
522 | | protected: |
523 | | //Generic bottleneck: pack n items of size itemSize from p. |
524 | | virtual void bytes(void *p,size_t n,size_t itemSize,dataType t); |
525 | | virtual void bytes_device(void *p,size_t n,size_t itemSize,dataType t); |
526 | | |
527 | | virtual void pup_buffer(void *&p, size_t n, size_t itemSize, dataType t); |
528 | | virtual void pup_buffer(void *&p, size_t n, size_t itemSize, dataType t, std::function<void *(size_t)> allocate, std::function<void (void *)> deallocate); |
529 | | |
530 | | public: |
531 | | //Write data to the given buffer, with an optional device region for |
532 | | //DEVICE-mode buffers. |
533 | | toMem(void* Nbuf, void* gpuNbuf, const unsigned int purpose = 0) |
534 | | : mem(IS_PACKING, (myByte*)Nbuf, (myByte*)gpuNbuf, purpose) |
535 | | { |
536 | | } |
537 | | toMem(void* Nbuf, const unsigned int purpose = 0) |
538 | 9.20M | : mem(IS_PACKING, (myByte*)Nbuf, nullptr, purpose) |
539 | 9.20M | { |
540 | 9.20M | } |
541 | | }; |
542 | | template <class T> |
543 | | inline void toMemBuf(T &t,void *buf, size_t len) { |
544 | | PUP::toMem p(buf); |
545 | | p|t; |
546 | | if (p.size()!=len) CmiAbort("Size mismatch during PUP::toMemBuf!\n" |
547 | | "This means your pup routine doesn't match during sizing and packing"); |
548 | | } |
549 | | |
550 | | //For unpacking from a memory buffer |
551 | | class fromMem : public mem { |
552 | | protected: |
553 | | //Generic bottleneck: unpack n items of size itemSize from p. |
554 | | virtual void bytes(void *p,size_t n,size_t itemSize,dataType t); |
555 | | virtual void bytes_device(void *p,size_t n,size_t itemSize,dataType t); |
556 | | |
557 | | virtual void pup_buffer(void *&p, size_t n, size_t itemSize, dataType t); |
558 | | virtual void pup_buffer(void *&p, size_t n, size_t itemSize, dataType t, std::function<void *(size_t)> allocate, std::function<void (void *)> deallocate); |
559 | | |
560 | | void pup_buffer_generic(void *&p,size_t n, size_t itemSize, dataType t, std::function<void *(size_t)> allocate, bool isMalloc); |
561 | | |
562 | | public: |
563 | | //Read data from the given buffer, with an optional device region holding |
564 | | //the DEVICE-mode buffers the sender packed. |
565 | | fromMem(const void* Nbuf, const void* gpuNbuf, const unsigned int purpose = 0) |
566 | | : mem(IS_UNPACKING, (myByte*)Nbuf, (myByte*)gpuNbuf, purpose) |
567 | | { |
568 | | } |
569 | | fromMem(const void* Nbuf, const unsigned int purpose = 0) |
570 | 12.8M | : mem(IS_UNPACKING, (myByte*)Nbuf, nullptr, purpose) |
571 | 12.8M | { |
572 | 12.8M | } |
573 | | }; |
574 | | template <class T> |
575 | | inline void fromMemBuf(T &t,void *buf,size_t len) { |
576 | | PUP::fromMem p(buf); |
577 | | p|t; |
578 | | if (p.size()!=len) CmiAbort("Size mismatch during PUP::fromMemBuf!\n" |
579 | | "This means your pup routine doesn't match during packing and unpacking"); |
580 | | } |
581 | | |
582 | | /********** PUP::er -- Binary disk file pack/unpack *********/ |
583 | | class disk : public er { |
584 | | protected: |
585 | | FILE *F;//Disk file to read from/write to |
586 | | disk(const unsigned int type, FILE* f, const unsigned int purpose = 0) |
587 | | : er(type | purpose), F(f) |
588 | | { |
589 | | CmiAssert((purpose & TYPE_MASK) == 0); |
590 | | } |
591 | | |
592 | | disk(const disk &p); //You don't want to copy |
593 | | void operator=(const disk &p); // You don't want to copy |
594 | | |
595 | | //For seeking (pack/unpack in different orders) |
596 | | virtual void impl_startSeek(seekBlock &s); /*Begin a seeking block*/ |
597 | | virtual size_t impl_tell(seekBlock &s); /*Give the current offset*/ |
598 | | virtual void impl_seek(seekBlock &s,size_t off); /*Seek to the given offset*/ |
599 | | }; |
600 | | |
601 | | //For packing to a disk file |
602 | | class toDisk : public disk { |
603 | | protected: |
604 | | //Generic bottleneck: pack n items of size itemSize from p. |
605 | | virtual void bytes(void *p,size_t n,size_t itemSize,dataType t); |
606 | | |
607 | | virtual void pup_buffer(void *&p, size_t n, size_t itemSize, dataType t); |
608 | | virtual void pup_buffer(void *&p, size_t n, size_t itemSize, dataType t, std::function<void *(size_t)> allocate, std::function<void (void *)> deallocate); |
609 | | |
610 | | bool error; |
611 | | public: |
612 | | // Write data to the given file pointer |
613 | | // (must be opened for binary write) |
614 | | // You must close the file yourself when done. |
615 | | toDisk(FILE* f, const unsigned int purpose = 0) : disk(IS_PACKING, f, purpose) |
616 | | { |
617 | | error = false; |
618 | | } |
619 | | bool checkError(){return error;} |
620 | | }; |
621 | | |
622 | | //For unpacking from a disk file |
623 | | class fromDisk : public disk { |
624 | | protected: |
625 | | //Generic bottleneck: unpack n items of size itemSize from p. |
626 | | virtual void bytes(void *p,size_t n,size_t itemSize,dataType t); |
627 | | |
628 | | virtual void pup_buffer(void *&p, size_t n, size_t itemSize, dataType t); |
629 | | virtual void pup_buffer(void *&p, size_t n, size_t itemSize, dataType t, std::function<void *(size_t)> allocate, std::function<void (void *)> deallocate); |
630 | | |
631 | | public: |
632 | | // Read data from the given file pointer |
633 | | // (must be opened for binary read) |
634 | | // You must close the file yourself when done. |
635 | | fromDisk(FILE* f, const unsigned int purpose = 0) : disk(IS_UNPACKING, f, purpose) {} |
636 | | }; |
637 | | |
638 | | /************** PUP::er -- Text *****************/ |
639 | | class toTextUtil : public er { |
640 | | private: |
641 | | char *cur; /*Current output buffer*/ |
642 | | size_t maxCount; /*Max length of cur buffer*/ |
643 | | int level; /*Indentation distance*/ |
644 | | void beginEnv(const char *type,int n=0); |
645 | | void endEnv(const char *type); |
646 | | char *beginLine(void); |
647 | | void endLine(void); |
648 | | protected: |
649 | | virtual char *advance(char *cur)=0; /*Consume current buffer and return next*/ |
650 | | toTextUtil(unsigned int inType,char *buf,size_t len); |
651 | | toTextUtil(const toTextUtil &p); //You don't want to copy |
652 | | void operator=(const toTextUtil &p); // You don't want to copy |
653 | | public: |
654 | | virtual void comment(const char *message); |
655 | | virtual void synchronize(unsigned int m); |
656 | | protected: |
657 | | virtual void bytes(void *p,size_t n,size_t itemSize,dataType t); |
658 | | |
659 | | virtual void pup_buffer(void *&p, size_t n, size_t itemSize, dataType t); |
660 | | virtual void pup_buffer(void *&p, size_t n, size_t itemSize, dataType t, std::function<void *(size_t)> allocate, std::function<void (void *)> deallocate); |
661 | | |
662 | | virtual void object(able** a); |
663 | | }; |
664 | | /* Return the number of characters, including terminating NULL */ |
665 | | class sizerText : public toTextUtil { |
666 | | public: |
667 | | static constexpr int lineLen = 1000; |
668 | | private: |
669 | | char line[lineLen]; |
670 | | size_t charCount; /*Total characters seen so far (not including NULL) */ |
671 | | protected: |
672 | | virtual char *advance(char *cur); |
673 | | public: |
674 | | sizerText(void); |
675 | 0 | size_t size(void) const {return charCount+1; /*add NULL*/ } |
676 | | }; |
677 | | |
678 | | /* Copy data to this C string, including terminating NULL. */ |
679 | | class toText : public toTextUtil { |
680 | | private: |
681 | | char *buf; |
682 | | size_t charCount; /*Total characters written so far (not including NULL) */ |
683 | | size_t maxCount; /*Max length of buf*/ |
684 | | protected: |
685 | | virtual char *advance(char *cur); |
686 | | public: |
687 | | toText(char *outStr, size_t len); |
688 | | toText(const toText &p); //You don't want to copy |
689 | | void operator=(const toText &p); // You don't want to copy |
690 | 0 | size_t size(void) const {return charCount+1; /*add NULL*/ } |
691 | | }; |
692 | | |
693 | | class toTextFile : public er { |
694 | | protected: |
695 | | FILE *f; |
696 | | virtual void bytes(void *p,size_t n,size_t itemSize,dataType t); |
697 | | |
698 | | virtual void pup_buffer(void *&p, size_t n, size_t itemSize, dataType t); |
699 | | virtual void pup_buffer(void *&p, size_t n, size_t itemSize, dataType t, std::function<void *(size_t)> allocate, std::function<void (void *)> deallocate); |
700 | | |
701 | | public: |
702 | | //Begin writing to this file, which should be opened for ascii write. |
703 | | // You must close the file yourself when done. |
704 | | toTextFile(FILE *f_) :er(IS_PACKING), f(f_) {} |
705 | | toTextFile(const toTextFile &p); //You don't want to copy |
706 | | void operator=(const toTextFile &p); // You don't want to copy |
707 | | virtual void comment(const char *message); |
708 | | }; |
709 | | class fromTextFile : public er { |
710 | | protected: |
711 | | FILE *f; |
712 | | int readInt(const char *fmt="%d"); |
713 | | unsigned int readUint(const char *fmt="%u"); |
714 | | CMK_TYPEDEF_INT8 readLongInt(const char *fmt="%lld"); |
715 | | double readDouble(void); |
716 | | |
717 | | virtual void bytes(void *p,size_t n,size_t itemSize,dataType t); |
718 | | |
719 | | virtual void pup_buffer(void *&p, size_t n, size_t itemSize, dataType t); |
720 | | virtual void pup_buffer(void *&p, size_t n, size_t itemSize, dataType t, std::function<void *(size_t)> allocate, std::function<void (void *)> deallocate); |
721 | | |
722 | | virtual void parseError(const char *what); |
723 | | public: |
724 | | //Begin writing to this file, which should be opened for ascii read. |
725 | | // You must close the file yourself when done. |
726 | | fromTextFile(FILE *f_) :er(IS_UNPACKING), f(f_) {} |
727 | | fromTextFile(const fromTextFile &p); //You don't want to copy |
728 | | void operator=(const fromTextFile &p); // You don't want to copy |
729 | | virtual void comment(const char *message); |
730 | | }; |
731 | | |
732 | | /********** PUP::er -- Heterogenous machine pack/unpack *********/ |
733 | | //This object describes the data representation of a machine. |
734 | | class machineInfo { |
735 | | public: |
736 | | typedef unsigned char myByte; |
737 | | myByte magic[4];//Magic number (to identify machineInfo structs) |
738 | | myByte version;//0-- current version |
739 | | |
740 | | myByte intBytes[5]; //<- sizeof(char,short,int,long,int128) |
741 | | myByte intFormat;//0-- big endian. 1-- little endian. |
742 | | |
743 | | myByte floatBytes; //<- sizeof(...) |
744 | | myByte doubleBytes; |
745 | | myByte floatFormat;//0-- big endian IEEE. 1-- little endian IEEE. |
746 | | |
747 | | myByte boolBytes; |
748 | | myByte pointerBytes; |
749 | | |
750 | | // myByte padding[1];//Padding to 16 bytes |
751 | | |
752 | | //Return true if our magic number is valid. |
753 | | bool valid(void) const; |
754 | | //Return true if we differ from the current (running) machine. |
755 | | bool needsConversion(void) const; |
756 | | |
757 | | //Get a machineInfo for the current machine |
758 | | static const machineInfo ¤t(void); |
759 | | |
760 | | void pup(er &p) { |
761 | | myByte padding; |
762 | | |
763 | | p(magic, 4); |
764 | | p(version); |
765 | | if (version == 0) p(intBytes, 4); |
766 | | else p(intBytes, 5); |
767 | | p(intFormat); |
768 | | p(floatBytes); p(doubleBytes); p(floatFormat); |
769 | | p(boolBytes); p(pointerBytes); |
770 | | if (version == 0) p(padding); |
771 | | } |
772 | | }; |
773 | | |
774 | | /// "Wrapped" PUP::er: forwards requests to another PUP::er. |
775 | | class wrap_er : public er { |
776 | | protected: |
777 | | er &p; |
778 | | public: |
779 | 16 | wrap_er(er &p_,unsigned int newFlags=0) :er(p_.getStateFlags()|newFlags), p(p_) {} |
780 | 0 | virtual size_t size(void) const { return p.size(); } |
781 | | |
782 | | virtual void impl_startSeek(seekBlock &s); /*Begin a seeking block*/ |
783 | | virtual size_t impl_tell(seekBlock &s); /*Give the current offset*/ |
784 | | virtual void impl_seek(seekBlock &s,size_t off); /*Seek to the given offset*/ |
785 | | virtual void impl_endSeek(seekBlock &s);/*End a seeking block*/ |
786 | | }; |
787 | | |
788 | | //For translating some odd disk/memory representation into the |
789 | | // current machine representation. (We really only need to |
790 | | // translate during unpack-- "reader makes right".) |
791 | | class xlater : public wrap_er { |
792 | | protected: |
793 | | typedef void (*dataConverterFn)(int N,const myByte *in,myByte *out,size_t nElem); |
794 | | |
795 | | //This table is indexed by dataType, and contains an appropriate |
796 | | // conversion function to unpack a n-item array of the corresponding |
797 | | // data type (possibly in-place). |
798 | | dataConverterFn convertFn[dataType_last]; |
799 | | //Maps dataType to source machine's dataSize |
800 | | size_t convertSize[dataType_last]; |
801 | | void setConverterInt(const machineInfo &m,const machineInfo &cur, |
802 | | int isUnsigned,int intType,dataType dest); |
803 | | |
804 | | //Generic bottleneck: unpack n items of size itemSize from p. |
805 | | virtual void bytes(void *p,size_t n,size_t itemSize,dataType t); |
806 | | |
807 | | virtual void pup_buffer(void *&p, size_t n, size_t itemSize, dataType t); |
808 | | virtual void pup_buffer(void *&p, size_t n, size_t itemSize, dataType t, std::function<void *(size_t)> allocate, std::function<void (void *)> deallocate); |
809 | | |
810 | | public: |
811 | | xlater(const machineInfo &fromMachine, er &fromData); |
812 | | }; |
813 | | |
814 | | /*************** PUP::able support ***************/ |
815 | | //The base class of system-allocatable objects with pup routines |
816 | | class able { |
817 | | public: |
818 | | //A globally-unique, persistent identifier for an allocatable object |
819 | | class PUP_ID { |
820 | | public: |
821 | | enum {len=8}; |
822 | | unsigned char hash[len]; |
823 | 219k | PUP_ID() {} |
824 | 8.46k | PUP_ID(int val) {for (int i=0;i<len;i++) hash[i]=val;} |
825 | 10.1k | PUP_ID(const char *name) {setName(name);} |
826 | | void setName(const char *name);//Write name into hash |
827 | 129k | bool operator==(const PUP_ID &other) const { |
828 | 129k | for (int i=0;i<len;i++) |
829 | 129k | if (hash[i]!=other.hash[i]) |
830 | 129k | return false; |
831 | 137 | return true; |
832 | 129k | } |
833 | 128k | void pup(er &p) { |
834 | 128k | p((char *)hash,sizeof(unsigned char)*len); |
835 | 128k | } |
836 | 99.2k | void pup(er &p) const { |
837 | 99.2k | p((char *)hash,sizeof(unsigned char)*len); |
838 | 99.2k | } |
839 | | }; |
840 | | |
841 | | protected: |
842 | | able() {} |
843 | | able(CkMigrateMessage *) {} |
844 | | |
845 | | public: |
846 | | virtual ~able();//Virtual destructor may be needed by some child |
847 | | |
848 | | //Constructor function registration: |
849 | | typedef able* (*constructor_function)(void); |
850 | | static PUP_ID register_constructor(const char *className, |
851 | | constructor_function fn); |
852 | | static constructor_function get_constructor(const PUP_ID &id); |
853 | | virtual /*PUP::*/able *clone(void) const; |
854 | | |
855 | | //Target methods: |
856 | | virtual void pup(er &p); |
857 | | virtual const PUP_ID &get_PUP_ID(void) const=0; |
858 | | }; |
859 | | |
860 | | template <typename T, bool PUPable = std::is_base_of<PUP::able, T>::value> |
861 | | struct ptr_helper; |
862 | | |
863 | | #define SINGLE_ARG(...) __VA_ARGS__ |
864 | | |
865 | | //Declarations which create routines implemeting the | operator. |
866 | | // Macros to be used inside a class body. |
867 | | #define PUPable_operator_inside(className)\ |
868 | | friend inline void operator|(PUP::er &p,className &a) {a.pup(p);}\ |
869 | | friend inline void operator|(PUP::er &p,className* &a) {\ |
870 | | PUP::able *pa=a; p(&pa); a=dynamic_cast<className *>(pa);\ |
871 | | } |
872 | | |
873 | | // Macros to be used outside a class body. |
874 | | #define PUPable_operator_outside(className)\ |
875 | | inline void operator|(PUP::er &p,className &a) {a.pup(p);}\ |
876 | | inline void operator|(PUP::er &p,className* &a) {\ |
877 | | PUP::able *pa=a; p(&pa); a=dynamic_cast<className *>(pa);\ |
878 | | } |
879 | | |
880 | | //Declarations to include in a PUP::able's body. |
881 | | // Convenient, but only usable if class is not inside a namespace. |
882 | | #define PUPable_decl(className) \ |
883 | | PUPable_decl_inside(className) \ |
884 | | PUPable_operator_inside(className) |
885 | | |
886 | | #define PUPable_decl_template(className) \ |
887 | | PUPable_decl_inside_template(SINGLE_ARG(className)) \ |
888 | | PUPable_operator_inside(SINGLE_ARG(className)) |
889 | | |
890 | | //PUPable_decl for classes inside a namespace: inside body |
891 | | #define PUPable_decl_inside(className) \ |
892 | | private: \ |
893 | | static PUP::able *call_PUP_constructor(void); \ |
894 | | static PUP::able::PUP_ID my_PUP_ID;\ |
895 | | public:\ |
896 | | virtual const PUP::able::PUP_ID &get_PUP_ID(void) const; \ |
897 | | static void register_PUP_ID(const char* name); |
898 | | |
899 | | #define PUPable_decl_base_template(baseClassName, className) \ |
900 | | PUPable_decl_inside_base_template(SINGLE_ARG(baseClassName), \ |
901 | | SINGLE_ARG(className)) \ |
902 | | PUPable_operator_inside(SINGLE_ARG(className)) |
903 | | |
904 | | #define PUPable_decl_inside_template(className) \ |
905 | | private: \ |
906 | | static PUP::able* call_PUP_constructor(void) { \ |
907 | | className* pupobj = new className((CkMigrateMessage*)0); \ |
908 | | _MEMCHECK(pupobj); \ |
909 | | return pupobj; } \ |
910 | | static PUP::able::PUP_ID my_PUP_ID;\ |
911 | | public: \ |
912 | | virtual const PUP::able::PUP_ID &get_PUP_ID(void) const { \ |
913 | | return my_PUP_ID; } \ |
914 | | static void register_PUP_ID(const char* name) { \ |
915 | | my_PUP_ID=register_constructor(name,call_PUP_constructor);} |
916 | | |
917 | | #define PUPable_decl_inside_base_template(baseClassName, className) \ |
918 | | private: \ |
919 | | static PUP::able *call_PUP_constructor(void) { \ |
920 | | className* pupobj = new className((CkMigrateMessage*)0); \ |
921 | | _MEMCHECK(pupobj); \ |
922 | | return pupobj; \ |
923 | | } \ |
924 | | static PUP::able::PUP_ID my_PUP_ID; \ |
925 | | \ |
926 | | public: \ |
927 | | virtual const PUP::able::PUP_ID &get_PUP_ID(void) const { \ |
928 | | return my_PUP_ID; \ |
929 | | } \ |
930 | | static void register_PUP_ID(const char *name) { \ |
931 | | my_PUP_ID = \ |
932 | | baseClassName::register_constructor(name, call_PUP_constructor); \ |
933 | | } |
934 | | |
935 | | //PUPable_decl for classes inside a namespace: in header at file scope |
936 | | #define PUPable_decl_outside(className) \ |
937 | | PUPable_operator_outside(className) |
938 | | |
939 | | //PUPable_decl for classes inside a namespace: in header at file scope |
940 | | #define PUPable_decl_outside_template(templateParameters,className) \ |
941 | | template<templateParameters> inline void operator|(PUP::er &p,className &a) {a.pup(p);} \ |
942 | | template<templateParameters> inline void operator|(PUP::er &p,className* &a) { \ |
943 | | PUP::able *pa=a; p(&pa); a=(className *)pa; } \ |
944 | | template<templateParameters> PUP::able *className::call_PUP_constructor(void) { \ |
945 | | className* pupobj = new className((CkMigrateMessage*)0); \ |
946 | | _MEMCHECK(pupobj); \ |
947 | | return pupobj; } \ |
948 | | template<templateParameters> const PUP::able::PUP_ID &className::get_PUP_ID(void) const { \ |
949 | | return className::my_PUP_ID; } \ |
950 | | template<templateParameters> void className::register_PUP_ID(const char* name) { \ |
951 | | my_PUP_ID=register_constructor(name,className::call_PUP_constructor);} |
952 | | |
953 | | |
954 | | //Declarations to include in an abstract PUP::able's body. |
955 | | // Abstract PUP::ables do not need def or reg. |
956 | | #define PUPable_abstract(className) \ |
957 | | public:\ |
958 | | virtual const PUP::able::PUP_ID &get_PUP_ID(void) const =0; \ |
959 | | PUPable_operator_inside(className) |
960 | | |
961 | | //Definitions to include exactly once at file scope |
962 | | #define PUPable_def(className) \ |
963 | | PUP::able *className::call_PUP_constructor(void) { \ |
964 | | className* pupobj = new className((CkMigrateMessage*)0); \ |
965 | | _MEMCHECK(pupobj); \ |
966 | | return pupobj; } \ |
967 | | const PUP::able::PUP_ID &className::get_PUP_ID(void) const\ |
968 | | { return className::my_PUP_ID; }\ |
969 | | PUP::able::PUP_ID className::my_PUP_ID;\ |
970 | | void className::register_PUP_ID(const char* name)\ |
971 | | {my_PUP_ID=register_constructor(name,\ |
972 | | className::call_PUP_constructor);} |
973 | | |
974 | | //Definitions to include exactly once at file scope |
975 | | #define PUPable_def_template(className) \ |
976 | | template<> PUP::able::PUP_ID className::my_PUP_ID = 0; |
977 | | |
978 | | //Definitions to include exactly once at file scope |
979 | | #define PUPable_def_template_nonInst(className) \ |
980 | | PUP::able::PUP_ID className::my_PUP_ID = 0; |
981 | | |
982 | | //Code to execute exactly once at program start time |
983 | | #define PUPable_reg(className) \ |
984 | | className::register_PUP_ID(#className); |
985 | | #define PUPable_reg2(classIdentifier,className) \ |
986 | | classIdentifier::register_PUP_ID(className); |
987 | | |
988 | | inline void operator|(er &p,able &a) {a.pup(p);} |
989 | | inline void operator|(er &p,able* &a) {p(&a);} |
990 | | } //<- End namespace PUP |
991 | | |
992 | | |
993 | | //Holds a pointer to a (possibly dynamically allocated) PUP::able. |
994 | | // Extracting the pointer hands the deletion responsibility over. |
995 | | // This is used by parameter marshalling, which doesn't work well |
996 | | // with bare pointers. |
997 | | // CkPointer<T> t is the parameter-marshalling equivalent of T *t |
998 | | template <class T> |
999 | | class CkPointer { |
1000 | | T *allocated; //Pointer that PUP dynamically allocated for us (recv only) |
1001 | | T *ptr; //Read-only pointer |
1002 | | |
1003 | | CkPointer(const CkPointer<T> &) = delete; |
1004 | | void operator=(const CkPointer<T> &) = delete; |
1005 | | void operator=(CkPointer<T> &&) = delete; |
1006 | | protected: |
1007 | | T *peek(void) {return ptr;} |
1008 | | CkPointer(T *src, T *alloc): allocated(alloc), ptr(src) {} |
1009 | | public: |
1010 | | /// Used on the send side, and does *not* delete the object. |
1011 | | CkPointer(T *src): CkPointer(src, nullptr) {} |
1012 | | /// Begin completely empty: used on marshalling recv side. |
1013 | | CkPointer(void): CkPointer(nullptr, nullptr) {} |
1014 | | /// Move constructor (copy src fields, then invalidate) |
1015 | | CkPointer(CkPointer<T> &&src): CkPointer(src.ptr, src.allocated) |
1016 | | { src.allocated = src.ptr = nullptr; } |
1017 | | |
1018 | | ~CkPointer() { if (allocated) delete allocated; } |
1019 | | |
1020 | | /// Extract the object held by this class. |
1021 | | /// Deleting the pointer is now the user's responsibility |
1022 | | inline operator T* () { allocated=0; return ptr; } |
1023 | | |
1024 | | inline void pup(PUP::er &p) { |
1025 | | PUP::ptr_helper<T>()(p, ptr); |
1026 | | if (p.isUnpacking()) { |
1027 | | allocated = ptr; |
1028 | | } |
1029 | | } |
1030 | | friend inline void operator|(PUP::er &p,CkPointer<T> &v) {v.pup(p);} |
1031 | | }; |
1032 | | #define PUPable_marshall CkPointer |
1033 | | |
1034 | | //Like CkPointer, but keeps deletion responsibility forever. |
1035 | | // CkReference<T> t is the parameter-marshalling equivalent of T &t |
1036 | | template<class T> |
1037 | | class CkReference : private CkPointer<T> { |
1038 | | public: |
1039 | | /// Used on the send side, and does *not* delete the object. |
1040 | | CkReference(T &src) ///< Marshall this object. |
1041 | | :CkPointer<T>(&src) { } |
1042 | | |
1043 | | /// Begin completely empty: used on the recv side. |
1044 | | CkReference(void) {} |
1045 | | |
1046 | | /// Look at the object held by this class. Does *not* hand over |
1047 | | /// deletion responsiblity. |
1048 | | inline operator T& () { return *this->peek(); } |
1049 | | |
1050 | | inline void pup(PUP::er &p) {CkPointer<T>::pup(p);} |
1051 | | |
1052 | | friend inline void operator|(PUP::er &p,CkReference<T> &v) {v.pup(p);} |
1053 | | }; |
1054 | | |
1055 | | // For people that forget the "::" |
1056 | | typedef PUP::er PUPer; |
1057 | | typedef PUP::able PUPable; |
1058 | | |
1059 | | /******** PUP via pipe: another way to access PUP::ers ***** |
1060 | | The parameter marshalling system pups each variable v using just: |
1061 | | p|v; |
1062 | | Thus we need a "void operator|(PUP::er &p,T &v)" for all types |
1063 | | that work with parameter marshalling. |
1064 | | */ |
1065 | | |
1066 | | namespace PUP { |
1067 | | /** |
1068 | | Traits class: decide if the type T can be safely |
1069 | | pupped as raw bytes. This is true of classes that |
1070 | | do not contain pointers and do not need pup routines. |
1071 | | Use this like: |
1072 | | if (PUP::as_bytes<someClass>::value) { ... } |
1073 | | */ |
1074 | | template<class T> class as_bytes { |
1075 | | /* default is to not pack as bytes by default */ |
1076 | | public: enum {value=0}; |
1077 | | }; |
1078 | | |
1079 | | |
1080 | | // Defines is_pupable to allow enums to be pupped in pup_stl.h |
1081 | | namespace details { |
1082 | | |
1083 | | template <typename... Ts> |
1084 | | struct make_void { |
1085 | | typedef void type; |
1086 | | }; |
1087 | | template <typename... Ts> |
1088 | | using void_t = typename make_void<Ts...>::type; |
1089 | | |
1090 | | template <typename T, typename U = void> |
1091 | | struct is_pupable : std::false_type {}; |
1092 | | |
1093 | | template <typename T> |
1094 | | struct is_pupable< |
1095 | | T, void_t<decltype(std::declval<T>().pup(std::declval<PUP::er &>()))>> |
1096 | | : std::true_type {}; |
1097 | | |
1098 | | } |
1099 | | |
1100 | | /** |
1101 | | Default operator|: call pup routine (as long as T has a pup function). |
1102 | | */ |
1103 | | template <class T, typename std::enable_if<details::is_pupable<T>::value, int>::type = 0> |
1104 | 2.68k | inline void operator|(PUP::er &p, T &t) { |
1105 | 2.68k | p.syncComment(PUP::sync_begin_object); |
1106 | 2.68k | t.pup(p); |
1107 | 2.68k | p.syncComment(PUP::sync_end_object); |
1108 | 2.68k | } |
1109 | | |
1110 | | /** |
1111 | | Default PUParray: pup each element. |
1112 | | */ |
1113 | | template<class T> |
1114 | | inline void PUParray(PUP::er &p,T *t,size_t n) { |
1115 | | p.syncComment(PUP::sync_begin_array); |
1116 | | for (size_t i=0;i<n;i++) { |
1117 | | p.syncComment(PUP::sync_item); |
1118 | | p|t[i]; |
1119 | | } |
1120 | | p.syncComment(PUP::sync_end_array); |
1121 | | } |
1122 | | |
1123 | | /// Copy this type as raw memory (like memcpy). |
1124 | | #define PUPbytes(type) \ |
1125 | | namespace PUP { inline void operator|(PUP::er &p,type &t) {p((char *)&t,sizeof(type));} } \ |
1126 | | namespace PUP { inline void PUParray(PUP::er &p,type *ta,size_t n) { p((char *)ta,n*sizeof(type)); } } \ |
1127 | | namespace PUP { template<> class as_bytes<type> { \ |
1128 | | public: enum {value=1}; \ |
1129 | | }; } |
1130 | | |
1131 | | /// Make PUP work with this function pointer type, copied as raw bytes. |
1132 | | #define PUPfunctionpointer(fnPtrType) \ |
1133 | | inline void operator|(PUP::er &p,fnPtrType &t) {p((char *)&t,sizeof(fnPtrType));} |
1134 | | |
1135 | | /// Make PUP work with this enum type, copied as an "int". |
1136 | | #define PUPenum(enumType) \ |
1137 | | inline void operator|(PUP::er &p,enumType &e) { int v=e; p|v; e=v; } |
1138 | | |
1139 | | } |
1140 | | |
1141 | | /** |
1142 | | For all builtin types, like "int", |
1143 | | operator| and PUParray use p(t) and p(ta,n). |
1144 | | */ |
1145 | | #define PUP_BUILTIN_SUPPORT(type) \ |
1146 | 131M | namespace PUP { inline void operator|(er &p,type &t) {p(t);} } \Line | Count | Source | 1146 | 77.3M | namespace PUP { inline void operator|(er &p,type &t) {p(t);} } \ |
Line | Count | Source | 1146 | 9.40M | namespace PUP { inline void operator|(er &p,type &t) {p(t);} } \ |
Line | Count | Source | 1146 | 24.4M | namespace PUP { inline void operator|(er &p,type &t) {p(t);} } \ |
Line | Count | Source | 1146 | 20.4M | namespace PUP { inline void operator|(er &p,type &t) {p(t);} } \ |
|
1147 | 2.46M | namespace PUP { inline void PUParray(er &p,type *ta,size_t n) { p(ta,n); } } \ |
1148 | | namespace PUP { template<> class as_bytes<type> { \ |
1149 | | public: enum {value=1}; \ |
1150 | | }; } |
1151 | | PUP_BUILTIN_SUPPORT(signed char) |
1152 | | #if CMK_SIGNEDCHAR_DIFF_CHAR |
1153 | | PUP_BUILTIN_SUPPORT(char) |
1154 | | #endif |
1155 | | PUP_BUILTIN_SUPPORT(unsigned char) |
1156 | | PUP_BUILTIN_SUPPORT(short) |
1157 | | PUP_BUILTIN_SUPPORT(int) |
1158 | | PUP_BUILTIN_SUPPORT(long) |
1159 | | PUP_BUILTIN_SUPPORT(unsigned short) |
1160 | | PUP_BUILTIN_SUPPORT(unsigned int) |
1161 | | PUP_BUILTIN_SUPPORT(unsigned long) |
1162 | | PUP_BUILTIN_SUPPORT(float) |
1163 | | PUP_BUILTIN_SUPPORT(double) |
1164 | | PUP_BUILTIN_SUPPORT(bool) |
1165 | | #if CMK_LONG_DOUBLE_DEFINED |
1166 | | PUP_BUILTIN_SUPPORT(long double) |
1167 | | #endif |
1168 | | #ifdef CMK_PUP_LONG_LONG |
1169 | | PUP_BUILTIN_SUPPORT(CMK_PUP_LONG_LONG) |
1170 | | PUP_BUILTIN_SUPPORT(unsigned CMK_PUP_LONG_LONG) |
1171 | | #endif |
1172 | | #if CMK_HAS_INT16 |
1173 | | PUP_BUILTIN_SUPPORT(CmiInt16) |
1174 | | PUP_BUILTIN_SUPPORT(CmiUInt16) |
1175 | | #endif |
1176 | | |
1177 | | |
1178 | | //This macro is useful in simple pup routines: |
1179 | | // It's just p|x, but it also documents the *name* of the variable. |
1180 | | // You must have a PUP::er named p. |
1181 | | #define PUPn(field) \ |
1182 | | do{ if (p.hasComments()) p.comment(#field); p|field; } while(0) |
1183 | | |
1184 | | //Like PUPn(x), above, but for arrays. |
1185 | | #define PUPv(field,len) \ |
1186 | | do{ if (p.hasComments()) p.comment(#field); PUParray(p,field,len); } while(0) |
1187 | | |
1188 | | namespace PUP { |
1189 | | template <typename T> |
1190 | | struct ptr_helper<T, true> { |
1191 | | inline void operator()(PUP::er &p, T *&t) const { |
1192 | | bool is_nullptr = nullptr == t; |
1193 | | p | is_nullptr; |
1194 | | if (!is_nullptr) { |
1195 | | PUP::able *t_able = static_cast<PUP::able *>(t); |
1196 | | p | t_able; |
1197 | | if (p.isUnpacking()) t = static_cast<T *>(t_able); |
1198 | | } |
1199 | | } |
1200 | | }; |
1201 | | |
1202 | | template <typename T> |
1203 | | struct ptr_helper<T, false> { |
1204 | | inline void operator()(PUP::er &p, T *&t) const { |
1205 | | bool is_nullptr = nullptr == t; |
1206 | | p | is_nullptr; |
1207 | | if (!is_nullptr) { |
1208 | | if (p.isUnpacking()) { |
1209 | | initialize_ptr(t); |
1210 | | } |
1211 | | p | *t; |
1212 | | } |
1213 | | } |
1214 | | |
1215 | | protected: |
1216 | | template <typename U> |
1217 | | inline typename std::enable_if<std::is_constructible<U, reconstruct>::value>::type |
1218 | | initialize_ptr(U *&u) const { |
1219 | | u = new U(reconstruct()); |
1220 | | } |
1221 | | |
1222 | | template <typename U> |
1223 | | inline typename std::enable_if<!std::is_constructible<U, reconstruct>::value>::type |
1224 | | initialize_ptr(U *&u) const { |
1225 | | u = new U(); |
1226 | | } |
1227 | | }; |
1228 | | } |
1229 | | |
1230 | | #endif //def __CK_PUP_H |
1231 | | |
1232 | | |