You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
12 KiB

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