1999-04-08 02:10:10 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Zend Engine |
|
|
|
|
+----------------------------------------------------------------------+
|
2004-01-09 01:33:29 +08:00
|
|
|
| Copyright (c) 1998-2004 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-04-08 02:10:10 +08:00
|
|
|
#include "zend.h"
|
|
|
|
#include "zend_alloc.h"
|
|
|
|
#include "zend_globals.h"
|
1999-12-28 03:07:33 +08:00
|
|
|
#include "zend_fast_cache.h"
|
1999-09-07 00:14:08 +08:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
# include <signal.h>
|
1999-07-09 19:19:38 +08:00
|
|
|
#endif
|
1999-09-07 00:14:08 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
1999-07-09 19:19:38 +08:00
|
|
|
#endif
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2002-06-17 01:17:47 +08:00
|
|
|
#include "zend_mm.h"
|
|
|
|
|
1999-04-08 02:10:10 +08:00
|
|
|
#ifndef ZTS
|
1999-12-30 13:25:44 +08:00
|
|
|
ZEND_API zend_alloc_globals alloc_globals;
|
1999-04-08 02:10:10 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2002-06-24 21:41:26 +08:00
|
|
|
#ifdef ZEND_MM
|
2003-07-04 23:38:12 +08:00
|
|
|
#define ZEND_DISABLE_MEMORY_CACHE 0
|
2002-06-24 21:41:26 +08:00
|
|
|
#else
|
1999-11-27 08:04:36 +08:00
|
|
|
#define ZEND_DISABLE_MEMORY_CACHE 0
|
2002-06-24 21:41:26 +08:00
|
|
|
#endif
|
1999-11-27 08:04:36 +08:00
|
|
|
|
2002-06-20 00:53:06 +08:00
|
|
|
#ifdef ZEND_MM
|
2002-06-17 01:17:47 +08:00
|
|
|
#define ZEND_DO_MALLOC(size) zend_mm_alloc(&AG(mm_heap), size)
|
|
|
|
#define ZEND_DO_FREE(ptr) zend_mm_free(&AG(mm_heap), ptr)
|
|
|
|
#define ZEND_DO_REALLOC(ptr, size) zend_mm_realloc(&AG(mm_heap), ptr, size)
|
|
|
|
#elif defined(ZEND_WIN32)
|
2001-08-03 15:06:05 +08:00
|
|
|
#define ZEND_DO_MALLOC(size) (AG(memory_heap) ? HeapAlloc(AG(memory_heap), HEAP_NO_SERIALIZE, size) : malloc(size))
|
|
|
|
#define ZEND_DO_FREE(ptr) (AG(memory_heap) ? HeapFree(AG(memory_heap), HEAP_NO_SERIALIZE, ptr) : free(ptr))
|
|
|
|
#define ZEND_DO_REALLOC(ptr, size) (AG(memory_heap) ? HeapReAlloc(AG(memory_heap), HEAP_NO_SERIALIZE, ptr, size) : realloc(ptr, size))
|
|
|
|
#else
|
|
|
|
#define ZEND_DO_MALLOC(size) malloc(size)
|
2001-08-03 16:15:16 +08:00
|
|
|
#define ZEND_DO_FREE(ptr) free(ptr)
|
2001-08-03 15:06:05 +08:00
|
|
|
#define ZEND_DO_REALLOC(ptr, size) realloc(ptr, size)
|
|
|
|
#endif
|
|
|
|
|
1999-04-08 02:10:10 +08:00
|
|
|
#if ZEND_DEBUG
|
|
|
|
# define END_MAGIC_SIZE sizeof(long)
|
2001-04-29 21:00:46 +08:00
|
|
|
static long mem_block_end_magic = MEM_BLOCK_END_MAGIC;
|
1999-04-08 02:10:10 +08:00
|
|
|
#else
|
|
|
|
# define END_MAGIC_SIZE 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
# if MEMORY_LIMIT
|
|
|
|
# if ZEND_DEBUG
|
2000-06-10 01:51:37 +08:00
|
|
|
#define CHECK_MEMORY_LIMIT(s, rs) _CHECK_MEMORY_LIMIT(s, rs, __zend_filename, __zend_lineno)
|
1999-04-08 02:10:10 +08:00
|
|
|
# else
|
2001-08-11 23:56:40 +08:00
|
|
|
#define CHECK_MEMORY_LIMIT(s, rs) _CHECK_MEMORY_LIMIT(s, rs, NULL, 0)
|
1999-04-08 02:10:10 +08:00
|
|
|
# endif
|
|
|
|
|
2000-06-10 01:51:37 +08:00
|
|
|
#define _CHECK_MEMORY_LIMIT(s, rs, file, lineno) { AG(allocated_memory) += rs;\
|
1999-04-10 22:44:35 +08:00
|
|
|
if (AG(memory_limit)<AG(allocated_memory)) {\
|
2003-01-11 03:18:20 +08:00
|
|
|
int php_mem_limit = AG(memory_limit); \
|
|
|
|
if (AG(memory_limit)+1048576 > AG(allocated_memory) - rs) { \
|
|
|
|
AG(memory_limit) = AG(allocated_memory) + 1048576; \
|
|
|
|
if (file) { \
|
|
|
|
zend_error(E_ERROR,"Allowed memory size of %d bytes exhausted at %s:%d (tried to allocate %d bytes)", php_mem_limit, file, lineno, s); \
|
2003-10-17 10:29:06 +08:00
|
|
|
} else { \
|
|
|
|
zend_error(E_ERROR,"Allowed memory size of %d bytes exhausted (tried to allocate %d bytes)", php_mem_limit, s); \
|
2000-08-20 00:35:02 +08:00
|
|
|
} \
|
2003-01-11 03:18:20 +08:00
|
|
|
} else { \
|
|
|
|
if (file) { \
|
|
|
|
fprintf(stderr, "Allowed memory size of %d bytes exhausted at %s:%d (tried to allocate %d bytes)\n", php_mem_limit, file, lineno, s); \
|
|
|
|
} else { \
|
|
|
|
fprintf(stderr, "Allowed memory size of %d bytes exhausted (tried to allocate %d bytes)\n", php_mem_limit, s); \
|
|
|
|
} \
|
|
|
|
exit(1); \
|
|
|
|
} \
|
1999-04-08 02:10:10 +08:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
|
|
|
#ifndef CHECK_MEMORY_LIMIT
|
2000-06-10 01:51:37 +08:00
|
|
|
#define CHECK_MEMORY_LIMIT(s, rs)
|
1999-04-08 02:10:10 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#define REMOVE_POINTER_FROM_LIST(p) \
|
2002-06-22 22:37:49 +08:00
|
|
|
if (p==AG(head)) { \
|
1999-04-08 02:10:10 +08:00
|
|
|
AG(head) = p->pNext; \
|
|
|
|
} else { \
|
|
|
|
p->pLast->pNext = p->pNext; \
|
|
|
|
} \
|
|
|
|
if (p->pNext) { \
|
|
|
|
p->pNext->pLast = p->pLast; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ADD_POINTER_TO_LIST(p) \
|
2002-06-22 22:37:49 +08:00
|
|
|
p->pNext = AG(head); \
|
|
|
|
if (AG(head)) { \
|
|
|
|
AG(head)->pLast = p; \
|
|
|
|
} \
|
|
|
|
AG(head) = p; \
|
1999-12-27 04:45:42 +08:00
|
|
|
p->pLast = (zend_mem_header *) NULL;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2001-04-17 23:19:14 +08:00
|
|
|
#define DECLARE_CACHE_VARS() \
|
2000-06-09 23:54:14 +08:00
|
|
|
unsigned int real_size; \
|
2001-04-17 23:19:14 +08:00
|
|
|
unsigned int cache_index
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2000-06-15 03:10:30 +08:00
|
|
|
#define REAL_SIZE(size) ((size+7) & ~0x7)
|
2000-06-09 23:54:14 +08:00
|
|
|
|
|
|
|
#define CALCULATE_REAL_SIZE_AND_CACHE_INDEX(size) \
|
|
|
|
real_size = REAL_SIZE(size); \
|
|
|
|
cache_index = real_size >> 3;
|
|
|
|
|
|
|
|
#define SIZE real_size
|
|
|
|
|
|
|
|
#define CACHE_INDEX cache_index
|
1999-04-23 11:32:33 +08:00
|
|
|
|
1999-08-28 18:18:54 +08:00
|
|
|
ZEND_API void *_emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
1999-12-27 04:45:42 +08:00
|
|
|
zend_mem_header *p;
|
2001-04-17 23:19:14 +08:00
|
|
|
DECLARE_CACHE_VARS();
|
2001-07-28 18:51:54 +08:00
|
|
|
TSRMLS_FETCH();
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2000-06-09 23:54:14 +08:00
|
|
|
CALCULATE_REAL_SIZE_AND_CACHE_INDEX(size);
|
|
|
|
|
2003-05-29 06:56:56 +08:00
|
|
|
#if !ZEND_DISABLE_MEMORY_CACHE
|
|
|
|
if ((CACHE_INDEX < MAX_CACHED_MEMORY) && (AG(cache_count)[CACHE_INDEX] > 0)) {
|
2000-06-09 23:54:14 +08:00
|
|
|
p = AG(cache)[CACHE_INDEX][--AG(cache_count)[CACHE_INDEX]];
|
1999-04-08 02:10:10 +08:00
|
|
|
#if ZEND_DEBUG
|
1999-08-28 03:17:19 +08:00
|
|
|
p->filename = __zend_filename;
|
|
|
|
p->lineno = __zend_lineno;
|
1999-08-28 18:18:54 +08:00
|
|
|
p->orig_filename = __zend_orig_filename;
|
|
|
|
p->orig_lineno = __zend_orig_lineno;
|
1999-04-08 02:10:10 +08:00
|
|
|
p->magic = MEM_BLOCK_START_MAGIC;
|
1999-06-26 19:48:22 +08:00
|
|
|
p->reported = 0;
|
2000-11-18 10:43:23 +08:00
|
|
|
/* Setting the thread id should not be necessary, because we fetched this block
|
|
|
|
* from this thread's cache
|
|
|
|
*/
|
2000-06-09 23:54:14 +08:00
|
|
|
AG(cache_stats)[CACHE_INDEX][1]++;
|
2001-04-29 21:00:46 +08:00
|
|
|
memcpy((((char *) p) + sizeof(zend_mem_header) + MEM_HEADER_PADDING + size), &mem_block_end_magic, sizeof(long));
|
1999-04-08 02:10:10 +08:00
|
|
|
#endif
|
|
|
|
p->cached = 0;
|
2000-06-09 23:54:14 +08:00
|
|
|
p->size = size;
|
2001-04-29 21:00:46 +08:00
|
|
|
return (void *)((char *)p + sizeof(zend_mem_header) + MEM_HEADER_PADDING);
|
1999-04-08 02:10:10 +08:00
|
|
|
} else {
|
2003-05-29 06:56:56 +08:00
|
|
|
#endif
|
1999-12-28 00:42:59 +08:00
|
|
|
#if ZEND_DEBUG
|
2000-06-09 23:54:14 +08:00
|
|
|
if (CACHE_INDEX<MAX_CACHED_MEMORY) {
|
|
|
|
AG(cache_stats)[CACHE_INDEX][0]++;
|
1999-12-28 00:42:59 +08:00
|
|
|
}
|
|
|
|
#endif
|
2001-08-03 15:06:05 +08:00
|
|
|
p = (zend_mem_header *) ZEND_DO_MALLOC(sizeof(zend_mem_header) + MEM_HEADER_PADDING + SIZE + END_MAGIC_SIZE);
|
2003-05-29 06:56:56 +08:00
|
|
|
#if !ZEND_DISABLE_MEMORY_CACHE
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2003-05-29 06:56:56 +08:00
|
|
|
#endif
|
1999-04-08 02:10:10 +08:00
|
|
|
|
1999-12-21 23:48:56 +08:00
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
|
|
|
|
1999-04-08 02:10:10 +08:00
|
|
|
if (!p) {
|
1999-05-30 21:28:56 +08:00
|
|
|
fprintf(stderr,"FATAL: emalloc(): Unable to allocate %ld bytes\n", (long) size);
|
1999-09-07 00:14:08 +08:00
|
|
|
#if ZEND_DEBUG && defined(HAVE_KILL) && defined(HAVE_GETPID)
|
1999-07-09 19:10:19 +08:00
|
|
|
kill(getpid(), SIGSEGV);
|
|
|
|
#else
|
1999-04-08 02:10:10 +08:00
|
|
|
exit(1);
|
1999-07-09 19:10:19 +08:00
|
|
|
#endif
|
1999-04-08 02:10:10 +08:00
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
|
|
|
return (void *)p;
|
|
|
|
}
|
2002-06-22 22:37:49 +08:00
|
|
|
p->cached = 0;
|
2002-06-24 15:22:25 +08:00
|
|
|
#if ZEND_DEBUG || !defined(ZEND_MM)
|
1999-04-08 02:10:10 +08:00
|
|
|
ADD_POINTER_TO_LIST(p);
|
2002-06-24 15:22:25 +08:00
|
|
|
#endif
|
2000-06-09 23:54:14 +08:00
|
|
|
p->size = size; /* Save real size for correct cache output */
|
1999-04-08 02:10:10 +08:00
|
|
|
#if ZEND_DEBUG
|
1999-08-28 03:17:19 +08:00
|
|
|
p->filename = __zend_filename;
|
|
|
|
p->lineno = __zend_lineno;
|
1999-08-28 18:18:54 +08:00
|
|
|
p->orig_filename = __zend_orig_filename;
|
|
|
|
p->orig_lineno = __zend_orig_lineno;
|
1999-04-08 02:10:10 +08:00
|
|
|
p->magic = MEM_BLOCK_START_MAGIC;
|
1999-06-26 19:48:22 +08:00
|
|
|
p->reported = 0;
|
2000-11-18 10:43:23 +08:00
|
|
|
# ifdef ZTS
|
|
|
|
p->thread_id = tsrm_thread_id();
|
|
|
|
# endif
|
2001-04-29 21:00:46 +08:00
|
|
|
memcpy((((char *) p) + sizeof(zend_mem_header) + MEM_HEADER_PADDING + size), &mem_block_end_magic, sizeof(long));
|
1999-04-08 02:10:10 +08:00
|
|
|
#endif
|
|
|
|
#if MEMORY_LIMIT
|
2000-06-10 01:51:37 +08:00
|
|
|
CHECK_MEMORY_LIMIT(size, SIZE);
|
2001-05-17 19:22:49 +08:00
|
|
|
if (AG(allocated_memory) > AG(allocated_memory_peak)) {
|
2001-05-16 23:02:30 +08:00
|
|
|
AG(allocated_memory_peak) = AG(allocated_memory);
|
|
|
|
}
|
2001-05-12 02:16:41 +08:00
|
|
|
#endif
|
|
|
|
|
1999-04-08 02:10:10 +08:00
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
2001-04-29 21:00:46 +08:00
|
|
|
return (void *)((char *)p + sizeof(zend_mem_header) + MEM_HEADER_PADDING);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
2003-04-24 11:35:06 +08:00
|
|
|
#include "zend_multiply.h"
|
|
|
|
|
|
|
|
ZEND_API void *_safe_emalloc(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (nmemb < LONG_MAX
|
|
|
|
&& size < LONG_MAX
|
|
|
|
&& offset < LONG_MAX
|
|
|
|
&& nmemb >= 0
|
|
|
|
&& size >= 0
|
|
|
|
&& offset >= 0) {
|
|
|
|
long lval;
|
|
|
|
double dval;
|
|
|
|
int use_dval;
|
|
|
|
|
|
|
|
ZEND_SIGNED_MULTIPLY_LONG(nmemb, size, lval, dval, use_dval);
|
|
|
|
|
|
|
|
if (!use_dval
|
2003-07-16 16:48:22 +08:00
|
|
|
&& lval < (long) (LONG_MAX - offset)) {
|
2003-04-24 11:35:06 +08:00
|
|
|
return emalloc_rel(lval + offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-14 00:49:50 +08:00
|
|
|
zend_error(E_ERROR, "Possible integer overflow in memory allocation (%zd * %zd + %zd)", nmemb, size, offset);
|
2003-04-24 11:35:06 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-04-08 02:10:10 +08:00
|
|
|
|
1999-08-28 18:18:54 +08:00
|
|
|
ZEND_API void _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2001-04-29 21:00:46 +08:00
|
|
|
zend_mem_header *p = (zend_mem_header *) ((char *)ptr - sizeof(zend_mem_header) - MEM_HEADER_PADDING);
|
2001-04-17 23:19:14 +08:00
|
|
|
DECLARE_CACHE_VARS();
|
2001-07-28 18:51:54 +08:00
|
|
|
TSRMLS_FETCH();
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2001-07-23 12:19:32 +08:00
|
|
|
#if defined(ZTS) && TSRM_DEBUG
|
2000-11-18 10:43:23 +08:00
|
|
|
if (p->thread_id != tsrm_thread_id()) {
|
|
|
|
tsrm_error(TSRM_ERROR_LEVEL_ERROR, "Memory block allocated at %s:(%d) on thread %x freed at %s:(%d) on thread %x, ignoring",
|
|
|
|
p->filename, p->lineno, p->thread_id,
|
|
|
|
__zend_filename, __zend_lineno, tsrm_thread_id());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-06-09 23:54:14 +08:00
|
|
|
CALCULATE_REAL_SIZE_AND_CACHE_INDEX(p->size);
|
1999-04-08 02:10:10 +08:00
|
|
|
#if ZEND_DEBUG
|
1999-08-28 18:18:54 +08:00
|
|
|
if (!_mem_block_check(ptr, 1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC)) {
|
1999-05-12 05:38:39 +08:00
|
|
|
return;
|
|
|
|
}
|
2001-04-29 21:00:46 +08:00
|
|
|
memset(ptr, 0x5a, p->size);
|
1999-04-08 02:10:10 +08:00
|
|
|
#endif
|
2003-05-29 06:56:56 +08:00
|
|
|
#if !ZEND_DISABLE_MEMORY_CACHE
|
|
|
|
if ((CACHE_INDEX < MAX_CACHED_MEMORY) && (AG(cache_count)[CACHE_INDEX] < MAX_CACHED_ENTRIES)) {
|
2000-06-09 23:54:14 +08:00
|
|
|
AG(cache)[CACHE_INDEX][AG(cache_count)[CACHE_INDEX]++] = p;
|
1999-04-08 02:10:10 +08:00
|
|
|
p->cached = 1;
|
|
|
|
#if ZEND_DEBUG
|
|
|
|
p->magic = MEM_BLOCK_CACHED_MAGIC;
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2003-05-29 06:56:56 +08:00
|
|
|
#endif
|
1999-04-08 02:10:10 +08:00
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
2002-06-24 15:22:25 +08:00
|
|
|
#if ZEND_DEBUG || !defined(ZEND_MM)
|
1999-04-08 02:10:10 +08:00
|
|
|
REMOVE_POINTER_FROM_LIST(p);
|
2002-06-24 15:22:25 +08:00
|
|
|
#endif
|
1999-04-08 02:10:10 +08:00
|
|
|
|
|
|
|
#if MEMORY_LIMIT
|
2000-06-09 23:54:14 +08:00
|
|
|
AG(allocated_memory) -= SIZE;
|
1999-04-08 02:10:10 +08:00
|
|
|
#endif
|
|
|
|
|
2001-08-03 15:06:05 +08:00
|
|
|
ZEND_DO_FREE(p);
|
1999-04-08 02:10:10 +08:00
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-28 14:24:15 +08:00
|
|
|
ZEND_API void *_ecalloc(size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
|
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
int final_size = size*nmemb;
|
|
|
|
|
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
|
|
|
p = _emalloc(final_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
|
|
|
|
if (!p) {
|
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
|
|
|
return (void *) p;
|
|
|
|
}
|
|
|
|
memset(p, 0, final_size);
|
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2001-04-30 13:39:37 +08:00
|
|
|
zend_mem_header *p;
|
|
|
|
zend_mem_header *orig;
|
2001-04-17 23:19:14 +08:00
|
|
|
DECLARE_CACHE_VARS();
|
2001-07-28 18:51:54 +08:00
|
|
|
TSRMLS_FETCH();
|
1999-04-08 02:10:10 +08:00
|
|
|
|
|
|
|
if (!ptr) {
|
1999-08-28 18:18:54 +08:00
|
|
|
return _emalloc(size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2000-06-09 23:54:14 +08:00
|
|
|
|
2001-04-30 13:39:37 +08:00
|
|
|
p = orig = (zend_mem_header *) ((char *)ptr-sizeof(zend_mem_header)-MEM_HEADER_PADDING);
|
|
|
|
|
2001-07-23 12:21:11 +08:00
|
|
|
#if defined(ZTS) && TSRM_DEBUG
|
2000-11-18 10:43:23 +08:00
|
|
|
if (p->thread_id != tsrm_thread_id()) {
|
|
|
|
void *new_p;
|
|
|
|
|
|
|
|
tsrm_error(TSRM_ERROR_LEVEL_ERROR, "Memory block allocated at %s:(%d) on thread %x reallocated at %s:(%d) on thread %x, duplicating",
|
|
|
|
p->filename, p->lineno, p->thread_id,
|
|
|
|
__zend_filename, __zend_lineno, tsrm_thread_id());
|
|
|
|
new_p = _emalloc(size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
|
|
|
|
memcpy(new_p, ptr, p->size);
|
|
|
|
return new_p;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-06-09 23:54:14 +08:00
|
|
|
CALCULATE_REAL_SIZE_AND_CACHE_INDEX(size);
|
|
|
|
|
1999-04-08 02:10:10 +08:00
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
2002-06-24 15:22:25 +08:00
|
|
|
#if ZEND_DEBUG || !defined(ZEND_MM)
|
1999-04-08 02:10:10 +08:00
|
|
|
REMOVE_POINTER_FROM_LIST(p);
|
2002-06-24 15:22:25 +08:00
|
|
|
#endif
|
2001-08-11 23:56:40 +08:00
|
|
|
p = (zend_mem_header *) ZEND_DO_REALLOC(p, sizeof(zend_mem_header)+MEM_HEADER_PADDING+SIZE+END_MAGIC_SIZE);
|
1999-04-08 02:10:10 +08:00
|
|
|
if (!p) {
|
1999-07-09 19:03:56 +08:00
|
|
|
if (!allow_failure) {
|
|
|
|
fprintf(stderr,"FATAL: erealloc(): Unable to allocate %ld bytes\n", (long) size);
|
1999-07-09 19:19:38 +08:00
|
|
|
#if ZEND_DEBUG && HAVE_KILL && HAVE_GETPID
|
1999-07-09 19:10:19 +08:00
|
|
|
kill(getpid(), SIGSEGV);
|
|
|
|
#else
|
1999-07-09 19:03:56 +08:00
|
|
|
exit(1);
|
1999-07-09 19:10:19 +08:00
|
|
|
#endif
|
1999-07-09 19:03:56 +08:00
|
|
|
}
|
2002-06-24 15:22:25 +08:00
|
|
|
#if ZEND_DEBUG || !defined(ZEND_MM)
|
1999-04-08 02:10:10 +08:00
|
|
|
ADD_POINTER_TO_LIST(orig);
|
2002-06-24 15:22:25 +08:00
|
|
|
#endif
|
1999-07-09 19:03:56 +08:00
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
1999-04-08 02:10:10 +08:00
|
|
|
return (void *)NULL;
|
|
|
|
}
|
2002-06-24 15:22:25 +08:00
|
|
|
#if ZEND_DEBUG || !defined(ZEND_MM)
|
1999-04-08 02:10:10 +08:00
|
|
|
ADD_POINTER_TO_LIST(p);
|
2002-06-24 15:22:25 +08:00
|
|
|
#endif
|
1999-04-08 02:10:10 +08:00
|
|
|
#if ZEND_DEBUG
|
1999-08-28 03:17:19 +08:00
|
|
|
p->filename = __zend_filename;
|
|
|
|
p->lineno = __zend_lineno;
|
1999-04-08 02:10:10 +08:00
|
|
|
p->magic = MEM_BLOCK_START_MAGIC;
|
2001-04-29 21:00:46 +08:00
|
|
|
memcpy((((char *) p) + sizeof(zend_mem_header) + MEM_HEADER_PADDING + size), &mem_block_end_magic, sizeof(long));
|
1999-04-08 02:10:10 +08:00
|
|
|
#endif
|
|
|
|
#if MEMORY_LIMIT
|
2000-06-10 01:51:37 +08:00
|
|
|
CHECK_MEMORY_LIMIT(size - p->size, SIZE - REAL_SIZE(p->size));
|
2001-05-16 23:02:30 +08:00
|
|
|
if (AG(allocated_memory) > AG(allocated_memory_peak)) {
|
|
|
|
AG(allocated_memory_peak) = AG(allocated_memory);
|
2001-05-17 20:51:24 +08:00
|
|
|
}
|
2001-05-12 02:16:41 +08:00
|
|
|
#endif
|
|
|
|
|
1999-04-08 02:10:10 +08:00
|
|
|
p->size = size;
|
|
|
|
|
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
2001-04-29 21:00:46 +08:00
|
|
|
return (void *)((char *)p+sizeof(zend_mem_header)+MEM_HEADER_PADDING);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-28 18:18:54 +08:00
|
|
|
ZEND_API char *_estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
|
|
|
int length;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
length = strlen(s)+1;
|
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
1999-08-28 18:18:54 +08:00
|
|
|
p = (char *) _emalloc(length ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
|
1999-04-08 02:10:10 +08:00
|
|
|
if (!p) {
|
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
|
|
|
return (char *)NULL;
|
|
|
|
}
|
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
2001-04-28 23:59:39 +08:00
|
|
|
memcpy(p, s, length);
|
1999-04-08 02:10:10 +08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-28 18:18:54 +08:00
|
|
|
ZEND_API char *_estrndup(const char *s, uint length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
HANDLE_BLOCK_INTERRUPTIONS();
|
1999-08-28 18:18:54 +08:00
|
|
|
p = (char *) _emalloc(length+1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
|
1999-04-08 02:10:10 +08:00
|
|
|
if (!p) {
|
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
|
|
|
return (char *)NULL;
|
|
|
|
}
|
|
|
|
HANDLE_UNBLOCK_INTERRUPTIONS();
|
2001-04-28 23:59:39 +08:00
|
|
|
memcpy(p, s, length);
|
|
|
|
p[length] = 0;
|
1999-04-08 02:10:10 +08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ZEND_API char *zend_strndup(const char *s, uint length)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = (char *) malloc(length+1);
|
|
|
|
if (!p) {
|
|
|
|
return (char *)NULL;
|
|
|
|
}
|
|
|
|
if (length) {
|
2001-04-28 23:59:39 +08:00
|
|
|
memcpy(p, s, length);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2001-04-28 23:59:39 +08:00
|
|
|
p[length] = 0;
|
1999-04-08 02:10:10 +08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
#if MEMORY_LIMIT
|
2001-07-28 18:51:54 +08:00
|
|
|
TSRMLS_FETCH();
|
1999-04-10 22:44:35 +08:00
|
|
|
|
|
|
|
AG(memory_limit) = memory_limit;
|
|
|
|
return SUCCESS;
|
|
|
|
#else
|
|
|
|
return FAILURE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-28 18:51:54 +08:00
|
|
|
ZEND_API void start_memory_manager(TSRMLS_D)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2002-06-22 22:37:49 +08:00
|
|
|
AG(head) = NULL;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
|
|
|
#if MEMORY_LIMIT
|
2001-08-17 22:53:55 +08:00
|
|
|
AG(memory_limit) = 1<<30; /* ridiculous limit, effectively no limit */
|
2001-04-28 23:59:39 +08:00
|
|
|
AG(allocated_memory) = 0;
|
|
|
|
AG(memory_exhausted) = 0;
|
2001-05-16 23:02:30 +08:00
|
|
|
AG(allocated_memory_peak) = 0;
|
2001-05-12 02:16:41 +08:00
|
|
|
#endif
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2002-06-24 21:41:26 +08:00
|
|
|
#if ZEND_ENABLE_FAST_CACHE
|
1999-12-28 03:07:33 +08:00
|
|
|
memset(AG(fast_cache_list_head), 0, sizeof(AG(fast_cache_list_head)));
|
2002-06-24 21:41:26 +08:00
|
|
|
#endif
|
|
|
|
#if !ZEND_DISABLE_MEMORY_CACHE
|
2001-04-29 21:00:46 +08:00
|
|
|
memset(AG(cache_count), 0, sizeof(AG(cache_count)));
|
2002-06-24 21:41:26 +08:00
|
|
|
#endif
|
2000-05-16 23:42:16 +08:00
|
|
|
|
2002-06-20 00:53:06 +08:00
|
|
|
#ifdef ZEND_MM
|
2002-06-17 01:17:47 +08:00
|
|
|
zend_mm_startup(&AG(mm_heap), 256*1024);
|
|
|
|
#elif defined(ZEND_WIN32)
|
2001-08-03 15:06:05 +08:00
|
|
|
AG(memory_heap) = HeapCreate(HEAP_NO_SERIALIZE, 256*1024, 0);
|
|
|
|
#endif
|
|
|
|
|
2000-06-22 17:17:38 +08:00
|
|
|
#if ZEND_DEBUG
|
|
|
|
memset(AG(cache_stats), 0, sizeof(AG(cache_stats)));
|
|
|
|
memset(AG(fast_cache_stats), 0, sizeof(AG(fast_cache_stats)));
|
|
|
|
#endif
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-27 03:17:26 +08:00
|
|
|
ZEND_API void shutdown_memory_manager(int silent, int clean_cache TSRMLS_DC)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2003-08-29 00:41:20 +08:00
|
|
|
#if ZEND_DEBUG || !defined(ZEND_MM)
|
1999-12-27 04:45:42 +08:00
|
|
|
zend_mem_header *p, *t;
|
2003-08-29 00:41:20 +08:00
|
|
|
#endif
|
|
|
|
#if ZEND_DEBUG
|
2003-08-19 06:31:11 +08:00
|
|
|
zend_uint grand_total_leaks=0;
|
2003-08-29 00:41:20 +08:00
|
|
|
#endif
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2002-06-26 19:07:35 +08:00
|
|
|
#if defined(ZEND_MM) && !ZEND_DEBUG
|
|
|
|
if (clean_cache) {
|
|
|
|
zend_mm_shutdown(&AG(mm_heap));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#elif defined(ZEND_WIN32) && !ZEND_DEBUG
|
|
|
|
if (clean_cache && AG(memory_heap)) {
|
|
|
|
HeapDestroy(AG(memory_heap));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-06-25 01:47:46 +08:00
|
|
|
#if ZEND_ENABLE_FAST_CACHE
|
2002-06-26 19:07:35 +08:00
|
|
|
{
|
|
|
|
zend_fast_cache_list_entry *fast_cache_list_entry, *next_fast_cache_list_entry;
|
|
|
|
unsigned int fci;
|
|
|
|
|
|
|
|
for (fci=0; fci<MAX_FAST_CACHE_TYPES; fci++) {
|
|
|
|
fast_cache_list_entry = AG(fast_cache_list_head)[fci];
|
|
|
|
while (fast_cache_list_entry) {
|
|
|
|
next_fast_cache_list_entry = fast_cache_list_entry->next;
|
|
|
|
efree(fast_cache_list_entry);
|
|
|
|
fast_cache_list_entry = next_fast_cache_list_entry;
|
|
|
|
}
|
|
|
|
AG(fast_cache_list_head)[fci] = NULL;
|
1999-12-28 03:07:33 +08:00
|
|
|
}
|
1999-12-27 05:56:59 +08:00
|
|
|
}
|
2002-06-25 02:49:13 +08:00
|
|
|
#endif /* ZEND_ENABLE_FAST_CACHE */
|
|
|
|
|
2002-12-02 04:15:09 +08:00
|
|
|
#if !ZEND_DISABLE_MEMORY_CACHE && !defined(ZEND_MM)
|
2001-06-20 01:59:07 +08:00
|
|
|
if (1 || clean_cache) {
|
2002-06-25 02:49:13 +08:00
|
|
|
unsigned int i, j;
|
2001-06-20 01:59:07 +08:00
|
|
|
zend_mem_header *ptr;
|
|
|
|
|
|
|
|
for (i=1; i<MAX_CACHED_MEMORY; i++) {
|
|
|
|
for (j=0; j<AG(cache_count)[i]; j++) {
|
2001-06-30 15:58:34 +08:00
|
|
|
ptr = (zend_mem_header *) AG(cache)[i][j];
|
2001-06-29 08:36:34 +08:00
|
|
|
#if MEMORY_LIMIT
|
|
|
|
AG(allocated_memory) -= REAL_SIZE(ptr->size);
|
|
|
|
#endif
|
2001-06-20 01:59:07 +08:00
|
|
|
REMOVE_POINTER_FROM_LIST(ptr);
|
2001-08-03 15:06:05 +08:00
|
|
|
ZEND_DO_FREE(ptr);
|
2001-06-20 01:59:07 +08:00
|
|
|
}
|
|
|
|
AG(cache_count)[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
2002-06-25 02:49:13 +08:00
|
|
|
#endif /* !ZEND_DISABLE_MEMORY_CACHE */
|
|
|
|
|
2002-06-25 01:47:46 +08:00
|
|
|
#if ZEND_DEBUG || !defined(ZEND_MM)
|
2001-04-28 23:59:39 +08:00
|
|
|
p = AG(head);
|
|
|
|
t = AG(head);
|
1999-04-08 02:10:10 +08:00
|
|
|
while (t) {
|
2001-04-28 23:45:50 +08:00
|
|
|
if (!t->cached) {
|
1999-04-08 02:10:10 +08:00
|
|
|
#if ZEND_DEBUG
|
1999-06-26 19:48:22 +08:00
|
|
|
if (!t->cached && !t->reported) {
|
1999-12-27 04:45:42 +08:00
|
|
|
zend_mem_header *iterator;
|
1999-06-26 19:48:22 +08:00
|
|
|
int total_leak=0, total_leak_count=0;
|
|
|
|
|
2003-08-19 06:31:11 +08:00
|
|
|
grand_total_leaks++;
|
1999-06-26 19:48:22 +08:00
|
|
|
if (!silent) {
|
|
|
|
zend_message_dispatcher(ZMSG_MEMORY_LEAK_DETECTED, t);
|
|
|
|
}
|
|
|
|
t->reported = 1;
|
|
|
|
for (iterator=t->pNext; iterator; iterator=iterator->pNext) {
|
1999-07-11 06:50:44 +08:00
|
|
|
if (!iterator->cached
|
|
|
|
&& iterator->filename==t->filename
|
1999-06-26 19:48:22 +08:00
|
|
|
&& iterator->lineno==t->lineno) {
|
|
|
|
total_leak += iterator->size;
|
|
|
|
total_leak_count++;
|
|
|
|
iterator->reported = 1;
|
1999-05-22 19:20:56 +08:00
|
|
|
}
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
1999-06-26 19:48:22 +08:00
|
|
|
if (!silent && total_leak_count>0) {
|
|
|
|
zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, (void *) (long) (total_leak_count));
|
|
|
|
}
|
2003-08-21 06:15:01 +08:00
|
|
|
grand_total_leaks += total_leak_count;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
#endif
|
2000-08-20 00:51:30 +08:00
|
|
|
#if MEMORY_LIMIT
|
2002-08-08 16:42:22 +08:00
|
|
|
AG(allocated_memory) -= REAL_SIZE(t->size);
|
2000-08-20 00:51:30 +08:00
|
|
|
#endif
|
1999-04-08 02:10:10 +08:00
|
|
|
p = t->pNext;
|
|
|
|
REMOVE_POINTER_FROM_LIST(t);
|
2001-08-03 15:06:05 +08:00
|
|
|
ZEND_DO_FREE(t);
|
1999-04-08 02:10:10 +08:00
|
|
|
t = p;
|
|
|
|
} else {
|
|
|
|
t = t->pNext;
|
|
|
|
}
|
|
|
|
}
|
1999-12-28 00:42:59 +08:00
|
|
|
|
2003-10-15 01:26:36 +08:00
|
|
|
#if ZEND_DEBUG
|
2003-08-21 07:27:04 +08:00
|
|
|
if (!silent && grand_total_leaks > 0) {
|
2003-08-19 06:31:11 +08:00
|
|
|
zend_message_dispatcher(ZMSG_MEMORY_LEAKS_GRAND_TOTAL, &grand_total_leaks);
|
|
|
|
}
|
2003-10-15 01:26:36 +08:00
|
|
|
#endif
|
2003-08-19 06:31:11 +08:00
|
|
|
|
2000-08-20 00:51:30 +08:00
|
|
|
#if MEMORY_LIMIT
|
2000-08-20 00:35:02 +08:00
|
|
|
AG(memory_exhausted)=0;
|
2001-05-18 22:32:11 +08:00
|
|
|
AG(allocated_memory_peak) = 0;
|
2000-08-20 00:51:30 +08:00
|
|
|
#endif
|
2001-05-16 23:02:30 +08:00
|
|
|
|
2000-06-23 01:51:14 +08:00
|
|
|
|
2000-02-01 02:21:54 +08:00
|
|
|
#if (ZEND_DEBUG)
|
1999-12-28 00:42:59 +08:00
|
|
|
do {
|
|
|
|
zval display_memory_cache_stats;
|
|
|
|
int i, j;
|
|
|
|
|
1999-12-28 01:24:33 +08:00
|
|
|
if (clean_cache) {
|
|
|
|
/* we're shutting down completely, don't even touch the INI subsystem */
|
|
|
|
break;
|
|
|
|
}
|
2001-01-08 02:39:11 +08:00
|
|
|
if (zend_get_configuration_directive("display_memory_cache_stats", sizeof("display_memory_cache_stats"), &display_memory_cache_stats)==FAILURE) {
|
1999-12-28 00:42:59 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!atoi(display_memory_cache_stats.value.str.val)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "Memory cache statistics\n"
|
|
|
|
"-----------------------\n\n"
|
1999-12-29 04:33:02 +08:00
|
|
|
"[zval, %2ld]\t\t%d / %d (%.2f%%)\n"
|
|
|
|
"[hash, %2ld]\t\t%d / %d (%.2f%%)\n",
|
1999-12-30 01:22:47 +08:00
|
|
|
(long) sizeof(zval),
|
1999-12-28 03:07:33 +08:00
|
|
|
AG(fast_cache_stats)[ZVAL_CACHE_LIST][1], AG(fast_cache_stats)[ZVAL_CACHE_LIST][0]+AG(fast_cache_stats)[ZVAL_CACHE_LIST][1],
|
|
|
|
((double) AG(fast_cache_stats)[ZVAL_CACHE_LIST][1] / (AG(fast_cache_stats)[ZVAL_CACHE_LIST][0]+AG(fast_cache_stats)[ZVAL_CACHE_LIST][1]))*100,
|
1999-12-30 01:22:47 +08:00
|
|
|
(long) sizeof(HashTable),
|
1999-12-28 03:07:33 +08:00
|
|
|
AG(fast_cache_stats)[HASHTABLE_CACHE_LIST][1], AG(fast_cache_stats)[HASHTABLE_CACHE_LIST][0]+AG(fast_cache_stats)[HASHTABLE_CACHE_LIST][1],
|
|
|
|
((double) AG(fast_cache_stats)[HASHTABLE_CACHE_LIST][1] / (AG(fast_cache_stats)[HASHTABLE_CACHE_LIST][0]+AG(fast_cache_stats)[HASHTABLE_CACHE_LIST][1]))*100);
|
1999-12-28 00:42:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
for (i=0; i<MAX_CACHED_MEMORY; i+=2) {
|
1999-12-28 03:07:33 +08:00
|
|
|
fprintf(stderr, "[%2d, %2d]\t\t", i, i+1);
|
1999-12-28 00:42:59 +08:00
|
|
|
for (j=0; j<2; j++) {
|
|
|
|
fprintf(stderr, "%d / %d (%.2f%%)\t\t",
|
|
|
|
AG(cache_stats)[i+j][1], AG(cache_stats)[i+j][0]+AG(cache_stats)[i+j][1],
|
|
|
|
((double) AG(cache_stats)[i+j][1] / (AG(cache_stats)[i+j][0]+AG(cache_stats)[i+j][1]))*100);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (0);
|
2002-06-25 02:49:13 +08:00
|
|
|
#endif
|
2001-08-03 15:06:05 +08:00
|
|
|
|
2002-06-24 15:22:25 +08:00
|
|
|
#endif
|
2002-06-25 02:49:13 +08:00
|
|
|
|
2002-06-26 19:07:35 +08:00
|
|
|
#if defined(ZEND_MM) && ZEND_DEBUG
|
2002-06-25 02:49:13 +08:00
|
|
|
if (clean_cache) {
|
|
|
|
zend_mm_shutdown(&AG(mm_heap));
|
2002-06-26 19:07:35 +08:00
|
|
|
return;
|
2002-06-25 02:49:13 +08:00
|
|
|
}
|
2002-06-26 19:07:35 +08:00
|
|
|
#elif defined(ZEND_WIN32) && ZEND_DEBUG
|
2002-06-25 02:49:13 +08:00
|
|
|
if (clean_cache && AG(memory_heap)) {
|
|
|
|
HeapDestroy(AG(memory_heap));
|
2002-06-26 19:07:35 +08:00
|
|
|
return;
|
2002-06-25 02:49:13 +08:00
|
|
|
}
|
1999-12-28 00:42:59 +08:00
|
|
|
#endif
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if ZEND_DEBUG
|
1999-05-12 05:38:39 +08:00
|
|
|
void zend_debug_alloc_output(char *format, ...)
|
|
|
|
{
|
|
|
|
char output_buf[256];
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
vsprintf(output_buf, format, args);
|
|
|
|
va_end(args);
|
|
|
|
|
2000-02-11 23:59:30 +08:00
|
|
|
#ifdef ZEND_WIN32
|
1999-05-12 05:38:39 +08:00
|
|
|
OutputDebugString(output_buf);
|
|
|
|
#else
|
2000-10-05 03:29:28 +08:00
|
|
|
fprintf(stderr, "%s", output_buf);
|
1999-05-12 05:38:39 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
2001-04-29 21:00:46 +08:00
|
|
|
zend_mem_header *p = (zend_mem_header *) ((char *)ptr - sizeof(zend_mem_header) - MEM_HEADER_PADDING);
|
1999-04-08 02:10:10 +08:00
|
|
|
int no_cache_notice=0;
|
|
|
|
int valid_beginning=1;
|
|
|
|
int had_problems=0;
|
2001-04-29 21:00:46 +08:00
|
|
|
long end_magic;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
|
|
|
if (silent==2) {
|
2001-04-28 23:59:39 +08:00
|
|
|
silent = 1;
|
|
|
|
no_cache_notice = 1;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
if (silent==3) {
|
2001-04-28 23:59:39 +08:00
|
|
|
silent = 0;
|
|
|
|
no_cache_notice = 1;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
if (!silent) {
|
1999-06-01 02:39:29 +08:00
|
|
|
zend_message_dispatcher(ZMSG_LOG_SCRIPT_NAME, NULL);
|
1999-05-12 05:38:39 +08:00
|
|
|
zend_debug_alloc_output("---------------------------------------\n");
|
1999-08-28 03:17:19 +08:00
|
|
|
zend_debug_alloc_output("%s(%d) : Block 0x%0.8lX status:\n" ZEND_FILE_LINE_RELAY_CC, (long) p);
|
1999-08-28 18:18:54 +08:00
|
|
|
if (__zend_orig_filename) {
|
|
|
|
zend_debug_alloc_output("%s(%d) : Actual location (location was relayed)\n" ZEND_FILE_LINE_ORIG_RELAY_CC);
|
|
|
|
}
|
1999-05-12 05:38:39 +08:00
|
|
|
zend_debug_alloc_output("%10s\t","Beginning: ");
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (p->magic) {
|
|
|
|
case MEM_BLOCK_START_MAGIC:
|
|
|
|
if (!silent) {
|
1999-05-12 05:38:39 +08:00
|
|
|
zend_debug_alloc_output("OK (allocated on %s:%d, %d bytes)\n", p->filename, p->lineno, p->size);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
break; /* ok */
|
|
|
|
case MEM_BLOCK_FREED_MAGIC:
|
|
|
|
if (!silent) {
|
1999-05-12 05:38:39 +08:00
|
|
|
zend_debug_alloc_output("Freed\n");
|
2001-04-28 23:59:39 +08:00
|
|
|
had_problems = 1;
|
1999-04-08 02:10:10 +08:00
|
|
|
} else {
|
1999-08-28 18:18:54 +08:00
|
|
|
return _mem_block_check(ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MEM_BLOCK_CACHED_MAGIC:
|
|
|
|
if (!silent) {
|
|
|
|
if (!no_cache_notice) {
|
1999-05-12 05:38:39 +08:00
|
|
|
zend_debug_alloc_output("Cached (allocated on %s:%d, %d bytes)\n", p->filename, p->lineno, p->size);
|
2001-04-28 23:59:39 +08:00
|
|
|
had_problems = 1;
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!no_cache_notice) {
|
1999-08-28 18:18:54 +08:00
|
|
|
return _mem_block_check(ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (!silent) {
|
1999-05-12 05:38:39 +08:00
|
|
|
zend_debug_alloc_output("Overrun (magic=0x%0.8lX, expected=0x%0.8lX)\n", p->magic, MEM_BLOCK_START_MAGIC);
|
1999-04-08 02:10:10 +08:00
|
|
|
} else {
|
1999-08-28 18:18:54 +08:00
|
|
|
return _mem_block_check(ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2001-04-28 23:59:39 +08:00
|
|
|
had_problems = 1;
|
|
|
|
valid_beginning = 0;
|
1999-04-08 02:10:10 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-29 21:00:46 +08:00
|
|
|
memcpy(&end_magic, (((char *) p)+sizeof(zend_mem_header)+MEM_HEADER_PADDING+p->size), sizeof(long));
|
|
|
|
|
|
|
|
if (valid_beginning && (end_magic != MEM_BLOCK_END_MAGIC)) {
|
2001-04-30 13:39:37 +08:00
|
|
|
char *overflow_ptr, *magic_ptr=(char *) &mem_block_end_magic;
|
1999-04-08 02:10:10 +08:00
|
|
|
int overflows=0;
|
2003-09-25 23:54:17 +08:00
|
|
|
long i;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
|
|
|
if (silent) {
|
1999-08-28 18:18:54 +08:00
|
|
|
return _mem_block_check(ptr, 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
2001-04-28 23:59:39 +08:00
|
|
|
had_problems = 1;
|
2001-04-30 13:39:37 +08:00
|
|
|
overflow_ptr = (char *) &end_magic;
|
1999-04-08 02:10:10 +08:00
|
|
|
|
2003-09-26 16:09:56 +08:00
|
|
|
for (i=0; i<(int)sizeof(long); i++) {
|
1999-04-08 02:10:10 +08:00
|
|
|
if (overflow_ptr[i]!=magic_ptr[i]) {
|
|
|
|
overflows++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-05-12 05:38:39 +08:00
|
|
|
zend_debug_alloc_output("%10s\t", "End:");
|
2001-04-29 21:00:46 +08:00
|
|
|
zend_debug_alloc_output("Overflown (magic=0x%0.8lX instead of 0x%0.8lX)\n", end_magic, MEM_BLOCK_END_MAGIC);
|
1999-05-12 05:38:39 +08:00
|
|
|
zend_debug_alloc_output("%10s\t","");
|
2003-09-26 16:09:56 +08:00
|
|
|
if (overflows>=(int)sizeof(long)) {
|
1999-05-12 05:38:39 +08:00
|
|
|
zend_debug_alloc_output("At least %d bytes overflown\n", sizeof(long));
|
1999-04-08 02:10:10 +08:00
|
|
|
} else {
|
1999-05-12 05:38:39 +08:00
|
|
|
zend_debug_alloc_output("%d byte(s) overflown\n", overflows);
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
} else if (!silent) {
|
1999-05-12 05:38:39 +08:00
|
|
|
zend_debug_alloc_output("%10s\t", "End:");
|
1999-04-08 02:10:10 +08:00
|
|
|
if (valid_beginning) {
|
1999-05-12 05:38:39 +08:00
|
|
|
zend_debug_alloc_output("OK\n");
|
1999-04-08 02:10:10 +08:00
|
|
|
} else {
|
1999-05-12 05:38:39 +08:00
|
|
|
zend_debug_alloc_output("Unknown\n");
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!silent) {
|
1999-05-12 05:38:39 +08:00
|
|
|
zend_debug_alloc_output("---------------------------------------\n");
|
1999-04-08 02:10:10 +08:00
|
|
|
}
|
|
|
|
return ((!had_problems) ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-28 18:18:54 +08:00
|
|
|
ZEND_API void _full_mem_check(int silent ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
|
1999-04-08 02:10:10 +08:00
|
|
|
{
|
1999-12-27 04:45:42 +08:00
|
|
|
zend_mem_header *p;
|
1999-04-08 02:10:10 +08:00
|
|
|
int errors=0;
|
2001-07-28 18:51:54 +08:00
|
|
|
TSRMLS_FETCH();
|
1999-04-21 11:49:09 +08:00
|
|
|
|
|
|
|
p = AG(head);
|
|
|
|
|
1999-04-08 02:10:10 +08:00
|
|
|
|
1999-05-12 05:38:39 +08:00
|
|
|
zend_debug_alloc_output("------------------------------------------------\n");
|
1999-08-28 03:17:19 +08:00
|
|
|
zend_debug_alloc_output("Full Memory Check at %s:%d\n" ZEND_FILE_LINE_RELAY_CC);
|
1999-04-08 02:10:10 +08:00
|
|
|
|
|
|
|
while (p) {
|
2001-04-29 21:00:46 +08:00
|
|
|
if (!_mem_block_check((void *)((char *)p + sizeof(zend_mem_header) + MEM_HEADER_PADDING), (silent?2:3) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC)) {
|
1999-04-08 02:10:10 +08:00
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
p = p->pNext;
|
|
|
|
}
|
1999-08-28 03:17:19 +08:00
|
|
|
zend_debug_alloc_output("End of full memory check %s:%d (%d errors)\n" ZEND_FILE_LINE_RELAY_CC, errors);
|
1999-05-12 05:38:39 +08:00
|
|
|
zend_debug_alloc_output("------------------------------------------------\n");
|
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:
|
|
|
|
*/
|