Skip to content

Commit 3bb3b35

Browse files
committed
Init current_frame in RINIT
1 parent 7daf8b2 commit 3bb3b35

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

memprof.c

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ static int memprof_enabled = 0;
192192
static int memprof_dumped = 0;
193193
static int track_mallocs = 0;
194194

195-
static frame default_frame;
196-
static frame * current_frame = &default_frame;
197-
static alloc_list_head * current_alloc_list = &default_frame.allocs;
195+
static frame root_frame;
196+
static frame * current_frame;
197+
static alloc_list_head * current_alloc_list;
198198
static alloc_buckets current_alloc_buckets;
199199

200200
static Pvoid_t allocs_set = (Pvoid_t) NULL;
@@ -245,9 +245,11 @@ static void alloc_check_single(alloc * alloc, const char * function, int line) {
245245
static void alloc_check(alloc * alloc, const char * function, int line) {
246246
/* fprintf(stderr, "checking %p at %s:%d\n", alloc, function, line); */
247247
alloc_check_single(alloc, function, line);
248+
/*
248249
for (alloc = current_alloc_list->lh_first; alloc; alloc = alloc->list.le_next) {
249250
alloc_check_single(alloc, function, line);
250251
}
252+
*/
251253
}
252254

253255
# define ALLOC_CHECK(alloc) alloc_check(alloc, __FUNCTION__, __LINE__);
@@ -262,7 +264,15 @@ static void alloc_buckets_destroy(alloc_buckets * buckets)
262264
for (i = 0; i < buckets->nbuckets; ++i) {
263265
free(buckets->buckets[i]);
264266
}
267+
268+
#if MEMPROF_DEBUG
269+
memset(buckets->buckets, 0x5a, buckets->nbuckets * sizeof(buckets->buckets[0]));
270+
#endif
265271
free(buckets->buckets);
272+
273+
#if MEMPROF_DEBUG
274+
memset(buckets, 0x5a, sizeof(*buckets));
275+
#endif
266276
}
267277

268278
static void alloc_buckets_grow(alloc_buckets * buckets)
@@ -321,6 +331,9 @@ static void destroy_frame(frame * f)
321331
{
322332
alloc * a;
323333

334+
#if MEMPROF_DEBUG
335+
memset(f->name, 0x5a, f->name_len);
336+
#endif
324337
free(f->name);
325338

326339
while (f->allocs.lh_first) {
@@ -330,6 +343,10 @@ static void destroy_frame(frame * f)
330343
}
331344

332345
zend_hash_destroy(&f->next_cache);
346+
347+
#if MEMPROF_DEBUG
348+
memset(f, 0x5a, sizeof(*f));
349+
#endif
333350
}
334351

335352
/* HashTable destructor */
@@ -393,7 +410,7 @@ static int frame_stack_depth(const frame * f)
393410
const frame * prev;
394411
int depth = 0;
395412

396-
for (prev = f; prev; prev = prev->prev) {
413+
for (prev = f; prev != &root_frame; prev = prev->prev) {
397414
depth ++;
398415
}
399416

@@ -700,7 +717,7 @@ static PHP_INI_MH(OnChangeMemoryLimit)
700717

701718
ret = origOnChangeMemoryLimit(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
702719

703-
if (memprof_enabled) {
720+
if (memprof_enabled && orig_zheap) {
704721
zend_mm_set_heap(orig_zheap);
705722
ret = zend_set_memory_limit(PG(memory_limit));
706723
zend_mm_set_heap(zheap);
@@ -713,8 +730,11 @@ static void memprof_enable()
713730
{
714731
alloc_buckets_init(&current_alloc_buckets);
715732

716-
init_frame(&default_frame, NULL, "root", sizeof("root")-1);
717-
default_frame.calls = 1;
733+
init_frame(&root_frame, &root_frame, "root", sizeof("root")-1);
734+
root_frame.calls = 1;
735+
736+
current_frame = &root_frame;
737+
current_alloc_list = &root_frame.allocs;
718738

719739
MALLOC_HOOK_SAVE_OLD();
720740
MALLOC_HOOK_SET_OWN();
@@ -759,7 +779,7 @@ static void memprof_disable()
759779

760780
memprof_enabled = 0;
761781

762-
destroy_frame(&default_frame);
782+
destroy_frame(&root_frame);
763783

764784
alloc_buckets_destroy(&current_alloc_buckets);
765785

@@ -1188,7 +1208,7 @@ static void dump_frames_pprof(php_stream * stream, HashTable * symbols, frame *
11881208
stream_write_word(stream, size);
11891209
stream_write_word(stream, stack_depth);
11901210

1191-
for (prev = f; prev; prev = prev->prev) {
1211+
for (prev = f; prev != &root_frame; prev = prev->prev) {
11921212
zend_uintptr_t symaddr;
11931213
symaddr = (zend_uintptr_t) zend_hash_str_find_ptr(symbols, prev->name, prev->name_len);
11941214
if (symaddr == 0) {
@@ -1260,7 +1280,7 @@ PHP_FUNCTION(memprof_dump_array)
12601280

12611281
WITHOUT_MALLOC_TRACKING {
12621282

1263-
dump_frame_array(return_value, &default_frame);
1283+
dump_frame_array(return_value, &root_frame);
12641284

12651285
} END_WITHOUT_MALLOC_TRACKING;
12661286

@@ -1296,7 +1316,7 @@ PHP_FUNCTION(memprof_dump_callgrind)
12961316
stream_printf(stream, "events: MemorySize BlocksCount\n");
12971317
stream_printf(stream, "\n");
12981318

1299-
dump_frame_callgrind(stream, &default_frame, "root", &total_size, &total_count);
1319+
dump_frame_callgrind(stream, &root_frame, "root", &total_size, &total_count);
13001320

13011321
stream_printf(stream, "total: %zu %zu\n", total_size, total_count);
13021322

@@ -1333,7 +1353,7 @@ PHP_FUNCTION(memprof_dump_pprof)
13331353

13341354
stream_printf(stream, "--- symbol\n");
13351355
stream_printf(stream, "binary=todo.php\n");
1336-
dump_frames_pprof_symbols(stream, &symbols, &default_frame);
1356+
dump_frames_pprof_symbols(stream, &symbols, &root_frame);
13371357
stream_printf(stream, "---\n");
13381358
stream_printf(stream, "--- profile\n");
13391359

@@ -1350,7 +1370,7 @@ PHP_FUNCTION(memprof_dump_pprof)
13501370
/* unused padding */
13511371
stream_write_word(stream, 0);
13521372

1353-
dump_frames_pprof(stream, &symbols, &default_frame);
1373+
dump_frames_pprof(stream, &symbols, &root_frame);
13541374

13551375
zend_hash_destroy(&symbols);
13561376

0 commit comments

Comments
 (0)