zend_shared_alloc.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend OPcache |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 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. | https://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@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. | Stanislav Malyshev <stas@zend.com> |
  18. | Dmitry Stogov <dmitry@php.net> |
  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_MMAP_POSIX
  34. # define USE_SHM_OPEN 1
  35. # endif
  36. # ifdef HAVE_SHM_IPC
  37. # define USE_SHM 1
  38. # endif
  39. # ifdef HAVE_SHM_MMAP_ANON
  40. # define USE_MMAP 1
  41. # endif
  42. #elif defined(__sparc) || defined(__sun)
  43. # ifdef HAVE_SHM_MMAP_POSIX
  44. # define USE_SHM_OPEN 1
  45. # endif
  46. # ifdef HAVE_SHM_IPC
  47. # define USE_SHM 1
  48. # endif
  49. # if defined(__i386)
  50. # ifdef HAVE_SHM_MMAP_ANON
  51. # define USE_MMAP 1
  52. # endif
  53. # endif
  54. #else
  55. # ifdef HAVE_SHM_MMAP_POSIX
  56. # define USE_SHM_OPEN 1
  57. # endif
  58. # ifdef HAVE_SHM_MMAP_ANON
  59. # define USE_MMAP 1
  60. # endif
  61. # ifdef HAVE_SHM_IPC
  62. # define USE_SHM 1
  63. # endif
  64. #endif
  65. #define ALLOC_FAILURE 0
  66. #define ALLOC_SUCCESS 1
  67. #define FAILED_REATTACHED 2
  68. #define SUCCESSFULLY_REATTACHED 4
  69. #define ALLOC_FAIL_MAPPING 8
  70. #define ALLOC_FALLBACK 9
  71. typedef struct _zend_shared_segment {
  72. size_t size;
  73. size_t end;
  74. size_t pos; /* position for simple stack allocator */
  75. void *p;
  76. } zend_shared_segment;
  77. typedef int (*create_segments_t)(size_t requested_size, zend_shared_segment ***shared_segments, int *shared_segment_count, char **error_in);
  78. typedef int (*detach_segment_t)(zend_shared_segment *shared_segment);
  79. typedef struct {
  80. create_segments_t create_segments;
  81. detach_segment_t detach_segment;
  82. size_t (*segment_type_size)(void);
  83. } zend_shared_memory_handlers;
  84. typedef struct _handler_entry {
  85. const char *name;
  86. zend_shared_memory_handlers *handler;
  87. } zend_shared_memory_handler_entry;
  88. typedef struct _zend_shared_memory_state {
  89. int *positions; /* current positions for each segment */
  90. size_t shared_free; /* amount of free shared memory */
  91. } zend_shared_memory_state;
  92. typedef struct _zend_smm_shared_globals {
  93. /* Shared Memory Manager */
  94. zend_shared_segment **shared_segments;
  95. /* Number of allocated shared segments */
  96. int shared_segments_count;
  97. /* Amount of free shared memory */
  98. size_t shared_free;
  99. /* Amount of shared memory allocated by garbage */
  100. size_t wasted_shared_memory;
  101. /* No more shared memory flag */
  102. bool memory_exhausted;
  103. /* Saved Shared Allocator State */
  104. zend_shared_memory_state shared_memory_state;
  105. /* Pointer to the application's shared data structures */
  106. void *app_shared_globals;
  107. /* Reserved shared memory */
  108. void *reserved;
  109. size_t reserved_size;
  110. } zend_smm_shared_globals;
  111. extern zend_smm_shared_globals *smm_shared_globals;
  112. #define ZSMMG(element) (smm_shared_globals->element)
  113. #define SHARED_ALLOC_REATTACHED (SUCCESS+1)
  114. int zend_shared_alloc_startup(size_t requested_size, size_t reserved_size);
  115. void zend_shared_alloc_shutdown(void);
  116. /* allocate shared memory block */
  117. void *zend_shared_alloc_pages(size_t requested_size);
  118. void *zend_shared_alloc(size_t size);
  119. /* copy into shared memory */
  120. void *zend_shared_memdup_get_put_free(void *source, size_t size);
  121. void *zend_shared_memdup_put_free(void *source, size_t size);
  122. void *zend_shared_memdup_free(void *source, size_t size);
  123. void *zend_shared_memdup_get_put(void *source, size_t size);
  124. void *zend_shared_memdup_put(void *source, size_t size);
  125. void *zend_shared_memdup(void *source, size_t size);
  126. int zend_shared_memdup_size(void *p, size_t size);
  127. int zend_accel_in_shm(void *ptr);
  128. typedef union _align_test {
  129. void *ptr;
  130. double dbl;
  131. zend_long lng;
  132. } align_test;
  133. #if ZEND_GCC_VERSION >= 2000
  134. # define PLATFORM_ALIGNMENT (__alignof__(align_test) < 8 ? 8 : __alignof__(align_test))
  135. #else
  136. # define PLATFORM_ALIGNMENT (sizeof(align_test))
  137. #endif
  138. #define ZEND_ALIGNED_SIZE(size) \
  139. ZEND_MM_ALIGNED_SIZE_EX(size, PLATFORM_ALIGNMENT)
  140. /* exclusive locking */
  141. void zend_shared_alloc_lock(void);
  142. void zend_shared_alloc_unlock(void); /* returns the allocated size during lock..unlock */
  143. void zend_shared_alloc_safe_unlock(void);
  144. /* old/new mapping functions */
  145. void zend_shared_alloc_init_xlat_table(void);
  146. void zend_shared_alloc_destroy_xlat_table(void);
  147. void zend_shared_alloc_clear_xlat_table(void);
  148. uint32_t zend_shared_alloc_checkpoint_xlat_table(void);
  149. void zend_shared_alloc_restore_xlat_table(uint32_t checkpoint);
  150. void zend_shared_alloc_register_xlat_entry(const void *old, const void *new);
  151. void *zend_shared_alloc_get_xlat_entry(const void *old);
  152. size_t zend_shared_alloc_get_free_memory(void);
  153. void zend_shared_alloc_save_state(void);
  154. void zend_shared_alloc_restore_state(void);
  155. const char *zend_accel_get_shared_model(void);
  156. /* memory write protection */
  157. void zend_accel_shared_protect(int mode);
  158. #ifdef USE_MMAP
  159. extern zend_shared_memory_handlers zend_alloc_mmap_handlers;
  160. #endif
  161. #ifdef USE_SHM
  162. extern zend_shared_memory_handlers zend_alloc_shm_handlers;
  163. #endif
  164. #ifdef USE_SHM_OPEN
  165. extern zend_shared_memory_handlers zend_alloc_posix_handlers;
  166. #endif
  167. #ifdef ZEND_WIN32
  168. extern zend_shared_memory_handlers zend_alloc_win32_handlers;
  169. void zend_shared_alloc_create_lock(void);
  170. void zend_shared_alloc_lock_win32(void);
  171. void zend_shared_alloc_unlock_win32(void);
  172. #endif
  173. #endif /* ZEND_SHARED_ALLOC_H */