serialize.hh (2632:1bb2f91485ea) serialize.hh (2665:a124942bacb8)
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Erik Hallnor
29 * Steve Reinhardt
27 */
28
29/* @file
30 * Serialization Interface Declarations
31 */
32
33#ifndef __SERIALIZE_HH__
34#define __SERIALIZE_HH__
35
36
37#include <list>
38#include <iostream>
39#include <map>
40
41#include "sim/host.hh"
42#include "sim/configfile.hh"
43
44class Serializable;
45class Checkpoint;
46
47template <class T>
48void paramOut(std::ostream &os, const std::string &name, const T &param);
49
50template <class T>
51void paramIn(Checkpoint *cp, const std::string &section,
52 const std::string &name, T &param);
53
54template <class T>
55void arrayParamOut(std::ostream &os, const std::string &name,
56 const T *param, int size);
57
58template <class T>
59void arrayParamIn(Checkpoint *cp, const std::string &section,
60 const std::string &name, T *param, int size);
61
62void
63objParamIn(Checkpoint *cp, const std::string &section,
64 const std::string &name, Serializable * &param);
65
66
67//
68// These macros are streamlined to use in serialize/unserialize
69// functions. It's assumed that serialize() has a parameter 'os' for
70// the ostream, and unserialize() has parameters 'cp' and 'section'.
71#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar)
72
73#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar)
74
75// ENUMs are like SCALARs, but we cast them to ints on the way out
76#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar)
77
78#define UNSERIALIZE_ENUM(scalar) \
79 do { \
80 int tmp; \
81 paramIn(cp, section, #scalar, tmp); \
82 scalar = (typeof(scalar))tmp; \
83 } while (0)
84
85#define SERIALIZE_ARRAY(member, size) \
86 arrayParamOut(os, #member, member, size)
87
88#define UNSERIALIZE_ARRAY(member, size) \
89 arrayParamIn(cp, section, #member, member, size)
90
91#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name())
92
93#define UNSERIALIZE_OBJPTR(objptr) \
94 do { \
95 Serializable *sptr; \
96 objParamIn(cp, section, #objptr, sptr); \
97 objptr = dynamic_cast<typeof(objptr)>(sptr); \
98 } while (0)
99
100/*
101 * Basic support for object serialization.
102 */
103class Serializable
104{
105 protected:
106 void nameOut(std::ostream &os);
107 void nameOut(std::ostream &os, const std::string &_name);
108
109 public:
110 Serializable() {}
111 virtual ~Serializable() {}
112
113 // manditory virtual function, so objects must provide names
114 virtual const std::string name() const = 0;
115
116 virtual void serialize(std::ostream &os) {}
117 virtual void unserialize(Checkpoint *cp, const std::string &section) {}
118
119 static Serializable *create(Checkpoint *cp,
120 const std::string &section);
121
122 static int ckptCount;
123 static int ckptMaxCount;
124 static int ckptPrevCount;
125 static void serializeAll();
126 static void unserializeGlobals(Checkpoint *cp);
127};
128
129//
130// A SerializableBuilder serves as an evaluation context for a set of
131// parameters that describe a specific instance of a Serializable. This
132// evaluation context corresponds to a section in the .ini file (as
133// with the base ParamContext) plus an optional node in the
134// configuration hierarchy (the configNode member) for resolving
135// Serializable references. SerializableBuilder is an abstract superclass;
136// derived classes specialize the class for particular subclasses of
137// Serializable (e.g., BaseCache).
138//
139// For typical usage, see the definition of
140// SerializableClass::createObject().
141//
142class SerializableBuilder
143{
144 public:
145
146 SerializableBuilder() {}
147
148 virtual ~SerializableBuilder() {}
149
150 // Create the actual Serializable corresponding to the parameter
151 // values in this context. This function is overridden in derived
152 // classes to call a specific constructor for a particular
153 // subclass of Serializable.
154 virtual Serializable *create() = 0;
155};
156
157//
158// An instance of SerializableClass corresponds to a class derived from
159// Serializable. The SerializableClass instance serves to bind the string
160// name (found in the config file) to a function that creates an
161// instance of the appropriate derived class.
162//
163// This would be much cleaner in Smalltalk or Objective-C, where types
164// are first-class objects themselves.
165//
166class SerializableClass
167{
168 public:
169
170 // Type CreateFunc is a pointer to a function that creates a new
171 // simulation object builder based on a .ini-file parameter
172 // section (specified by the first string argument), a unique name
173 // for the object (specified by the second string argument), and
174 // an optional config hierarchy node (specified by the third
175 // argument). A pointer to the new SerializableBuilder is returned.
176 typedef Serializable *(*CreateFunc)(Checkpoint *cp,
177 const std::string &section);
178
179 static std::map<std::string,CreateFunc> *classMap;
180
181 // Constructor. For example:
182 //
183 // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
184 // newBaseCacheSerializableBuilder);
185 //
186 SerializableClass(const std::string &className, CreateFunc createFunc);
187
188 // create Serializable given name of class and pointer to
189 // configuration hierarchy node
190 static Serializable *createObject(Checkpoint *cp,
191 const std::string &section);
192};
193
194//
195// Macros to encapsulate the magic of declaring & defining
196// SerializableBuilder and SerializableClass objects
197//
198
199#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \
200SerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \
201 OBJ_CLASS::createForUnserialize);
202
203class Checkpoint
204{
205 private:
206
207 IniFile *db;
208 const std::string basePath;
209 const ConfigNode *configNode;
210 std::map<std::string, Serializable*> objMap;
211
212 public:
213 Checkpoint(const std::string &cpt_dir, const std::string &path,
214 const ConfigNode *_configNode);
215
216 const std::string cptDir;
217
218 bool find(const std::string &section, const std::string &entry,
219 std::string &value);
220
221 bool findObj(const std::string &section, const std::string &entry,
222 Serializable *&value);
223
224 bool sectionExists(const std::string &section);
225
226 // The following static functions have to do with checkpoint
227 // creation rather than restoration. This class makes a handy
228 // namespace for them though.
229
230 // Export current checkpoint directory name so other objects can
231 // derive filenames from it (e.g., memory). The return value is
232 // guaranteed to end in '/' so filenames can be directly appended.
233 // This function is only valid while a checkpoint is being created.
234 static std::string dir();
235
236 // Filename for base checkpoint file within directory.
237 static const char *baseFilename;
238
239 // Set up a checkpoint creation event or series of events.
240 static void setup(Tick when, Tick period = 0);
241};
242
243#endif // __SERIALIZE_HH__
30 */
31
32/* @file
33 * Serialization Interface Declarations
34 */
35
36#ifndef __SERIALIZE_HH__
37#define __SERIALIZE_HH__
38
39
40#include <list>
41#include <iostream>
42#include <map>
43
44#include "sim/host.hh"
45#include "sim/configfile.hh"
46
47class Serializable;
48class Checkpoint;
49
50template <class T>
51void paramOut(std::ostream &os, const std::string &name, const T &param);
52
53template <class T>
54void paramIn(Checkpoint *cp, const std::string &section,
55 const std::string &name, T &param);
56
57template <class T>
58void arrayParamOut(std::ostream &os, const std::string &name,
59 const T *param, int size);
60
61template <class T>
62void arrayParamIn(Checkpoint *cp, const std::string &section,
63 const std::string &name, T *param, int size);
64
65void
66objParamIn(Checkpoint *cp, const std::string &section,
67 const std::string &name, Serializable * &param);
68
69
70//
71// These macros are streamlined to use in serialize/unserialize
72// functions. It's assumed that serialize() has a parameter 'os' for
73// the ostream, and unserialize() has parameters 'cp' and 'section'.
74#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar)
75
76#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar)
77
78// ENUMs are like SCALARs, but we cast them to ints on the way out
79#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar)
80
81#define UNSERIALIZE_ENUM(scalar) \
82 do { \
83 int tmp; \
84 paramIn(cp, section, #scalar, tmp); \
85 scalar = (typeof(scalar))tmp; \
86 } while (0)
87
88#define SERIALIZE_ARRAY(member, size) \
89 arrayParamOut(os, #member, member, size)
90
91#define UNSERIALIZE_ARRAY(member, size) \
92 arrayParamIn(cp, section, #member, member, size)
93
94#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name())
95
96#define UNSERIALIZE_OBJPTR(objptr) \
97 do { \
98 Serializable *sptr; \
99 objParamIn(cp, section, #objptr, sptr); \
100 objptr = dynamic_cast<typeof(objptr)>(sptr); \
101 } while (0)
102
103/*
104 * Basic support for object serialization.
105 */
106class Serializable
107{
108 protected:
109 void nameOut(std::ostream &os);
110 void nameOut(std::ostream &os, const std::string &_name);
111
112 public:
113 Serializable() {}
114 virtual ~Serializable() {}
115
116 // manditory virtual function, so objects must provide names
117 virtual const std::string name() const = 0;
118
119 virtual void serialize(std::ostream &os) {}
120 virtual void unserialize(Checkpoint *cp, const std::string &section) {}
121
122 static Serializable *create(Checkpoint *cp,
123 const std::string &section);
124
125 static int ckptCount;
126 static int ckptMaxCount;
127 static int ckptPrevCount;
128 static void serializeAll();
129 static void unserializeGlobals(Checkpoint *cp);
130};
131
132//
133// A SerializableBuilder serves as an evaluation context for a set of
134// parameters that describe a specific instance of a Serializable. This
135// evaluation context corresponds to a section in the .ini file (as
136// with the base ParamContext) plus an optional node in the
137// configuration hierarchy (the configNode member) for resolving
138// Serializable references. SerializableBuilder is an abstract superclass;
139// derived classes specialize the class for particular subclasses of
140// Serializable (e.g., BaseCache).
141//
142// For typical usage, see the definition of
143// SerializableClass::createObject().
144//
145class SerializableBuilder
146{
147 public:
148
149 SerializableBuilder() {}
150
151 virtual ~SerializableBuilder() {}
152
153 // Create the actual Serializable corresponding to the parameter
154 // values in this context. This function is overridden in derived
155 // classes to call a specific constructor for a particular
156 // subclass of Serializable.
157 virtual Serializable *create() = 0;
158};
159
160//
161// An instance of SerializableClass corresponds to a class derived from
162// Serializable. The SerializableClass instance serves to bind the string
163// name (found in the config file) to a function that creates an
164// instance of the appropriate derived class.
165//
166// This would be much cleaner in Smalltalk or Objective-C, where types
167// are first-class objects themselves.
168//
169class SerializableClass
170{
171 public:
172
173 // Type CreateFunc is a pointer to a function that creates a new
174 // simulation object builder based on a .ini-file parameter
175 // section (specified by the first string argument), a unique name
176 // for the object (specified by the second string argument), and
177 // an optional config hierarchy node (specified by the third
178 // argument). A pointer to the new SerializableBuilder is returned.
179 typedef Serializable *(*CreateFunc)(Checkpoint *cp,
180 const std::string &section);
181
182 static std::map<std::string,CreateFunc> *classMap;
183
184 // Constructor. For example:
185 //
186 // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
187 // newBaseCacheSerializableBuilder);
188 //
189 SerializableClass(const std::string &className, CreateFunc createFunc);
190
191 // create Serializable given name of class and pointer to
192 // configuration hierarchy node
193 static Serializable *createObject(Checkpoint *cp,
194 const std::string &section);
195};
196
197//
198// Macros to encapsulate the magic of declaring & defining
199// SerializableBuilder and SerializableClass objects
200//
201
202#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \
203SerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \
204 OBJ_CLASS::createForUnserialize);
205
206class Checkpoint
207{
208 private:
209
210 IniFile *db;
211 const std::string basePath;
212 const ConfigNode *configNode;
213 std::map<std::string, Serializable*> objMap;
214
215 public:
216 Checkpoint(const std::string &cpt_dir, const std::string &path,
217 const ConfigNode *_configNode);
218
219 const std::string cptDir;
220
221 bool find(const std::string &section, const std::string &entry,
222 std::string &value);
223
224 bool findObj(const std::string &section, const std::string &entry,
225 Serializable *&value);
226
227 bool sectionExists(const std::string &section);
228
229 // The following static functions have to do with checkpoint
230 // creation rather than restoration. This class makes a handy
231 // namespace for them though.
232
233 // Export current checkpoint directory name so other objects can
234 // derive filenames from it (e.g., memory). The return value is
235 // guaranteed to end in '/' so filenames can be directly appended.
236 // This function is only valid while a checkpoint is being created.
237 static std::string dir();
238
239 // Filename for base checkpoint file within directory.
240 static const char *baseFilename;
241
242 // Set up a checkpoint creation event or series of events.
243 static void setup(Tick when, Tick period = 0);
244};
245
246#endif // __SERIALIZE_HH__