serialize.hh revision 237
12623SN/A/* 210030SAli.Saidi@ARM.com * Copyright (c) 2003 The Regents of The University of Michigan 37725SAli.Saidi@ARM.com * All rights reserved. 47725SAli.Saidi@ARM.com * 57725SAli.Saidi@ARM.com * Redistribution and use in source and binary forms, with or without 67725SAli.Saidi@ARM.com * modification, are permitted provided that the following conditions are 77725SAli.Saidi@ARM.com * met: redistributions of source code must retain the above copyright 87725SAli.Saidi@ARM.com * notice, this list of conditions and the following disclaimer; 97725SAli.Saidi@ARM.com * redistributions in binary form must reproduce the above copyright 107725SAli.Saidi@ARM.com * notice, this list of conditions and the following disclaimer in the 117725SAli.Saidi@ARM.com * documentation and/or other materials provided with the distribution; 127725SAli.Saidi@ARM.com * neither the name of the copyright holders nor the names of its 137725SAli.Saidi@ARM.com * contributors may be used to endorse or promote products derived from 142623SN/A * this software without specific prior written permission. 152623SN/A * 162623SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172623SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182623SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192623SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202623SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212623SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222623SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232623SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242623SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252623SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262623SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272623SN/A */ 282623SN/A 292623SN/A/* @file 302623SN/A * Serialization Interface Declarations 312623SN/A */ 322623SN/A 332623SN/A#ifndef __SERIALIZE_HH__ 342623SN/A#define __SERIALIZE_HH__ 352623SN/A 362623SN/A 372623SN/A#include <list> 382623SN/A#include <iostream> 392665Ssaidi@eecs.umich.edu#include <map> 402665Ssaidi@eecs.umich.edu 412623SN/A#include "sim/host.hh" 422623SN/A#include "sim/configfile.hh" 433170Sstever@eecs.umich.edu 448105Sgblack@eecs.umich.educlass Serializeable; 452623SN/Aclass Checkpoint; 464040Ssaidi@eecs.umich.edu 476658Snate@binkert.orgtemplate <class T> 488229Snate@binkert.orgvoid paramOut(std::ostream &os, const std::string &name, const T& param); 492623SN/A 508232Snate@binkert.orgtemplate <class T> 519152Satgutier@umich.eduvoid paramIn(Checkpoint *cp, const std::string §ion, 528232Snate@binkert.org const std::string &name, T& param); 538232Snate@binkert.org 543348Sbinkertn@umich.edutemplate <class T> 553348Sbinkertn@umich.eduvoid arrayParamOut(std::ostream &os, const std::string &name, 564762Snate@binkert.org const T *param, int size); 577678Sgblack@eecs.umich.edu 588779Sgblack@eecs.umich.edutemplate <class T> 592901Ssaidi@eecs.umich.eduvoid arrayParamIn(Checkpoint *cp, const std::string §ion, 602623SN/A const std::string &name, T *param, int size); 612623SN/A 622623SN/Avoid 632623SN/AobjParamIn(Checkpoint *cp, const std::string §ion, 642623SN/A const std::string &name, Serializeable * ¶m); 652623SN/A 662623SN/A 672623SN/A// 688921Sandreas.hansson@arm.com// These macros are streamlined to use in serialize/unserialize 698921Sandreas.hansson@arm.com// functions. It's assumed that serialize() has a parameter 'os' for 708921Sandreas.hansson@arm.com// the ostream, and unserialize() has parameters 'cp' and 'section'. 718921Sandreas.hansson@arm.com#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar) 729433SAndreas.Sandberg@ARM.com 738779Sgblack@eecs.umich.edu#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar) 748779Sgblack@eecs.umich.edu 758779Sgblack@eecs.umich.edu// ENUMs are like SCALARs, but we cast them to ints on the way out 768779Sgblack@eecs.umich.edu#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar) 778779Sgblack@eecs.umich.edu 782623SN/A#define UNSERIALIZE_ENUM(scalar) \ 792623SN/A do { \ 802623SN/A int tmp; \ 812623SN/A paramIn(cp, section, #scalar, tmp); \ 828707Sandreas.hansson@arm.com scalar = (typeof(scalar))tmp; \ 832948Ssaidi@eecs.umich.edu } while (0) 842948Ssaidi@eecs.umich.edu 855606Snate@binkert.org#define SERIALIZE_ARRAY(member, size) \ 862948Ssaidi@eecs.umich.edu arrayParamOut(os, #member, member, size) 872948Ssaidi@eecs.umich.edu 885529Snate@binkert.org#define UNSERIALIZE_ARRAY(member, size) \ 898707Sandreas.hansson@arm.com arrayParamIn(cp, section, #member, member, size) 909179Sandreas.hansson@arm.com 919442SAndreas.Sandberg@ARM.com#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name()) 922623SN/A 932623SN/A#define UNSERIALIZE_OBJPTR(objptr) \ 943647Srdreslin@umich.edu do { \ 957897Shestness@cs.utexas.edu Serializeable *sptr; \ 962623SN/A objParamIn(cp, section, #objptr, sptr); \ 972623SN/A objptr = dynamic_cast<typeof(objptr)>(sptr); \ 982623SN/A } while (0) 9910030SAli.Saidi@ARM.com 1002623SN/A/* 1012623SN/A * Basic support for object serialization. 1022623SN/A */ 1032623SN/Aclass Serializeable 1042901Ssaidi@eecs.umich.edu{ 1059342SAndreas.Sandberg@arm.com public: 1062798Sktlim@umich.edu 1079448SAndreas.Sandberg@ARM.com friend class Serializer; 1089448SAndreas.Sandberg@ARM.com 1099448SAndreas.Sandberg@ARM.com protected: 1109448SAndreas.Sandberg@ARM.com // object name: should be unique 1119342SAndreas.Sandberg@arm.com std::string objName; 1129448SAndreas.Sandberg@ARM.com 1139442SAndreas.Sandberg@ARM.com bool serialized; 1142901Ssaidi@eecs.umich.edu static Serializer *serializer; 1152798Sktlim@umich.edu 1169342SAndreas.Sandberg@arm.com void mark(); 1179442SAndreas.Sandberg@ARM.com void nameOut(std::ostream& os); 1189442SAndreas.Sandberg@ARM.com void nameOut(std::ostream& os, const std::string &_name); 1199442SAndreas.Sandberg@ARM.com 1209442SAndreas.Sandberg@ARM.com public: 1219442SAndreas.Sandberg@ARM.com Serializeable(const std::string &n); 1229448SAndreas.Sandberg@ARM.com virtual ~Serializeable(); 1239648Sdam.sunwoo@arm.com 1249442SAndreas.Sandberg@ARM.com void setName(const std::string &name); 1252901Ssaidi@eecs.umich.edu 1262798Sktlim@umich.edu // return name 1272623SN/A const std::string &name() const { return objName; } 1282623SN/A 1292623SN/A virtual void nameChildren() {} 1309342SAndreas.Sandberg@arm.com virtual void serialize(std::ostream& os) {} 1312623SN/A virtual void unserialize(Checkpoint *cp, const std::string §ion) {} 1329442SAndreas.Sandberg@ARM.com 1339448SAndreas.Sandberg@ARM.com static Serializeable *create(Checkpoint *cp, 1349448SAndreas.Sandberg@ARM.com const std::string §ion); 1359448SAndreas.Sandberg@ARM.com}; 1369442SAndreas.Sandberg@ARM.com 1375221Ssaidi@eecs.umich.educlass Serializer 1389523SAndreas.Sandberg@ARM.com{ 1393201Shsul@eecs.umich.edu friend class Serializeable; 1409448SAndreas.Sandberg@ARM.com 1419448SAndreas.Sandberg@ARM.com protected: 1429448SAndreas.Sandberg@ARM.com typedef std::list<Serializeable *> serlist_t; 1439448SAndreas.Sandberg@ARM.com serlist_t objects; 1449448SAndreas.Sandberg@ARM.com std::string file; 1455710Scws3k@cs.virginia.edu std::ostream *output; 1469448SAndreas.Sandberg@ARM.com std::ostream &out() const; 1479837Slena@cs.wisc,edu 1489448SAndreas.Sandberg@ARM.com public: 1499448SAndreas.Sandberg@ARM.com Serializer(); 1509837Slena@cs.wisc,edu virtual ~Serializer(); 1512623SN/A 1529442SAndreas.Sandberg@ARM.com private: 1532798Sktlim@umich.edu void add_object(Serializeable *obj); 1549442SAndreas.Sandberg@ARM.com void add_objects(); 1559442SAndreas.Sandberg@ARM.com 1569442SAndreas.Sandberg@ARM.com public: 1579442SAndreas.Sandberg@ARM.com void serialize(const std::string &file); 1589442SAndreas.Sandberg@ARM.com const std::string &filename() const { return file; } 1599442SAndreas.Sandberg@ARM.com}; 1609442SAndreas.Sandberg@ARM.com 1619442SAndreas.Sandberg@ARM.com// 1629442SAndreas.Sandberg@ARM.com// A SerializeableBuilder serves as an evaluation context for a set of 1639442SAndreas.Sandberg@ARM.com// parameters that describe a specific instance of a Serializeable. This 1649442SAndreas.Sandberg@ARM.com// evaluation context corresponds to a section in the .ini file (as 1659442SAndreas.Sandberg@ARM.com// with the base ParamContext) plus an optional node in the 1669442SAndreas.Sandberg@ARM.com// configuration hierarchy (the configNode member) for resolving 1679442SAndreas.Sandberg@ARM.com// Serializeable references. SerializeableBuilder is an abstract superclass; 1689442SAndreas.Sandberg@ARM.com// derived classes specialize the class for particular subclasses of 1692798Sktlim@umich.edu// Serializeable (e.g., BaseCache). 1702798Sktlim@umich.edu// 1712798Sktlim@umich.edu// For typical usage, see the definition of 1722798Sktlim@umich.edu// SerializeableClass::createObject(). 1732798Sktlim@umich.edu// 1749429SAndreas.Sandberg@ARM.comclass SerializeableBuilder 1759429SAndreas.Sandberg@ARM.com{ 1769442SAndreas.Sandberg@ARM.com public: 1779342SAndreas.Sandberg@arm.com 1789442SAndreas.Sandberg@ARM.com SerializeableBuilder() {} 1799442SAndreas.Sandberg@ARM.com 1809442SAndreas.Sandberg@ARM.com virtual ~SerializeableBuilder() {} 1819179Sandreas.hansson@arm.com 1822623SN/A // Create the actual Serializeable corresponding to the parameter 1832623SN/A // values in this context. This function is overridden in derived 1842623SN/A // classes to call a specific constructor for a particular 1852623SN/A // subclass of Serializeable. 1862623SN/A virtual Serializeable *create() = 0; 1872623SN/A}; 1889429SAndreas.Sandberg@ARM.com 1892623SN/A// 1909179Sandreas.hansson@arm.com// An instance of SerializeableClass corresponds to a class derived from 1912623SN/A// Serializeable. The SerializeableClass instance serves to bind the string 1922623SN/A// name (found in the config file) to a function that creates an 1939523SAndreas.Sandberg@ARM.com// instance of the appropriate derived class. 1949523SAndreas.Sandberg@ARM.com// 1959523SAndreas.Sandberg@ARM.com// This would be much cleaner in Smalltalk or Objective-C, where types 1969524SAndreas.Sandberg@ARM.com// are first-class objects themselves. 1979523SAndreas.Sandberg@ARM.com// 1989523SAndreas.Sandberg@ARM.comclass SerializeableClass 1999523SAndreas.Sandberg@ARM.com{ 2009523SAndreas.Sandberg@ARM.com public: 2012623SN/A 2022623SN/A // Type CreateFunc is a pointer to a function that creates a new 2039180Sandreas.hansson@arm.com // simulation object builder based on a .ini-file parameter 2042623SN/A // section (specified by the first string argument), a unique name 2055221Ssaidi@eecs.umich.edu // for the object (specified by the second string argument), and 2065221Ssaidi@eecs.umich.edu // an optional config hierarchy node (specified by the third 2072623SN/A // argument). A pointer to the new SerializeableBuilder is returned. 2082683Sktlim@umich.edu typedef Serializeable *(*CreateFunc)(Checkpoint *cp, 2092623SN/A const std::string §ion); 2102623SN/A 2112623SN/A static std::map<std::string,CreateFunc> *classMap; 2129837Slena@cs.wisc,edu 2139342SAndreas.Sandberg@arm.com // Constructor. For example: 2143686Sktlim@umich.edu // 2152623SN/A // SerializeableClass baseCacheSerializeableClass("BaseCacheSerializeable", 2169179Sandreas.hansson@arm.com // newBaseCacheSerializeableBuilder); 2172623SN/A // 2182623SN/A SerializeableClass(const std::string &className, CreateFunc createFunc); 2192623SN/A 2202623SN/A // create Serializeable given name of class and pointer to 2218737Skoansin.tan@gmail.com // configuration hierarchy node 2222623SN/A static Serializeable *createObject(Checkpoint *cp, 2235221Ssaidi@eecs.umich.edu const std::string §ion); 2245221Ssaidi@eecs.umich.edu}; 2252623SN/A 2262683Sktlim@umich.edu// 2272623SN/A// Macros to encapsulate the magic of declaring & defining 2286043Sgblack@eecs.umich.edu// SerializeableBuilder and SerializeableClass objects 2296043Sgblack@eecs.umich.edu// 2306043Sgblack@eecs.umich.edu 2319342SAndreas.Sandberg@arm.com#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \ 2322623SN/ASerializeableClass the##OBJ_CLASS##Class(CLASS_NAME, \ 2332644Sstever@eecs.umich.edu OBJ_CLASS::createForUnserialize); 2342644Sstever@eecs.umich.edu 2352623SN/Aclass Checkpoint 2369837Slena@cs.wisc,edu{ 2372623SN/A private: 2382623SN/A 2392623SN/A IniFile *db; 2405728Sgblack@eecs.umich.edu const std::string basePath; 2415728Sgblack@eecs.umich.edu const ConfigNode *configNode; 2425728Sgblack@eecs.umich.edu std::map<std::string, Serializeable*> objMap; 2435728Sgblack@eecs.umich.edu 2448105Sgblack@eecs.umich.edu public: 2459180Sandreas.hansson@arm.com Checkpoint(const std::string &filename, const std::string &path, 2469179Sandreas.hansson@arm.com const ConfigNode *_configNode); 2475728Sgblack@eecs.umich.edu 2485728Sgblack@eecs.umich.edu bool find(const std::string §ion, const std::string &entry, 2498975Sandreas.hansson@arm.com std::string &value); 2505728Sgblack@eecs.umich.edu 2515728Sgblack@eecs.umich.edu bool findObj(const std::string §ion, const std::string &entry, 2525728Sgblack@eecs.umich.edu Serializeable *&value); 2535728Sgblack@eecs.umich.edu}; 2545728Sgblack@eecs.umich.edu 2555728Sgblack@eecs.umich.edu 2565728Sgblack@eecs.umich.edu// 2575728Sgblack@eecs.umich.edu// Export checkpoint filename param so other objects can derive 2585728Sgblack@eecs.umich.edu// filenames from it (e.g., memory). 2592623SN/A// 2605894Sgblack@eecs.umich.eduextern std::string serializeFilename; 2616973Stjones1@inf.ed.ac.uk 2626973Stjones1@inf.ed.ac.uk#endif // __SERIALIZE_HH__ 2635744Sgblack@eecs.umich.edu