shared_alloc_mmap.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #include "zend_shared_alloc.h"
  22. #ifdef USE_MMAP
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <sys/mman.h>
  28. #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
  29. # define MAP_ANONYMOUS MAP_ANON
  30. #endif
  31. static int create_segments(size_t requested_size, zend_shared_segment ***shared_segments_p, int *shared_segments_count, char **error_in)
  32. {
  33. zend_shared_segment *shared_segment;
  34. *shared_segments_count = 1;
  35. *shared_segments_p = (zend_shared_segment **) calloc(1, sizeof(zend_shared_segment) + sizeof(void *));
  36. if (!*shared_segments_p) {
  37. *error_in = "calloc";
  38. return ALLOC_FAILURE;
  39. }
  40. shared_segment = (zend_shared_segment *)((char *)(*shared_segments_p) + sizeof(void *));
  41. (*shared_segments_p)[0] = shared_segment;
  42. shared_segment->p = mmap(0, requested_size, PROT_READ | PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
  43. if (shared_segment->p == MAP_FAILED) {
  44. *error_in = "mmap";
  45. return ALLOC_FAILURE;
  46. }
  47. shared_segment->pos = 0;
  48. shared_segment->size = requested_size;
  49. return ALLOC_SUCCESS;
  50. }
  51. static int detach_segment(zend_shared_segment *shared_segment)
  52. {
  53. munmap(shared_segment->p, shared_segment->size);
  54. return 0;
  55. }
  56. static size_t segment_type_size(void)
  57. {
  58. return sizeof(zend_shared_segment);
  59. }
  60. zend_shared_memory_handlers zend_alloc_mmap_handlers = {
  61. create_segments,
  62. detach_segment,
  63. segment_type_size
  64. };
  65. #endif /* USE_MMAP */