zend_shared_alloc.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend OPcache |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2018 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@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_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. #define ALLOC_FALLBACK 9
  68. typedef struct _zend_shared_segment {
  69. size_t size;
  70. size_t pos; /* position for simple stack allocator */
  71. void *p;
  72. } zend_shared_segment;
  73. typedef int (*create_segments_t)(size_t requested_size, zend_shared_segment ***shared_segments, int *shared_segment_count, char **error_in);
  74. typedef int (*detach_segment_t)(zend_shared_segment *shared_segment);
  75. typedef struct {
  76. create_segments_t create_segments;
  77. detach_segment_t detach_segment;
  78. size_t (*segment_type_size)(void);
  79. } zend_shared_memory_handlers;
  80. typedef struct _handler_entry {
  81. const char *name;
  82. zend_shared_memory_handlers *handler;
  83. } zend_shared_memory_handler_entry;
  84. typedef struct _zend_shared_memory_state {
  85. int *positions; /* current positions for each segment */
  86. size_t shared_free; /* amount of free shared memory */
  87. } zend_shared_memory_state;
  88. typedef struct _zend_smm_shared_globals {
  89. /* Shared Memory Manager */
  90. zend_shared_segment **shared_segments;
  91. /* Number of allocated shared segments */
  92. int shared_segments_count;
  93. /* Amount of free shared memory */
  94. size_t shared_free;
  95. /* Amount of shared memory allocated by garbage */
  96. size_t wasted_shared_memory;
  97. /* No more shared memory flag */
  98. zend_bool memory_exhausted;
  99. /* Saved Shared Allocator State */
  100. zend_shared_memory_state shared_memory_state;
  101. /* Pointer to the application's shared data structures */
  102. void *app_shared_globals;
  103. } zend_smm_shared_globals;
  104. extern zend_smm_shared_globals *smm_shared_globals;
  105. #define ZSMMG(element) (smm_shared_globals->element)
  106. #define SHARED_ALLOC_REATTACHED (SUCCESS+1)
  107. int zend_shared_alloc_startup(size_t requested_size);
  108. void zend_shared_alloc_shutdown(void);
  109. /* allocate shared memory block */
  110. void *zend_shared_alloc(size_t size);
  111. /* copy into shared memory */
  112. void *_zend_shared_memdup(void *p, size_t size, zend_bool free_source);
  113. int zend_shared_memdup_size(void *p, size_t size);
  114. int zend_accel_in_shm(void *ptr);
  115. typedef union _align_test {
  116. void *ptr;
  117. double dbl;
  118. zend_long lng;
  119. } align_test;
  120. #if ZEND_GCC_VERSION >= 2000
  121. # define PLATFORM_ALIGNMENT (__alignof__(align_test) < 8 ? 8 : __alignof__(align_test))
  122. #else
  123. # define PLATFORM_ALIGNMENT (sizeof(align_test))
  124. #endif
  125. #define ZEND_ALIGNED_SIZE(size) \
  126. ZEND_MM_ALIGNED_SIZE_EX(size, PLATFORM_ALIGNMENT)
  127. /* exclusive locking */
  128. void zend_shared_alloc_lock(void);
  129. void zend_shared_alloc_unlock(void); /* returns the allocated size during lock..unlock */
  130. void zend_shared_alloc_safe_unlock(void);
  131. /* old/new mapping functions */
  132. void zend_shared_alloc_init_xlat_table(void);
  133. void zend_shared_alloc_destroy_xlat_table(void);
  134. void zend_shared_alloc_clear_xlat_table(void);
  135. void zend_shared_alloc_register_xlat_entry(const void *old, const void *new);
  136. void *zend_shared_alloc_get_xlat_entry(const void *old);
  137. size_t zend_shared_alloc_get_free_memory(void);
  138. void zend_shared_alloc_save_state(void);
  139. void zend_shared_alloc_restore_state(void);
  140. const char *zend_accel_get_shared_model(void);
  141. /* memory write protection */
  142. void zend_accel_shared_protect(int mode);
  143. #ifdef USE_MMAP
  144. extern zend_shared_memory_handlers zend_alloc_mmap_handlers;
  145. #endif
  146. #ifdef USE_SHM
  147. extern zend_shared_memory_handlers zend_alloc_shm_handlers;
  148. #endif
  149. #ifdef USE_SHM_OPEN
  150. extern zend_shared_memory_handlers zend_alloc_posix_handlers;
  151. #endif
  152. #ifdef ZEND_WIN32
  153. extern zend_shared_memory_handlers zend_alloc_win32_handlers;
  154. void zend_shared_alloc_create_lock(void);
  155. void zend_shared_alloc_lock_win32(void);
  156. void zend_shared_alloc_unlock_win32(void);
  157. #endif
  158. #endif /* ZEND_SHARED_ALLOC_H */