(win32) Add mmap implementation

This commit is contained in:
Mukund Sivaraman 2008-09-05 14:31:52 +00:00
parent bbd5e129fe
commit 68a6c95d1c
3 changed files with 87 additions and 1 deletions

View File

@ -14,8 +14,14 @@
#include <stdlib.h> /* free */
#include <string.h>
#ifdef WIN32
#include <windows.h>
#endif
#include <libgen.h> /* basename */
#ifndef WIN32
#include <sys/mman.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
@ -24,6 +30,7 @@
#include "ggets.h"
#include "transmission.h"
#include "platform.h"
#include "blocklist.h"
#include "net.h" /* tr_netResolve() */
#include "utils.h"
@ -80,7 +87,11 @@ blocklistLoad( tr_blocklist * b )
return;
}
b->rules = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 );
#ifndef WIN32
b->rules = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 );
#else
b->rules = mmap( NULL, st.st_size, 0, 0, fd, 0 );
#endif
if( !b->rules ) {
tr_err( err_fmt, b->filename, tr_strerror(errno) );
close( fd );

View File

@ -638,3 +638,73 @@ tr_lockfile( const char * filename )
return ret;
}
#ifdef WIN32
/* The following mmap functions are by Joerg Walter, and were taken from
* his paper at: http://www.genesys-e.de/jwalter/mix4win.htm
*/
/* Wait for spin lock */
static int slwait (int *sl) {
while (InterlockedCompareExchange ((void **) sl, (void *) 1, (void *) 0) != 0)
Sleep (0);
return 0;
}
/* Release spin lock */
static int slrelease (int *sl) {
InterlockedExchange (sl, 0);
return 0;
}
static int g_sl;
void *mmap (void *ptr, long size, long prot, long type, long handle, long arg) {
static long g_pagesize;
static long g_regionsize;
/* Wait for spin lock */
slwait (&g_sl);
/* First time initialization */
if (! g_pagesize)
g_pagesize = getpagesize ();
if (! g_regionsize)
g_regionsize = getregionsize ();
/* Allocate this */
ptr = VirtualAlloc (ptr, size,
MEM_RESERVE | MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE);
if (! ptr) {
ptr = MMAP_FAILURE;
goto mmap_exit;
}
mmap_exit:
/* Release spin lock */
slrelease (&g_sl);
return ptr;
}
long munmap (void *ptr, long size) {
static long g_pagesize;
static long g_regionsize;
int rc = MUNMAP_FAILURE;
/* Wait for spin lock */
slwait (&g_sl);
/* First time initialization */
if (! g_pagesize)
g_pagesize = getpagesize ();
if (! g_regionsize)
g_regionsize = getregionsize ();
/* Free this */
if (! VirtualFree (ptr, 0,
MEM_RELEASE))
goto munmap_exit;
rc = 0;
munmap_exit:
/* Release spin lock */
slrelease (&g_sl);
return rc;
}
#endif

View File

@ -58,4 +58,9 @@ int tr_lockHave ( const tr_lock * );
tr_lockfile_state_t tr_lockfile ( const char * filename );
#ifdef WIN32
void *mmap (void *ptr, long size, long prot, long type, long handle, long arg);
long munmap (void *ptr, long size);
#endif
#endif