source: trunk/mmap.c @ 83

Last change on this file since 83 was 83, checked in by moo, 8 years ago

fix win32 emulated anonymous mmap

File size: 5.4 KB
RevLine 
[1]1
2#undef ALLOC_DEBUG
3
4#include <stdio.h>
5#include <assert.h>
6#include <limits.h>
7#include <string.h>
8#include <stdlib.h>
[11]9
[1]10/* mmap */
[11]11#ifdef ZEND_WIN32
12#   define ftruncate chsize
13#   define getuid() 0
[83]14#   include <process.h>
[57]15#   define XCacheCreateFileMapping(size, perm, name) \
[11]16        CreateFileMapping(INVALID_HANDLE_VALUE, NULL, perm, (sizeof(xc_shmsize_t) > 4) ? size >> 32 : 0, size & 0xffffffff, name)
17#   define XCACHE_MAP_FAILED NULL
18#   define munmap(p, s) UnmapViewOfFile(p)
19#else
20#   include <unistd.h>
[79]21/* make sure to mark(change) it to NULL to keep consistent */
[11]22#   define XCACHE_MAP_FAILED MAP_FAILED
23#endif
24
[1]25#include <sys/types.h>
26#include <sys/stat.h>
27#include <fcntl.h>
[11]28
29#ifndef ZEND_WIN32
[1]30#include <sys/mman.h>
[11]31#endif
[1]32
33#include "php.h"
34#include "myshm.h"
35
36#ifndef max
37#define max(a, b) ((a) < (b) ? (b) : (a))
38#endif
39
40// {{{ xc_shm_t
41struct _xc_shm_t {
42    void *ptr;
43    void *ptr_ro;
44    long  diff;
45    xc_shmsize_t size;
[11]46    char *name;
[57]47    int newfile;
[11]48#ifdef ZEND_WIN32
49    HANDLE hmap;
50    HANDLE hmap_ro;
51#endif
[1]52};
53
[11]54#undef NDEBUG
[1]55#ifdef ALLOC_DEBUG
56#   define inline
57#else
58#   define NDEBUG
59#endif
60#include <assert.h>
61/* }}} */
62#define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0)
[11]63#define PTR_ADD(ptr, v) (((char *) (ptr)) + (v))
64#define PTR_SUB(ptr, v) (((char *) (ptr)) - (v))
[1]65
66int xc_shm_can_readonly(xc_shm_t *shm) /* {{{ */
67{
68    return shm->ptr_ro != NULL;
69}
70/* }}} */
71int xc_shm_is_readwrite(xc_shm_t *shm, const void *p) /* {{{ */
72{
73    return p >= shm->ptr && (char *)p < (char *)shm->ptr + shm->size;
74}
75/* }}} */
76int xc_shm_is_readonly(xc_shm_t *shm, const void *p) /* {{{ */
77{
78    return xc_shm_can_readonly(shm) && p >= shm->ptr_ro && (char *)p < (char *)shm->ptr_ro + shm->size;
79}
80/* }}} */
81void *xc_shm_to_readwrite(xc_shm_t *shm, void *p) /* {{{ */
82{
83    if (shm->diff) {
84        assert(xc_shm_is_readonly(p));
[59]85        p = PTR_SUB(p, shm->diff);
[1]86    }
87    assert(xc_shm_is_readwrite(p));
88    return p;
89}
90/* }}} */
91void *xc_shm_to_readonly(xc_shm_t *shm, void *p) /* {{{ */
92{
93    assert(xc_shm_is_readwrite(p));
94    if (shm->diff) {
[11]95        p = PTR_ADD(p, shm->diff);
[1]96        assert(xc_shm_is_readonly(p));
97    }
98    return p;
99}
100/* }}} */
101
102void xc_shm_destroy(xc_shm_t *shm) /* {{{ */
103{
104    if (shm->ptr_ro) {
105        munmap(shm->ptr_ro, shm->size);
106        /*
107        shm->ptr_ro = NULL;
108        */
109    }
110    if (shm->ptr) {
111        /* shm->size depends on shm->ptr */
112        munmap(shm->ptr, shm->size);
113        /*
114        shm->ptr = NULL;
115        */
116    }
[11]117#ifdef ZEND_WIN32
118    if (shm->hmap) {
119        CloseHandle(shm->hmap);
120    }
121    if (shm->hmap_ro) {
122        CloseHandle(shm->hmap_ro);
123    }
124#endif
125
126    if (shm->name) {
127#ifdef __CYGWIN__
[57]128        if (shm->newfile) {
129            unlink(shm->name);
130        }
[11]131#endif
132        free(shm->name);
133    }
[1]134    /*
135    shm->size = NULL;
136    shm->diff = 0;
137    */
138
139    free(shm);
140    return;
141}
142/* }}} */
143xc_shm_t *xc_shm_init(const char *path, xc_shmsize_t size, zend_bool readonly_protection) /* {{{ */
144{
[21]145#ifdef ZEND_WIN32
146#   define TMP_PATH "XCache"
147#else
148#   define TMP_PATH "/tmp/XCache"
149#endif
[1]150    xc_shm_t *shm = NULL;
[11]151    int fd = -1;
[1]152    int ro_ok;
153    volatile void *romem;
[21]154    char tmpname[sizeof(TMP_PATH) - 1 + 100];
[1]155
156    CHECK(shm = calloc(1, sizeof(xc_shm_t)), "shm OOM");
157    shm->size = size;
[11]158
[1]159    if (path == NULL || !path[0]) {
[11]160        static int inc = 0;
[83]161        snprintf(tmpname, sizeof(tmpname) - 1, "%s.%d.%d.%d.%d", TMP_PATH, (int) getuid(), (int) getpid(), inc ++, rand());
[11]162        path = tmpname;
[1]163    }
[83]164#ifdef ZEND_WIN32
165    else {
166        static int inc2 = 0;
167        snprintf(tmpname, sizeof(tmpname) - 1, "%s.%d.%d.%d.%d", path, (int) getuid(), (int) getpid(), inc2 ++, rand());
168        path = tmpname;
169    }
170#endif
[11]171
172    shm->name = strdup(path);
173
174#ifndef ZEND_WIN32
175#   define XCACHE_MMAP_PERMISSION (S_IRUSR | S_IWUSR)
176    fd = open(shm->name, O_RDWR, XCACHE_MMAP_PERMISSION);
[1]177    if (fd == -1) {
[61]178        /* do not create file in /dev */
179        if (strncmp(shm->name, "/dev", 4) == 0) {
180            goto err;
181        }
[11]182        fd = open(shm->name, O_CREAT | O_RDWR, XCACHE_MMAP_PERMISSION);
[57]183        shm->newfile = 1;
[1]184        if (fd == -1) {
185            goto err;
186        }
187    }
188    ftruncate(fd, size);
[11]189#endif
[1]190
[11]191#ifdef ZEND_WIN32
[57]192    shm->hmap = XCacheCreateFileMapping(size, PAGE_READWRITE, shm->name);
[11]193    shm->ptr = (LPSTR) MapViewOfFile(shm->hmap, FILE_MAP_WRITE, 0, 0, 0);
194#else
[1]195    shm->ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
[11]196#endif
197
198    if (shm->ptr == XCACHE_MAP_FAILED) {
[1]199        shm->ptr = NULL;
200        goto err;
201    }
202
[79]203    /* {{{ readonly protection, mmap it readonly and check if ptr_ro works */
[1]204    if (readonly_protection) {
[79]205        ro_ok = 0;
206
[11]207#ifdef ZEND_WIN32
[57]208        shm->hmap_ro = XCacheCreateFileMapping(size, PAGE_READONLY, shm->name);
[11]209        shm->ptr_ro = (LPSTR) MapViewOfFile(shm->hmap_ro, FILE_MAP_READ, 0, 0, 0);
210#else
[1]211        shm->ptr_ro = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
[11]212#endif
[79]213        if (shm->ptr_ro == XCACHE_MAP_FAILED) {
214            shm->ptr_ro = NULL;
215        }
[1]216        romem = shm->ptr_ro;
217
218        do {
[79]219            if (romem == NULL || romem == shm->ptr) {
[1]220                break;
221            }
222            *(char *)shm->ptr = 1;
223            if (*(char *)romem != 1) {
224                break;
225            }
226            *(char *)shm->ptr = 2;
227            if (*(char *)romem != 2) {
228                break;
229            }
230            ro_ok = 1;
231        } while (0);
232
[79]233        if (ro_ok) {
234            shm->diff = PTR_SUB(shm->ptr_ro, (char *) shm->ptr);
235            /* no overlap */
236            assert(abs(shm->diff) >= size);
[1]237        }
[79]238        else {
239            if (shm->ptr_ro) {
240                munmap(shm->ptr_ro, size);
241            }
242#ifdef ZEND_WIN32
243            if (shm->hmap_ro) {
244                CloseHandle(shm->hmap_ro);
245            }
246#endif
247            shm->ptr_ro = NULL;
248            shm->diff = 0;
249        }
[1]250    }
[79]251
[1]252    /* }}} */
253
254    close(fd);
[11]255#ifndef __CYGWIN__
[57]256    if (shm->newfile) {
257        unlink(shm->name);
258    }
[11]259#endif
[1]260
261    return shm;
262
263err:
[11]264    if (fd != -1) {
265        close(fd);
266    }
[1]267    if (shm) {
268        xc_shm_destroy(shm);
269    }
270    return NULL;
271}
272/* }}} */
273
274void *xc_shm_ptr(xc_shm_t *shm) /* {{{ */
275{
276    return shm->ptr;
277}
278/* }}} */
Note: See TracBrowser for help on using the repository browser.