CVS update: php3
| From: | zeev | Date: | Fri, 05 Feb 1999 15:06:53 +0000 |
| Subject: | CVS update: php3 | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-3923@lists.php.net to get a copy of this message | ||
Date: Friday February 5, 1999 @ 10:06
Author: zeev
Update of /repository/php3
In directory asf:/u/temp/cvs-serv7196
Modified Files:
alloc.c alloc.h
Log Message:
Backport the Zend memory manager. We have a magical memory manager now.
Index: php3/alloc.c
diff -c php3/alloc.c:1.76 php3/alloc.c:1.77
*** php3/alloc.c:1.76 Thu Feb 4 08:10:27 1999
--- php3/alloc.c Fri Feb 5 10:06:53 1999
***************
*** 1,35 ****
/*
! +----------------------------------------------------------------------+
! | PHP HTML Embedded Scripting Language Version 3.0 |
! +----------------------------------------------------------------------+
! | Copyright (c) 1997-1999 PHP Development Team (See Credits file) |
! +----------------------------------------------------------------------+
! | This program is free software; you can redistribute it and/or modify |
! | it under the terms of one of the following licenses: |
! | |
! | A) the GNU General Public License as published by the Free Software |
! | Foundation; either version 2 of the License, or (at your option) |
! | any later version. |
! | |
! | B) the PHP License as published by the PHP Development Team and |
! | included in the distribution in the file: LICENSE |
! | |
! | This program is distributed in the hope that it will be useful, |
! | but WITHOUT ANY WARRANTY; without even the implied warranty of |
! | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
! | GNU General Public License for more details. |
! | |
! | You should have received a copy of both licenses referred to here. |
! | If you did not, or have any questions about PHP licensing, please |
! | contact core@php.net. |
! +----------------------------------------------------------------------+
! | Authors: Andi Gutmans <andi@php.net> |
! | Zeev Suraski <bourbon@netvision.net.il> |
! | Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
! +----------------------------------------------------------------------+
*/
! /* $Id: alloc.c,v 1.76 1999/02/04 13:10:27 thies Exp $ */
#ifdef THREAD_SAFE
#include "tls.h"
#endif
--- 1,12 ----
/*
! * This file is a part of the Zend scripting language engine. It may
! * be freely distributed as an integrated part of the PHP scripting language,
! * or under the Zend license, which is not yet available at this time.
! *
! * Authors: Andi Gutmans <andi@zend.com>, Zeev Suraski <zeev@zend.com>
*/
!
#ifdef THREAD_SAFE
#include "tls.h"
#endif
***************
*** 38,43 ****
--- 15,26 ----
#include "main.h"
+ #if DEBUG
+ # define END_MAGIC_SIZE sizeof(long)
+ #else
+ # define END_MAGIC_SIZE 0
+ #endif
+
#ifndef THREAD_SAFE
static mem_header *head;
***************
*** 120,132 ****
if ((size < MAX_CACHED_MEMORY) && (GLOBAL(cache_count[size]) > 0)) {
p = GLOBAL(cache[size][--GLOBAL(cache_count[size])]);
#if DEBUG
! p->filename = strdup(filename);
p->lineno = lineno;
#endif
UNBLOCK_INTERRUPTIONS;
return (void *)((char *)p + sizeof(mem_header) + PLATFORM_PADDING);
} else {
! p = (mem_header *) malloc(sizeof(mem_header) + size + PLATFORM_PADDING);
}
if (!p) {
--- 103,116 ----
if ((size < MAX_CACHED_MEMORY) && (GLOBAL(cache_count[size]) > 0)) {
p = GLOBAL(cache[size][--GLOBAL(cache_count[size])]);
#if DEBUG
! p->filename = filename;
p->lineno = lineno;
+ p->magic = MEM_BLOCK_START_MAGIC;
#endif
UNBLOCK_INTERRUPTIONS;
return (void *)((char *)p + sizeof(mem_header) + PLATFORM_PADDING);
} else {
! p = (mem_header *) malloc(sizeof(mem_header) + size + PLATFORM_PADDING + END_MAGIC_SIZE);
}
if (!p) {
***************
*** 141,151 ****
}
p->pLast = (mem_header *) NULL;
GLOBAL(head) = p;
#if DEBUG
! p->filename = strdup(filename);
p->lineno = lineno;
#endif
- p->size = size;
#if MEMORY_LIMIT
CHECK_MEMORY_LIMIT(size);
#endif
--- 125,137 ----
}
p->pLast = (mem_header *) NULL;
GLOBAL(head) = p;
+ p->size = size;
#if DEBUG
! p->filename = filename;
p->lineno = lineno;
+ p->magic = MEM_BLOCK_START_MAGIC;
+ *((long *)(((char *) p) + sizeof(mem_header)+size+PLATFORM_PADDING)) = MEM_BLOCK_END_MAGIC;
#endif
#if MEMORY_LIMIT
CHECK_MEMORY_LIMIT(size);
#endif
***************
*** 162,181 ****
{
mem_header *p = (mem_header *) ((char *)ptr - sizeof(mem_header) - PLATFORM_PADDING);
TLS_VARS;
!
if ((p->size < MAX_CACHED_MEMORY) && (GLOBAL(cache_count[p->size]) <
MAX_CACHED_ENTRIES)) {
GLOBAL(cache[p->size][GLOBAL(cache_count[p->size]++)]) = p;
#if DEBUG
! free(p->filename);
! p->filename = NULL;
#endif
return;
}
BLOCK_INTERRUPTIONS;
REMOVE_POINTER_FROM_LIST(p);
- #if DEBUG
- free(p->filename);
- #endif
#if MEMORY_LIMIT
allocated_memory -= p->size;
--- 148,168 ----
{
mem_header *p = (mem_header *) ((char *)ptr - sizeof(mem_header) - PLATFORM_PADDING);
TLS_VARS;
!
! #if DEBUG
! _mem_block_check(ptr, 1, filename, lineno);
! memset(ptr, 0x5a, p->size);
! #endif
!
if ((p->size < MAX_CACHED_MEMORY) && (GLOBAL(cache_count[p->size]) <
MAX_CACHED_ENTRIES)) {
GLOBAL(cache[p->size][GLOBAL(cache_count[p->size]++)]) = p;
#if DEBUG
! p->magic = MEM_BLOCK_CACHED_MAGIC;
#endif
return;
}
BLOCK_INTERRUPTIONS;
REMOVE_POINTER_FROM_LIST(p);
#if MEMORY_LIMIT
allocated_memory -= p->size;
***************
*** 222,230 ****
mem_header *orig = p;
TLS_VARS;
BLOCK_INTERRUPTIONS;
REMOVE_POINTER_FROM_LIST(p);
! p = (mem_header *) realloc(p,sizeof(mem_header)+size+PLATFORM_PADDING);
if (!p) {
fprintf(stderr,"FATAL: erealloc(): Unable to allocate %d bytes\n", size);
exit(1);
--- 209,224 ----
mem_header *orig = p;
TLS_VARS;
+ if (!ptr) {
+ #if DEBUG
+ return _emalloc(size, filename, lineno);
+ #else
+ return emalloc(size);
+ #endif
+ }
BLOCK_INTERRUPTIONS;
REMOVE_POINTER_FROM_LIST(p);
! p = (mem_header *) realloc(p,sizeof(mem_header)+size+PLATFORM_PADDING+END_MAGIC_SIZE);
if (!p) {
fprintf(stderr,"FATAL: erealloc(): Unable to allocate %d bytes\n", size);
exit(1);
***************
*** 244,252 ****
p->pLast = (mem_header *) NULL;
GLOBAL(head) = p;
#if DEBUG
! free(p->filename);
! p->filename = strdup(filename);
p->lineno = lineno;
#endif
#if MEMORY_LIMIT
CHECK_MEMORY_LIMIT(size - p->size);
--- 238,247 ----
p->pLast = (mem_header *) NULL;
GLOBAL(head) = p;
#if DEBUG
! p->filename = filename;
p->lineno = lineno;
+ p->magic = MEM_BLOCK_START_MAGIC;
+ *((long *)(((char *) p) + sizeof(mem_header)+size+PLATFORM_PADDING)) = MEM_BLOCK_END_MAGIC;
#endif
#if MEMORY_LIMIT
CHECK_MEMORY_LIMIT(size - p->size);
***************
*** 349,359 ****
for (j=0; j<GLOBAL(cache_count[i]); j++) {
p = GLOBAL(cache[i][j]);
REMOVE_POINTER_FROM_LIST(p);
- #if DEBUG
- if (p->filename) {
- free(p->filename);
- }
- #endif
free(p);
}
}
--- 344,349 ----
***************
*** 375,393 ****
log_error(memory_leak_buf,GLOBAL(php3_rqst)->server);
#endif
# else
! php3_printf("Freeing %x (%d bytes), allocated in %s on line %d<br>\n",(void
*)((char
*)t+sizeof(mem_header)+PLATFORM_PADDING),t->size,t->filename?t->filename:"<????>",t->lineno);
# endif
}
#endif
p = t->pNext;
- #if DEBUG
- free(t->filename);
- #endif
free(t);
t = p;
}
GLOBAL(initialized) &= ~INIT_MEMORY_MANAGER;
}
/*
* Local variables:
--- 365,508 ----
log_error(memory_leak_buf,GLOBAL(php3_rqst)->server);
#endif
# else
! php3_printf("Freeing 0x%0.8X (%d bytes), allocated in %s on line
%d<br>\n",(void *)((char
*)t+sizeof(mem_header)+PLATFORM_PADDING),t->size,t->filename,t->lineno);
# endif
}
#endif
p = t->pNext;
free(t);
t = p;
}
GLOBAL(initialized) &= ~INIT_MEMORY_MANAGER;
}
+
+
+ #if DEBUG
+ int _mem_block_check(void *ptr, int silent, char *filename, int lineno)
+ {
+ mem_header *p = (mem_header *) ((char *)ptr - sizeof(mem_header) - PLATFORM_PADDING);
+ int no_cache_notice=0;
+ int valid_beginning=1;
+ int valid_end=1;
+
+ if (silent==2) {
+ silent=1;
+ no_cache_notice=1;
+ }
+ if (silent==3) {
+ silent=0;
+ no_cache_notice=1;
+ }
+ if ((p->magic != MEM_BLOCK_START_MAGIC)
+ && !(p->magic==MEM_BLOCK_CACHED_MAGIC && no_cache_notice)) {
+ if (silent) {
+ return _mem_block_check(ptr, 0, filename, lineno);
+ }
+ fprintf(stderr,"---------------------------------------\n");
+ fprintf(stderr,"Block 0x%0.8X status at %s:%d:\n", p, filename, lineno);
+ fprintf(stderr,"%10s\t","Beginning: ");
+ }
+
+ switch (p->magic) {
+ case MEM_BLOCK_START_MAGIC:
+ if (!silent) {
+ fprintf(stderr, "OK (allocated on %s:%d, %d bytes)\n", p->filename, p->lineno,
p->size);
+ }
+ break; /* ok */
+ case MEM_BLOCK_FREED_MAGIC:
+ if (!silent) {
+ fprintf(stderr,"Freed\n");
+ } else {
+ return _mem_block_check(ptr, 0, filename, lineno);
+ }
+ valid_beginning=0;
+ break;
+ case MEM_BLOCK_CACHED_MAGIC:
+ if (no_cache_notice) {
+ break;
+ }
+ if (!silent) {
+ fprintf(stderr,"Cached (allocated on %s:%d, %d bytes)\n", p->filename,
p->lineno, p->size);
+ } else {
+ return _mem_block_check(ptr, 0, filename, lineno);
+ }
+ valid_beginning=0;
+ break;
+ default:
+ if (!silent) {
+ fprintf(stderr,"Overrun (magic=0x%0.8X, expected=0x%0.8X)\n", p->magic,
MEM_BLOCK_START_MAGIC);
+ } else {
+ return _mem_block_check(ptr, 0, filename, lineno);
+ }
+ valid_beginning=0;
+ break;
+ }
+
+
+ if (valid_beginning
+ && *((long *)(((char *) p)+sizeof(mem_header)+p->size+PLATFORM_PADDING)) !=
MEM_BLOCK_END_MAGIC) {
+ long magic_num = MEM_BLOCK_END_MAGIC;
+ char *overflow_ptr, *magic_ptr=(char *) &magic_num;
+ int overflows=0;
+ int i;
+
+ if (silent) {
+ return _mem_block_check(ptr, 0, filename, lineno);
+ }
+ valid_end = 0;
+ overflow_ptr = ((char *) p)+sizeof(mem_header)+p->size+PLATFORM_PADDING;
+
+ for (i=0; i<sizeof(long); i++) {
+ if (overflow_ptr[i]!=magic_ptr[i]) {
+ overflows++;
+ }
+ }
+
+ fprintf(stderr,"%10s\t", "End:");
+ fprintf(stderr,"Overflown (magic=0x%0.8X instead of 0x%0.8X)\n",
+ *((long *)(((char *) p) + sizeof(mem_header)+p->size+PLATFORM_PADDING)),
MEM_BLOCK_END_MAGIC);
+ fprintf(stderr,"%10s\t","");
+ if (overflows>=sizeof(long)) {
+ fprintf(stderr, "At least %d bytes overflown\n", sizeof(long));
+ } else {
+ fprintf(stderr, "%d byte(s) overflown\n", overflows);
+ }
+ } else if (!silent) {
+ fprintf(stderr,"%10s\t", "End:");
+ if (valid_beginning) {
+ fprintf(stderr,"OK\n");
+ } else {
+ fprintf(stderr,"Unknown\n");
+ }
+ }
+
+ if (!silent) {
+ fprintf(stderr,"---------------------------------------\n");
+ }
+ return ((valid_end&&valid_beginning) ? 1 : 0);
+ }
+
+
+ void _full_mem_check(int silent, char *filename, uint lineno)
+ {
+ mem_header *p = head;
+ int errors=0;
+
+ fprintf(stderr,"------------------------------------------------\n");
+ fprintf(stderr,"Full Memory Check at %s:%d\n", filename, lineno);
+
+ while (p) {
+ if (!_mem_block_check((void *)((char *)p + sizeof(mem_header) + PLATFORM_PADDING), (silent?2:3),
filename, lineno)) {
+ errors++;
+ }
+ p = p->pNext;
+ }
+ fprintf(stderr,"End of full memory check %s:%d (%d errors)\n", filename, lineno,
errors);
+ fprintf(stderr,"------------------------------------------------\n");
+ }
+ #endif
+
+
/*
* Local variables:
Index: php3/alloc.h
diff -c php3/alloc.h:1.31 php3/alloc.h:1.32
*** php3/alloc.h:1.31 Fri Jan 1 12:58:48 1999
--- php3/alloc.h Fri Feb 5 10:06:53 1999
***************
*** 1,48 ****
/*
! +----------------------------------------------------------------------+
! | PHP HTML Embedded Scripting Language Version 3.0 |
! +----------------------------------------------------------------------+
! | Copyright (c) 1997-1999 PHP Development Team (See Credits file) |
! +----------------------------------------------------------------------+
! | This program is free software; you can redistribute it and/or modify |
! | it under the terms of one of the following licenses: |
! | |
! | A) the GNU General Public License as published by the Free Software |
! | Foundation; either version 2 of the License, or (at your option) |
! | any later version. |
! | |
! | B) the PHP License as published by the PHP Development Team and |
! | included in the distribution in the file: LICENSE |
! | |
! | This program is distributed in the hope that it will be useful, |
! | but WITHOUT ANY WARRANTY; without even the implied warranty of |
! | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
! | GNU General Public License for more details. |
! | |
! | You should have received a copy of both licenses referred to here. |
! | If you did not, or have any questions about PHP licensing, please |
! | contact core@php.net. |
! +----------------------------------------------------------------------+
! | Authors: Andi Gutmans <andi@php.net> |
! | Zeev Suraski <bourbon@netvision.net.il> |
! | Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
! +----------------------------------------------------------------------+
*/
- /* $Id: alloc.h,v 1.31 1999/01/01 17:58:48 zeev Exp $ */
-
#ifndef _ALLOC_H
#define _ALLOC_H
#include <stdio.h>
#include "php.h"
typedef struct _mem_header {
- struct _mem_header *pNext;
- struct _mem_header *pLast;
#if DEBUG
char *filename;
uint lineno;
#endif
int size;
} mem_header;
--- 1,29 ----
/*
! * This file is a part of the Zend scripting language engine. It may
! * be freely distributed as an integrated part of the PHP scripting language,
! * or under the Zend license, which is not yet available at this time.
! *
! * Authors: Andi Gutmans <andi@zend.com>, Zeev Suraski <zeev@zend.com>
*/
#ifndef _ALLOC_H
#define _ALLOC_H
#include <stdio.h>
#include "php.h"
+ #define MEM_BLOCK_START_MAGIC 0x7312F8DCL
+ #define MEM_BLOCK_END_MAGIC 0x2A8FCC84L
+ #define MEM_BLOCK_FREED_MAGIC 0x99954317L
+ #define MEM_BLOCK_CACHED_MAGIC 0xFB8277DCL
+
typedef struct _mem_header {
#if DEBUG
+ long magic;
char *filename;
uint lineno;
#endif
+ struct _mem_header *pNext;
+ struct _mem_header *pLast;
int size;
} mem_header;
***************
*** 52,58 ****
long lng;
} align_test;
! #define MAX_CACHED_MEMORY 64
#define MAX_CACHED_ENTRIES 16
#define PLATFORM_ALIGNMENT (sizeof(align_test))
--- 33,39 ----
long lng;
} align_test;
! #define MAX_CACHED_MEMORY 64
#define MAX_CACHED_ENTRIES 16
#define PLATFORM_ALIGNMENT (sizeof(align_test))
***************
*** 99,104 ****
--- 80,97 ----
extern void start_memory_manager(void);
extern void shutdown_memory_manager(void);
+
+ #if DEBUG
+ int _mem_block_check(void *ptr, int silent, char *filename, int lineno);
+ void _full_mem_check(int silent, char *filename, uint lineno);
+ #define mem_block_check(ptr, silent) _mem_block_check(ptr, silent, __FILE__, __LINE__)
+ #define mem_block_check(ptr, silent) _mem_block_check(ptr, silent, __FILE__, __LINE__)
+ #define full_mem_check(silent) _full_mem_check(silent, __FILE__, __LINE__)
+ #else
+ #define mem_block_check(type, ptr, silent)
+ #define full_mem_check(silent)
+ #endif
+
#endif
--
PHP Development Mailing List http://www.php.net/
To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net
For help: php-dev-help@lists.php.net