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

(win32) Add mmap implementation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.