1999-04-08 02:10:10 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Zend Engine |
|
|
|
|
+----------------------------------------------------------------------+
|
2006-01-05 07:53:05 +08:00
|
|
|
| Copyright (c) 1998-2006 Zend Technologies Ltd. (http://www.zend.com) |
|
1999-04-08 02:10:10 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2001-12-11 23:16:21 +08:00
|
|
|
| This source file is subject to version 2.00 of the Zend license, |
|
1999-07-16 22:58:16 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
2001-12-11 23:16:21 +08:00
|
|
|
| http://www.zend.com/license/2_00.txt. |
|
1999-07-16 22:58:16 +08:00
|
|
|
| If you did not receive a copy of the Zend license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@zend.com so we can mail you a copy immediately. |
|
1999-04-08 02:10:10 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Andi Gutmans <andi@zend.com> |
|
|
|
|
| Zeev Suraski <zeev@zend.com> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
2003-02-01 09:49:15 +08:00
|
|
|
/* $Id$ */
|
1999-07-16 22:58:16 +08:00
|
|
|
|
2000-07-03 08:55:36 +08:00
|
|
|
#ifndef ZEND_ALLOC_H
|
|
|
|
#define ZEND_ALLOC_H
|
1999-04-08 02:10:10 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2000-11-18 10:43:23 +08:00
|
|
|
#include "../TSRM/TSRM.h"
|
2006-07-18 17:06:33 +08:00
|
|
|
#include "zend.h"
|
1999-09-06 03:03:35 +08:00
|
|
|
|
2006-07-18 17:06:33 +08:00
|
|
|
typedef struct _zend_leak_info {
|
|
|
|
void *addr;
|
|
|
|
size_t size;
|
1999-04-08 02:10:10 +08:00
|
|
|
char *filename;
|
|
|
|
uint lineno;
|
1999-08-28 18:18:54 +08:00
|
|
|
char *orig_filename;
|
|
|
|
uint orig_lineno;
|
2006-07-18 17:06:33 +08:00
|
|
|
} zend_leak_info;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
1999-04-23 11:32:33 +08:00
|
|
|
BEGIN_EXTERN_C()
|
|
|
|
|
2003-08-28 13:50:15 +08:00
|
|
|
ZEND_API char *zend_strndup(const char *s, unsigned int length) ZEND_ATTRIBUTE_MALLOC;
|
2000-10-30 06:40:01 +08:00
|
|
|
|
2003-08-28 13:50:15 +08:00
|
|
|
ZEND_API void *_emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
|
|
|
|
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;
|
2004-07-21 05:55:57 +08:00
|
|
|
ZEND_API void *_safe_malloc(size_t nmemb, size_t size, size_t offset) ZEND_ATTRIBUTE_MALLOC;
|
1999-08-28 18:18:54 +08:00
|
|
|
ZEND_API void _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
|
2003-08-28 13:50:15 +08:00
|
|
|
ZEND_API void *_ecalloc(size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
|
1999-08-28 18:18:54 +08:00
|
|
|
ZEND_API void *_erealloc(void *ptr, size_t size, int allow_failure ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
|
2003-08-28 13:50:15 +08:00
|
|
|
ZEND_API char *_estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
|
|
|
|
ZEND_API char *_estrndup(const char *s, unsigned int length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
|
2006-07-18 17:06:33 +08:00
|
|
|
ZEND_API size_t _zend_mem_block_size(void *ptr TSRMLS_DC ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
|
2004-08-07 08:45:34 +08:00
|
|
|
|
1999-08-28 03:17:19 +08:00
|
|
|
/* Standard wrapper macros */
|
2006-07-18 17:06:33 +08:00
|
|
|
#define emalloc(size) _emalloc((size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
|
|
|
|
#define safe_emalloc(nmemb, size, offset) _safe_emalloc((nmemb), (size), (offset) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
|
|
|
|
#define efree(ptr) _efree((ptr) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
|
|
|
|
#define ecalloc(nmemb, size) _ecalloc((nmemb), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
|
|
|
|
#define erealloc(ptr, size) _erealloc((ptr), (size), 0 ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
|
|
|
|
#define erealloc_recoverable(ptr, size) _erealloc((ptr), (size), 1 ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
|
|
|
|
#define estrdup(s) _estrdup((s) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
|
|
|
|
#define estrndup(s, length) _estrndup((s), (length) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
|
|
|
|
#define zend_mem_block_size(ptr) _zend_mem_block_size((ptr) TSRMLS_CC ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
|
1999-08-28 03:17:19 +08:00
|
|
|
|
|
|
|
/* Relay wrapper macros */
|
2006-07-18 17:06:33 +08:00
|
|
|
#define emalloc_rel(size) _emalloc((size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
|
|
|
|
#define safe_emalloc_rel(nmemb, size, offset) _safe_emalloc((nmemb), (size), (offset) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
|
|
|
|
#define efree_rel(ptr) _efree((ptr) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
|
|
|
|
#define ecalloc_rel(nmemb, size) _ecalloc((nmemb), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
|
|
|
|
#define erealloc_rel(ptr, size) _erealloc((ptr), (size), 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
|
|
|
|
#define erealloc_recoverable_rel(ptr, size) _erealloc((ptr), (size), 1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
|
|
|
|
#define estrdup_rel(s) _estrdup((s) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
|
|
|
|
#define estrndup_rel(s, length) _estrndup((s), (length) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
|
|
|
|
#define zend_mem_block_size_rel(ptr) _zend_mem_block_size((ptr) TSRMLS_CC ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
|
1999-08-28 03:17:19 +08:00
|
|
|
|
|
|
|
/* Selective persistent/non persistent allocation macros */
|
2001-08-11 23:56:40 +08:00
|
|
|
#define pemalloc(size, persistent) ((persistent)?malloc(size):emalloc(size))
|
2004-07-21 05:55:57 +08:00
|
|
|
#define safe_pemalloc(nmemb, size, offset, persistent) ((persistent)?_safe_malloc(nmemb, size, offset):safe_emalloc(nmemb, size, offset))
|
2001-08-11 23:56:40 +08:00
|
|
|
#define pefree(ptr, persistent) ((persistent)?free(ptr):efree(ptr))
|
|
|
|
#define pecalloc(nmemb, size, persistent) ((persistent)?calloc((nmemb), (size)):ecalloc((nmemb), (size)))
|
|
|
|
#define perealloc(ptr, size, persistent) ((persistent)?realloc((ptr), (size)):erealloc((ptr), (size)))
|
|
|
|
#define perealloc_recoverable(ptr, size, persistent) ((persistent)?realloc((ptr), (size)):erealloc_recoverable((ptr), (size)))
|
|
|
|
#define pestrdup(s, persistent) ((persistent)?strdup(s):estrdup(s))
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2003-08-19 05:17:26 +08:00
|
|
|
#define pemalloc_rel(size, persistent) ((persistent)?malloc(size):emalloc_rel(size))
|
|
|
|
#define pefree_rel(ptr, persistent) ((persistent)?free(ptr):efree_rel(ptr))
|
|
|
|
#define pecalloc_rel(nmemb, size, persistent) ((persistent)?calloc((nmemb), (size)):ecalloc_rel((nmemb), (size)))
|
|
|
|
#define perealloc_rel(ptr, size, persistent) ((persistent)?realloc((ptr), (size)):erealloc_rel((ptr), (size)))
|
|
|
|
#define perealloc_recoverable_rel(ptr, size, persistent) ((persistent)?realloc((ptr), (size)):erealloc_recoverable_rel((ptr), (size)))
|
|
|
|
#define pestrdup_rel(s, persistent) ((persistent)?strdup(s):estrdup_rel(s))
|
|
|
|
|
2005-04-08 04:15:21 +08:00
|
|
|
#define safe_estrdup(ptr) ((ptr)?(estrdup(ptr)):STR_EMPTY_ALLOC())
|
|
|
|
#define safe_estrndup(ptr, len) ((ptr)?(estrndup((ptr), (len))):STR_EMPTY_ALLOC())
|
2004-08-07 08:45:34 +08:00
|
|
|
|
1999-04-11 00:24:28 +08:00
|
|
|
ZEND_API int zend_set_memory_limit(unsigned int memory_limit);
|
1999-04-10 22:44:35 +08:00
|
|
|
|
2001-07-28 18:51:54 +08:00
|
|
|
ZEND_API void start_memory_manager(TSRMLS_D);
|
2004-05-25 16:33:11 +08:00
|
|
|
ZEND_API void shutdown_memory_manager(int silent, int full_shutdown TSRMLS_DC);
|
1999-04-08 02:10:10 +08:00
|
|
|
|
|
|
|
#if ZEND_DEBUG
|
1999-08-28 18:18:54 +08:00
|
|
|
ZEND_API int _mem_block_check(void *ptr, int silent ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
|
|
|
|
ZEND_API void _full_mem_check(int silent ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
|
1999-12-02 06:55:20 +08:00
|
|
|
void zend_debug_alloc_output(char *format, ...);
|
1999-08-28 18:18:54 +08:00
|
|
|
#define mem_block_check(ptr, silent) _mem_block_check(ptr, silent ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
|
|
|
|
#define full_mem_check(silent) _full_mem_check(silent ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
|
1999-04-08 02:10:10 +08:00
|
|
|
#else
|
|
|
|
#define mem_block_check(type, ptr, silent)
|
|
|
|
#define full_mem_check(silent)
|
|
|
|
#endif
|
|
|
|
|
2006-07-18 17:06:33 +08:00
|
|
|
#if MEMORY_LIMIT
|
2006-07-25 21:40:05 +08:00
|
|
|
ZEND_API size_t zend_memory_usage(int real_usage TSRMLS_DC);
|
|
|
|
ZEND_API size_t zend_memory_peak_usage(int real_usage TSRMLS_DC);
|
2006-07-18 17:06:33 +08:00
|
|
|
#endif
|
|
|
|
|
1999-04-23 11:32:33 +08:00
|
|
|
END_EXTERN_C()
|
|
|
|
|
2006-07-18 17:06:33 +08:00
|
|
|
/* Macroses for zend_fast_cache.h compatibility */
|
|
|
|
|
|
|
|
#define ZEND_FAST_ALLOC(p, type, fc_type) \
|
|
|
|
(p) = (type *) emalloc(sizeof(type))
|
|
|
|
|
|
|
|
#define ZEND_FAST_FREE(p, fc_type) \
|
|
|
|
efree(p)
|
|
|
|
|
|
|
|
#define ZEND_FAST_ALLOC_REL(p, type, fc_type) \
|
|
|
|
(p) = (type *) emalloc_rel(sizeof(type))
|
|
|
|
|
|
|
|
#define ZEND_FAST_FREE_REL(p, fc_type) \
|
|
|
|
efree_rel(p)
|
|
|
|
|
|
|
|
/* fast cache for zval's */
|
|
|
|
#define ALLOC_ZVAL(z) \
|
|
|
|
ZEND_FAST_ALLOC(z, zval, ZVAL_CACHE_LIST)
|
|
|
|
|
|
|
|
#define FREE_ZVAL(z) \
|
|
|
|
ZEND_FAST_FREE(z, ZVAL_CACHE_LIST)
|
|
|
|
|
|
|
|
#define ALLOC_ZVAL_REL(z) \
|
|
|
|
ZEND_FAST_ALLOC_REL(z, zval, ZVAL_CACHE_LIST)
|
|
|
|
|
|
|
|
#define FREE_ZVAL_REL(z) \
|
|
|
|
ZEND_FAST_FREE_REL(z, ZVAL_CACHE_LIST)
|
|
|
|
|
|
|
|
/* fast cache for HashTables */
|
|
|
|
#define ALLOC_HASHTABLE(ht) \
|
|
|
|
ZEND_FAST_ALLOC(ht, HashTable, HASHTABLE_CACHE_LIST)
|
|
|
|
|
|
|
|
#define FREE_HASHTABLE(ht) \
|
|
|
|
ZEND_FAST_FREE(ht, HASHTABLE_CACHE_LIST)
|
|
|
|
|
|
|
|
#define ALLOC_HASHTABLE_REL(ht) \
|
|
|
|
ZEND_FAST_ALLOC_REL(ht, HashTable, HASHTABLE_CACHE_LIST)
|
|
|
|
|
|
|
|
#define FREE_HASHTABLE_REL(ht) \
|
|
|
|
ZEND_FAST_FREE_REL(ht, HASHTABLE_CACHE_LIST)
|
|
|
|
|
|
|
|
/* Heap functions */
|
|
|
|
typedef struct _zend_mm_heap zend_mm_heap;
|
|
|
|
|
|
|
|
ZEND_API zend_mm_heap *zend_mm_startup(void);
|
|
|
|
ZEND_API void zend_mm_shutdown(zend_mm_heap *heap, int full_shutdown, int silent);
|
|
|
|
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;
|
|
|
|
ZEND_API void _zend_mm_free(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
|
|
|
|
ZEND_API void *_zend_mm_realloc(zend_mm_heap *heap, void *p, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
|
|
|
|
ZEND_API size_t _zend_mm_block_size(zend_mm_heap *heap, void *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
|
|
|
|
|
|
|
|
#define zend_mm_alloc(heap, size) _zend_mm_alloc((heap), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
|
|
|
|
#define zend_mm_free(heap, p) _zend_mm_free((heap), (p) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
|
|
|
|
#define zend_mm_realloc(heap, p, size) _zend_mm_realloc((heap), (p), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
|
|
|
|
#define zend_mm_block_size(heap, p) _zend_mm_block_size((heap), (p), ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
|
|
|
|
|
|
|
|
#define zend_mm_alloc_rel(heap, size) _zend_mm_alloc((heap), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
|
|
|
|
#define zend_mm_free_rel(heap, p) _zend_mm_free((heap), (p) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
|
|
|
|
#define zend_mm_realloc_rel(heap, p, size) _zend_mm_realloc((heap), (p), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC)
|
|
|
|
#define zend_mm_block_size_rel(heap, p) _zend_mm_block_size((heap), (p), ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
|
|
|
|
|
|
|
|
/* Heaps with user defined storage */
|
|
|
|
typedef struct _zend_mm_storage zend_mm_storage;
|
|
|
|
|
|
|
|
typedef struct _zend_mm_segment {
|
|
|
|
size_t size;
|
|
|
|
struct _zend_mm_segment *next_segment;
|
|
|
|
} zend_mm_segment;
|
|
|
|
|
|
|
|
typedef struct _zend_mm_mem_handlers {
|
|
|
|
const char *name;
|
|
|
|
zend_mm_storage* (*init)(void *params);
|
|
|
|
void (*dtor)(zend_mm_storage *storage);
|
|
|
|
zend_mm_segment* (*_alloc)(zend_mm_storage *storage, size_t size);
|
|
|
|
zend_mm_segment* (*_realloc)(zend_mm_storage *storage, zend_mm_segment *ptr, size_t size);
|
|
|
|
void (*_free)(zend_mm_storage *storage, zend_mm_segment *ptr);
|
|
|
|
} zend_mm_mem_handlers;
|
|
|
|
|
|
|
|
struct _zend_mm_storage {
|
|
|
|
const zend_mm_mem_handlers *handlers;
|
|
|
|
};
|
|
|
|
|
|
|
|
ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_mem_handlers *handlers, size_t block_size, void *params);
|
|
|
|
ZEND_API zend_mm_heap *zend_mm_set_heap(zend_mm_heap *new_heap TSRMLS_DC);
|
|
|
|
|
1999-04-08 02:10:10 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
2003-02-01 09:49:15 +08:00
|
|
|
* indent-tabs-mode: t
|
1999-04-08 02:10:10 +08:00
|
|
|
* End:
|
|
|
|
*/
|