Add a pair of functions to hide system specific details of mapping a file in for...
authorChris Lattner <sabre@nondot.org>
Fri, 28 May 2004 00:23:48 +0000 (00:23 +0000)
committerChris Lattner <sabre@nondot.org>
Fri, 28 May 2004 00:23:48 +0000 (00:23 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@13863 91177308-0d34-0410-b5e6-96231b3b80d8

include/Support/FileUtilities.h
include/llvm/Support/FileUtilities.h
lib/Support/FileUtilities.cpp

index 0543d76f5a991658c98f24c6206c3c3ce35471e6..d5db63f6d7c48e261899cdde647a87e1a7797f8b 100644 (file)
@@ -42,7 +42,7 @@ bool IsSharedObject(const std::string &FN);
 /// FileOpenable - Returns true IFF Filename names an existing regular file
 /// which we can successfully open.
 ///
-bool FileOpenable (const std::string &Filename);
+bool FileOpenable(const std::string &Filename);
 
 /// DiffFiles - Compare the two files specified, returning true if they are
 /// different or if there is a file error.  If you specify a string to fill in
@@ -93,6 +93,16 @@ long long getFileSize(const std::string &Filename);
 /// not exist or there is an error getting the time-stamp, zero is returned.
 unsigned long long getFileTimestamp(const std::string &Filename);
 
+/// ReadFileIntoAddressSpace - Attempt to map the specific file into the 
+/// address space of the current process for reading.  If this succeeds, 
+/// return the address of the buffer and the length of the file mapped.  On 
+/// failure, return null.
+void *ReadFileIntoAddressSpace(const std::string &Filename, unsigned &Length);
+
+/// UnmapFileFromAddressSpace - Remove the specified file from the current
+/// address space.
+void UnmapFileFromAddressSpace(void *Buffer, unsigned Length);
+
 
 /// FDHandle - Simple handle class to make sure a file descriptor gets closed
 /// when the object is destroyed.  This handle acts similarly to an
index 0543d76f5a991658c98f24c6206c3c3ce35471e6..d5db63f6d7c48e261899cdde647a87e1a7797f8b 100644 (file)
@@ -42,7 +42,7 @@ bool IsSharedObject(const std::string &FN);
 /// FileOpenable - Returns true IFF Filename names an existing regular file
 /// which we can successfully open.
 ///
-bool FileOpenable (const std::string &Filename);
+bool FileOpenable(const std::string &Filename);
 
 /// DiffFiles - Compare the two files specified, returning true if they are
 /// different or if there is a file error.  If you specify a string to fill in
@@ -93,6 +93,16 @@ long long getFileSize(const std::string &Filename);
 /// not exist or there is an error getting the time-stamp, zero is returned.
 unsigned long long getFileTimestamp(const std::string &Filename);
 
+/// ReadFileIntoAddressSpace - Attempt to map the specific file into the 
+/// address space of the current process for reading.  If this succeeds, 
+/// return the address of the buffer and the length of the file mapped.  On 
+/// failure, return null.
+void *ReadFileIntoAddressSpace(const std::string &Filename, unsigned &Length);
+
+/// UnmapFileFromAddressSpace - Remove the specified file from the current
+/// address space.
+void UnmapFileFromAddressSpace(void *Buffer, unsigned Length);
+
 
 /// FDHandle - Simple handle class to make sure a file descriptor gets closed
 /// when the object is destroyed.  This handle acts similarly to an
index 1b8df43629a2681703efb31d3ab799280693d394..74df8b253c2637cf939659896d284dd0c00c3748 100644 (file)
 
 #include "Support/FileUtilities.h"
 #include "Config/unistd.h"
+#include "Config/fcntl.h"
 #include "Config/sys/stat.h"
 #include "Config/sys/types.h"
+#include "Config/sys/mman.h"
 #include <fstream>
 #include <iostream>
 #include <cstdio>
@@ -215,8 +217,40 @@ unsigned long long llvm::getFileTimestamp(const std::string &Filename) {
   return StatBuf.st_mtime;  
 }
 
+/// ReadFileIntoAddressSpace - Attempt to map the specific file into the
+/// address space of the current process for reading.  If this succeeds,
+/// return the address of the buffer and the length of the file mapped.  On
+/// failure, return null.
+void *llvm::ReadFileIntoAddressSpace(const std::string &Filename, 
+                                     unsigned &Length) {
+#ifdef HAVE_MMAP_FILE
+  Length = getFileSize(Filename);
+  if ((int)Length == -1) return 0;
 
+  FDHandle FD(open(Filename.c_str(), O_RDONLY));
+  if (FD == -1) return 0;
 
+  // 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
+  return 0;
+#endif
+}
+
+/// UnmapFileFromAddressSpace - Remove the specified file from the current
+/// address space.
+void llvm::UnmapFileFromAddressSpace(void *Buffer, unsigned Length) {
+#ifdef HAVE_MMAP_FILE
+  munmap((char*)Buffer, Length);
+#else
+  free(Buffer);
+#endif
+}
 
 //===----------------------------------------------------------------------===//
 // FDHandle class implementation