Sfoglia il codice sorgente

r7028@Kushana: nickm | 2006-08-04 13:10:16 -0700
Make data and size fields visible in tor_mmap_t; hide win magic differently.


svn:r6986

Nick Mathewson 18 anni fa
parent
commit
dbac3fb481
2 ha cambiato i file con 23 aggiunte e 23 eliminazioni
  1. 19 22
      src/common/compat.c
  2. 4 1
      src/common/compat.h

+ 19 - 22
src/common/compat.c

@@ -109,10 +109,6 @@ const char compat_c_id[] =
 #endif
 
 #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)
 {
@@ -155,19 +151,18 @@ tor_mmap_file(const char *filename, const char **data, size_t *size_out)
 void
 tor_munmap_file(tor_mmap_t *handle)
 {
-  munmap(handle->data, handle->size);
+  munmap((char*)handle->data, handle->size);
 }
 #elif defined(MS_WINDOWS)
-typdef struct tor_mmap_t {
-  char *data;
+typedef struct win_mmap_t {
+  tor_mmap_t base;
   HANDLE file_handle;
   HANDLE mmap_handle;
-  size_t size;
-} tor_mmap_t;
+} tor_mmap_impl_t;
 tor_mmap_t *
 tor_mmap_file(const char *filename, const char **data, size_t *size)
 {
-  win_mmap_t *res = tor_malloc_zero(res);
+  win_mmap_t *res = tor_malloc_zero(sizeof(win_mmap_t));
   res->mmap_handle = res->file_handle = INVALID_HANDLE_VALUE;
 
   res->file_handle = CreateFileForMapping(filename,
@@ -175,7 +170,7 @@ tor_mmap_file(const char *filename, const char **data, size_t *size)
                                           0, NULL,
                                           OPEN_EXISTING,
                                           FILE_ATTRIBUTE_NORMAL, 0);
-  res->size = GetFileSize(res->file_handle, NULL);
+  res->base.size = GetFileSize(res->file_handle, NULL);
 
   res->mmap_handle = CreateFileMapping(res->file_handle,
                                        NULL,
@@ -185,16 +180,16 @@ tor_mmap_file(const char *filename, const char **data, size_t *size)
                                        NULL);
   if (res->mmap_handle != INVALID_HANDLE_VALUE)
     goto err;
-  res->data = (char*) MapViewOfFile(res->mmap_handle,
-                                    access,
-                                    0, 0, 0);
+  res->base.data = (char*) MapViewOfFile(res->mmap_handle,
+                                         access,
+                                         0, 0, 0);
   if (!res->data)
     goto err;
 
   *size = res->size;
   *data = res->data;
 
-  return res;
+  return &(res->base);
  err:
   tor_munmap_file(res);
   return NULL;
@@ -202,6 +197,8 @@ tor_mmap_file(const char *filename, const char **data, size_t *size)
 void
 tor_munmap_file(tor_mmap_t *handle)
 {
+  win_mmap_t *h = (win_mmap_t*)
+    (((char*)handle) - STRUCT_OFFSET(win_mmap_t, base));
   if (handle->data)
     UnmapViewOfFile(handle->data);
   if (res->mmap_handle != INVALID_HANDLE_VALUE)
@@ -211,25 +208,25 @@ tor_munmap_file(tor_mmap_t *handle)
   tor_free(res);
 }
 #else
-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;
+  if (! res)
+    return NULL;
   handle = tor_malloc_zero(sizeof(tor_mmap_t));
   *data = handle->data = res;
+  *size = handle->size = strlen(res) + 1;
   return handle;
 }
-
 void
 tor_munmap_file(tor_mmap_t *handle)
 {
-  tor_free(handle->data);
+  char *d = (char*)handle->data;
+  tor_free(d);
+  memset(handle, sizeof(tor_mmap_t), 0);
+  tor_free(handle);
 }
 #endif
 

+ 4 - 1
src/common/compat.h

@@ -115,7 +115,10 @@ size_t strlcpy(char *dst, const char *src, size_t siz);
 #endif
 
 /** Opaque bookkeeping type used for mmap accounting. */
-typedef struct tor_mmap_t tor_mmap_t;
+typedef struct tor_mmap_t {
+  const char *data;
+  size_t size;
+} tor_mmap_t;
 
 tor_mmap_t *tor_mmap_file(const char *filename,
                           const char **data, size_t *size);