serialize.hh revision 2868
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 * 282760Sbinkertn@umich.edu * Authors: Nathan Binkert 292760Sbinkertn@umich.edu * Erik Hallnor 302665Ssaidi@eecs.umich.edu * Steve Reinhardt 312SN/A */ 322SN/A 332SN/A/* @file 342SN/A * Serialization Interface Declarations 352SN/A */ 362SN/A 372SN/A#ifndef __SERIALIZE_HH__ 382SN/A#define __SERIALIZE_HH__ 392SN/A 402SN/A 412SN/A#include <list> 422SN/A#include <iostream> 43217SN/A#include <map> 442SN/A 4556SN/A#include "sim/host.hh" 462SN/A 472738Sstever@eecs.umich.educlass IniFile; 48395SN/Aclass Serializable; 49237SN/Aclass Checkpoint; 502SN/A 51217SN/Atemplate <class T> 52502SN/Avoid paramOut(std::ostream &os, const std::string &name, const T ¶m); 53217SN/A 54217SN/Atemplate <class T> 55237SN/Avoid paramIn(Checkpoint *cp, const std::string §ion, 56502SN/A const std::string &name, T ¶m); 57217SN/A 58217SN/Atemplate <class T> 59217SN/Avoid arrayParamOut(std::ostream &os, const std::string &name, 60217SN/A const T *param, int size); 61217SN/A 62217SN/Atemplate <class T> 63237SN/Avoid arrayParamIn(Checkpoint *cp, const std::string §ion, 64217SN/A const std::string &name, T *param, int size); 65217SN/A 66237SN/Avoid 67237SN/AobjParamIn(Checkpoint *cp, const std::string §ion, 68395SN/A const std::string &name, Serializable * ¶m); 69237SN/A 70237SN/A 71217SN/A// 72217SN/A// These macros are streamlined to use in serialize/unserialize 73217SN/A// functions. It's assumed that serialize() has a parameter 'os' for 74237SN/A// the ostream, and unserialize() has parameters 'cp' and 'section'. 75222SN/A#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar) 76217SN/A 77237SN/A#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar) 78217SN/A 79223SN/A// ENUMs are like SCALARs, but we cast them to ints on the way out 80223SN/A#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar) 81223SN/A 82223SN/A#define UNSERIALIZE_ENUM(scalar) \ 83223SN/A do { \ 84223SN/A int tmp; \ 85237SN/A paramIn(cp, section, #scalar, tmp); \ 86223SN/A scalar = (typeof(scalar))tmp; \ 87223SN/A } while (0) 88223SN/A 89217SN/A#define SERIALIZE_ARRAY(member, size) \ 90217SN/A arrayParamOut(os, #member, member, size) 91217SN/A 92217SN/A#define UNSERIALIZE_ARRAY(member, size) \ 93237SN/A arrayParamIn(cp, section, #member, member, size) 94237SN/A 95237SN/A#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name()) 96237SN/A 97237SN/A#define UNSERIALIZE_OBJPTR(objptr) \ 98237SN/A do { \ 99395SN/A Serializable *sptr; \ 100237SN/A objParamIn(cp, section, #objptr, sptr); \ 101237SN/A objptr = dynamic_cast<typeof(objptr)>(sptr); \ 102237SN/A } while (0) 103217SN/A 1042SN/A/* 1052SN/A * Basic support for object serialization. 1062SN/A */ 107395SN/Aclass Serializable 1082SN/A{ 1092SN/A protected: 110510SN/A void nameOut(std::ostream &os); 111510SN/A void nameOut(std::ostream &os, const std::string &_name); 1122SN/A 1132SN/A public: 114395SN/A Serializable() {} 115395SN/A virtual ~Serializable() {} 1162SN/A 117265SN/A // manditory virtual function, so objects must provide names 118512SN/A virtual const std::string name() const = 0; 1192SN/A 120510SN/A virtual void serialize(std::ostream &os) {} 121237SN/A virtual void unserialize(Checkpoint *cp, const std::string §ion) {} 122237SN/A 123395SN/A static Serializable *create(Checkpoint *cp, 124237SN/A const std::string §ion); 1252SN/A 1262287SN/A static int ckptCount; 1272287SN/A static int ckptMaxCount; 1282287SN/A static int ckptPrevCount; 1292868Sktlim@umich.edu static void serializeAll(const std::string &cpt_dir); 1302868Sktlim@umich.edu static void unserializeAll(const std::string &cpt_dir); 131395SN/A static void unserializeGlobals(Checkpoint *cp); 1322SN/A}; 1332SN/A 1342SN/A// 135395SN/A// A SerializableBuilder serves as an evaluation context for a set of 136395SN/A// parameters that describe a specific instance of a Serializable. This 1372SN/A// evaluation context corresponds to a section in the .ini file (as 1382SN/A// with the base ParamContext) plus an optional node in the 1392SN/A// configuration hierarchy (the configNode member) for resolving 140395SN/A// Serializable references. SerializableBuilder is an abstract superclass; 1412SN/A// derived classes specialize the class for particular subclasses of 142395SN/A// Serializable (e.g., BaseCache). 1432SN/A// 1442SN/A// For typical usage, see the definition of 145395SN/A// SerializableClass::createObject(). 1462SN/A// 147395SN/Aclass SerializableBuilder 1482SN/A{ 1492SN/A public: 1502SN/A 151395SN/A SerializableBuilder() {} 1522SN/A 153395SN/A virtual ~SerializableBuilder() {} 1542SN/A 155395SN/A // Create the actual Serializable corresponding to the parameter 1562SN/A // values in this context. This function is overridden in derived 1572SN/A // classes to call a specific constructor for a particular 158395SN/A // subclass of Serializable. 159395SN/A virtual Serializable *create() = 0; 1602SN/A}; 1612SN/A 1622SN/A// 163395SN/A// An instance of SerializableClass corresponds to a class derived from 164395SN/A// Serializable. The SerializableClass instance serves to bind the string 1652SN/A// name (found in the config file) to a function that creates an 1662SN/A// instance of the appropriate derived class. 1672SN/A// 1682SN/A// This would be much cleaner in Smalltalk or Objective-C, where types 1692SN/A// are first-class objects themselves. 1702SN/A// 171395SN/Aclass SerializableClass 1722SN/A{ 1732SN/A public: 1742SN/A 1752SN/A // Type CreateFunc is a pointer to a function that creates a new 1762SN/A // simulation object builder based on a .ini-file parameter 1772SN/A // section (specified by the first string argument), a unique name 1782SN/A // for the object (specified by the second string argument), and 1792SN/A // an optional config hierarchy node (specified by the third 180395SN/A // argument). A pointer to the new SerializableBuilder is returned. 181395SN/A typedef Serializable *(*CreateFunc)(Checkpoint *cp, 1822738Sstever@eecs.umich.edu const std::string §ion); 1832SN/A 1842SN/A static std::map<std::string,CreateFunc> *classMap; 1852SN/A 1862SN/A // Constructor. For example: 1872SN/A // 188395SN/A // SerializableClass baseCacheSerializableClass("BaseCacheSerializable", 189395SN/A // newBaseCacheSerializableBuilder); 1902SN/A // 191395SN/A SerializableClass(const std::string &className, CreateFunc createFunc); 1922SN/A 193395SN/A // create Serializable given name of class and pointer to 1942SN/A // configuration hierarchy node 195395SN/A static Serializable *createObject(Checkpoint *cp, 1962738Sstever@eecs.umich.edu const std::string §ion); 1972SN/A}; 1982SN/A 1992SN/A// 2002SN/A// Macros to encapsulate the magic of declaring & defining 201395SN/A// SerializableBuilder and SerializableClass objects 2022SN/A// 2032SN/A 204237SN/A#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \ 205395SN/ASerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \ 206237SN/A OBJ_CLASS::createForUnserialize); 2072SN/A 2082797Sktlim@umich.eduvoid 2092868Sktlim@umich.edusetCheckpointDir(const std::string &name); 2102797Sktlim@umich.edu 211237SN/Aclass Checkpoint 212237SN/A{ 213237SN/A private: 214237SN/A 215237SN/A IniFile *db; 216237SN/A const std::string basePath; 217395SN/A std::map<std::string, Serializable*> objMap; 218237SN/A 219237SN/A public: 2202738Sstever@eecs.umich.edu Checkpoint(const std::string &cpt_dir, const std::string &path); 221237SN/A 222937SN/A const std::string cptDir; 223937SN/A 224237SN/A bool find(const std::string §ion, const std::string &entry, 225237SN/A std::string &value); 226237SN/A 227237SN/A bool findObj(const std::string §ion, const std::string &entry, 228395SN/A Serializable *&value); 229304SN/A 230304SN/A bool sectionExists(const std::string §ion); 231449SN/A 232449SN/A // The following static functions have to do with checkpoint 233449SN/A // creation rather than restoration. This class makes a handy 234449SN/A // namespace for them though. 235449SN/A 236449SN/A // Export current checkpoint directory name so other objects can 237449SN/A // derive filenames from it (e.g., memory). The return value is 238449SN/A // guaranteed to end in '/' so filenames can be directly appended. 239449SN/A // This function is only valid while a checkpoint is being created. 240449SN/A static std::string dir(); 241449SN/A 242449SN/A // Filename for base checkpoint file within directory. 243449SN/A static const char *baseFilename; 244237SN/A}; 2452SN/A 2462SN/A#endif // __SERIALIZE_HH__ 247