2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2008 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Rasmus Lerdorf <rasmus@php.net> |
16 +----------------------------------------------------------------------+
18 This software was contributed to PHP by Community Connect Inc. in 2002
19 and revised in 2005 by Yahoo! Inc. to add support for PHP 5.1.
20 Future revisions and derivatives of this source code must acknowledge
21 Community Connect Inc. as the original contributor of this module by
22 leaving this note intact in the source code.
24 All other licensing and usage conditions are those of the PHP Group.
28 /* $Id: apc_mmap.c,v 3.7.2.2 2008/05/11 18:57:00 rasmus Exp $ */
35 #include <sys/types.h>
39 * Some operating systems (like FreeBSD) have a MAP_NOSYNC flag that
40 * tells whatever update daemons might be running to not flush dirty
41 * vm pages to disk unless absolutely necessary. My guess is that
42 * most systems that don't have this probably default to only synching
43 * to disk when absolutely necessary.
49 void *apc_mmap(char *file_mask, size_t size)
51 void* shmaddr; /* the shared memory address */
53 /* If no filename was provided, do an anonymous mmap */
54 if(!file_mask || (file_mask && !strlen(file_mask))) {
55 shmaddr = (void *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
60 * If the filemask contains .shm we try to do a POSIX-compliant shared memory
61 * backed mmap which should avoid synchs on some platforms. At least on
62 * FreeBSD this implies MAP_NOSYNC and on Linux it is equivalent of mmap'ing
63 * a file in a mounted shmfs. For this to work on Linux you need to make sure
64 * you actually have shmfs mounted. Also on Linux, make sure the file_mask you
65 * pass in has a leading / and no other /'s. eg. /apc.shm.XXXXXX
66 * On FreeBSD these are mapped onto the regular filesystem so you can put whatever
69 if(strstr(file_mask,".shm")) {
71 fd = shm_open(file_mask, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
73 apc_eprint("apc_mmap: shm_open on %s failed:", file_mask);
76 if (ftruncate(fd, size) < 0) {
78 shm_unlink(file_mask);
79 apc_eprint("apc_mmap: ftruncate failed:");
82 shmaddr = (void *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
83 shm_unlink(file_mask);
87 * Support anonymous mmap through the /dev/zero interface as well
89 else if(!strcmp(file_mask,"/dev/zero")) {
90 fd = open("/dev/zero", O_RDWR, S_IRUSR | S_IWUSR);
92 apc_eprint("apc_mmap: open on /dev/zero failed:");
95 shmaddr = (void *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
99 * Otherwise we do a normal filesystem mmap
102 fd = mkstemp(file_mask);
104 apc_eprint("apc_mmap: mkstemp on %s failed:", file_mask);
107 if (ftruncate(fd, size) < 0) {
110 apc_eprint("apc_mmap: ftruncate failed:");
112 shmaddr = (void *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_NOSYNC, fd, 0);
117 if((long)shmaddr == -1) {
118 apc_eprint("apc_mmap: mmap failed:");
123 void apc_unmap(void* shmaddr, size_t size)
125 if (munmap(shmaddr, size) < 0) {
126 apc_wprint("apc_unmap: munmap failed:");
137 * vim600: expandtab sw=4 ts=4 sts=4 fdm=marker
138 * vim<600: expandtab sw=4 ts=4 sts=4