changeset a8c16e2d466a in /z/repo/gem5
details: http://repo.gem5.org/gem5?cmd=changeset;node=a8c16e2d466a
description:
        mem: Use const pointers for port proxy write functions

        This patch changes the various write functions in the port proxies
        to use const pointers for all sources (similar to how memcpy works).

        The one unfortunate aspect is the need for a const_cast in the packet,
        to avoid having to juggle a const and a non-const data pointer. This
        design decision can always be re-evaluated at a later stage.

diffstat:

 src/mem/fs_translating_port_proxy.cc |   6 +++---
 src/mem/fs_translating_port_proxy.hh |   6 +++---
 src/mem/packet.hh                    |  17 +++++++++++++++++
 src/mem/port_proxy.cc                |  25 ++++++++++++++++++++-----
 src/mem/port_proxy.hh                |   8 ++------
 src/mem/se_translating_port_proxy.cc |   5 +++--
 src/mem/se_translating_port_proxy.hh |   4 ++--
 7 files changed, 50 insertions(+), 21 deletions(-)

diffs (202 lines):

diff -r 755b18321206 -r a8c16e2d466a src/mem/fs_translating_port_proxy.cc
--- a/src/mem/fs_translating_port_proxy.cc      Tue Dec 02 06:07:36 2014 -0500
+++ b/src/mem/fs_translating_port_proxy.cc      Tue Dec 02 06:07:38 2014 -0500
@@ -89,7 +89,7 @@
 }
 
 void
-FSTranslatingPortProxy::writeBlob(Addr addr, uint8_t *p, int size) const
+FSTranslatingPortProxy::writeBlob(Addr addr, const uint8_t *p, int size) const
 {
     Addr paddr;
     for (ChunkGenerator gen(addr, size, TheISA::PageBytes); !gen.done();
@@ -129,7 +129,7 @@
 }
 
 void
-CopyIn(ThreadContext *tc, Addr dest, void *source, size_t cplen)
+CopyIn(ThreadContext *tc, Addr dest, const void *source, size_t cplen)
 {
     uint8_t *src = (uint8_t *)source;
     tc->getVirtProxy().writeBlob(dest, src, cplen);
@@ -154,7 +154,7 @@
 }
 
 void
-CopyStringIn(ThreadContext *tc, char *src, Addr vaddr)
+CopyStringIn(ThreadContext *tc, const char *src, Addr vaddr)
 {
     FSTranslatingPortProxy &vp = tc->getVirtProxy();
     for (ChunkGenerator gen(vaddr, strlen(src), TheISA::PageBytes); 
!gen.done();
diff -r 755b18321206 -r a8c16e2d466a src/mem/fs_translating_port_proxy.hh
--- a/src/mem/fs_translating_port_proxy.hh      Tue Dec 02 06:07:36 2014 -0500
+++ b/src/mem/fs_translating_port_proxy.hh      Tue Dec 02 06:07:38 2014 -0500
@@ -89,7 +89,7 @@
 
     /** Version of writeBlob that translates virt->phys and deals
       * with page boundries. */
-    virtual void writeBlob(Addr addr, uint8_t *p, int size) const;
+    virtual void writeBlob(Addr addr, const uint8_t *p, int size) const;
 
     /**
      * Fill size bytes starting at addr with byte value val.
@@ -98,8 +98,8 @@
 };
 
 void CopyOut(ThreadContext *tc, void *dest, Addr src, size_t cplen);
-void CopyIn(ThreadContext *tc, Addr dest, void *source, size_t cplen);
+void CopyIn(ThreadContext *tc, Addr dest, const void *source, size_t cplen);
 void CopyStringOut(ThreadContext *tc, char *dst, Addr vaddr, size_t maxlen);
-void CopyStringIn(ThreadContext *tc, char *src, Addr vaddr);
+void CopyStringIn(ThreadContext *tc, const char *src, Addr vaddr);
 
 #endif //__MEM_FS_PORT_PROXY_HH__
diff -r 755b18321206 -r a8c16e2d466a src/mem/packet.hh
--- a/src/mem/packet.hh Tue Dec 02 06:07:36 2014 -0500
+++ b/src/mem/packet.hh Tue Dec 02 06:07:38 2014 -0500
@@ -814,6 +814,23 @@
     }
 
     /**
+     * Set the data pointer to the following value that should not be
+     * freed. This version of the function allows the pointer passed
+     * to us to be const. To avoid issues down the line we cast the
+     * constness away, the alternative would be to keep both a const
+     * and non-const data pointer and cleverly choose between
+     * them. Note that this is only allowed for static data.
+     */
+    template <typename T>
+    void
+    dataStaticConst(const T *p)
+    {
+        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
+        data = const_cast<PacketDataPtr>(p);
+        flags.set(STATIC_DATA);
+    }
+
+    /**
      * Set the data pointer to a value that should have delete []
      * called on it.
      */
diff -r 755b18321206 -r a8c16e2d466a src/mem/port_proxy.cc
--- a/src/mem/port_proxy.cc     Tue Dec 02 06:07:36 2014 -0500
+++ b/src/mem/port_proxy.cc     Tue Dec 02 06:07:38 2014 -0500
@@ -41,14 +41,14 @@
 #include "mem/port_proxy.hh"
 
 void
-PortProxy::blobHelper(Addr addr, uint8_t *p, int size, MemCmd cmd) const
+PortProxy::readBlob(Addr addr, uint8_t *p, int size) const
 {
     Request req;
 
-    for (ChunkGenerator gen(addr, size, _cacheLineSize);
-         !gen.done(); gen.next()) {
+    for (ChunkGenerator gen(addr, size, _cacheLineSize); !gen.done();
+         gen.next()) {
         req.setPhys(gen.addr(), gen.size(), 0, Request::funcMasterId);
-        Packet pkt(&req, cmd);
+        Packet pkt(&req, MemCmd::ReadReq);
         pkt.dataStatic(p);
         _port.sendFunctional(&pkt);
         p += gen.size();
@@ -56,13 +56,28 @@
 }
 
 void
+PortProxy::writeBlob(Addr addr, const uint8_t *p, int size) const
+{
+    Request req;
+
+    for (ChunkGenerator gen(addr, size, _cacheLineSize); !gen.done();
+         gen.next()) {
+        req.setPhys(gen.addr(), gen.size(), 0, Request::funcMasterId);
+        Packet pkt(&req, MemCmd::WriteReq);
+        pkt.dataStaticConst(p);
+        _port.sendFunctional(&pkt);
+        p += gen.size();
+    }
+}
+
+void
 PortProxy::memsetBlob(Addr addr, uint8_t v, int size) const
 {
     // quick and dirty...
     uint8_t *buf = new uint8_t[size];
 
     std::memset(buf, v, size);
-    blobHelper(addr, buf, size, MemCmd::WriteReq);
+    PortProxy::writeBlob(addr, buf, size);
 
     delete [] buf;
 }
diff -r 755b18321206 -r a8c16e2d466a src/mem/port_proxy.hh
--- a/src/mem/port_proxy.hh     Tue Dec 02 06:07:36 2014 -0500
+++ b/src/mem/port_proxy.hh     Tue Dec 02 06:07:38 2014 -0500
@@ -91,8 +91,6 @@
     /** Granularity of any transactions issued through this proxy. */
     const unsigned int _cacheLineSize;
 
-    void blobHelper(Addr addr, uint8_t *p, int size, MemCmd cmd) const;
-
   public:
     PortProxy(MasterPort &port, unsigned int cacheLineSize) :
         _port(port), _cacheLineSize(cacheLineSize) { }
@@ -101,14 +99,12 @@
     /**
      * Read size bytes memory at address and store in p.
      */
-    virtual void readBlob(Addr addr, uint8_t* p, int size) const
-    { blobHelper(addr, p, size, MemCmd::ReadReq); }
+    virtual void readBlob(Addr addr, uint8_t* p, int size) const;
 
     /**
      * Write size bytes from p to address.
      */
-    virtual void writeBlob(Addr addr, uint8_t* p, int size) const
-    { blobHelper(addr, p, size, MemCmd::WriteReq); }
+    virtual void writeBlob(Addr addr, const uint8_t* p, int size) const;
 
     /**
      * Fill size bytes starting at addr with byte value val.
diff -r 755b18321206 -r a8c16e2d466a src/mem/se_translating_port_proxy.cc
--- a/src/mem/se_translating_port_proxy.cc      Tue Dec 02 06:07:36 2014 -0500
+++ b/src/mem/se_translating_port_proxy.cc      Tue Dec 02 06:07:38 2014 -0500
@@ -90,7 +90,8 @@
 
 
 bool
-SETranslatingPortProxy::tryWriteBlob(Addr addr, uint8_t *p, int size) const
+SETranslatingPortProxy::tryWriteBlob(Addr addr, const uint8_t *p,
+                                     int size) const
 {
     int prevSize = 0;
 
@@ -121,7 +122,7 @@
 
 
 void
-SETranslatingPortProxy::writeBlob(Addr addr, uint8_t *p, int size) const
+SETranslatingPortProxy::writeBlob(Addr addr, const uint8_t *p, int size) const
 {
     if (!tryWriteBlob(addr, p, size))
         fatal("writeBlob(0x%x, ...) failed", addr);
diff -r 755b18321206 -r a8c16e2d466a src/mem/se_translating_port_proxy.hh
--- a/src/mem/se_translating_port_proxy.hh      Tue Dec 02 06:07:36 2014 -0500
+++ b/src/mem/se_translating_port_proxy.hh      Tue Dec 02 06:07:38 2014 -0500
@@ -84,13 +84,13 @@
     virtual ~SETranslatingPortProxy();
 
     bool tryReadBlob(Addr addr, uint8_t *p, int size) const;
-    bool tryWriteBlob(Addr addr, uint8_t *p, int size) const;
+    bool tryWriteBlob(Addr addr, const uint8_t *p, int size) const;
     bool tryMemsetBlob(Addr addr, uint8_t val, int size) const;
     bool tryWriteString(Addr addr, const char *str) const;
     bool tryReadString(std::string &str, Addr addr) const;
 
     virtual void readBlob(Addr addr, uint8_t *p, int size) const;
-    virtual void writeBlob(Addr addr, uint8_t *p, int size) const;
+    virtual void writeBlob(Addr addr, const uint8_t *p, int size) const;
     virtual void memsetBlob(Addr addr, uint8_t val, int size) const;
 
     void writeString(Addr addr, const char *str) const;
_______________________________________________
gem5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/gem5-dev

Reply via email to