zend_gc.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend 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.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: David Wang <planetbeing@gmail.com> |
  16. | Dmitry Stogov <dmitry@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #ifndef ZEND_GC_H
  20. #define ZEND_GC_H
  21. BEGIN_EXTERN_C()
  22. typedef struct _zend_gc_status {
  23. uint32_t runs;
  24. uint32_t collected;
  25. uint32_t threshold;
  26. uint32_t num_roots;
  27. } zend_gc_status;
  28. ZEND_API extern int (*gc_collect_cycles)(void);
  29. ZEND_API void ZEND_FASTCALL gc_possible_root(zend_refcounted *ref);
  30. ZEND_API void ZEND_FASTCALL gc_remove_from_buffer(zend_refcounted *ref);
  31. /* enable/disable automatic start of GC collection */
  32. ZEND_API bool gc_enable(bool enable);
  33. ZEND_API bool gc_enabled(void);
  34. /* enable/disable possible root additions */
  35. ZEND_API bool gc_protect(bool protect);
  36. ZEND_API bool gc_protected(void);
  37. /* The default implementation of the gc_collect_cycles callback. */
  38. ZEND_API int zend_gc_collect_cycles(void);
  39. ZEND_API void zend_gc_get_status(zend_gc_status *status);
  40. void gc_globals_ctor(void);
  41. void gc_globals_dtor(void);
  42. void gc_reset(void);
  43. #ifdef ZTS
  44. size_t zend_gc_globals_size(void);
  45. #endif
  46. #define GC_REMOVE_FROM_BUFFER(p) do { \
  47. zend_refcounted *_p = (zend_refcounted*)(p); \
  48. if (GC_TYPE_INFO(_p) & GC_INFO_MASK) { \
  49. gc_remove_from_buffer(_p); \
  50. } \
  51. } while (0)
  52. #define GC_MAY_LEAK(ref) \
  53. ((GC_TYPE_INFO(ref) & \
  54. (GC_INFO_MASK | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT))) == 0)
  55. static zend_always_inline void gc_check_possible_root(zend_refcounted *ref)
  56. {
  57. if (EXPECTED(GC_TYPE_INFO(ref) == GC_REFERENCE)) {
  58. zval *zv = &((zend_reference*)ref)->val;
  59. if (!Z_COLLECTABLE_P(zv)) {
  60. return;
  61. }
  62. ref = Z_COUNTED_P(zv);
  63. }
  64. if (UNEXPECTED(GC_MAY_LEAK(ref))) {
  65. gc_possible_root(ref);
  66. }
  67. }
  68. /* These APIs can be used to simplify object get_gc implementations
  69. * over heterogeneous structures. See zend_generator_get_gc() for
  70. * a usage example. */
  71. typedef struct {
  72. zval *cur;
  73. zval *end;
  74. zval *start;
  75. } zend_get_gc_buffer;
  76. ZEND_API zend_get_gc_buffer *zend_get_gc_buffer_create(void);
  77. ZEND_API void zend_get_gc_buffer_grow(zend_get_gc_buffer *gc_buffer);
  78. static zend_always_inline void zend_get_gc_buffer_add_zval(
  79. zend_get_gc_buffer *gc_buffer, zval *zv) {
  80. if (Z_REFCOUNTED_P(zv)) {
  81. if (UNEXPECTED(gc_buffer->cur == gc_buffer->end)) {
  82. zend_get_gc_buffer_grow(gc_buffer);
  83. }
  84. ZVAL_COPY_VALUE(gc_buffer->cur, zv);
  85. gc_buffer->cur++;
  86. }
  87. }
  88. static zend_always_inline void zend_get_gc_buffer_add_obj(
  89. zend_get_gc_buffer *gc_buffer, zend_object *obj) {
  90. if (UNEXPECTED(gc_buffer->cur == gc_buffer->end)) {
  91. zend_get_gc_buffer_grow(gc_buffer);
  92. }
  93. ZVAL_OBJ(gc_buffer->cur, obj);
  94. gc_buffer->cur++;
  95. }
  96. static zend_always_inline void zend_get_gc_buffer_use(
  97. zend_get_gc_buffer *gc_buffer, zval **table, int *n) {
  98. *table = gc_buffer->start;
  99. *n = gc_buffer->cur - gc_buffer->start;
  100. }
  101. END_EXTERN_C()
  102. #endif /* ZEND_GC_H */