root/ext/opcache/shared_alloc_mmap.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. create_segments
  2. detach_segment
  3. segment_type_size

   1 /*
   2    +----------------------------------------------------------------------+
   3    | Zend OPcache                                                         |
   4    +----------------------------------------------------------------------+
   5    | Copyright (c) 1998-2016 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: Andi Gutmans <andi@zend.com>                                |
  16    |          Zeev Suraski <zeev@zend.com>                                |
  17    |          Stanislav Malyshev <stas@zend.com>                          |
  18    |          Dmitry Stogov <dmitry@zend.com>                             |
  19    +----------------------------------------------------------------------+
  20 */
  21 
  22 #include "zend_shared_alloc.h"
  23 
  24 #ifdef USE_MMAP
  25 
  26 #include <sys/types.h>
  27 #include <sys/stat.h>
  28 #include <stdio.h>
  29 #include <stdlib.h>
  30 #include <sys/mman.h>
  31 
  32 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
  33 # define MAP_ANONYMOUS MAP_ANON
  34 #endif
  35 
  36 static int create_segments(size_t requested_size, zend_shared_segment ***shared_segments_p, int *shared_segments_count, char **error_in)
  37 {
  38         zend_shared_segment *shared_segment;
  39 
  40         *shared_segments_count = 1;
  41         *shared_segments_p = (zend_shared_segment **) calloc(1, sizeof(zend_shared_segment) + sizeof(void *));
  42         if (!*shared_segments_p) {
  43                 *error_in = "calloc";
  44                 return ALLOC_FAILURE;
  45         }
  46         shared_segment = (zend_shared_segment *)((char *)(*shared_segments_p) + sizeof(void *));
  47         (*shared_segments_p)[0] = shared_segment;
  48 
  49 #ifdef MAP_HUGETLB
  50         /* Try to allocate huge pages first to reduce dTLB misses.
  51          * OS has to be configured properly
  52          * (e.g. https://wiki.debian.org/Hugepages#Enabling_HugeTlbPage)
  53          * You may verify huge page usage with the following command:
  54          * `grep "Huge" /proc/meminfo`
  55          */
  56         shared_segment->p = mmap(0, requested_size, PROT_READ | PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS|MAP_HUGETLB, -1, 0);
  57         if (shared_segment->p != MAP_FAILED) {
  58                 shared_segment->pos = 0;
  59                 shared_segment->size = requested_size;
  60 
  61                 return ALLOC_SUCCESS;
  62         }
  63 #endif
  64 
  65         shared_segment->p = mmap(0, requested_size, PROT_READ | PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
  66         if (shared_segment->p == MAP_FAILED) {
  67                 *error_in = "mmap";
  68                 return ALLOC_FAILURE;
  69         }
  70 
  71         shared_segment->pos = 0;
  72         shared_segment->size = requested_size;
  73 
  74         return ALLOC_SUCCESS;
  75 }
  76 
  77 static int detach_segment(zend_shared_segment *shared_segment)
  78 {
  79         munmap(shared_segment->p, shared_segment->size);
  80         return 0;
  81 }
  82 
  83 static size_t segment_type_size(void)
  84 {
  85         return sizeof(zend_shared_segment);
  86 }
  87 
  88 zend_shared_memory_handlers zend_alloc_mmap_handlers = {
  89         create_segments,
  90         detach_segment,
  91         segment_type_size
  92 };
  93 
  94 #endif /* USE_MMAP */

/* [<][>][^][v][top][bottom][index][help] */