[or-cvs] Add a basic mmap function, with a "fake-it" wrapper to do r...

Nick Mathewson nickm at seul.org
Sun May 28 16:54:41 UTC 2006


Update of /home/or/cvsroot/tor/src/common
In directory moria:/tmp/cvs-serv6366/src/common

Modified Files:
	compat.c compat.h 
Log Message:
Add a basic mmap function, with a "fake-it" wrapper to do read_file_from_str instead.  Based on code from Michael Mohr.

Index: compat.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/compat.c,v
retrieving revision 1.86
retrieving revision 1.87
diff -u -p -d -r1.86 -r1.87
--- compat.c	23 May 2006 08:23:03 -0000	1.86
+++ compat.c	28 May 2006 16:54:39 -0000	1.87
@@ -87,6 +87,9 @@ const char compat_c_id[] =
 #ifdef HAVE_SYS_UTIME_H
 #include <sys/utime.h>
 #endif
+#ifdef HAVE_SYS_MMAN_H
+#include <sys/mman.h>
+#endif
 
 #include "log.h"
 #include "util.h"
@@ -104,6 +107,63 @@ const char compat_c_id[] =
 #define INADDR_NONE ((unsigned long) -1)
 #endif
 
+#ifdef HAVE_SYS_MMAP
+const char *
+tor_mmap_file(const char *filename, size_t *size)
+{
+  int fd; /* router file */
+  char *string;
+  int page_size;
+
+  tor_assert(filename);
+  tor_assert(size);
+
+  fd = open(filename, O_RDONLY, 0);
+  if (fd<0) {
+    log_warn(LD_FS,"Could not open \"%s\" for mmap().",filename);
+    return NULL;
+  }
+
+  *size = lseek(fd, 0, SEEK_END);
+  lseek(fd, 0, SEEK_SET);
+  /* ensure page alignment */
+  page_size = getpagesize();
+  *size += (page_size + (page_size-(*size%page_size)));
+
+  string = mmap(0, *size, PROT_READ, MAP_PRIVATE, fd, 0);
+  if(string == MAP_FAILED) {
+    log_warn(LD_FS,"Could not mmap file \"%s\": %s", filename,
+             strerror(errno));
+    return NULL;
+  }
+
+  close(fd);
+
+  return string;
+}
+
+void
+tor_munmap_file(const char *memory, size_t size)
+{
+  munmap((char*)memory, size);
+}
+#else
+const char *
+tor_mmap_file(const char *filename, size_t *size)
+{
+  char *res = read_file_to_str(filename, 1);
+  *size = strlen(res) + 1;
+  return res;
+}
+
+void
+tor_munmap_file(const char *memory, size_t size)
+{
+  char *mem = (char*) memory;
+  tor_free(mem);
+}
+#endif
+
 /** Replacement for snprintf.  Differs from platform snprintf in two
  * ways: First, always NUL-terminates its output.  Second, always
  * returns -1 if the result is truncated.  (Note that this return

Index: compat.h
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/compat.h,v
retrieving revision 1.45
retrieving revision 1.46
diff -u -p -d -r1.45 -r1.46
--- compat.h	23 May 2006 08:23:03 -0000	1.45
+++ compat.h	28 May 2006 16:54:39 -0000	1.46
@@ -101,6 +101,9 @@ size_t strlcpy(char *dst, const char *sr
 #define U64_LITERAL(n) (n ## llu)
 #endif
 
+const char *tor_mmap_file(const char *filename, size_t *size);
+void tor_munmap_file(const char *memory, size_t size);
+
 int tor_snprintf(char *str, size_t size, const char *format, ...)
      CHECK_PRINTF(3,4);
 int tor_vsnprintf(char *str, size_t size, const char *format, va_list args);



More information about the tor-commits mailing list