php-src/Zend/zend_alloc.c

624 lines
17 KiB
C
Raw Normal View History

1999-04-08 02:10:10 +08:00
/*
+----------------------------------------------------------------------+
| Zend Engine |
+----------------------------------------------------------------------+
2000-03-06 13:26:39 +08:00
| Copyright (c) 1998-2000 Zend Technologies Ltd. (http://www.zend.com) |
1999-04-08 02:10:10 +08:00
+----------------------------------------------------------------------+
2000-03-06 13:26:39 +08:00
| This source file is subject to version 0.92 of the Zend license, |
1999-07-16 22:58:16 +08:00
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
2000-03-06 13:26:39 +08:00
| http://www.zend.com/license/0_92.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> |
+----------------------------------------------------------------------+
*/
1999-07-16 22:58:16 +08:00
1999-04-08 02:10:10 +08:00
#include <stdlib.h>
#include "zend.h"
#include "zend_alloc.h"
#include "zend_globals.h"
#include "zend_fast_cache.h"
#ifdef HAVE_SIGNAL_H
# include <signal.h>
1999-07-09 19:19:38 +08:00
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
1999-07-09 19:19:38 +08:00
#endif
1999-04-08 02:10:10 +08:00
#ifndef ZTS
ZEND_API zend_alloc_globals alloc_globals;
1999-04-08 02:10:10 +08:00
#endif
#define ZEND_DISABLE_MEMORY_CACHE 0
1999-04-08 02:10:10 +08:00
#if ZEND_DEBUG
# define END_MAGIC_SIZE sizeof(long)
# define END_ALIGNMENT(size) (((size)%PLATFORM_ALIGNMENT)?(PLATFORM_ALIGNMENT-((size)%PLATFORM_ALIGNMENT)):0)
#else
# define END_MAGIC_SIZE 0
# define END_ALIGNMENT(size) 0
#endif
# if MEMORY_LIMIT
# if ZEND_DEBUG
#define CHECK_MEMORY_LIMIT(s) _CHECK_MEMORY_LIMIT(s, __zend_filename, __zend_lineno)
1999-04-08 02:10:10 +08:00
# else
#define CHECK_MEMORY_LIMIT(s) _CHECK_MEMORY_LIMIT(s,NULL,0)
# endif
#define _CHECK_MEMORY_LIMIT(s,file,lineno) { AG(allocated_memory) += (s);\
1999-04-10 22:44:35 +08:00
if (AG(memory_limit)<AG(allocated_memory)) {\
1999-04-08 02:10:10 +08:00
if (!file) { \
1999-04-10 22:44:35 +08:00
zend_error(E_ERROR,"Allowed memory size of %d bytes exhausted (tried to allocate %d bytes)", AG(memory_limit),s); \
1999-04-08 02:10:10 +08:00
} else { \
1999-04-10 22:44:35 +08:00
zend_error(E_ERROR,"Allowed memory size of %d bytes exhausted at %s:%d (tried to allocate %d bytes)", AG(memory_limit), file, lineno, s); \
1999-04-08 02:10:10 +08:00
} \
} \
}
# endif
#ifndef CHECK_MEMORY_LIMIT
#define CHECK_MEMORY_LIMIT(s)
#endif
#define REMOVE_POINTER_FROM_LIST(p) \
if (!p->persistent && p==AG(head)) { \
AG(head) = p->pNext; \
} else if (p->persistent && p==AG(phead)) { \
AG(phead) = p->pNext; \
} else { \
p->pLast->pNext = p->pNext; \
} \
if (p->pNext) { \
p->pNext->pLast = p->pLast; \
}
#define ADD_POINTER_TO_LIST(p) \
if (p->persistent) { \
p->pNext = AG(phead); \
if (AG(phead)) { \
AG(phead)->pLast = p; \
} \
AG(phead) = p; \
} else { \
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
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;
ALS_FETCH();
1999-04-08 02:10:10 +08:00
if (!ZEND_DISABLE_MEMORY_CACHE && (size < MAX_CACHED_MEMORY) && (AG(cache_count)[size] > 0)) {
1999-04-08 02:10:10 +08:00
p = AG(cache)[size][--AG(cache_count)[size]];
#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;
p->reported = 0;
1999-12-28 00:42:59 +08:00
AG(cache_stats)[size][1]++;
1999-04-08 02:10:10 +08:00
#endif
p->persistent = 0;
p->cached = 0;
1999-12-27 04:45:42 +08:00
return (void *)((char *)p + sizeof(zend_mem_header) + PLATFORM_PADDING);
1999-04-08 02:10:10 +08:00
} else {
1999-12-28 00:42:59 +08:00
#if ZEND_DEBUG
if (size<MAX_CACHED_MEMORY) {
AG(cache_stats)[size][0]++;
}
#endif
1999-12-27 04:45:42 +08:00
p = (zend_mem_header *) malloc(sizeof(zend_mem_header) + size + PLATFORM_PADDING + END_ALIGNMENT(size) + END_MAGIC_SIZE);
1999-04-08 02:10:10 +08:00
}
HANDLE_BLOCK_INTERRUPTIONS();
1999-04-08 02:10:10 +08:00
if (!p) {
fprintf(stderr,"FATAL: emalloc(): Unable to allocate %ld bytes\n", (long) size);
#if ZEND_DEBUG && defined(HAVE_KILL) && defined(HAVE_GETPID)
kill(getpid(), SIGSEGV);
#else
1999-04-08 02:10:10 +08:00
exit(1);
#endif
1999-04-08 02:10:10 +08:00
HANDLE_UNBLOCK_INTERRUPTIONS();
return (void *)p;
}
p->persistent = p->cached = 0;
ADD_POINTER_TO_LIST(p);
p->size = size;
#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;
p->reported = 0;
1999-12-27 04:45:42 +08:00
*((long *)(((char *) p) + sizeof(zend_mem_header)+size+PLATFORM_PADDING+END_ALIGNMENT(size))) = MEM_BLOCK_END_MAGIC;
1999-04-08 02:10:10 +08:00
#endif
#if MEMORY_LIMIT
CHECK_MEMORY_LIMIT(size);
#endif
HANDLE_UNBLOCK_INTERRUPTIONS();
1999-12-27 04:45:42 +08:00
return (void *)((char *)p + sizeof(zend_mem_header) + PLATFORM_PADDING);
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
{
1999-12-27 04:45:42 +08:00
zend_mem_header *p = (zend_mem_header *) ((char *)ptr - sizeof(zend_mem_header) - PLATFORM_PADDING);
ALS_FETCH();
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;
}
1999-04-08 02:10:10 +08:00
memset(ptr, 0x5a, p->size);
#endif
if (!ZEND_DISABLE_MEMORY_CACHE
&& !p->persistent && (p->size < MAX_CACHED_MEMORY) && (AG(cache_count)[p->size] < MAX_CACHED_ENTRIES)) {
1999-04-08 02:10:10 +08:00
AG(cache)[p->size][AG(cache_count)[p->size]++] = p;
p->cached = 1;
#if ZEND_DEBUG
p->magic = MEM_BLOCK_CACHED_MAGIC;
#endif
return;
}
HANDLE_BLOCK_INTERRUPTIONS();
REMOVE_POINTER_FROM_LIST(p);
#if MEMORY_LIMIT
AG(allocated_memory) -= p->size;
#endif
free(p);
HANDLE_UNBLOCK_INTERRUPTIONS();
}
1999-08-28 18:18:54 +08:00
ZEND_API void *_ecalloc(size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
1999-04-08 02:10:10 +08:00
{
void *p;
int final_size=size*nmemb;
HANDLE_BLOCK_INTERRUPTIONS();
1999-08-28 18:18:54 +08:00
p = _emalloc(final_size 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 (void *) p;
}
memset(p,(int)NULL,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
{
1999-12-27 04:45:42 +08:00
zend_mem_header *p = (zend_mem_header *) ((char *)ptr-sizeof(zend_mem_header)-PLATFORM_PADDING);
zend_mem_header *orig = p;
ALS_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
}
HANDLE_BLOCK_INTERRUPTIONS();
REMOVE_POINTER_FROM_LIST(p);
1999-12-27 04:45:42 +08:00
p = (zend_mem_header *) realloc(p,sizeof(zend_mem_header)+size+PLATFORM_PADDING+END_ALIGNMENT(size)+END_MAGIC_SIZE);
1999-04-08 02:10:10 +08:00
if (!p) {
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
kill(getpid(), SIGSEGV);
#else
exit(1);
#endif
}
1999-04-08 02:10:10 +08:00
ADD_POINTER_TO_LIST(orig);
HANDLE_UNBLOCK_INTERRUPTIONS();
1999-04-08 02:10:10 +08:00
return (void *)NULL;
}
ADD_POINTER_TO_LIST(p);
#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;
1999-12-27 04:45:42 +08:00
*((long *)(((char *) p) + sizeof(zend_mem_header)+size+PLATFORM_PADDING+END_ALIGNMENT(size))) = MEM_BLOCK_END_MAGIC;
1999-04-08 02:10:10 +08:00
#endif
#if MEMORY_LIMIT
CHECK_MEMORY_LIMIT(size - p->size);
#endif
p->size = size;
HANDLE_UNBLOCK_INTERRUPTIONS();
1999-12-27 04:45:42 +08:00
return (void *)((char *)p+sizeof(zend_mem_header)+PLATFORM_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();
memcpy(p,s,length);
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();
memcpy(p,s,length);
p[length]=0;
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) {
memcpy(p,s,length);
}
p[length]=0;
return p;
}
ZEND_API int zend_set_memory_limit(unsigned int memory_limit)
1999-04-10 22:44:35 +08:00
{
#if MEMORY_LIMIT
ALS_FETCH();
AG(memory_limit) = memory_limit;
return SUCCESS;
#else
return FAILURE;
#endif
}
1999-09-06 03:03:35 +08:00
ZEND_API void start_memory_manager(ALS_D)
1999-04-08 02:10:10 +08:00
{
AG(phead) = AG(head) = NULL;
#if MEMORY_LIMIT
1999-04-10 22:44:35 +08:00
AG(memory_limit)=1<<30; /* rediculous limit, effectively no limit */
1999-04-08 02:10:10 +08:00
AG(allocated_memory)=0;
AG(memory_exhausted)=0;
#endif
1999-12-28 00:42:59 +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)));
1999-12-28 00:42:59 +08:00
#endif
memset(AG(fast_cache_list_head), 0, sizeof(AG(fast_cache_list_head)));
1999-04-08 02:10:10 +08:00
memset(AG(cache_count),0,MAX_CACHED_MEMORY*sizeof(unsigned char));
}
ZEND_API void shutdown_memory_manager(int silent, int clean_cache)
{
1999-12-27 04:45:42 +08:00
zend_mem_header *p, *t;
int fci;
1999-08-29 05:43:24 +08:00
#if ZEND_DEBUG
int had_leaks=0;
#endif
zend_fast_cache_list_entry *fast_cache_list_entry, *next_fast_cache_list_entry;
ALS_FETCH();
1999-04-08 02:10:10 +08:00
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-04-08 02:10:10 +08:00
p=AG(head);
t=AG(head);
while (t) {
if (!t->cached || clean_cache) {
#if ZEND_DEBUG
if (!t->cached && !t->reported) {
1999-12-27 04:45:42 +08:00
zend_mem_header *iterator;
int total_leak=0, total_leak_count=0;
had_leaks=1;
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
&& iterator->lineno==t->lineno) {
total_leak += iterator->size;
total_leak_count++;
iterator->reported = 1;
}
1999-04-08 02:10:10 +08:00
}
if (!silent && total_leak_count>0) {
zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, (void *) (long) (total_leak_count));
}
1999-04-08 02:10:10 +08:00
}
#endif
p = t->pNext;
REMOVE_POINTER_FROM_LIST(t);
free(t);
t = p;
} else {
t = t->pNext;
}
}
1999-12-28 00:42:59 +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;
}
1999-12-28 00:42:59 +08:00
if (zend_get_ini_entry("display_memory_cache_stats", sizeof("display_memory_cache_stats"), &display_memory_cache_stats)==FAILURE) {
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",
(long) sizeof(zval),
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,
(long) sizeof(HashTable),
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) {
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);
#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
fprintf(stderr, output_buf);
#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
{
1999-12-27 04:45:42 +08:00
zend_mem_header *p = (zend_mem_header *) ((char *)ptr - sizeof(zend_mem_header) - PLATFORM_PADDING);
1999-04-08 02:10:10 +08:00
int no_cache_notice=0;
int valid_beginning=1;
int had_problems=0;
if (silent==2) {
silent=1;
no_cache_notice=1;
}
if (silent==3) {
silent=0;
no_cache_notice=1;
}
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");
1999-04-08 02:10:10 +08:00
had_problems=1;
} 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);
1999-04-08 02:10:10 +08:00
had_problems=1;
}
} 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
}
had_problems=1;
valid_beginning=0;
break;
}
if (valid_beginning
1999-12-27 04:45:42 +08:00
&& *((long *)(((char *) p)+sizeof(zend_mem_header)+p->size+PLATFORM_PADDING+END_ALIGNMENT(p->size))) != MEM_BLOCK_END_MAGIC) {
1999-04-08 02:10:10 +08:00
long magic_num = MEM_BLOCK_END_MAGIC;
char *overflow_ptr, *magic_ptr=(char *) &magic_num;
int overflows=0;
int i;
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
}
had_problems=1;
1999-12-27 04:45:42 +08:00
overflow_ptr = ((char *) p)+sizeof(zend_mem_header)+p->size+PLATFORM_PADDING;
1999-04-08 02:10:10 +08:00
for (i=0; i<sizeof(long); i++) {
if (overflow_ptr[i]!=magic_ptr[i]) {
overflows++;
}
}
1999-05-12 05:38:39 +08:00
zend_debug_alloc_output("%10s\t", "End:");
zend_debug_alloc_output("Overflown (magic=0x%0.8lX instead of 0x%0.8lX)\n",
1999-12-27 04:45:42 +08:00
*((long *)(((char *) p) + sizeof(zend_mem_header)+p->size+PLATFORM_PADDING+END_ALIGNMENT(p->size))), MEM_BLOCK_END_MAGIC);
1999-05-12 05:38:39 +08:00
zend_debug_alloc_output("%10s\t","");
1999-04-08 02:10:10 +08:00
if (overflows>=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 (had_problems) {
int foo = 5;
foo+=1;
}
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;
ALS_FETCH();
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) {
1999-12-27 04:45:42 +08:00
if (!_mem_block_check((void *)((char *)p + sizeof(zend_mem_header) + PLATFORM_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
2000-03-26 03:23:16 +08:00
ZEND_API int _persist_alloc(void *ptr 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 = (zend_mem_header *) ((char *)ptr-sizeof(zend_mem_header)-PLATFORM_PADDING);
ALS_FETCH();
1999-04-08 02:10:10 +08:00
#if ZEND_DEBUG
1999-08-28 18:18:54 +08:00
_mem_block_check(ptr, 1 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC);
1999-04-08 02:10:10 +08:00
#endif
HANDLE_BLOCK_INTERRUPTIONS();
1999-04-08 02:10:10 +08:00
/* remove the block from the non persistent list */
REMOVE_POINTER_FROM_LIST(p);
p->persistent = 1;
/* add the block to the persistent list */
ADD_POINTER_TO_LIST(p);
HANDLE_UNBLOCK_INTERRUPTIONS();
2000-05-09 00:18:20 +08:00
return p->size+sizeof(zend_mem_header)+PLATFORM_PADDING;
1999-04-08 02:10:10 +08:00
}
1999-04-26 11:03:39 +08:00
1999-04-08 02:10:10 +08:00
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
*/