[llvm-commits] CVS: llvm/lib/Support/FileUtilities.cpp

Chris Lattner lattner at cs.uiuc.edu
Thu May 27 19:38:01 PDT 2004


Changes in directory llvm/lib/Support:

FileUtilities.cpp updated: 1.20 -> 1.21

---
Log message:

Add support for zero length files


---
Diffs of the changes:  (+10 -1)

Index: llvm/lib/Support/FileUtilities.cpp
diff -u llvm/lib/Support/FileUtilities.cpp:1.20 llvm/lib/Support/FileUtilities.cpp:1.21
--- llvm/lib/Support/FileUtilities.cpp:1.20	Thu May 27 19:23:48 2004
+++ llvm/lib/Support/FileUtilities.cpp	Thu May 27 19:34:42 2004
@@ -230,11 +230,17 @@
   FDHandle FD(open(Filename.c_str(), O_RDONLY));
   if (FD == -1) return 0;
 
+  // If the file has a length of zero, mmap might return a null pointer.  In 
+  // this case, allocate a single byte of memory and return it instead.
+  if (Length == 0)
+    return malloc(1);
+
   // mmap in the file all at once...
   void *Buffer = (void*)mmap(0, Length, PROT_READ, MAP_PRIVATE, FD, 0);
 
   if (Buffer == (void*)MAP_FAILED)
     return 0;
+
   return Buffer;
 #else
   // FIXME: implement with read/write
@@ -246,7 +252,10 @@
 /// address space.
 void llvm::UnmapFileFromAddressSpace(void *Buffer, unsigned Length) {
 #ifdef HAVE_MMAP_FILE
-  munmap((char*)Buffer, Length);
+  if (Length)
+    munmap((char*)Buffer, Length);
+  else
+    free(Buffer);  // Zero byte files are malloc(1)'s.
 #else
   free(Buffer);
 #endif





More information about the llvm-commits mailing list