zend_shared_alloc.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #ifndef ZEND_SHARED_ALLOC_H
  22. #define ZEND_SHARED_ALLOC_H
  23. #include "zend.h"
  24. #include "ZendAccelerator.h"
  25. #if defined(__APPLE__) && defined(__MACH__) /* darwin */
  26. # ifdef HAVE_SHM_MMAP_POSIX
  27. # define USE_SHM_OPEN 1
  28. # endif
  29. # ifdef HAVE_SHM_MMAP_ANON
  30. # define USE_MMAP 1
  31. # endif
  32. #elif defined(__linux__) || defined(_AIX)
  33. # ifdef HAVE_SHM_IPC
  34. # define USE_SHM 1
  35. # endif
  36. # ifdef HAVE_SHM_MMAP_ANON
  37. # define USE_MMAP 1
  38. # endif
  39. #elif defined(__sparc) || defined(__sun)
  40. # ifdef HAVE_SHM_MMAP_POSIX
  41. # define USE_SHM_OPEN 1
  42. # endif
  43. # ifdef HAVE_SHM_IPC
  44. # define USE_SHM 1
  45. # endif
  46. # if defined(__i386)
  47. # ifdef HAVE_SHM_MMAP_ANON
  48. # define USE_MMAP 1
  49. # endif
  50. # endif
  51. #else
  52. # ifdef HAVE_SHM_MMAP_POSIX
  53. # define USE_SHM_OPEN 1
  54. # endif
  55. # ifdef HAVE_SHM_MMAP_ANON
  56. # define USE_MMAP 1
  57. # endif
  58. # ifdef HAVE_SHM_IPC
  59. # define USE_SHM 1
  60. # endif
  61. #endif
  62. #define ALLOC_FAILURE 0
  63. #define ALLOC_SUCCESS 1
  64. #define FAILED_REATTACHED 2
  65. #define SUCCESSFULLY_REATTACHED 4
  66. #define ALLOC_FAIL_MAPPING 8
  67. typedef struct _zend_shared_segment {
  68. size_t size;
  69. size_t pos; /* position for simple stack allocator */
  70. void *p;
  71. } zend_shared_segment;
  72. typedef int (*create_segments_t)(size_t requested_size, zend_shared_segment ***shared_segments, int *shared_segment_count, char **error_in);
  73. typedef int (*detach_segment_t)(zend_shared_segment *shared_segment);
  74. typedef struct {
  75. create_segments_t create_segments;
  76. detach_segment_t detach_segment;
  77. size_t (*segment_type_size)(void);
  78. } zend_shared_memory_handlers;
  79. typedef struct _handler_entry {
  80. const char *name;
  81. zend_shared_memory_handlers *handler;
  82. } zend_shared_memory_handler_entry;
  83. typedef struct _zend_shared_memory_state {
  84. int *positions; /* current positions for each segment */
  85. size_t shared_free; /* amount of free shared memory */
  86. } zend_shared_memory_state;
  87. typedef struct _zend_smm_shared_globals {
  88. /* Shared Memory Manager */
  89. zend_shared_segment **shared_segments;
  90. /* Number of allocated shared segments */
  91. int shared_segments_count;
  92. /* Amount of free shared memory */
  93. size_t shared_free;
  94. /* Amount of shared memory allocated by garbage */
  95. int wasted_shared_memory;
  96. /* No more shared memory flag */
  97. zend_bool memory_exhausted;
  98. /* Saved Shared Allocator State */
  99. zend_shared_memory_state shared_memory_state;
  100. /* Pointer to the application's shared data structures */
  101. void *app_shared_globals;
  102. } zend_smm_shared_globals;
  103. extern zend_smm_shared_globals *smm_shared_globals;
  104. #define ZSMMG(element) (smm_shared_globals->element)
  105. #define SHARED_ALLOC_REATTACHED (SUCCESS+1)
  106. int zend_shared_alloc_startup(size_t requested_size);
  107. void zend_shared_alloc_shutdown(void);
  108. /* allocate shared memory block */
  109. void *zend_shared_alloc(size_t size);
  110. /* copy into shared memory */
  111. void *_zend_shared_memdup(void *p, size_t size, zend_bool free_source TSRMLS_DC);
  112. int zend_shared_memdup_size(void *p, size_t size);
  113. typedef union _align_test {
  114. void *ptr;
  115. double dbl;
  116. long lng;
  117. } align_test;
  118. #if ZEND_GCC_VERSION >= 2000
  119. # define PLATFORM_ALIGNMENT (__alignof__ (align_test))
  120. #else
  121. # define PLATFORM_ALIGNMENT (sizeof(align_test))
  122. #endif
  123. #define ZEND_ALIGNED_SIZE(size) \
  124. ((size + PLATFORM_ALIGNMENT - 1) & ~(PLATFORM_ALIGNMENT - 1))
  125. /* exclusive locking */
  126. void zend_shared_alloc_lock(TSRMLS_D);
  127. void zend_shared_alloc_unlock(TSRMLS_D); /* returns the allocated size during lock..unlock */
  128. void zend_shared_alloc_safe_unlock(TSRMLS_D);
  129. /* old/new mapping functions */
  130. void zend_shared_alloc_clear_xlat_table(void);
  131. void zend_shared_alloc_register_xlat_entry(const void *old, const void *new);
  132. void *zend_shared_alloc_get_xlat_entry(const void *old);
  133. size_t zend_shared_alloc_get_free_memory(void);
  134. void zend_shared_alloc_save_state(void);
  135. void zend_shared_alloc_restore_state(void);
  136. const char *zend_accel_get_shared_model(void);
  137. /* memory write protection */
  138. void zend_accel_shared_protect(int mode TSRMLS_DC);
  139. #ifdef USE_MMAP
  140. extern zend_shared_memory_handlers zend_alloc_mmap_handlers;
  141. #endif
  142. #ifdef USE_SHM
  143. extern zend_shared_memory_handlers zend_alloc_shm_handlers;
  144. #endif
  145. #ifdef USE_SHM_OPEN
  146. extern zend_shared_memory_handlers zend_alloc_posix_handlers;
  147. #endif
  148. #ifdef ZEND_WIN32
  149. extern zend_shared_memory_handlers zend_alloc_win32_handlers;
  150. void zend_shared_alloc_create_lock(void);
  151. void zend_shared_alloc_lock_win32(void);
  152. void zend_shared_alloc_unlock_win32(void);
  153. #endif
  154. #endif /* ZEND_SHARED_ALLOC_H */