[llvm-commits] [llvm] r109746 - in /llvm/trunk: include/llvm/MC/MCContext.h include/llvm/MC/MCDwarf.h lib/MC/MCContext.cpp

Benjamin Kramer benny.kra at googlemail.com
Thu Jul 29 06:53:19 PDT 2010


Author: d0k
Date: Thu Jul 29 08:53:19 2010
New Revision: 109746

URL: http://llvm.org/viewvc/llvm-project?rev=109746&view=rev
Log:
Stop leaking std::strings in GetDwarfFile.

Modified:
    llvm/trunk/include/llvm/MC/MCContext.h
    llvm/trunk/include/llvm/MC/MCDwarf.h
    llvm/trunk/lib/MC/MCContext.cpp

Modified: llvm/trunk/include/llvm/MC/MCContext.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/MCContext.h?rev=109746&r1=109745&r2=109746&view=diff
==============================================================================
--- llvm/trunk/include/llvm/MC/MCContext.h (original)
+++ llvm/trunk/include/llvm/MC/MCContext.h Thu Jul 29 08:53:19 2010
@@ -70,7 +70,7 @@
 
     /// The dwarf file and directory tables from the dwarf .file directive.
     std::vector<MCDwarfFile *> MCDwarfFiles;
-    std::vector<std::string *> MCDwarfDirs;
+    std::vector<StringRef> MCDwarfDirs;
 
     /// Allocator - Allocator object used for creating machine code objects.
     ///

Modified: llvm/trunk/include/llvm/MC/MCDwarf.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/MCDwarf.h?rev=109746&r1=109745&r2=109746&view=diff
==============================================================================
--- llvm/trunk/include/llvm/MC/MCDwarf.h (original)
+++ llvm/trunk/include/llvm/MC/MCDwarf.h Thu Jul 29 08:53:19 2010
@@ -16,7 +16,7 @@
 #ifndef LLVM_MC_MCDWARF_H
 #define LLVM_MC_MCDWARF_H
 
-#include <string>
+#include "llvm/ADT/StringRef.h"
 
 namespace llvm {
   class MCContext;
@@ -29,21 +29,22 @@
   /// index 0 is not used and not a valid dwarf file number).
   class MCDwarfFile {
     // Name - the base name of the file without its directory path.
-    std::string Name;
+    // The StringRef references memory allocated in the MCContext.
+    StringRef Name;
 
     // DirIndex - the index into the list of directory names for this file name.
     unsigned DirIndex;
 
   private:  // MCContext creates and uniques these.
     friend class MCContext;
-    MCDwarfFile(std::string name, unsigned dirIndex)
+    MCDwarfFile(StringRef name, unsigned dirIndex)
       : Name(name), DirIndex(dirIndex) {}
 
     MCDwarfFile(const MCDwarfFile&);       // DO NOT IMPLEMENT
     void operator=(const MCDwarfFile&); // DO NOT IMPLEMENT
   public:
     /// getName - Get the base name of this MCDwarfFile.
-    std::string getName() const { return Name; }
+    StringRef getName() const { return Name; }
 
     /// print - Print the value to the stream \arg OS.
     void print(raw_ostream &OS) const;

Modified: llvm/trunk/lib/MC/MCContext.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCContext.cpp?rev=109746&r1=109745&r2=109746&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCContext.cpp (original)
+++ llvm/trunk/lib/MC/MCContext.cpp Thu Jul 29 08:53:19 2010
@@ -213,7 +213,6 @@
   std::pair<StringRef, StringRef> Slash = FileName.rsplit('/');
 
   // Find or make a entry in the MCDwarfDirs vector for this Directory.
-  StringRef Directory;
   StringRef Name;
   unsigned DirIndex;
   // Capture directory name.
@@ -221,23 +220,24 @@
     Name = Slash.first;
     DirIndex = 0; // For FileNames with no directories a DirIndex of 0 is used.
   } else {
-    Directory = Slash.first;
+    StringRef Directory = Slash.first;
     Name = Slash.second;
     for (DirIndex = 1; DirIndex < MCDwarfDirs.size(); DirIndex++) {
-      std::string *&Dir = MCDwarfDirs[DirIndex];
-      if (Directory == *Dir)
+      if (Directory == MCDwarfDirs[DirIndex])
 	break;
     }
     if (DirIndex >= MCDwarfDirs.size()) {
-      MCDwarfDirs.resize(DirIndex + 1);
-      std::string *&NewDir = MCDwarfDirs[DirIndex];
-      NewDir = new (*this) std::string(Directory);
+      char *Buf = static_cast<char *>(Allocate(Directory.size()));
+      memcpy(Buf, Directory.data(), Directory.size());
+      MCDwarfDirs.push_back(StringRef(Buf, Directory.size()));
     }
   }
   
   // Now make the MCDwarfFile entry and place it in the slot in the MCDwarfFiles
   // vector.
-  File = new (*this) MCDwarfFile(Name, DirIndex);
+  char *Buf = static_cast<char *>(Allocate(Name.size()));
+  memcpy(Buf, Name.data(), Name.size());
+  File = new (*this) MCDwarfFile(StringRef(Buf, Name.size()), DirIndex);
 
   // return the allocated FileNumber.
   return FileNumber;





More information about the llvm-commits mailing list