base: Transition CP annotate to use shared_ptr
[gem5.git] / src / sim / serialize.hh
index 12b787a5e1e81109c3c0d24690fd373853d975bc..e9de6f713aaf84020e55ba06b2000bb558ffb23a 100644 (file)
 #include <map>
 #include <vector>
 
+#include "base/bitunion.hh"
 #include "base/types.hh"
 
 class IniFile;
 class Serializable;
 class Checkpoint;
 class SimObject;
+class EventQueue;
+
+/** The current version of the checkpoint format.
+ * This should be incremented by 1 and only 1 for every new version, where a new
+ * version is defined as a checkpoint created before this version won't work on
+ * the current version until the checkpoint format is updated. Adding a new
+ * SimObject shouldn't cause the version number to increase, only changes to
+ * existing objects such as serializing/unserializing more state, changing sizes
+ * of serialized arrays, etc. */
+static const uint64_t gem5CheckpointVersion = 0x000000000000000d;
 
 template <class T>
 void paramOut(std::ostream &os, const std::string &name, const T &param);
 
+template <typename DataType, typename BitUnion>
+void paramOut(std::ostream &os, const std::string &name,
+              const BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
+{
+    paramOut(os, name, p.__data);
+}
+
 template <class T>
 void paramIn(Checkpoint *cp, const std::string &section,
              const std::string &name, T &param);
 
+template <typename DataType, typename BitUnion>
+void paramIn(Checkpoint *cp, const std::string &section,
+             const std::string &name,
+             BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
+{
+    paramIn(cp, section, name, p.__data);
+}
+
 template <class T>
 bool optParamIn(Checkpoint *cp, const std::string &section,
              const std::string &name, T &param);
 
+template <typename DataType, typename BitUnion>
+bool optParamIn(Checkpoint *cp, const std::string &section,
+                const std::string &name,
+                BitfieldBackend::BitUnionOperators<DataType, BitUnion> &p)
+{
+    return optParamIn(cp, section, name, p.__data);
+}
+
 template <class T>
 void arrayParamOut(std::ostream &os, const std::string &name,
                    const T *param, unsigned size);
@@ -89,6 +123,17 @@ void
 objParamIn(Checkpoint *cp, const std::string &section,
            const std::string &name, SimObject * &param);
 
+template <typename T>
+void fromInt(T &t, int i)
+{
+    t = (T)i;
+}
+
+template <typename T>
+void fromSimObject(T &t, SimObject *s)
+{
+    t = dynamic_cast<T>(s);
+}
 
 //
 // These macros are streamlined to use in serialize/unserialize
@@ -106,7 +151,7 @@ objParamIn(Checkpoint *cp, const std::string &section,
  do {                                           \
     int tmp;                                    \
     paramIn(cp, section, #scalar, tmp);         \
-    scalar = (typeof(scalar))tmp;               \
+    fromInt(scalar, tmp);                    \
   } while (0)
 
 #define SERIALIZE_ARRAY(member, size)           \
@@ -121,11 +166,17 @@ objParamIn(Checkpoint *cp, const std::string &section,
   do {                                                  \
     SimObject *sptr;                                    \
     objParamIn(cp, section, #objptr, sptr);             \
-    objptr = dynamic_cast<typeof(objptr)>(sptr);        \
+    fromSimObject(objptr, sptr);                        \
   } while (0)
 
-/*
+/**
  * Basic support for object serialization.
+ *
+ * @note Many objects that support serialization need to be put in a
+ * consistent state when serialization takes place. We refer to the
+ * action of forcing an object into a consistent state as
+ * 'draining'. Objects that need draining inherit from Drainable. See
+ * Drainable for more information.
  */
 class Serializable
 {
@@ -152,6 +203,8 @@ class Serializable
     static void unserializeGlobals(Checkpoint *cp);
 };
 
+void debug_serialize(const std::string &cpt_dir);
+
 //
 // A SerializableBuilder serves as an evaluation context for a set of
 // parameters that describe a specific instance of a Serializable.  This
@@ -226,14 +279,29 @@ class SerializableClass
 SerializableClass the##OBJ_CLASS##Class(CLASS_NAME,                        \
                                          OBJ_CLASS::createForUnserialize);
 
+// Base class to wrap object resolving functionality.  This can be
+// provided to Checkpoint to allow it to map object names onto
+// object C++ objects in which to unserialize
+class SimObjectResolver
+{
+  public:
+    virtual ~SimObjectResolver() { }
+
+    // Find a SimObject given a full path name
+    virtual SimObject *resolveSimObject(const std::string &name) = 0;
+};
+
 class Checkpoint
 {
   private:
 
     IniFile *db;
 
+    SimObjectResolver &objNameResolver;
+
   public:
-    Checkpoint(const std::string &cpt_dir);
+    Checkpoint(const std::string &cpt_dir, SimObjectResolver &resolver);
+    ~Checkpoint();
 
     const std::string cptDir;