summaryrefslogtreecommitdiffstats
path: root/llvm/lib
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2004-05-28 00:23:48 +0000
committerChris Lattner <sabre@nondot.org>2004-05-28 00:23:48 +0000
commit0e713bc208951b2bfe497b7d4d9aae1645ac9f52 (patch)
treecafcd29a638b358d111883e895bc5dfb784d0b96 /llvm/lib
parent9a6bb5fab765885e71c4d2e948960fa6df5415ac (diff)
downloadbcm5719-llvm-0e713bc208951b2bfe497b7d4d9aae1645ac9f52.tar.gz
bcm5719-llvm-0e713bc208951b2bfe497b7d4d9aae1645ac9f52.zip
Add a pair of functions to hide system specific details of mapping a file in for reading.
llvm-svn: 13863
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/Support/FileUtilities.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/llvm/lib/Support/FileUtilities.cpp b/llvm/lib/Support/FileUtilities.cpp
index 1b8df43629a..74df8b253c2 100644
--- a/llvm/lib/Support/FileUtilities.cpp
+++ b/llvm/lib/Support/FileUtilities.cpp
@@ -14,8 +14,10 @@
#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
OpenPOWER on IntegriCloud