1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
/*
+----------------------------------------------------------------------+
| PHP HTML Embedded Scripting Language Version 3.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997,1998 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> |
+----------------------------------------------------------------------+
*/
#ifdef THREAD_SAFE
#include "tls.h"
#endif
#include "alloc.h"
#include "php.h"
#include "main.h"
#ifndef THREAD_SAFE
static mem_header *head;
void *cache[MAX_CACHED_MEMORY][MAX_CACHED_ENTRIES];
unsigned char cache_count[MAX_CACHED_MEMORY];
# if MEMORY_LIMIT
static unsigned int allocated_memory;
#endif
#endif
# if MEMORY_LIMIT
# if DEBUG
#define CHECK_MEMORY_LIMIT(s) _CHECK_MEMORY_LIMIT(s,filename,lineno)
# else
#define CHECK_MEMORY_LIMIT(s) _CHECK_MEMORY_LIMIT(s,NULL,0)
# endif
#define _CHECK_MEMORY_LIMIT(s,file,lineno) { allocated_memory += (s);\
if (php3_ini.memory_limit<allocated_memory) {\
if (allocated_memory>php3_ini.memory_limit+1048576) {\
if (file) { \
char buf[64]; \
snprintf(buf,64,"Called exit() on %s:%d (tried to allocate %d bytes)\n",file,lineno,s); \
php3_log_err(buf); \
} \
exit(1); \
} else {\
if (!GLOBAL(shutdown_requested)) { \
if (!file) { \
php3_error(E_ERROR,"Allowed memory size of %d bytes exhausted (tried to allocate %d bytes)",php3_ini.memory_limit,s); \
} else { \
php3_error(E_ERROR,"Allowed memory size of %d bytes exhausted at %s:%d (tried to allocate %d bytes)",php3_ini.memory_limit,file,lineno,s); \
} \
} \
} \
}\
}
# endif
#ifndef CHECK_MEMORY_LIMIT
#define CHECK_MEMORY_LIMIT(s)
#endif
#define REMOVE_POINTER_FROM_LIST(p) \
if (p == GLOBAL(head)) { \
GLOBAL(head) = p->pNext; \
} else { \
p->pLast->pNext = p->pNext; \
} \
if (p->pNext) { \
p->pNext->pLast = p->pLast; \
}
/* used by ISAPI and NSAPI */
#if DEBUG
PHPAPI void *_emalloc(size_t size, char *filename, uint lineno)
#else
PHPAPI void *_emalloc(size_t size)
#endif
{
mem_header *p;
TLS_VARS;
#if DEBUG
if (!(initialized & INIT_MEMORY_MANAGER)) {
fprintf(stderr,"WARNING: Call to emalloc() before memory manager is started from %s:%d (%d bytes)\n",filename,lineno,size);
}
#endif
BLOCK_INTERRUPTIONS;
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) {
UNBLOCK_INTERRUPTIONS;
return (void *)p;
}
p->pNext = GLOBAL(head);
if (GLOBAL(head)) {
GLOBAL(head)->pLast = p;
}
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
UNBLOCK_INTERRUPTIONS;
return (void *)((char *)p + sizeof(mem_header) + PLATFORM_PADDING);
}
#if DEBUG
PHPAPI void _efree(void *ptr, char *filename, uint lineno)
#else
PHPAPI void _efree(void *ptr)
#endif
{
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;
#endif
free(p);
UNBLOCK_INTERRUPTIONS;
}
#if DEBUG
PHPAPI void *_ecalloc(size_t nmemb, size_t size, char *filename, uint lineno)
#else
PHPAPI void *_ecalloc(size_t nmemb, size_t size)
#endif
{
void *p;
int final_size=size*nmemb;
TLS_VARS;
BLOCK_INTERRUPTIONS;
#if DEBUG
p = _emalloc(final_size,filename,lineno);
#else
p = emalloc(final_size);
#endif
if (!p) {
UNBLOCK_INTERRUPTIONS;
return (void *) p;
}
memset(p,(int)NULL,final_size);
UNBLOCK_INTERRUPTIONS;
return p;
}
#if DEBUG
PHPAPI void *_erealloc(void *ptr, size_t size, char *filename, uint lineno)
#else
PHPAPI void *_erealloc(void *ptr, size_t size)
#endif
{
mem_header *p = (mem_header *) ((char *)ptr-sizeof(mem_header)-PLATFORM_PADDING);
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) {
orig->pNext = GLOBAL(head);
if (GLOBAL(head)) {
GLOBAL(head)->pLast = orig;
}
orig->pLast = (mem_header *) NULL;
GLOBAL(head) = orig;
UNBLOCK_INTERRUPTIONS;
return (void *)NULL;
}
p->pNext = GLOBAL(head);
if (GLOBAL(head)) {
GLOBAL(head)->pLast = p;
}
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);
#endif
p->size = size;
UNBLOCK_INTERRUPTIONS;
return (void *)((char *)p+sizeof(mem_header)+PLATFORM_PADDING);
}
#if DEBUG
PHPAPI char *_estrdup(const char *s, char *filename, uint lineno)
#else
PHPAPI char *_estrdup(const char *s)
#endif
{
int length;
char *p;
length = strlen(s)+1;
BLOCK_INTERRUPTIONS;
#if DEBUG
p = (char *) _emalloc(length,filename,lineno);
#else
p = (char *) emalloc(length);
#endif
if (!p) {
UNBLOCK_INTERRUPTIONS;
return (char *)NULL;
}
UNBLOCK_INTERRUPTIONS;
memcpy(p,s,length);
return p;
}
#if DEBUG
PHPAPI char *_estrndup(const char *s, uint length, char *filename, uint lineno)
#else
PHPAPI char *_estrndup(const char *s, uint length)
#endif
{
char *p;
BLOCK_INTERRUPTIONS;
#if DEBUG
p = (char *) _emalloc(length+1,filename,lineno);
#else
p = (char *) emalloc(length+1);
#endif
if (!p) {
UNBLOCK_INTERRUPTIONS;
return (char *)NULL;
}
UNBLOCK_INTERRUPTIONS;
memcpy(p,s,length);
p[length]=0;
return p;
}
PHPAPI char *php3_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;
}
void start_memory_manager(void)
{
TLS_VARS;
GLOBAL(head) = NULL;
#if MEMORY_LIMIT
allocated_memory=0;
#endif
memset(GLOBAL(cache_count),0,MAX_CACHED_MEMORY*sizeof(unsigned char));
GLOBAL(initialized) |= INIT_MEMORY_MANAGER;
}
void shutdown_memory_manager(void)
{
mem_header *p, *t;
unsigned int i,j;
TLS_VARS;
BLOCK_INTERRUPTIONS;
for (i=0; i<MAX_CACHED_MEMORY; i++) {
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);
}
}
UNBLOCK_INTERRUPTIONS;
p=GLOBAL(head);
t=GLOBAL(head);
while (t) {
#if DEBUG
/* does not use php3_error() *intentionally* */
if (GLOBAL(error_reporting) & E_WARNING && GLOBAL(shutdown_requested)!=ABNORMAL_SHUTDOWN) {
# if APACHE /* log into the errorlog, since at this time we can't send messages to the browser */
char memory_leak_buf[512];
snprintf(memory_leak_buf,512,"Possible PHP3 memory leak detected (harmless): %d bytes from %s:%d",t->size,t->filename,t->lineno);
#if MODULE_MAGIC_NUMBER >= 19970831
aplog_error(NULL, 0, APLOG_ERR | APLOG_NOERRNO, GLOBAL(php3_rqst)->server, memory_leak_buf);
#else
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->lineno);
# endif
}
#endif
p = t->pNext;
#if DEBUG
free(t->filename);
#endif
free(t);
t = p;
}
GLOBAL(initialized) &= ~INIT_MEMORY_MANAGER;
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
*/
|