summaryrefslogtreecommitdiff
path: root/Zend/zend_gc.c
blob: 55bd207ce154cb39b4a62a2170a879f39ba9bad6 (plain)
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/*
   +----------------------------------------------------------------------+
   | Zend Engine                                                          |
   +----------------------------------------------------------------------+
   | Copyright (c) 1998-2007 Zend Technologies Ltd. (http://www.zend.com) |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.00 of the Zend license,     |
   | that is bundled with this package in the file LICENSE, and is        |
   | available through the world-wide-web at the following url:           |
   | http://www.zend.com/license/2_00.txt.                                |
   | 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.              |
   +----------------------------------------------------------------------+
   | Authors: David Wang <planetbeing@gmail.com>                          |
   |          Dmitry Stogov <dmitry@zend.com>                             |
   +----------------------------------------------------------------------+
*/

/* $Id$ */

#include "zend.h"
#include "zend_API.h"

#define GC_ROOT_BUFFER_MAX_ENTRIES 10000

#ifdef ZTS
ZEND_API int gc_globals_id;
#else
ZEND_API zend_gc_globals gc_globals;
#endif

/* Forward declarations */
static int children_scan_black(zval **pz TSRMLS_DC);
static int children_mark_grey(zval **pz TSRMLS_DC);
static int children_collect_white(zval **pz TSRMLS_DC);
static int children_scan(zval **pz TSRMLS_DC);

static void root_buffer_dtor(zend_gc_globals *gc_globals TSRMLS_DC)
{
	if (gc_globals->buf) {
		free(gc_globals->buf);
		gc_globals->buf = NULL;
	}	
}

static void gc_globals_ctor_ex(zend_gc_globals *gc_globals TSRMLS_DC)
{
	gc_globals->gc_enabled = 0;
	gc_globals->gc_active = 0;

	gc_globals->buf = NULL;

	gc_globals->roots.next = NULL;
	gc_globals->roots.prev = NULL;
	gc_globals->unused = NULL;
	gc_globals->zval_to_free = NULL;
	gc_globals->free_list = NULL;
	gc_globals->next_to_free = NULL;

	gc_globals->gc_runs = 0;
	gc_globals->collected = 0;

#if GC_BENCH
	gc_globals->root_buf_length = 0;
	gc_globals->root_buf_peak = 0;
	gc_globals->zval_possible_root = 0;
	gc_globals->zobj_possible_root = 0;
	gc_globals->zval_buffered = 0;
	gc_globals->zobj_buffered = 0;
	gc_globals->zval_remove_from_buffer = 0;
	gc_globals->zobj_remove_from_buffer = 0;
	gc_globals->zval_marked_grey = 0;
	gc_globals->zobj_marked_grey = 0;
#endif
}

ZEND_API void gc_globals_ctor(TSRMLS_D)
{
#ifdef ZTS
	ts_allocate_id(&gc_globals_id, sizeof(zend_gc_globals), (ts_allocate_ctor) gc_globals_ctor_ex, (ts_allocate_dtor) root_buffer_dtor);
#else
	gc_globals_ctor_ex(&gc_globals);
#endif
}

ZEND_API void gc_globals_dtor(TSRMLS_D)
{
#ifndef ZTS
	root_buffer_dtor(&gc_globals TSRMLS_DC);
#endif
}

ZEND_API void gc_reset(TSRMLS_D)
{
	GC_G(gc_runs) = 0;
	GC_G(collected) = 0;

#if GC_BENCH
	GC_G(root_buf_length) = 0;
	GC_G(root_buf_peak) = 0;
	GC_G(zval_possible_root) = 0;
	GC_G(zobj_possible_root) = 0;
	GC_G(zval_buffered) = 0;
	GC_G(zobj_buffered) = 0;
	GC_G(zval_remove_from_buffer) = 0;
	GC_G(zobj_remove_from_buffer) = 0;
	GC_G(zval_marked_grey) = 0;
	GC_G(zobj_marked_grey) = 0;
#endif

	if (GC_G(buf)) {
		GC_G(roots).next = &GC_G(roots);
		GC_G(roots).prev = &GC_G(roots);

		GC_G(unused) = NULL;
		GC_G(first_unused) = GC_G(buf);

		GC_G(zval_to_free) = NULL;
	} else {
		GC_G(unused) = NULL;
		GC_G(first_unused) = NULL;
		GC_G(last_unused) = NULL;
	}
}

ZEND_API void gc_init(TSRMLS_D)
{
	if (GC_G(buf) == NULL && GC_G(gc_enabled)) {
		GC_G(buf) = (gc_root_buffer*) malloc(sizeof(gc_root_buffer) * GC_ROOT_BUFFER_MAX_ENTRIES);
		GC_G(last_unused) = &GC_G(buf)[GC_ROOT_BUFFER_MAX_ENTRIES];
		gc_reset(TSRMLS_C);
	}
}

ZEND_API void gc_zval_possible_root(zval *zv TSRMLS_DC)
{
	if (UNEXPECTED(GC_G(free_list) != NULL &&
	               GC_ZVAL_ADDRESS(zv) != NULL &&
		           GC_ZVAL_GET_COLOR(zv) == GC_BLACK) &&
		           (GC_ZVAL_ADDRESS(zv) < GC_G(buf) ||
		            GC_ZVAL_ADDRESS(zv) >= GC_G(last_unused))) {
		/* The given zval is a garbage that is going to be deleted by
		 * currently running GC */
		return;
	}

	if (zv->type == IS_OBJECT) {
		GC_ZOBJ_CHECK_POSSIBLE_ROOT(zv);
		return;
	}

	GC_BENCH_INC(zval_possible_root);

	if (GC_ZVAL_GET_COLOR(zv) != GC_PURPLE) {
		GC_ZVAL_SET_PURPLE(zv);

		if (!GC_ZVAL_ADDRESS(zv)) {
			gc_root_buffer *newRoot = GC_G(unused);

			if (newRoot) {
				GC_G(unused) = newRoot->prev;
			} else if (GC_G(first_unused) != GC_G(last_unused)) {
				newRoot = GC_G(first_unused);
				GC_G(first_unused)++;
			} else {
				if (!GC_G(gc_enabled)) {
					GC_ZVAL_SET_BLACK(zv);
					return;
				}
				zv->refcount__gc++;
				gc_collect_cycles(TSRMLS_C);
				zv->refcount__gc--;
				newRoot = GC_G(unused);
				if (!newRoot) {
					return;
				}
				GC_ZVAL_SET_PURPLE(zv);
				GC_G(unused) = newRoot->prev;
			}

			newRoot->next = GC_G(roots).next;
			newRoot->prev = &GC_G(roots);
			GC_G(roots).next->prev = newRoot;
			GC_G(roots).next = newRoot;

			GC_ZVAL_SET_ADDRESS(zv, newRoot);

			newRoot->handle = 0;
			newRoot->u.pz = zv;

			GC_BENCH_INC(zval_buffered);
			GC_BENCH_INC(root_buf_length);
			GC_BENCH_PEAK(root_buf_peak, root_buf_length);
		}
	}
}

ZEND_API void gc_zobj_possible_root(zval *zv TSRMLS_DC)
{
	struct _store_object *obj;

	if (UNEXPECTED(Z_OBJ_HT_P(zv)->get_properties == NULL ||
	    EG(objects_store).object_buckets == NULL)) {
		return;
	}

	GC_BENCH_INC(zobj_possible_root);

	obj = &EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(zv)].bucket.obj;
	if (GC_GET_COLOR(obj->buffered) != GC_PURPLE) {
		GC_SET_PURPLE(obj->buffered);
		if (!GC_ADDRESS(obj->buffered)) {
			gc_root_buffer *newRoot = GC_G(unused);

			if (newRoot) {
				GC_G(unused) = newRoot->prev;
			} else if (GC_G(first_unused) != GC_G(last_unused)) {
				newRoot = GC_G(first_unused);
				GC_G(first_unused)++;
			} else {
				if (!GC_G(gc_enabled)) {
					GC_ZVAL_SET_BLACK(zv);
					return;
				}
				zv->refcount__gc++;
				gc_collect_cycles(TSRMLS_C);
				zv->refcount__gc--;
				newRoot = GC_G(unused);
				if (!newRoot) {
					return;
				}
				obj = &EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(zv)].bucket.obj;
				GC_SET_PURPLE(obj->buffered);
				GC_G(unused) = newRoot->prev;
			}

			newRoot->next = GC_G(roots).next;
			newRoot->prev = &GC_G(roots);
			GC_G(roots).next->prev = newRoot;
			GC_G(roots).next = newRoot;

			GC_SET_ADDRESS(obj->buffered, newRoot);

			newRoot->handle = Z_OBJ_HANDLE_P(zv);
			newRoot->u.handlers = Z_OBJ_HT_P(zv);

			GC_BENCH_INC(zobj_buffered);
			GC_BENCH_INC(root_buf_length);
			GC_BENCH_PEAK(root_buf_peak, root_buf_length);
		}
	}
}

ZEND_API void gc_remove_zval_from_buffer(zval *zv)
{
	gc_root_buffer* root_buffer = GC_ADDRESS(((zval_gc_info*)zv)->u.buffered);
	TSRMLS_FETCH();

	if (UNEXPECTED(GC_G(free_list) != NULL &&
		           GC_ZVAL_GET_COLOR(zv) == GC_BLACK) &&
		           (GC_ZVAL_ADDRESS(zv) < GC_G(buf) ||
		            GC_ZVAL_ADDRESS(zv) >= GC_G(last_unused))) {
		/* The given zval is a garbage that is going to be deleted by
		 * currently running GC */
		if (GC_G(next_to_free) == (zval_gc_info*)zv) {
			GC_G(next_to_free) = ((zval_gc_info*)zv)->u.next;
		}
		return;
	}
	GC_BENCH_INC(zval_remove_from_buffer);
	GC_REMOVE_FROM_BUFFER(root_buffer);
	((zval_gc_info*)zv)->u.buffered = NULL;
}

static void zobj_scan_black(struct _store_object *obj, zval *pz TSRMLS_DC)
{
	GC_SET_BLACK(obj->buffered);

	if (EXPECTED(EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(pz)].valid &&
	             Z_OBJ_HANDLER_P(pz, get_properties) != NULL)) {
		zend_hash_apply(Z_OBJPROP_P(pz), (apply_func_t) children_scan_black TSRMLS_CC);
	}
}

static void zval_scan_black(zval *pz TSRMLS_DC)
{
	GC_ZVAL_SET_BLACK(pz);

	if (Z_TYPE_P(pz) == IS_OBJECT && EG(objects_store).object_buckets) {
		struct _store_object *obj = &EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(pz)].bucket.obj;

		obj->refcount++;
		if (GC_GET_COLOR(obj->buffered) != GC_BLACK) {
			zobj_scan_black(obj, pz TSRMLS_CC);
		}
	} else if (Z_TYPE_P(pz) == IS_ARRAY) {
		if (Z_ARRVAL_P(pz) != &EG(symbol_table)) {
			zend_hash_apply(Z_ARRVAL_P(pz), (apply_func_t) children_scan_black TSRMLS_CC);
		}
	}
}

static int children_scan_black(zval **pz TSRMLS_DC)
{
	if (Z_TYPE_PP(pz) != IS_ARRAY || Z_ARRVAL_PP(pz) != &EG(symbol_table)) {
		(*pz)->refcount__gc++;
	}

	if (GC_ZVAL_GET_COLOR(*pz) != GC_BLACK) {
		zval_scan_black(*pz TSRMLS_CC);
	}

	return 0;
}

static void zobj_mark_grey(struct _store_object *obj, zval *pz TSRMLS_DC)
{
	if (GC_GET_COLOR(obj->buffered) != GC_GREY) {
		GC_BENCH_INC(zobj_marked_grey);
		GC_SET_COLOR(obj->buffered, GC_GREY);
		if (EXPECTED(EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(pz)].valid &&
		             Z_OBJ_HANDLER_P(pz, get_properties) != NULL)) {
			zend_hash_apply(Z_OBJPROP_P(pz), (apply_func_t) children_mark_grey TSRMLS_CC);
		}
	}
}

static void zval_mark_grey(zval *pz TSRMLS_DC)
{
	if (GC_ZVAL_GET_COLOR(pz) != GC_GREY) {
		GC_BENCH_INC(zval_marked_grey);
		GC_ZVAL_SET_COLOR(pz, GC_GREY);

		if (Z_TYPE_P(pz) == IS_OBJECT && EG(objects_store).object_buckets) {
			struct _store_object *obj = &EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(pz)].bucket.obj;

			obj->refcount--;
			zobj_mark_grey(obj, pz TSRMLS_CC);
		} else if (Z_TYPE_P(pz) == IS_ARRAY) {
			if (Z_ARRVAL_P(pz) == &EG(symbol_table)) {
				GC_ZVAL_SET_BLACK(pz);
			} else {
				zend_hash_apply(Z_ARRVAL_P(pz), (apply_func_t) children_mark_grey TSRMLS_CC);
			}
		}
	}
}

static int children_mark_grey(zval **pz TSRMLS_DC)
{
	if (Z_TYPE_PP(pz) != IS_ARRAY || Z_ARRVAL_PP(pz) != &EG(symbol_table)) {
		(*pz)->refcount__gc--;
	}
	zval_mark_grey(*pz TSRMLS_CC);
	return 0;
}

static void gc_mark_roots(TSRMLS_D)
{
	gc_root_buffer *current = GC_G(roots).next;

	while (current != &GC_G(roots)) {
		if (current->handle && EG(objects_store).object_buckets) {
			struct _store_object *obj = &EG(objects_store).object_buckets[current->handle].bucket.obj;

			if (GC_GET_COLOR(obj->buffered) == GC_PURPLE) {
				zval z;

				INIT_PZVAL(&z);
				Z_OBJ_HANDLE(z) = current->handle;
				Z_OBJ_HT(z) = current->u.handlers;
				zobj_mark_grey(obj, &z TSRMLS_CC);
			} else {
				GC_SET_ADDRESS(obj->buffered, NULL);
				GC_REMOVE_FROM_BUFFER(current);
			}
		} else {
			if (GC_ZVAL_GET_COLOR(current->u.pz) == GC_PURPLE) {
				zval_mark_grey(current->u.pz TSRMLS_CC);
			} else {
				GC_ZVAL_SET_ADDRESS(current->u.pz, NULL);
				GC_REMOVE_FROM_BUFFER(current);
			}
		}
		current = current->next;
	}
}

static void zobj_scan(zval *pz TSRMLS_DC)
{
	if (EG(objects_store).object_buckets) {
		struct _store_object *obj = &EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(pz)].bucket.obj;

		if (GC_GET_COLOR(obj->buffered) == GC_GREY) {
			if (obj->refcount > 0) {
				zobj_scan_black(obj, pz TSRMLS_CC);
			} else {
				GC_SET_COLOR(obj->buffered, GC_WHITE);
				if (EXPECTED(EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(pz)].valid &&
				             Z_OBJ_HANDLER_P(pz, get_properties) != NULL)) {
					zend_hash_apply(Z_OBJPROP_P(pz), (apply_func_t) children_scan TSRMLS_CC);
				}
			}
		}
	}
}

static int zval_scan(zval *pz TSRMLS_DC)
{
	if (GC_ZVAL_GET_COLOR(pz) == GC_GREY) {
		if (pz->refcount__gc > 0) {
			zval_scan_black(pz TSRMLS_CC);
		} else {
			GC_ZVAL_SET_COLOR(pz, GC_WHITE);
			if (Z_TYPE_P(pz) == IS_OBJECT) {
				zobj_scan(pz TSRMLS_CC);
			} else if (Z_TYPE_P(pz) == IS_ARRAY) {
				if (Z_ARRVAL_P(pz) == &EG(symbol_table)) {
					GC_ZVAL_SET_BLACK(pz);
				} else {
					zend_hash_apply(Z_ARRVAL_P(pz), (apply_func_t) children_scan TSRMLS_CC);
				}
			}
		}
	}
	return 0;
}

static int children_scan(zval **pz TSRMLS_DC)
{
	zval_scan(*pz TSRMLS_CC);
	return 0;
}

static void gc_scan_roots(TSRMLS_D)
{
	gc_root_buffer *current = GC_G(roots).next;

	while (current != &GC_G(roots)) {
		if (current->handle) {
			zval z;

			INIT_PZVAL(&z);
			Z_OBJ_HANDLE(z) = current->handle;
			Z_OBJ_HT(z) = current->u.handlers;
			zobj_scan(&z TSRMLS_CC);
		} else {
			zval_scan(current->u.pz TSRMLS_CC);
		}
		current = current->next;
	}
}

static void zobj_collect_white(zval *pz TSRMLS_DC)
{
	if (EG(objects_store).object_buckets) {
		zend_object_handle handle = Z_OBJ_HANDLE_P(pz);
		struct _store_object *obj = &EG(objects_store).object_buckets[handle].bucket.obj;

		if (obj->buffered == (gc_root_buffer*)GC_WHITE) {
			GC_SET_BLACK(obj->buffered);

			if (EXPECTED(EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(pz)].valid &&
			             Z_OBJ_HANDLER_P(pz, get_properties) != NULL)) {
				zend_hash_apply(Z_OBJPROP_P(pz), (apply_func_t) children_collect_white TSRMLS_CC);
			}
		}
	}
}

static void zval_collect_white(zval *pz TSRMLS_DC)
{
	if (((zval_gc_info*)(pz))->u.buffered == (gc_root_buffer*)GC_WHITE) {
		GC_ZVAL_SET_BLACK(pz);

		if (Z_TYPE_P(pz) == IS_OBJECT) {
			zobj_collect_white(pz TSRMLS_CC);
		} else {
			if (Z_TYPE_P(pz) == IS_ARRAY) {
				zend_hash_apply(Z_ARRVAL_P(pz), (apply_func_t) children_collect_white TSRMLS_CC);
			}
		}

		/* restore refcount and put into list to free */
		pz->refcount__gc++;
		((zval_gc_info*)pz)->u.next = GC_G(zval_to_free);
		GC_G(zval_to_free) = (zval_gc_info*)pz;
	}
}

static int children_collect_white(zval **pz TSRMLS_DC)
{
	if (Z_TYPE_PP(pz) != IS_ARRAY || Z_ARRVAL_PP(pz) != &EG(symbol_table)) {
		(*pz)->refcount__gc++;
	}
	zval_collect_white(*pz TSRMLS_CC);
	return 0;
}

static void gc_collect_roots(TSRMLS_D)
{
	gc_root_buffer *current = GC_G(roots).next;

	while (current != &GC_G(roots)) {
		if (current->handle && EG(objects_store).object_buckets) {
			struct _store_object *obj = &EG(objects_store).object_buckets[current->handle].bucket.obj;
			zval z;

			GC_SET_ADDRESS(obj->buffered, NULL);
			INIT_PZVAL(&z);
			Z_OBJ_HANDLE(z) = current->handle;
			Z_OBJ_HT(z) = current->u.handlers;
			zobj_collect_white(&z TSRMLS_CC);
		} else {
			GC_ZVAL_SET_ADDRESS(current->u.pz, NULL);
			zval_collect_white(current->u.pz TSRMLS_CC);
		}

		GC_REMOVE_FROM_BUFFER(current);
		current = current->next;
	}
}

#define FREE_LIST_END ((zval_gc_info*)((-1)|~GC_COLOR))

ZEND_API int gc_collect_cycles(TSRMLS_D)
{
	int count = 0;

	if (GC_G(roots).next != &GC_G(roots)) {
		zval_gc_info *p, *q, *orig_free_list, *orig_next_to_free;

		if (GC_G(gc_active)) {
			return 0;
		}
		GC_G(gc_runs)++;
		GC_G(zval_to_free) = FREE_LIST_END;
		GC_G(gc_active) = 1;
		gc_mark_roots(TSRMLS_C);
		gc_scan_roots(TSRMLS_C);
		gc_collect_roots(TSRMLS_C);

		orig_free_list = GC_G(free_list);
		orig_next_to_free = GC_G(next_to_free);
		p = GC_G(free_list) = GC_G(zval_to_free);
		GC_G(zval_to_free) = NULL;
		GC_G(gc_active) = 0;

		/* First call destructors */
		while (p != FREE_LIST_END) {
			if (Z_TYPE(p->z) == IS_OBJECT) {
				if (EG(objects_store).object_buckets &&
					EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].valid &&
					EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].bucket.obj.refcount <= 0 &&
					EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].bucket.obj.dtor &&
					!EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].destructor_called) {

					EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].destructor_called = 1;
					EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].bucket.obj.refcount++;
					EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].bucket.obj.dtor(EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].bucket.obj.object, Z_OBJ_HANDLE(p->z) TSRMLS_CC);
					EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].bucket.obj.refcount--;
				}
			}
			count++;
			p = p->u.next;
		}

		/* Destroy zvals */
		p = GC_G(free_list);
		while (p != FREE_LIST_END) {
			GC_G(next_to_free) = p->u.next;
			if (Z_TYPE(p->z) == IS_OBJECT) {
				if (EG(objects_store).object_buckets &&
					EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].valid &&
					EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].bucket.obj.refcount <= 0) {
					EG(objects_store).object_buckets[Z_OBJ_HANDLE(p->z)].bucket.obj.refcount = 1;
					Z_TYPE(p->z) = IS_NULL;
					zend_objects_store_del_ref_by_handle(Z_OBJ_HANDLE(p->z) TSRMLS_CC);
				}
			} else if (Z_TYPE(p->z) == IS_ARRAY) {
				Z_TYPE(p->z) = IS_NULL;
				zend_hash_destroy(Z_ARRVAL(p->z));
				FREE_HASHTABLE(Z_ARRVAL(p->z));
			} else {
				zval_dtor(&p->z);
				Z_TYPE(p->z) = IS_NULL;
			}
			p = GC_G(next_to_free);
		}

		/* Free zvals */
		p = GC_G(free_list);
		while (p != FREE_LIST_END) {
			q = p->u.next;
			FREE_ZVAL_EX(&p->z);
			p = q;
		}
		GC_G(collected) += count;
		GC_G(free_list) = orig_free_list;
		GC_G(next_to_free) = orig_next_to_free;
	}

	return count;
}

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * indent-tabs-mode: t
 * End:
 */