zend_alloc.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2016 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: Andi Gutmans <andi@zend.com> |
  16. | Zeev Suraski <zeev@zend.com> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. #ifndef ZEND_ALLOC_H
  21. #define ZEND_ALLOC_H
  22. #include <stdio.h>
  23. #include "../TSRM/TSRM.h"
  24. #include "zend.h"
  25. #ifndef ZEND_MM_ALIGNMENT
  26. # define ZEND_MM_ALIGNMENT 8
  27. # define ZEND_MM_ALIGNMENT_LOG2 3
  28. #elif ZEND_MM_ALIGNMENT < 4
  29. # undef ZEND_MM_ALIGNMENT
  30. # undef ZEND_MM_ALIGNMENT_LOG2
  31. # define ZEND_MM_ALIGNMENT 4
  32. # define ZEND_MM_ALIGNMENT_LOG2 2
  33. #endif
  34. #define ZEND_MM_ALIGNMENT_MASK ~(ZEND_MM_ALIGNMENT-1)
  35. #define ZEND_MM_ALIGNED_SIZE(size) (((size) + ZEND_MM_ALIGNMENT - 1) & ZEND_MM_ALIGNMENT_MASK)
  36. typedef struct _zend_leak_info {
  37. void *addr;
  38. size_t size;
  39. const char *filename;
  40. uint lineno;
  41. const char *orig_filename;
  42. uint orig_lineno;
  43. } zend_leak_info;
  44. BEGIN_EXTERN_C()
  45. ZEND_API char *zend_strndup(const char *s, unsigned int length) ZEND_ATTRIBUTE_MALLOC;
  46. ZEND_API void *_emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE(1);
  47. ZEND_API void *_safe_emalloc(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
  48. ZEND_API void *_safe_emalloc_string(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
  49. ZEND_API void *_safe_malloc(size_t nmemb, size_t size, size_t offset) ZEND_ATTRIBUTE_MALLOC;
  50. ZEND_API void _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
  51. ZEND_API void *_ecalloc(size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE2(1,2);
  52. ZEND_API void *_erealloc(void *ptr, size_t size, int allow_failure ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_ALLOC_SIZE(2);
  53. ZEND_API void *_safe_erealloc(void *ptr, size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
  54. ZEND_API void *_safe_realloc(void *ptr, size_t nmemb, size_t size, size_t offset);
  55. ZEND_API char *_estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
  56. ZEND_API char *_estrndup(const char *s, unsigned int length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
  57. ZEND_API size_t _zend_mem_block_size(void *ptr TSRMLS_DC ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
  58. /* Standard wrapper macros */
  59. #define emalloc(size) _emalloc((size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  60. #define safe_emalloc(nmemb, size, offset) _safe_emalloc((nmemb), (size), (offset) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  61. #define safe_emalloc_string(nmemb, size, offset) _safe_emalloc_string((nmemb), (size), (offset) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  62. #define efree(ptr) _efree((ptr) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  63. #define ecalloc(nmemb, size) _ecalloc((nmemb), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  64. #define erealloc(ptr, size) _erealloc((ptr), (size), 0 ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  65. #define safe_erealloc(ptr, nmemb, size, offset) _safe_erealloc((ptr), (nmemb), (size), (offset) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  66. #define erealloc_recoverable(ptr, size) _erealloc((ptr), (size), 1 ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  67. #define estrdup(s) _estrdup((s) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  68. #define estrndup(s, length) _estrndup((s), (length) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  69. #define zend_mem_block_size(ptr) _zend_mem_block_size((ptr) TSRMLS_CC ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  70. /* Relay wrapper macros */
  71. #define emalloc_rel(size) _emalloc((size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  72. #define safe_emalloc_rel(nmemb, size, offset) _safe_emalloc((nmemb), (size), (offset) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  73. #define efree_rel(ptr) _efree((ptr) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  74. #define ecalloc_rel(nmemb, size) _ecalloc((nmemb), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  75. #define erealloc_rel(ptr, size) _erealloc((ptr), (size), 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  76. #define erealloc_recoverable_rel(ptr, size) _erealloc((ptr), (size), 1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  77. #define safe_erealloc_rel(ptr, nmemb, size, offset) _safe_erealloc((ptr), (nmemb), (size), (offset) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  78. #define estrdup_rel(s) _estrdup((s) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  79. #define estrndup_rel(s, length) _estrndup((s), (length) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  80. #define zend_mem_block_size_rel(ptr) _zend_mem_block_size((ptr) TSRMLS_CC ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  81. inline static void * __zend_malloc(size_t len)
  82. {
  83. void *tmp = malloc(len);
  84. if (tmp) {
  85. return tmp;
  86. }
  87. fprintf(stderr, "Out of memory\n");
  88. exit(1);
  89. }
  90. inline static void * __zend_calloc(size_t nmemb, size_t len)
  91. {
  92. void *tmp = _safe_malloc(nmemb, len, 0);
  93. memset(tmp, 0, nmemb * len);
  94. return tmp;
  95. }
  96. inline static void * __zend_realloc(void *p, size_t len)
  97. {
  98. p = realloc(p, len);
  99. if (p) {
  100. return p;
  101. }
  102. fprintf(stderr, "Out of memory\n");
  103. exit(1);
  104. }
  105. /* Selective persistent/non persistent allocation macros */
  106. #define pemalloc(size, persistent) ((persistent)?__zend_malloc(size):emalloc(size))
  107. #define safe_pemalloc(nmemb, size, offset, persistent) ((persistent)?_safe_malloc(nmemb, size, offset):safe_emalloc(nmemb, size, offset))
  108. #define pefree(ptr, persistent) ((persistent)?free(ptr):efree(ptr))
  109. #define pecalloc(nmemb, size, persistent) ((persistent)?__zend_calloc((nmemb), (size)):ecalloc((nmemb), (size)))
  110. #define perealloc(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc((ptr), (size)))
  111. #define safe_perealloc(ptr, nmemb, size, offset, persistent) ((persistent)?_safe_realloc((ptr), (nmemb), (size), (offset)):safe_erealloc((ptr), (nmemb), (size), (offset)))
  112. #define perealloc_recoverable(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc_recoverable((ptr), (size)))
  113. #define pestrdup(s, persistent) ((persistent)?strdup(s):estrdup(s))
  114. #define pestrndup(s, length, persistent) ((persistent)?zend_strndup((s),(length)):estrndup((s),(length)))
  115. #define pemalloc_rel(size, persistent) ((persistent)?__zend_malloc(size):emalloc_rel(size))
  116. #define pefree_rel(ptr, persistent) ((persistent)?free(ptr):efree_rel(ptr))
  117. #define pecalloc_rel(nmemb, size, persistent) ((persistent)?__zend_calloc((nmemb), (size)):ecalloc_rel((nmemb), (size)))
  118. #define perealloc_rel(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc_rel((ptr), (size)))
  119. #define perealloc_recoverable_rel(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc_recoverable_rel((ptr), (size)))
  120. #define pestrdup_rel(s, persistent) ((persistent)?strdup(s):estrdup_rel(s))
  121. #define safe_estrdup(ptr) ((ptr)?(estrdup(ptr)):STR_EMPTY_ALLOC())
  122. #define safe_estrndup(ptr, len) ((ptr)?(estrndup((ptr), (len))):STR_EMPTY_ALLOC())
  123. ZEND_API int zend_set_memory_limit(size_t memory_limit);
  124. ZEND_API void start_memory_manager(TSRMLS_D);
  125. ZEND_API void shutdown_memory_manager(int silent, int full_shutdown TSRMLS_DC);
  126. ZEND_API int is_zend_mm(TSRMLS_D);
  127. #if ZEND_DEBUG
  128. ZEND_API int _mem_block_check(void *ptr, int silent ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
  129. ZEND_API void _full_mem_check(int silent ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
  130. void zend_debug_alloc_output(char *format, ...);
  131. #define mem_block_check(ptr, silent) _mem_block_check(ptr, silent ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  132. #define full_mem_check(silent) _full_mem_check(silent ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  133. #else
  134. #define mem_block_check(type, ptr, silent)
  135. #define full_mem_check(silent)
  136. #endif
  137. ZEND_API size_t zend_memory_usage(int real_usage TSRMLS_DC);
  138. ZEND_API size_t zend_memory_peak_usage(int real_usage TSRMLS_DC);
  139. END_EXTERN_C()
  140. /* fast cache for zval's */
  141. #define ALLOC_ZVAL(z) \
  142. (z) = (zval *) emalloc(sizeof(zval))
  143. #define FREE_ZVAL(z) \
  144. efree_rel(z)
  145. #define ALLOC_ZVAL_REL(z) \
  146. (z) = (zval *) emalloc_rel(sizeof(zval))
  147. #define FREE_ZVAL_REL(z) \
  148. efree_rel(z)
  149. /* fast cache for HashTables */
  150. #define ALLOC_HASHTABLE(ht) \
  151. (ht) = (HashTable *) emalloc(sizeof(HashTable))
  152. #define FREE_HASHTABLE(ht) \
  153. efree(ht)
  154. #define ALLOC_HASHTABLE_REL(ht) \
  155. (ht) = (HashTable *) emalloc_rel(sizeof(HashTable))
  156. #define FREE_HASHTABLE_REL(ht) \
  157. efree_rel(ht)
  158. /* Heap functions */
  159. typedef struct _zend_mm_heap zend_mm_heap;
  160. ZEND_API zend_mm_heap *zend_mm_startup(void);
  161. ZEND_API void zend_mm_shutdown(zend_mm_heap *heap, int full_shutdown, int silent TSRMLS_DC);
  162. ZEND_API void *_zend_mm_alloc(zend_mm_heap *heap, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
  163. ZEND_API void _zend_mm_free(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
  164. ZEND_API void *_zend_mm_realloc(zend_mm_heap *heap, void *p, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
  165. ZEND_API size_t _zend_mm_block_size(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
  166. #define zend_mm_alloc(heap, size) _zend_mm_alloc((heap), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  167. #define zend_mm_free(heap, p) _zend_mm_free((heap), (p) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  168. #define zend_mm_realloc(heap, p, size) _zend_mm_realloc((heap), (p), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  169. #define zend_mm_block_size(heap, p) _zend_mm_block_size((heap), (p) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  170. #define zend_mm_alloc_rel(heap, size) _zend_mm_alloc((heap), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  171. #define zend_mm_free_rel(heap, p) _zend_mm_free((heap), (p) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  172. #define zend_mm_realloc_rel(heap, p, size) _zend_mm_realloc((heap), (p), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
  173. #define zend_mm_block_size_rel(heap, p) _zend_mm_block_size((heap), (p) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
  174. /* Heaps with user defined storage */
  175. typedef struct _zend_mm_storage zend_mm_storage;
  176. typedef struct _zend_mm_segment {
  177. size_t size;
  178. struct _zend_mm_segment *next_segment;
  179. } zend_mm_segment;
  180. typedef struct _zend_mm_mem_handlers {
  181. const char *name;
  182. zend_mm_storage* (*init)(void *params);
  183. void (*dtor)(zend_mm_storage *storage);
  184. void (*compact)(zend_mm_storage *storage);
  185. zend_mm_segment* (*_alloc)(zend_mm_storage *storage, size_t size);
  186. zend_mm_segment* (*_realloc)(zend_mm_storage *storage, zend_mm_segment *ptr, size_t size);
  187. void (*_free)(zend_mm_storage *storage, zend_mm_segment *ptr);
  188. } zend_mm_mem_handlers;
  189. struct _zend_mm_storage {
  190. const zend_mm_mem_handlers *handlers;
  191. void *data;
  192. };
  193. ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_mem_handlers *handlers, size_t block_size, size_t reserve_size, int internal, void *params);
  194. ZEND_API zend_mm_heap *zend_mm_set_heap(zend_mm_heap *new_heap TSRMLS_DC);
  195. ZEND_API zend_mm_storage *zend_mm_get_storage(zend_mm_heap *heap);
  196. ZEND_API void zend_mm_set_custom_handlers(zend_mm_heap *heap,
  197. void* (*_malloc)(size_t),
  198. void (*_free)(void*),
  199. void* (*_realloc)(void*, size_t));
  200. #endif
  201. /*
  202. * Local variables:
  203. * tab-width: 4
  204. * c-basic-offset: 4
  205. * indent-tabs-mode: t
  206. * End:
  207. */