Changeset 11 for trunk/mmap.c


Ignore:
Timestamp:
2006-05-24T09:52:48+02:00 (9 years ago)
Author:
moo
Message:

make it build on windows vc compiler

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/mmap.c

    r1 r11  
    77#include <string.h> 
    88#include <stdlib.h> 
     9 
    910/* mmap */ 
    10 #include <unistd.h> 
     11#ifdef ZEND_WIN32 
     12#   define ftruncate chsize 
     13#   define getuid() 0 
     14#   define XcacheCreateFileMapping(size, perm, name) \ 
     15        CreateFileMapping(INVALID_HANDLE_VALUE, NULL, perm, (sizeof(xc_shmsize_t) > 4) ? size >> 32 : 0, size & 0xffffffff, name) 
     16#   define XCACHE_MAP_FAILED NULL 
     17#   define munmap(p, s) UnmapViewOfFile(p) 
     18#else 
     19#   include <unistd.h> 
     20#   define XCACHE_MAP_FAILED MAP_FAILED 
     21#endif 
     22 
    1123#include <sys/types.h> 
    1224#include <sys/stat.h> 
    1325#include <fcntl.h> 
     26 
     27#ifndef ZEND_WIN32 
    1428#include <sys/mman.h> 
     29#endif 
    1530 
    1631#include "php.h" 
     
    2742    long  diff; 
    2843    xc_shmsize_t size; 
     44    char *name; 
     45#ifdef ZEND_WIN32 
     46    HANDLE hmap; 
     47    HANDLE hmap_ro; 
     48#endif 
    2949}; 
    3050 
     51#undef NDEBUG 
    3152#ifdef ALLOC_DEBUG 
    32 #   undef NDEBUG 
    3353#   define inline 
    3454#else 
     
    3858/* }}} */ 
    3959#define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0) 
     60#define PTR_ADD(ptr, v) (((char *) (ptr)) + (v)) 
     61#define PTR_SUB(ptr, v) (((char *) (ptr)) - (v)) 
    4062 
    4163int xc_shm_can_readonly(xc_shm_t *shm) /* {{{ */ 
     
    5880    if (shm->diff) { 
    5981        assert(xc_shm_is_readonly(p)); 
    60         p = p - shm->diff; 
     82        p = PTR_SUB(p, -shm->diff); 
    6183    } 
    6284    assert(xc_shm_is_readwrite(p)); 
     
    6890    assert(xc_shm_is_readwrite(p)); 
    6991    if (shm->diff) { 
    70         p = p + shm->diff; 
     92        p = PTR_ADD(p, shm->diff); 
    7193        assert(xc_shm_is_readonly(p)); 
    7294    } 
     
    90112        */ 
    91113    } 
     114#ifdef ZEND_WIN32 
     115    if (shm->hmap) { 
     116        CloseHandle(shm->hmap); 
     117    } 
     118    if (shm->hmap_ro) { 
     119        CloseHandle(shm->hmap_ro); 
     120    } 
     121#endif 
     122 
     123    if (shm->name) { 
     124#ifdef __CYGWIN__ 
     125        unlink(shm->name); 
     126#endif 
     127        free(shm->name); 
     128    } 
    92129    /* 
    93130    shm->size = NULL; 
     
    102139{ 
    103140    xc_shm_t *shm = NULL; 
    104     int fd; 
     141    int fd = -1; 
    105142    int ro_ok; 
    106143    volatile void *romem; 
    107     int created = 0; 
     144    char tmpname[sizeof("/tmp/xcache") - 1 + 100]; 
    108145 
    109146    CHECK(shm = calloc(1, sizeof(xc_shm_t)), "shm OOM"); 
    110147    shm->size = size; 
     148 
    111149    if (path == NULL || !path[0]) { 
    112         path = "/tmp/xcache"; 
    113     } 
    114     fd = open(path, O_RDWR, S_IRUSR | S_IWUSR); 
     150        static int inc = 0; 
     151        snprintf(tmpname, sizeof(tmpname) - 1, "/tmp/xcache.%d.%d.%d", (int) getuid(), inc ++, rand()); 
     152        path = tmpname; 
     153    } 
     154 
     155    shm->name = strdup(path); 
     156 
     157#ifndef ZEND_WIN32 
     158#   define XCACHE_MMAP_PERMISSION (S_IRUSR | S_IWUSR) 
     159    fd = open(shm->name, O_RDWR, XCACHE_MMAP_PERMISSION); 
    115160    if (fd == -1) { 
    116         created = 1; 
    117         fd = open(path, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); 
     161        fd = open(shm->name, O_CREAT | O_RDWR, XCACHE_MMAP_PERMISSION); 
    118162        if (fd == -1) { 
    119             if (created) { 
    120                 unlink(path); 
    121             } 
    122163            goto err; 
    123164        } 
    124165    } 
    125166    ftruncate(fd, size); 
    126  
     167#endif 
     168 
     169#ifdef ZEND_WIN32 
     170    shm->hmap = XcacheCreateFileMapping(size, PAGE_READWRITE, shm->name); 
     171    shm->ptr = (LPSTR) MapViewOfFile(shm->hmap, FILE_MAP_WRITE, 0, 0, 0); 
     172#else 
    127173    shm->ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 
    128     if (shm->ptr == MAP_FAILED) { 
     174#endif 
     175 
     176    if (shm->ptr == XCACHE_MAP_FAILED) { 
    129177        shm->ptr = NULL; 
    130         close(fd); 
    131         if (created) { 
    132             unlink(path); 
    133         } 
    134178        goto err; 
    135179    } 
     
    137181    ro_ok = 0; 
    138182    if (readonly_protection) { 
     183#ifdef ZEND_WIN32 
     184        shm->hmap_ro = XcacheCreateFileMapping(size, PAGE_READONLY, shm->name); 
     185        shm->ptr_ro = (LPSTR) MapViewOfFile(shm->hmap_ro, FILE_MAP_READ, 0, 0, 0); 
     186#else 
    139187        shm->ptr_ro = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); 
     188#endif 
    140189        romem = shm->ptr_ro; 
    141190 
    142191        /* {{{ check if ptr_ro works */ 
    143192        do { 
    144             if (shm->ptr_ro == MAP_FAILED || shm->ptr_ro == shm->ptr) { 
     193            if (shm->ptr_ro == XCACHE_MAP_FAILED || shm->ptr_ro == shm->ptr) { 
    145194                break; 
    146195            } 
     
    158207 
    159208    if (ro_ok) { 
    160         shm->diff = shm->ptr_ro - shm->ptr; 
     209        shm->diff = PTR_SUB(shm->ptr_ro, (char *) shm->ptr); 
    161210        assert(abs(shm->diff) >= size); 
    162211    } 
    163212    else { 
    164         if (shm->ptr_ro != MAP_FAILED) { 
     213        if (shm->ptr_ro != XCACHE_MAP_FAILED) { 
    165214            munmap(shm->ptr_ro, size); 
    166215        } 
     
    171220 
    172221    close(fd); 
    173     if (created) { 
    174         unlink(path); 
    175     } 
     222#ifndef __CYGWIN__ 
     223    unlink(shm->name); 
     224#endif 
    176225 
    177226    return shm; 
    178227 
    179228err: 
     229    if (fd != -1) { 
     230        close(fd); 
     231    } 
    180232    if (shm) { 
    181233        xc_shm_destroy(shm); 
Note: See TracChangeset for help on using the changeset viewer.