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
This commit is contained in:
Nick Mathewson 2006-08-05 17:53:08 +00:00
parent 714d1b66aa
commit dbac3fb481
2 changed files with 23 additions and 23 deletions

View File

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

View File

@ -115,7 +115,10 @@ size_t strlcpy(char *dst, const char *src, size_t siz);
#endif #endif
/** Opaque bookkeeping type used for mmap accounting. */ /** 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, tor_mmap_t *tor_mmap_file(const char *filename,
const char **data, size_t *size); const char **data, size_t *size);