serialize.hh revision 2665
12SN/A/* 21762SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan 32SN/A * All rights reserved. 42SN/A * 52SN/A * Redistribution and use in source and binary forms, with or without 62SN/A * modification, are permitted provided that the following conditions are 72SN/A * met: redistributions of source code must retain the above copyright 82SN/A * notice, this list of conditions and the following disclaimer; 92SN/A * redistributions in binary form must reproduce the above copyright 102SN/A * notice, this list of conditions and the following disclaimer in the 112SN/A * documentation and/or other materials provided with the distribution; 122SN/A * neither the name of the copyright holders nor the names of its 132SN/A * contributors may be used to endorse or promote products derived from 142SN/A * this software without specific prior written permission. 152SN/A * 162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Erik Hallnor 292665Ssaidi@eecs.umich.edu * Steve Reinhardt 302SN/A */ 312SN/A 322SN/A/* @file 332SN/A * Serialization Interface Declarations 342SN/A */ 352SN/A 362SN/A#ifndef __SERIALIZE_HH__ 372SN/A#define __SERIALIZE_HH__ 382SN/A 392SN/A 402SN/A#include <list> 412SN/A#include <iostream> 42217SN/A#include <map> 432SN/A 4456SN/A#include "sim/host.hh" 4556SN/A#include "sim/configfile.hh" 462SN/A 47395SN/Aclass Serializable; 48237SN/Aclass Checkpoint; 492SN/A 50217SN/Atemplate <class T> 51502SN/Avoid paramOut(std::ostream &os, const std::string &name, const T ¶m); 52217SN/A 53217SN/Atemplate <class T> 54237SN/Avoid paramIn(Checkpoint *cp, const std::string §ion, 55502SN/A const std::string &name, T ¶m); 56217SN/A 57217SN/Atemplate <class T> 58217SN/Avoid arrayParamOut(std::ostream &os, const std::string &name, 59217SN/A const T *param, int size); 60217SN/A 61217SN/Atemplate <class T> 62237SN/Avoid arrayParamIn(Checkpoint *cp, const std::string §ion, 63217SN/A const std::string &name, T *param, int size); 64217SN/A 65237SN/Avoid 66237SN/AobjParamIn(Checkpoint *cp, const std::string §ion, 67395SN/A const std::string &name, Serializable * ¶m); 68237SN/A 69237SN/A 70217SN/A// 71217SN/A// These macros are streamlined to use in serialize/unserialize 72217SN/A// functions. It's assumed that serialize() has a parameter 'os' for 73237SN/A// the ostream, and unserialize() has parameters 'cp' and 'section'. 74222SN/A#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar) 75217SN/A 76237SN/A#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar) 77217SN/A 78223SN/A// ENUMs are like SCALARs, but we cast them to ints on the way out 79223SN/A#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar) 80223SN/A 81223SN/A#define UNSERIALIZE_ENUM(scalar) \ 82223SN/A do { \ 83223SN/A int tmp; \ 84237SN/A paramIn(cp, section, #scalar, tmp); \ 85223SN/A scalar = (typeof(scalar))tmp; \ 86223SN/A } while (0) 87223SN/A 88217SN/A#define SERIALIZE_ARRAY(member, size) \ 89217SN/A arrayParamOut(os, #member, member, size) 90217SN/A 91217SN/A#define UNSERIALIZE_ARRAY(member, size) \ 92237SN/A arrayParamIn(cp, section, #member, member, size) 93237SN/A 94237SN/A#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name()) 95237SN/A 96237SN/A#define UNSERIALIZE_OBJPTR(objptr) \ 97237SN/A do { \ 98395SN/A Serializable *sptr; \ 99237SN/A objParamIn(cp, section, #objptr, sptr); \ 100237SN/A objptr = dynamic_cast<typeof(objptr)>(sptr); \ 101237SN/A } while (0) 102217SN/A 1032SN/A/* 1042SN/A * Basic support for object serialization. 1052SN/A */ 106395SN/Aclass Serializable 1072SN/A{ 1082SN/A protected: 109510SN/A void nameOut(std::ostream &os); 110510SN/A void nameOut(std::ostream &os, const std::string &_name); 1112SN/A 1122SN/A public: 113395SN/A Serializable() {} 114395SN/A virtual ~Serializable() {} 1152SN/A 116265SN/A // manditory virtual function, so objects must provide names 117512SN/A virtual const std::string name() const = 0; 1182SN/A 119510SN/A virtual void serialize(std::ostream &os) {} 120237SN/A virtual void unserialize(Checkpoint *cp, const std::string §ion) {} 121237SN/A 122395SN/A static Serializable *create(Checkpoint *cp, 123237SN/A const std::string §ion); 1242SN/A 1252287SN/A static int ckptCount; 1262287SN/A static int ckptMaxCount; 1272287SN/A static int ckptPrevCount; 128395SN/A static void serializeAll(); 129395SN/A static void unserializeGlobals(Checkpoint *cp); 1302SN/A}; 1312SN/A 1322SN/A// 133395SN/A// A SerializableBuilder serves as an evaluation context for a set of 134395SN/A// parameters that describe a specific instance of a Serializable. This 1352SN/A// evaluation context corresponds to a section in the .ini file (as 1362SN/A// with the base ParamContext) plus an optional node in the 1372SN/A// configuration hierarchy (the configNode member) for resolving 138395SN/A// Serializable references. SerializableBuilder is an abstract superclass; 1392SN/A// derived classes specialize the class for particular subclasses of 140395SN/A// Serializable (e.g., BaseCache). 1412SN/A// 1422SN/A// For typical usage, see the definition of 143395SN/A// SerializableClass::createObject(). 1442SN/A// 145395SN/Aclass SerializableBuilder 1462SN/A{ 1472SN/A public: 1482SN/A 149395SN/A SerializableBuilder() {} 1502SN/A 151395SN/A virtual ~SerializableBuilder() {} 1522SN/A 153395SN/A // Create the actual Serializable corresponding to the parameter 1542SN/A // values in this context. This function is overridden in derived 1552SN/A // classes to call a specific constructor for a particular 156395SN/A // subclass of Serializable. 157395SN/A virtual Serializable *create() = 0; 1582SN/A}; 1592SN/A 1602SN/A// 161395SN/A// An instance of SerializableClass corresponds to a class derived from 162395SN/A// Serializable. The SerializableClass instance serves to bind the string 1632SN/A// name (found in the config file) to a function that creates an 1642SN/A// instance of the appropriate derived class. 1652SN/A// 1662SN/A// This would be much cleaner in Smalltalk or Objective-C, where types 1672SN/A// are first-class objects themselves. 1682SN/A// 169395SN/Aclass SerializableClass 1702SN/A{ 1712SN/A public: 1722SN/A 1732SN/A // Type CreateFunc is a pointer to a function that creates a new 1742SN/A // simulation object builder based on a .ini-file parameter 1752SN/A // section (specified by the first string argument), a unique name 1762SN/A // for the object (specified by the second string argument), and 1772SN/A // an optional config hierarchy node (specified by the third 178395SN/A // argument). A pointer to the new SerializableBuilder is returned. 179395SN/A typedef Serializable *(*CreateFunc)(Checkpoint *cp, 180237SN/A const std::string §ion); 1812SN/A 1822SN/A static std::map<std::string,CreateFunc> *classMap; 1832SN/A 1842SN/A // Constructor. For example: 1852SN/A // 186395SN/A // SerializableClass baseCacheSerializableClass("BaseCacheSerializable", 187395SN/A // newBaseCacheSerializableBuilder); 1882SN/A // 189395SN/A SerializableClass(const std::string &className, CreateFunc createFunc); 1902SN/A 191395SN/A // create Serializable given name of class and pointer to 1922SN/A // configuration hierarchy node 193395SN/A static Serializable *createObject(Checkpoint *cp, 194237SN/A const std::string §ion); 1952SN/A}; 1962SN/A 1972SN/A// 1982SN/A// Macros to encapsulate the magic of declaring & defining 199395SN/A// SerializableBuilder and SerializableClass objects 2002SN/A// 2012SN/A 202237SN/A#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \ 203395SN/ASerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \ 204237SN/A OBJ_CLASS::createForUnserialize); 2052SN/A 206237SN/Aclass Checkpoint 207237SN/A{ 208237SN/A private: 209237SN/A 210237SN/A IniFile *db; 211237SN/A const std::string basePath; 212237SN/A const ConfigNode *configNode; 213395SN/A std::map<std::string, Serializable*> objMap; 214237SN/A 215237SN/A public: 216449SN/A Checkpoint(const std::string &cpt_dir, const std::string &path, 217237SN/A const ConfigNode *_configNode); 218237SN/A 219937SN/A const std::string cptDir; 220937SN/A 221237SN/A bool find(const std::string §ion, const std::string &entry, 222237SN/A std::string &value); 223237SN/A 224237SN/A bool findObj(const std::string §ion, const std::string &entry, 225395SN/A Serializable *&value); 226304SN/A 227304SN/A bool sectionExists(const std::string §ion); 228449SN/A 229449SN/A // The following static functions have to do with checkpoint 230449SN/A // creation rather than restoration. This class makes a handy 231449SN/A // namespace for them though. 232449SN/A 233449SN/A // Export current checkpoint directory name so other objects can 234449SN/A // derive filenames from it (e.g., memory). The return value is 235449SN/A // guaranteed to end in '/' so filenames can be directly appended. 236449SN/A // This function is only valid while a checkpoint is being created. 237449SN/A static std::string dir(); 238449SN/A 239449SN/A // Filename for base checkpoint file within directory. 240449SN/A static const char *baseFilename; 241449SN/A 242449SN/A // Set up a checkpoint creation event or series of events. 243449SN/A static void setup(Tick when, Tick period = 0); 244237SN/A}; 2452SN/A 2462SN/A#endif // __SERIALIZE_HH__ 247