Changeset 6702


Ignore:
Timestamp:
Sep 5, 2008, 2:31:52 PM (13 years ago)
Author:
muks
Message:

(win32) Add mmap implementation

Location:
trunk/libtransmission
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/blocklist.c

    r6490 r6702  
    1515#include <string.h>
    1616
     17#ifdef WIN32
     18#include <windows.h>
     19#endif
     20
    1721#include <libgen.h> /* basename */
     22#ifndef WIN32
    1823#include <sys/mman.h>
     24#endif
    1925#include <sys/types.h>
    2026#include <sys/stat.h>
     
    2531
    2632#include "transmission.h"
     33#include "platform.h"
    2734#include "blocklist.h"
    2835#include "net.h" /* tr_netResolve() */
     
    8188    }
    8289
    83     b->rules = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 );
     90#ifndef WIN32
     91     b->rules = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0 );
     92#else
     93    b->rules = mmap( NULL, st.st_size, 0, 0, fd, 0 );
     94#endif
    8495    if( !b->rules ) {
    8596        tr_err( err_fmt, b->filename, tr_strerror(errno) );
  • trunk/libtransmission/platform.c

    r6642 r6702  
    639639    return ret;
    640640}
     641
     642
     643#ifdef WIN32
     644
     645/* The following mmap functions are by Joerg Walter, and were taken from
     646 * his paper at: http://www.genesys-e.de/jwalter/mix4win.htm
     647 */
     648
     649/* Wait for spin lock */
     650static int slwait (int *sl) {
     651    while (InterlockedCompareExchange ((void **) sl, (void *) 1, (void *) 0) != 0)
     652        Sleep (0);
     653    return 0;
     654}
     655
     656/* Release spin lock */
     657static int slrelease (int *sl) {
     658    InterlockedExchange (sl, 0);
     659    return 0;
     660}
     661
     662static int g_sl;
     663
     664void *mmap (void *ptr, long size, long prot, long type, long handle, long arg) {
     665    static long g_pagesize;
     666    static long g_regionsize;
     667    /* Wait for spin lock */
     668    slwait (&g_sl);
     669    /* First time initialization */
     670    if (! g_pagesize)
     671        g_pagesize = getpagesize ();
     672    if (! g_regionsize)
     673        g_regionsize = getregionsize ();
     674    /* Allocate this */
     675    ptr = VirtualAlloc (ptr, size,
     676                        MEM_RESERVE | MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE);
     677    if (! ptr) {
     678        ptr = MMAP_FAILURE;
     679        goto mmap_exit;
     680    }
     681mmap_exit:
     682    /* Release spin lock */
     683    slrelease (&g_sl);
     684    return ptr;
     685}
     686
     687long munmap (void *ptr, long size) {
     688    static long g_pagesize;
     689    static long g_regionsize;
     690    int rc = MUNMAP_FAILURE;
     691    /* Wait for spin lock */
     692    slwait (&g_sl);
     693    /* First time initialization */
     694    if (! g_pagesize)
     695        g_pagesize = getpagesize ();
     696    if (! g_regionsize)
     697        g_regionsize = getregionsize ();
     698    /* Free this */
     699    if (! VirtualFree (ptr, 0,
     700                       MEM_RELEASE))
     701        goto munmap_exit;
     702    rc = 0;
     703munmap_exit:
     704    /* Release spin lock */
     705    slrelease (&g_sl);
     706    return rc;
     707}
     708
     709#endif
     710
  • trunk/libtransmission/platform.h

    r6559 r6702  
    5959tr_lockfile_state_t tr_lockfile       ( const char * filename );
    6060
     61#ifdef WIN32
     62void *mmap (void *ptr, long size, long prot, long type, long handle, long arg);
     63long munmap (void *ptr, long size);
    6164#endif
     65
     66#endif
Note: See TracChangeset for help on using the changeset viewer.