[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r6980: Add an "mmap handle" type to encapsulate bookkeeping element (in tor/trunk: . doc src/common src/or)



Author: nickm
Date: 2006-08-04 14:32:43 -0400 (Fri, 04 Aug 2006)
New Revision: 6980

Modified:
   tor/trunk/
   tor/trunk/doc/TODO
   tor/trunk/src/common/compat.c
   tor/trunk/src/common/compat.h
   tor/trunk/src/or/or.h
   tor/trunk/src/or/routerlist.c
Log:
 r7012@Kushana:  nickm | 2006-08-03 19:21:25 -0700
 Add an "mmap handle" type to encapsulate bookkeeping elements of mmap issues; add prelim win32 impl



Property changes on: tor/trunk
___________________________________________________________________
Name: svk:merge
   - 1f724f9b-111a-0410-b636-93f1a77c1813:/local/or/tor/trunk:8207
c95137ef-5f19-0410-b913-86e773d04f59:/tor/branches/eventdns:7014
c95137ef-5f19-0410-b913-86e773d04f59:/tor/branches/oo-connections:6950
   + 1f724f9b-111a-0410-b636-93f1a77c1813:/local/or/tor/trunk:8207
c95137ef-5f19-0410-b913-86e773d04f59:/tor/branches/eventdns:7014
c95137ef-5f19-0410-b913-86e773d04f59:/tor/branches/mmap:7012
c95137ef-5f19-0410-b913-86e773d04f59:/tor/branches/oo-connections:6950

Modified: tor/trunk/doc/TODO
===================================================================
--- tor/trunk/doc/TODO	2006-08-04 18:31:13 UTC (rev 6979)
+++ tor/trunk/doc/TODO	2006-08-04 18:32:43 UTC (rev 6980)
@@ -73,10 +73,14 @@
 
     - "bandwidth classes", for incoming vs initiated-here conns.
     o Asynchronous DNS
-      . And test it
+      o And test it
       . Make it work on windows.
         o Implement
+        . Enable
         - Test
+      - Make the Nameservers option documented, and make it work right on
+        reload.
+      - Fail when we have no configured nameservers!
       - Make it the default on platforms where it works
 
   - Security improvements

Modified: tor/trunk/src/common/compat.c
===================================================================
--- tor/trunk/src/common/compat.c	2006-08-04 18:31:13 UTC (rev 6979)
+++ tor/trunk/src/common/compat.c	2006-08-04 18:32:43 UTC (rev 6980)
@@ -24,8 +24,9 @@
 
 #ifdef MS_WINDOWS
 #include <process.h>
+#include <windows.h>
+#endif
 
-#endif
 #ifdef HAVE_UNAME
 #include <sys/utsname.h>
 #endif
@@ -107,16 +108,22 @@
 #define INADDR_NONE ((unsigned long) -1)
 #endif
 
-#ifdef HAVE_SYS_MMAN
-const char *
-tor_mmap_file(const char *filename, size_t *size)
+#ifdef HAVE_SYS_MMAN_H
+struct tor_mmap_t {
+  char *data;
+  size_t size;
+};
+tor_mmap_t *
+tor_mmap_file(const char *filename, const char **data, size_t *size_out)
 {
   int fd; /* router file */
   char *string;
   int page_size;
+  tor_mmap_t *res;
+  size_t size;
 
   tor_assert(filename);
-  tor_assert(size);
+  tor_assert(size_out);
 
   fd = open(filename, O_RDONLY, 0);
   if (fd<0) {
@@ -124,13 +131,13 @@
     return NULL;
   }
 
-  *size = lseek(fd, 0, SEEK_END);
+  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)));
+  size += (page_size + (page_size-(size%page_size)));
 
-  string = mmap(0, *size, PROT_READ, MAP_PRIVATE, fd, 0);
+  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));
@@ -139,30 +146,88 @@
 
   close(fd);
 
-  return string;
+  res = tor_malloc_zero(sizeof(tor_mmap_t));
+  *data = res->data = string;
+  *size_out = res->size = size;
+
+  return res;
 }
+void
+tor_munmap_file(tor_mmap_t *handle)
+{
+  munmap(handle->data, handle->size);
+}
+#elif defined(MS_WINDOWS)
+typdef struct tor_mmap_t {
+  char *data;
+  HANDLE file_handle;
+  HANDLE mmap_handle;
+  size_t size;
+} tor_mmap_t;
+tor_mmap_t *
+tor_mmap_file(const char *filename, const char **data, size_t *size)
+{
+  win_mmap_t *res = tor_malloc_zero(res);
+  res->mmap_handle = res->file_handle = INVALID_HANDLE_VALUE;
+  /* What's this about tags? */
 
+  /* Open the file. */
+  res->file_handle = XXXXX;
+  res->size = GetFileSize(res->file_handle, NULL);
+
+  res->mmap_handle = CreateFileMapping(res->file_handle,
+                                       NULL,
+                                       PAGE_READONLY,
+                                       0,
+                                       size,
+                                       tagname);
+  if (res->mmap_handle != INVALID_HANDLE_VALUE)
+    goto err;
+  res->data = (char*) MapViewOfFile(res->mmap_handle,
+                                    access,
+                                    0, 0, 0);
+  if (!res->data)
+    goto err;
+
+  *size = res->size;
+  *data = res->data;
+
+  return res;
+ err:
+  tor_munmap_file(res);
+  return NULL;
+}
 void
-tor_munmap_file(const char *memory, size_t size)
+tor_munmap_file(tor_mmap_t *handle)
 {
-  munmap((char*)memory, size);
+  if (handle->data)
+    UnmapViewOfFile(handle->data);
+  if (res->mmap_handle != INVALID_HANDLE_VALUE)
+    CloseHandle(res->mmap_handle);
+  if (res->file_handle != INVALID_HANDLE_VALUE)
+    CloseHandle(self->file_handle);
+  tor_free(res);
 }
 #else
-const char *
-tor_mmap_file(const char *filename, size_t *size)
+struct tor_mmap_t {
+  char *data;
+};
+tor_mmap_t *
+tor_mmap_file(const char *filename, const char **data, size_t *size)
 {
   char *res = read_file_to_str(filename, 1);
+  tor_mmap_t *handle;
   if (res)
     *size = strlen(res) + 1;
-  return res;
+  handle = tor_malloc_zero(sizeof(tor_mmap_t));
+  *data = handle->data = res;
+  return handle;
 }
 
 void
-tor_munmap_file(const char *memory, size_t size)
+tor_munmap_file(tor_mmap_t *handle)
 {
-  char *mem = (char*) memory;
-  (void)size;
-  tor_free(mem);
+  tor_free(handle->data);
 }
 #endif
 

Modified: tor/trunk/src/common/compat.h
===================================================================
--- tor/trunk/src/common/compat.h	2006-08-04 18:31:13 UTC (rev 6979)
+++ tor/trunk/src/common/compat.h	2006-08-04 18:32:43 UTC (rev 6980)
@@ -114,9 +114,13 @@
 #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);
+/** Opaque bookkeeping type used for mmap accounting. */
+typedef struct tor_mmap_t tor_mmap_t;
 
+tor_mmap_t *tor_mmap_file(const char *filename,
+                          const char **data, size_t *size);
+void tor_munmap_file(tor_mmap_t *handle);
+
 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);

Modified: tor/trunk/src/or/or.h
===================================================================
--- tor/trunk/src/or/or.h	2006-08-04 18:31:13 UTC (rev 6979)
+++ tor/trunk/src/or/or.h	2006-08-04 18:32:43 UTC (rev 6980)
@@ -1020,6 +1020,7 @@
   /** DOCDOC */
   const char *mmap_descriptors;
   size_t mmap_descriptors_len;
+  tor_mmap_t *mmap_handle;
 } routerlist_t;
 
 /** Information on router used when extending a circuit.  (We don't need a

Modified: tor/trunk/src/or/routerlist.c
===================================================================
--- tor/trunk/src/or/routerlist.c	2006-08-04 18:31:13 UTC (rev 6979)
+++ tor/trunk/src/or/routerlist.c	2006-08-04 18:32:43 UTC (rev 6980)
@@ -276,12 +276,12 @@
     goto done;
   }
   /* Our mmap is now invalid. */
-  if (routerlist->mmap_descriptors) {
-    tor_munmap_file(routerlist->mmap_descriptors,
-                    routerlist->mmap_descriptors_len);
-    routerlist->mmap_descriptors =
-      tor_mmap_file(fname, &routerlist->mmap_descriptors_len);
-    if (! routerlist->mmap_descriptors)
+  if (routerlist->mmap_handle) {
+    tor_munmap_file(routerlist->mmap_handle);
+    routerlist->mmap_handle = tor_mmap_file(fname,
+                                            &routerlist->mmap_descriptors,
+                                            &routerlist->mmap_descriptors_len);
+    if (! routerlist->mmap_handle)
       log_warn(LD_FS, "Unable to mmap new descriptor file at '%s'.",fname);
   }
 
@@ -340,8 +340,9 @@
   router_journal_len = router_store_len = 0;
 
   tor_snprintf(fname, fname_len, "%s/cached-routers", options->DataDirectory);
-  routerlist->mmap_descriptors =
-    tor_mmap_file(fname, &routerlist->mmap_descriptors_len);
+  routerlist->mmap_handle = tor_mmap_file(fname,
+                                          &routerlist->mmap_descriptors,
+                                          &routerlist->mmap_descriptors_len);
   if (routerlist->mmap_descriptors) {
     router_store_len = routerlist->mmap_descriptors_len;
     router_load_routers_from_string(routerlist->mmap_descriptors,