serialize.hh revision 4000:9bf49767a9e4
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: Nathan Binkert 29 * Erik Hallnor 30 * Steve Reinhardt 31 */ 32 33/* @file 34 * Serialization Interface Declarations 35 */ 36 37#ifndef __SERIALIZE_HH__ 38#define __SERIALIZE_HH__ 39 40 41#include <list> 42#include <iostream> 43#include <map> 44 45#include "sim/host.hh" 46 47class IniFile; 48class Serializable; 49class Checkpoint; 50class SimObject; 51 52template <class T> 53void paramOut(std::ostream &os, const std::string &name, const T ¶m); 54 55template <class T> 56void paramIn(Checkpoint *cp, const std::string §ion, 57 const std::string &name, T ¶m); 58 59template <class T> 60void arrayParamOut(std::ostream &os, const std::string &name, 61 const T *param, int size); 62 63template <class T> 64void arrayParamIn(Checkpoint *cp, const std::string §ion, 65 const std::string &name, T *param, int size); 66 67void 68objParamIn(Checkpoint *cp, const std::string §ion, 69 const std::string &name, SimObject * ¶m); 70 71 72// 73// These macros are streamlined to use in serialize/unserialize 74// functions. It's assumed that serialize() has a parameter 'os' for 75// the ostream, and unserialize() has parameters 'cp' and 'section'. 76#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar) 77 78#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar) 79 80// ENUMs are like SCALARs, but we cast them to ints on the way out 81#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar) 82 83#define UNSERIALIZE_ENUM(scalar) \ 84 do { \ 85 int tmp; \ 86 paramIn(cp, section, #scalar, tmp); \ 87 scalar = (typeof(scalar))tmp; \ 88 } while (0) 89 90#define SERIALIZE_ARRAY(member, size) \ 91 arrayParamOut(os, #member, member, size) 92 93#define UNSERIALIZE_ARRAY(member, size) \ 94 arrayParamIn(cp, section, #member, member, size) 95 96#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name()) 97 98#define UNSERIALIZE_OBJPTR(objptr) \ 99 do { \ 100 SimObject *sptr; \ 101 objParamIn(cp, section, #objptr, sptr); \ 102 objptr = dynamic_cast<typeof(objptr)>(sptr); \ 103 } while (0) 104 105/* 106 * Basic support for object serialization. 107 */ 108class Serializable 109{ 110 protected: 111 void nameOut(std::ostream &os); 112 void nameOut(std::ostream &os, const std::string &_name); 113 114 public: 115 Serializable() {} 116 virtual ~Serializable() {} 117 118 // manditory virtual function, so objects must provide names 119 virtual const std::string name() const = 0; 120 121 virtual void serialize(std::ostream &os) {} 122 virtual void unserialize(Checkpoint *cp, const std::string §ion) {} 123 124 static Serializable *create(Checkpoint *cp, 125 const std::string §ion); 126 127 static int ckptCount; 128 static int ckptMaxCount; 129 static int ckptPrevCount; 130 static void serializeAll(const std::string &cpt_dir); 131 static void unserializeAll(const std::string &cpt_dir); 132 static void unserializeGlobals(Checkpoint *cp); 133}; 134 135// 136// A SerializableBuilder serves as an evaluation context for a set of 137// parameters that describe a specific instance of a Serializable. This 138// evaluation context corresponds to a section in the .ini file (as 139// with the base ParamContext) plus an optional node in the 140// configuration hierarchy (the configNode member) for resolving 141// Serializable references. SerializableBuilder is an abstract superclass; 142// derived classes specialize the class for particular subclasses of 143// Serializable (e.g., BaseCache). 144// 145// For typical usage, see the definition of 146// SerializableClass::createObject(). 147// 148class SerializableBuilder 149{ 150 public: 151 152 SerializableBuilder() {} 153 154 virtual ~SerializableBuilder() {} 155 156 // Create the actual Serializable corresponding to the parameter 157 // values in this context. This function is overridden in derived 158 // classes to call a specific constructor for a particular 159 // subclass of Serializable. 160 virtual Serializable *create() = 0; 161}; 162 163// 164// An instance of SerializableClass corresponds to a class derived from 165// Serializable. The SerializableClass instance serves to bind the string 166// name (found in the config file) to a function that creates an 167// instance of the appropriate derived class. 168// 169// This would be much cleaner in Smalltalk or Objective-C, where types 170// are first-class objects themselves. 171// 172class SerializableClass 173{ 174 public: 175 176 // Type CreateFunc is a pointer to a function that creates a new 177 // simulation object builder based on a .ini-file parameter 178 // section (specified by the first string argument), a unique name 179 // for the object (specified by the second string argument), and 180 // an optional config hierarchy node (specified by the third 181 // argument). A pointer to the new SerializableBuilder is returned. 182 typedef Serializable *(*CreateFunc)(Checkpoint *cp, 183 const std::string §ion); 184 185 static std::map<std::string,CreateFunc> *classMap; 186 187 // Constructor. For example: 188 // 189 // SerializableClass baseCacheSerializableClass("BaseCacheSerializable", 190 // newBaseCacheSerializableBuilder); 191 // 192 SerializableClass(const std::string &className, CreateFunc createFunc); 193 194 // create Serializable given name of class and pointer to 195 // configuration hierarchy node 196 static Serializable *createObject(Checkpoint *cp, 197 const std::string §ion); 198}; 199 200// 201// Macros to encapsulate the magic of declaring & defining 202// SerializableBuilder and SerializableClass objects 203// 204 205#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \ 206SerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \ 207 OBJ_CLASS::createForUnserialize); 208 209void 210setCheckpointDir(const std::string &name); 211 212class Checkpoint 213{ 214 private: 215 216 IniFile *db; 217 const std::string basePath; 218 std::map<std::string, Serializable*> objMap; 219 220 public: 221 Checkpoint(const std::string &cpt_dir, const std::string &path); 222 223 const std::string cptDir; 224 225 bool find(const std::string §ion, const std::string &entry, 226 std::string &value); 227 228 bool findObj(const std::string §ion, const std::string &entry, 229 SimObject *&value); 230 231 bool sectionExists(const std::string §ion); 232 233 // The following static functions have to do with checkpoint 234 // creation rather than restoration. This class makes a handy 235 // namespace for them though. 236 237 // Export current checkpoint directory name so other objects can 238 // derive filenames from it (e.g., memory). The return value is 239 // guaranteed to end in '/' so filenames can be directly appended. 240 // This function is only valid while a checkpoint is being created. 241 static std::string dir(); 242 243 // Filename for base checkpoint file within directory. 244 static const char *baseFilename; 245}; 246 247#endif // __SERIALIZE_HH__ 248