mem: Fix memory allocation bug in deferred snoop handling
[gem5.git] / src / base / cprintf.hh
index cff73a228c80afd9fee717619b8a89d9bbcb9fc8..94a74728da6266407943259a89b70f078c68cfb5 100644 (file)
@@ -1,4 +1,5 @@
 /*
+ * Copyright (c) 2014 ARM Limited
  * Copyright (c) 2002-2006 The Regents of The University of Michigan
  * All rights reserved.
  *
@@ -27,6 +28,7 @@
  *
  * Authors: Nathan Binkert
  *          Steve Reinhardt
+ *          Andreas Sandberg
  */
 
 #ifndef __BASE_CPRINTF_HH__
 #include <list>
 #include <string>
 
-#include "base/varargs.hh"
 #include "base/cprintf_formats.hh"
 
 namespace cp {
 
-#define CPRINTF_DECLARATION VARARGS_DECLARATION(cp::Print)
-#define CPRINTF_DEFINITION VARARGS_DEFINITION(cp::Print)
-
 struct Print
 {
   protected:
     std::ostream &stream;
     const char *format;
     const char *ptr;
+    bool cont;
 
     std::ios::fmtflags saved_flags;
     char saved_fill;
     int saved_precision;
 
-    void process(Format &fmt);
+    Format fmt;
+    void process();
+    void process_flag();
 
   public:
     Print(std::ostream &stream, const std::string &format);
     Print(std::ostream &stream, const char *format);
     ~Print();
 
+    int
+    get_number(int data)
+    {
+        return data;
+    }
+    
+    template <typename T>
+    int
+    get_number(const T& data)
+    {
+        return 0;
+    }
+
     template <typename T>
     void
     add_arg(const T &data)
     {
-        Format fmt;
-        process(fmt);
+        if (!cont)
+            process();
+
+        if (fmt.get_width) {
+            fmt.get_width = false;
+            cont = true;
+            fmt.width = get_number(data);
+            return;
+        }
+            
+        if (fmt.get_precision) {
+            fmt.get_precision = false;
+            cont = true;
+            fmt.precision = get_number(data);
+            return;
+        }
 
         switch (fmt.format) {
           case Format::character:
@@ -96,35 +124,44 @@ struct Print
     void end_args();
 };
 
-/* end namespace cp */ }
-
-typedef VarArgs::List<cp::Print> CPrintfArgsList;
+} // namespace cp
 
 inline void
-ccprintf(std::ostream &stream, const char *format, const CPrintfArgsList &args)
+ccprintf(cp::Print &print)
 {
-    cp::Print print(stream, format);
-    args.add_args(print);
+    print.end_args();
 }
 
-inline void
-ccprintf(std::ostream &stream, const char *format, CPRINTF_DECLARATION)
+
+template<typename T, typename ...Args> void
+ccprintf(cp::Print &print, const T &value, const Args &...args)
+{
+    print.add_arg(value);
+
+    ccprintf(print, args...);
+}
+
+
+template<typename ...Args> void
+ccprintf(std::ostream &stream, const char *format, const Args &...args)
 {
     cp::Print print(stream, format);
-    VARARGS_ADDARGS(print);
+
+    ccprintf(print, args...);
 }
 
-inline void
-cprintf(const char *format, CPRINTF_DECLARATION)
+
+template<typename ...Args> void
+cprintf(const char *format, const Args &...args)
 {
-    ccprintf(std::cout, format, VARARGS_ALLARGS);
+    ccprintf(std::cout, format, args...);
 }
 
-inline std::string
-csprintf(const char *format, CPRINTF_DECLARATION)
+template<typename ...Args> std::string
+csprintf(const char *format, const Args &...args)
 {
     std::stringstream stream;
-    ccprintf(stream, format, VARARGS_ALLARGS);
+    ccprintf(stream, format, args...);
     return stream.str();
 }
 
@@ -133,31 +170,22 @@ csprintf(const char *format, CPRINTF_DECLARATION)
  * time converting const char * to std::string since we don't take
  * advantage of it.
  */
-inline void
-ccprintf(std::ostream &stream, const std::string &format,
-         const CPrintfArgsList &args)
+template<typename ...Args> void
+ccprintf(std::ostream &stream, const std::string &format, const Args &...args)
 {
-    ccprintf(stream, format.c_str(), args);
+    ccprintf(stream, format.c_str(), args...);
 }
 
-inline void
-ccprintf(std::ostream &stream, const std::string &format, CPRINTF_DECLARATION)
+template<typename ...Args> void
+cprintf(const std::string &format, const Args &...args)
 {
-    ccprintf(stream, format.c_str(), VARARGS_ALLARGS);
+    ccprintf(std::cout, format.c_str(), args...);
 }
 
-inline void
-cprintf(const std::string &format, CPRINTF_DECLARATION)
+template<typename ...Args> std::string
+csprintf(const std::string &format, const Args &...args)
 {
-    ccprintf(std::cout, format.c_str(), VARARGS_ALLARGS);
-}
-
-inline std::string
-csprintf(const std::string &format, CPRINTF_DECLARATION)
-{
-    std::stringstream stream;
-    ccprintf(stream, format.c_str(), VARARGS_ALLARGS);
-    return stream.str();
+    return csprintf(format.c_str(), args...);
 }
 
 #endif // __CPRINTF_HH__