[cfe-commits] r39081 - in /cfe/cfe/trunk: Basic/FileManager.cpp include/clang/Basic/FileManager.h

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:27:22 PDT 2007


Author: sabre
Date: Wed Jul 11 11:27:22 2007
New Revision: 39081

URL: http://llvm.org/viewvc/llvm-project?rev=39081&view=rev
Log:
Avoid some mallocs, and avoid leaking some memory, by making the
UniqueDirs/UniqueFiles maps own the memory for the FileEntry and DirEntries.

Modified:
    cfe/cfe/trunk/Basic/FileManager.cpp
    cfe/cfe/trunk/include/clang/Basic/FileManager.h

Modified: cfe/cfe/trunk/Basic/FileManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Basic/FileManager.cpp?rev=39081&r1=39080&r2=39081&view=diff

==============================================================================
--- cfe/cfe/trunk/Basic/FileManager.cpp (original)
+++ cfe/cfe/trunk/Basic/FileManager.cpp Wed Jul 11 11:27:22 2007
@@ -50,16 +50,15 @@
   
   // It exists.  See if we have already opened a directory with the same inode.
   // This occurs when one dir is symlinked to another, for example.
-  DirectoryEntry *&UDE = 
+  DirectoryEntry &UDE = 
     UniqueDirs[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
   
-  if (UDE)  // Already have an entry with this inode, return it.
-    return Ent = UDE;
+  if (UDE.getName()[0])  // Already have an entry with this inode, return it.
+    return Ent = &UDE;
   
   // Otherwise, we don't have this directory yet, add it.
-  DirectoryEntry *DE = new DirectoryEntry();
-  DE->Name           = Filename;
-  return Ent = UDE = DE;
+  UDE.Name   = Filename;
+  return Ent = &UDE;
 }
 
 /// getFile - Lookup, cache, and verify the specified file.  This returns null
@@ -113,19 +112,20 @@
   
   // It exists.  See if we have already opened a directory with the same inode.
   // This occurs when one dir is symlinked to another, for example.
-  FileEntry *&UFE = UniqueFiles[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
+  FileEntry &UFE = UniqueFiles[std::make_pair(StatBuf.st_dev, StatBuf.st_ino)];
   
-  if (UFE)  // Already have an entry with this inode, return it.
-    return Ent = UFE;
+  if (UFE.getUID() != ~0U)  // Already have an entry with this inode, return it.
+    return Ent = &UFE;
 
   // Otherwise, we don't have this directory yet, add it.
-  FileEntry *FE = new FileEntry();
-  FE->Name      = Filename;
-  FE->Size      = StatBuf.st_size;
-  FE->ModTime   = StatBuf.st_mtime;
-  FE->Dir       = DirInfo;
-  FE->UID       = NextFileUID++;
-  return Ent = UFE = FE;
+  // FIXME: Change the name to be a char* that points back to the 'FileEntries'
+  // key.
+  UFE.Name      = Filename;
+  UFE.Size      = StatBuf.st_size;
+  UFE.ModTime   = StatBuf.st_mtime;
+  UFE.Dir       = DirInfo;
+  UFE.UID       = NextFileUID++;
+  return Ent = &UFE;
 }
 
 void FileManager::PrintStats() const {

Modified: cfe/cfe/trunk/include/clang/Basic/FileManager.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Basic/FileManager.h?rev=39081&r1=39080&r2=39081&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Basic/FileManager.h (original)
+++ cfe/cfe/trunk/include/clang/Basic/FileManager.h Wed Jul 11 11:27:22 2007
@@ -27,9 +27,9 @@
 ///
 class DirectoryEntry {
   std::string Name;   // Name of the directory.
-  DirectoryEntry() {}
   friend class FileManager;
 public:
+  DirectoryEntry() {}
   const char *getName() const { return Name.c_str(); }
 };
 
@@ -41,9 +41,9 @@
   time_t ModTime;             // Modification time of file.
   const DirectoryEntry *Dir;  // Directory file lives in.
   unsigned UID;               // A unique (small) ID for the file.
-  FileEntry() {}
   friend class FileManager;
 public:
+  FileEntry() : UID(~0U) {}
   
   const char *getName() const { return Name.c_str(); }
   off_t getSize() const { return Size; }
@@ -62,17 +62,17 @@
 /// names (e.g. symlinked) will be treated as a single file.
 ///
 class FileManager {
+  /// UniqueDirs/UniqueFiles - Cache from ID's to existing directories/files.
+  ///
+  std::map<std::pair<dev_t, ino_t>, DirectoryEntry> UniqueDirs;
+  std::map<std::pair<dev_t, ino_t>, FileEntry> UniqueFiles;
+  
   /// DirEntries/FileEntries - This is a cache of directory/file entries we have
-  /// looked up.
+  /// looked up.  The actual Entry is owned by UniqueFiles/UniqueDirs above.
   ///
   std::map<std::string, DirectoryEntry*> DirEntries;
   std::map<std::string, FileEntry*> FileEntries;
   
-  /// UniqueDirs/UniqueFiles - Cache from ID's to existing directories/files.
-  ///
-  std::map<std::pair<dev_t, ino_t>, DirectoryEntry*> UniqueDirs;
-  std::map<std::pair<dev_t, ino_t>, FileEntry*> UniqueFiles;
-  
   /// NextFileUID - Each FileEntry we create is assigned a unique ID #.
   ///
   unsigned NextFileUID;





More information about the cfe-commits mailing list