-
Notifications
You must be signed in to change notification settings - Fork 23
/
hv.c
1883 lines (1683 loc) · 52.5 KB
/
hv.c
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Implementation of the HeapView type */
PyDoc_STRVAR(hv_doc,
"HeapView(root, heapdefs:tuple)\n"
"\n"
"Create a new HeapView object with arguments:\n"
"\n"
" root The initial value of the root member.\n"
" heapdefs Definitions of specially treated extension types.\n"
"\n"
"A HeapView object provides methods to get memory related information\n"
"about the system heap and about individual objects. \n"
"\n"
"It implements much of the low-level functionality for the Heapy\n"
"system. It is intended to provide what can not be done at all or would\n"
"be much slower if programmed directly in Python. It is not intended to\n"
"be used directly by a user, but to be wrapped in higher level objects.\n"
"\n"
"Some terms that are referred to in the method descriptions:\n"
"\n"
"Visible objects.\n"
"\n"
"The HeapView object attempts to restrict its view of the heap to only\n"
"the 'visible objects'. This is to make it possible to analyse the heap\n"
"via a Python library that inevitably itself is continually allocating\n"
"and deallocating objects. These should be hidden from the heap view\n"
"presented. This is primarily done via a special tag attribute, see\n"
"'_hiding_tag_' and 'register__hiding_tag__type'. Frames can be hidden\n"
"with another mechanism, see 'limitframe'. For hiding all objects of a\n"
"special type, 'register_hidden_exact_type' may be used. It is also\n"
"possible to use a separate interpreter and hide its root objects, see\n"
"'is_hiding_calling_interpreter'.\n"
"\n"
"Classifiers.\n"
"\n"
"The methods named cli_* are factory methods that create objects of\n"
"type ObjectClassifier. The principal difference between classifiers is\n"
"how a single object is classified. The single-object classification\n"
"function is available in classifier objects; it is the classify\n"
"method. There are also methods that operate on collections of objects,\n"
"namely partition and select. These eliminate the per-object\n"
"Python-level function call overhead that would occur if the classify\n"
"method were to be called from Python for each object in a collection.\n"
"See also the ObjectClassifier type.\n"
"\n"
"Individual size.\n"
"\n"
"The individual size of an object is its individually allocated memory size. \n"
"\n"
"It includes:\n"
"\n"
"o The basic object size, as can be found out in a standard way.\n"
"o The extra memory for variable size objects.\n"
"o For GC collected objects, the size of the GC information.\n"
"o An alignment to the next highest multiple of a pointer size.\n"
"o The size of any other memory allocated that belongs to the object.\n"
"\n"
"Some types of objects have extra memory allocated that can not be\n"
"accounted for in the standard way. This memory should nevertheless be\n"
"included in the individual size. To determine the size of these\n"
"objects, special functions are needed. These are defined for standard\n"
"builtin types, such as lists and dicts. Other types should be defined\n"
"via the heapdefs argument to the HeapView constructor.\n"
"\n"
"The individual size does not include:\n"
"\n"
"o Subobjects that are accounted for separately.\n"
"o Overhead for the memory allocation system. This varies depending\n"
" on the kind of memory allocator, the requested size, etc.\n"
);
#define Py_BUILD_CORE
/* PyGC_Head */
# undef _PyGC_FINALIZED
# include <internal/pycore_gc.h>
#undef Py_BUILD_CORE
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 13
# define Py_BUILD_CORE
/* _PySys_GetSizeOf */
# include <internal/pycore_sysmodule.h>
/* _PyObject_GetManagedDict */
# include <internal/pycore_object.h>
# undef Py_BUILD_CORE
#endif
#define ALIGN sizeof(void *)
#define ALIGN_MASK (ALIGN - 1)
#define XT_SIZE 1024
#define XT_MASK (XT_SIZE-1)
/* Forward declarations */
static PyObject *hv_heap(NyHeapViewObject *self, PyObject *args, PyObject *kwds);
static ExtraType *hv_new_extra_type(NyHeapViewObject *hv, PyTypeObject *type);
int cli_cmp_as_int(PyObject *cmp);
/* Helpers */
static PyObject *
hv_PyList_Pop(PyObject *list)
{
Py_ssize_t size = PyList_Size(list);
if (size > 0) {
PyObject *r = PyList_GetItem(list, size - 1);
Py_XINCREF(r);
if (r)
if (PyList_SetSlice(list, size - 1, size, 0) < 0)
return 0;
return r;
} else {
if (size == 0)
PyErr_Format(PyExc_IndexError, "pop from empty list");
return 0;
}
}
/* HeapView methods */
static int
hv_gc_traverse(NyHeapViewObject *hv, visitproc visit, void *arg)
{
Py_VISIT(hv->root);
Py_VISIT(hv->limitframe);
Py_VISIT(hv->static_types);
Py_VISIT(hv->weak_type_callback);
if (hv->xt_table) {
size_t i;
for (i = 0; i < hv->xt_size; i++) {
ExtraType *xt;
for (xt = hv->xt_table[i]; xt; xt = xt->xt_next) {
Py_VISIT(xt->xt_weak_type);
}
}
}
return 0;
}
void
xt_free_table(ExtraType **xt_table, size_t size)
{
size_t i;
if (!xt_table)
return;
for (i = 0; i < size; i++) {
ExtraType *xt = xt_table[i];
while (xt) {
ExtraType *xt_next = xt->xt_next;
Py_DECREF(xt->xt_weak_type);
PyMem_Del(xt);
xt = xt_next;
}
}
PyMem_Del(xt_table);
}
static int
hv_gc_clear(NyHeapViewObject *hv)
{
/* xxx Paranoid, clumsy, but recursion-safe variant? */
PyObject *ro = hv->root;
PyObject *lf = hv->limitframe;
PyObject *he = hv->_hiding_tag_;
PyObject *stob = hv->static_types;
PyObject *wtc = hv->weak_type_callback;
void *xt = hv->xt_table;
hv->root = 0;
hv->limitframe = 0;
hv->_hiding_tag_ = 0;
hv->static_types = 0;
hv->weak_type_callback = 0;
hv->xt_table = 0;
xt_free_table(xt, hv->xt_size);
Py_XDECREF(ro);
Py_XDECREF(lf);
Py_XDECREF(he);
Py_XDECREF(stob);
Py_XDECREF(wtc);
return 0;
}
static size_t
hv_default_size(PyObject *obj)
{
if (PyErr_Occurred())
return -1;
size_t z = _PySys_GetSizeOf(obj);
if (!PyErr_Occurred() || !PyErr_ExceptionMatches(PyExc_TypeError))
return z;
PyErr_Clear();
z = Py_TYPE(obj)->tp_basicsize;
if (Py_TYPE(obj)->tp_itemsize) {
Py_ssize_t itemsize = Py_TYPE(obj)->tp_itemsize;
if (itemsize < 0)
itemsize = -itemsize; /* For (e.g.) long(Should we check? */
z += Py_SIZE(obj) * itemsize;
z = (z + ALIGN_MASK) & ~ALIGN_MASK;
}
if (PyObject_IS_GC(obj))
z += sizeof(PyGC_Head);
return z;
}
static int
owht_relate(NyHeapRelate *r, PyTypeObject *type)
{
PyObject *v = r->src;
PyMemberDef *mp = type->tp_members;
if (mp) {
while (mp->name) {
if ((mp->type == T_OBJECT_EX || mp->type == T_OBJECT) &&
*((PyObject **)((char *)v+mp->offset)) == r->tgt) {
if (r->visit(NYHR_ATTRIBUTE, PyUnicode_FromString(mp->name), r))
return 1;
}
mp++;
}
}
return 0;
}
static NyHeapDef default_hd = {
0, /* flags */
0, /* type */
hv_default_size, /* size */
0, /* traverse */
0, /* relate */
};
static size_t
xt_error_size(PyObject *obj)
{
return -1;
}
static int
xt_default_relate(struct ExtraType *xt, NyHeapRelate *r)
{
PyTypeObject *type = xt->xt_type;
PyObject **dictptr;
if (owht_relate(r, type))
return 1;
dictptr = _PyObject_GetDictPtr(r->src);
if (dictptr) {
if (*dictptr == r->tgt) {
if (r->visit(NYHR_ATTRIBUTE, PyUnicode_FromString("__dict__"), r))
return 1;
}
if (dict_relate_kv(r, *dictptr, NYHR_HASATTR, NYHR_ATTRIBUTE)) {
return 1;
}
}
return 0;
}
static int
xt_hd_relate(struct ExtraType *xt, NyHeapRelate *r)
{
return xt->xt_hd->relate(r);
}
static int
xt_inherited_relate(struct ExtraType *xt, NyHeapRelate *r)
{
if (owht_relate(r, xt->xt_type))
return 1;
return xt->xt_base->xt_relate(xt->xt_base, r);
}
static int
xt_error_relate(struct ExtraType *xt, NyHeapRelate *r)
{
return -1;
}
static int
xt_error_traverse(struct ExtraType *xt, PyObject *obj, visitproc visit, void *arg)
{
return -1;
}
static int
xt_no_traverse(struct ExtraType *xt, PyObject *obj, visitproc visit, void *arg)
{
return 0;
}
static int
xt_tp_traverse(struct ExtraType *xt, PyObject *obj, visitproc visit, void *arg)
{
return Py_TYPE(obj)->tp_traverse(obj, visit, arg);
}
static int
xt_hd_traverse(struct ExtraType *xt, PyObject *obj, visitproc visit, void *arg)
{
PyErr_CheckSignals();
if (PyErr_Occurred())
return -1;
NyHeapTraverse ta;
NyHeapViewObject *hv = (void *)xt->xt_hv;
ta.flags = 0;
ta.obj = obj;
ta.visit = visit;
ta.arg = arg;
ta._hiding_tag_ = hv->_hiding_tag_;
ta.hv = (PyObject *)hv;
return xt->xt_hd->traverse(&ta);
}
static int
xt_he_traverse(struct ExtraType *xt, PyObject *obj, visitproc visit, void *arg)
{
Py_ssize_t offs = xt->xt_he_offs;
NyHeapViewObject *hv = (void *)xt->xt_hv;
PyObject **phe = (PyObject **)((char *)obj + offs);
if (*phe == hv->_hiding_tag_) {
return 0;
}
return xt->xt_he_traverse(xt, obj, visit, arg);
}
static ExtraType xt_error = {
0, /* xt_type */
xt_error_size, /* xt_size */
xt_error_traverse, /* xt_traverse */
xt_error_relate, /* xt_relate */
};
#define XT_ERROR 0
#define XT_HE 1
#define XT_TP 2
#define XT_NO 3
#define XT_HD 4
#define XT_HI 5
#define XT_HASH(hv, type) (((Py_uintptr_t)type >> 4) & XT_MASK)
void
xt_findout_size(ExtraType *xt)
{
if (xt->xt_hd->size)
xt->xt_size = xt->xt_hd->size;
else
xt->xt_size = hv_default_size;
}
void
xt_findout_traverse(ExtraType *xt)
{
if (xt->xt_hd->traverse) {
xt->xt_traverse = xt_hd_traverse;
xt->xt_trav_code = XT_HD;
return;
} else if (xt->xt_type->tp_traverse) {
xt->xt_traverse = xt_tp_traverse;
xt->xt_trav_code = XT_TP;
return;
} else {
xt->xt_traverse = xt_no_traverse;
xt->xt_trav_code = XT_NO;
return;
}
}
void
xt_findout_relate(ExtraType *xt)
{
if (xt->xt_hd->relate)
xt->xt_relate = xt_hd_relate;
else
xt->xt_relate = xt_default_relate;
}
static ExtraType *
hv_new_xt_for_type_at_xtp(NyHeapViewObject *hv, PyTypeObject *type, ExtraType **xtp)
{
ExtraType *xt = PyMem_New(ExtraType, 1);
if (!xt) {
PyErr_NoMemory();
return 0;
}
memset(xt, 0, sizeof(ExtraType));
*xtp = xt;
xt->xt_hv = (void *)hv;
xt->xt_type = type;
xt->xt_weak_type = PyWeakref_NewRef((PyObject *)type, hv->weak_type_callback);
if (!xt->xt_weak_type) {
PyMem_Del(xt);
return 0;
}
return xt;
}
static ExtraType *
hv_new_xt_for_type(NyHeapViewObject *hv, PyTypeObject *type)
{
int hash = XT_HASH(hv, type);
ExtraType **xtp = &hv->xt_table[hash];
ExtraType *xt;
while ((xt = *xtp)) {
if (xt->xt_type == type) {
PyErr_Format(PyExc_ValueError,
"Duplicate heap definition for type '%.50s'",
type->tp_name);
return 0;
}
xtp = &xt->xt_next;
}
return hv_new_xt_for_type_at_xtp(hv, type, xtp);
}
static void
xt_set_heapdef(ExtraType *xt, NyHeapDef *hd)
{
xt->xt_hd = hd;
xt_findout_traverse(xt);
xt_findout_size(xt);
xt_findout_relate(xt);
}
static ExtraType *
hv_extra_type(NyHeapViewObject *hv, PyTypeObject *type)
{
int hash = XT_HASH(hv, type);
ExtraType **xtp = &hv->xt_table[hash];
ExtraType *xt;
#ifdef COUNT_COLL
int i = 0;
#endif
while ((xt = *xtp)) {
if (xt->xt_type == type) {
#ifdef COUNT_COLL
if (i > maxcoll) {
maxcoll = i;
fprintf(stderr, "maxcoll %d\n", maxcoll);
}
#endif
return xt;
}
xtp = &xt->xt_next;
#ifdef COUNT_COLL
i += 1;
#endif
}
xt = hv_new_extra_type(hv, type);
if (!xt)
xt = &xt_error;
return xt;
}
static ExtraType *
hv_new_extra_type(NyHeapViewObject *hv, PyTypeObject *type)
{
ExtraType *xt;
if (!type->tp_base) {
xt = hv_new_xt_for_type(hv, type);
if (!xt)
return 0;
xt_set_heapdef(xt, &default_hd);
} else {
ExtraType *base = hv_extra_type(hv, type->tp_base);
if (base == &xt_error)
return 0;
xt = hv_new_xt_for_type(hv, type);
if (!xt)
return 0;
xt->xt_base = base;
xt->xt_hd = base->xt_hd;
if (base->xt_trav_code == XT_HE) {
xt->xt_he_xt = base->xt_he_xt;
xt->xt_trav_code = base->xt_trav_code;
xt->xt_traverse = base->xt_traverse;
xt->xt_he_traverse = base->xt_he_traverse;
xt->xt_he_offs = base->xt_he_offs;
} else {
xt_findout_traverse(xt); /* xxx ??? */
}
xt->xt_size = base->xt_size;
xt->xt_relate = xt_inherited_relate;
}
return xt;
}
#ifdef COUNT_COLL
int maxcoll = 0;
#endif
static int
xt_relate(ExtraType *xt, NyHeapRelate *hr)
{
PyTypeObject *type = Py_TYPE(hr->src);
if (PyType_Ready(type) == -1)
return -1;
if ((PyObject *)type == hr->tgt) {
if (hr->visit(NYHR_INTERATTR, PyUnicode_FromString("ob_type"), hr))
return 0;
}
return xt->xt_relate(xt, hr);
}
static size_t
xt_size(ExtraType *xt, PyObject *obj)
{
return xt->xt_size(obj);
}
static int
xt_traverse(ExtraType *xt, PyObject *obj, visitproc visit, void *arg)
{
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 11
if (Py_TYPE(obj)->tp_flags & Py_TPFLAGS_MANAGED_DICT) {
// FIXME: There's no way to distinguish between managed dict entries
// and other references. To keep our results stable we have to
// materialize this managed dict, which allocates a lot of memory
// and will add additional overhead.
_PyObject_GetDictPtr(obj);
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 13
if (Py_TYPE(obj)->tp_flags & Py_TPFLAGS_INLINE_VALUES) {
// Additionally, in 3.13, PyObject_VisitManagedDict skips traversing
// the managed dict, and visits the values directly. Force visiting
// the dict here.
PyDictObject *dict = _PyObject_GetManagedDict(obj);
Py_VISIT(dict);
// However, one must be careful not to call tp_traverse on a
// hidden object, because Py_TPFLAGS_INLINE_VALUES will traverse
// into the values of the dict, ignoring the _hiding_tag_ handling
// in stdtypes.c
if (PyDict_GetItem((PyObject *)dict, _hiding_tag__name) ==
xt->xt_hv->_hiding_tag_)
return 0;
}
#endif
}
#endif
if (xt->xt_trav_code == XT_NO)
return 0;
else if (xt->xt_trav_code == XT_TP)
return Py_TYPE(obj)->tp_traverse(obj, visit, arg);
else
return xt->xt_traverse(xt, obj, visit, arg);
}
NyNodeSetObject *
hv_mutnodeset_new(NyHeapViewObject *hv)
{
return NyMutNodeSet_NewHiding(hv->_hiding_tag_);
}
static size_t
hv_std_size(NyHeapViewObject *hv, PyObject *obj)
{
return xt_size(hv_extra_type(hv, Py_TYPE(obj)), obj);
}
static int
hv_std_relate(NyHeapRelate *hr)
{
return xt_relate(hv_extra_type((NyHeapViewObject *)hr->hv, Py_TYPE(hr->src)), hr);
}
static int
hv_std_traverse(NyHeapViewObject *hv,
PyObject *obj, visitproc visit, void *arg)
{
return xt_traverse(hv_extra_type(hv, Py_TYPE(obj)), obj, visit, arg);
}
typedef struct {
NyHeapViewObject *hv;
NyNodeSetObject *ns;
PyObject *rm;
} CMSTravArg;
int
hv_is_obj_hidden(NyHeapViewObject *hv, PyObject *obj)
{
PyTypeObject *type = Py_TYPE(obj);
ExtraType *xt = hv_extra_type(hv, type);
if (xt->xt_trav_code == XT_HE) {
Py_ssize_t offs = xt->xt_he_offs;
PyObject **phe = (PyObject **)((char *)obj + offs);
if (*phe == hv->_hiding_tag_) {
return 1;
}
} else if (xt->xt_trav_code == XT_HI) {
return 1;
} else if (type == &NyRootState_Type) {
/* Fixes a dominos confusion; see Notes Apr 20 2005 */
return 1;
} else {
PyObject **dp = _PyObject_GetDictPtr(obj);
if (dp && *dp && PyDict_GetItem(*dp, _hiding_tag__name) == hv->_hiding_tag_) {
return 1;
}
}
return 0;
}
static int
hv_cms_rec(PyObject *obj, CMSTravArg *ta)
{
if (hv_is_obj_hidden(ta->hv, obj)) {
if (PyList_Append(ta->rm, obj) == -1)
return -1;
}
return 0;
}
static int
hv_cleanup_mutset(NyHeapViewObject *hv, NyNodeSetObject *ns)
{
CMSTravArg ta;
int ret = -1;
Py_ssize_t i, size;
ta.hv = hv;
ta.ns = ns;
ta.rm = PyList_New(0);
if (!ta.rm)
goto err;
if (NyNodeSet_iterate(ta.ns, (visitproc)hv_cms_rec, &ta) == -1)
goto err;
size = PyList_Size(ta.rm);
for (i = 0; i < size; i++) {
PyObject *obj = PyList_GET_ITEM(ta.rm, i);
if (NyNodeSet_clrobj(ta.ns, obj) == -1)
goto err;
}
ret = 0;
err:
Py_XDECREF(ta.rm);
return ret;
}
static int
hv_add_heapdef(NyHeapViewObject *hv, NyHeapDef *hd)
{
ExtraType *xt = hv_new_xt_for_type(hv, hd->type);
if (!xt)
return -1;
xt_set_heapdef(xt, hd);
return 0;
}
static int
hv_add_heapdefs_array(NyHeapViewObject *hv, NyHeapDef *hd)
{
while (hd->type) {
if (hv_add_heapdef(hv, hd) == -1)
return -1;
hd++;
}
return 0;
}
static int
hv_add_heapdefs_tuple(NyHeapViewObject *hv, PyTupleObject *heapdefs)
{
Py_ssize_t i;
for (i = 0; i < PyTuple_Size((PyObject *)heapdefs); i++) {
PyObject *obj = PyTuple_GetItem((PyObject *)heapdefs, i);
if (!PyCapsule_CheckExact(obj)) {
PyErr_SetString(PyExc_TypeError, "heapdefs must be a capsule object");
return -1;
}
const char *name = PyCapsule_GetName(obj);
const char *dot = strrchr(name, '.');
if (!dot || strcmp(dot, "._NyHeapDefs_")) {
PyErr_SetString(PyExc_TypeError, "heapdefs must be named <package name>._NyHeapDefs_");
return -1;
}
NyHeapDef *hd = PyCapsule_GetPointer(obj, name);
if (!hd)
return -1;
if (hv_add_heapdefs_array(hv, hd) == -1)
return -1;
}
return 0;
}
PyObject *
NyHeapView_SubTypeNew(PyTypeObject *type, PyObject *root, PyTupleObject *heapdefs)
{
NyHeapViewObject *hv = (NyHeapViewObject *)type->tp_alloc(type, 1);
size_t i;
if (!hv)
return 0;
Py_INCREF(root);
hv->root = root;
hv->limitframe = 0;
hv->_hiding_tag_ = Py_None;
Py_INCREF(Py_None);
hv->static_types = 0;
hv->xt_size = XT_SIZE;
hv->xt_mask = XT_MASK;
hv->weak_type_callback = 0;
hv->xt_table = 0;
/* The HeapView object hv is now initialized to some well-defined state --
but we have waited to try allocation till now when all
allocated members have been set (to 0 etc) so
that hv now may be correctly deallocated. */
hv->weak_type_callback = PyObject_GetAttrString((PyObject *)hv, "delete_extra_type");
if (!(hv->weak_type_callback))
goto err;
hv->xt_table = PyMem_New(ExtraType *, hv->xt_size);
if (!hv->xt_table)
goto err;
for (i = 0; i < hv->xt_size; i++)
hv->xt_table[i] = 0;
hv->static_types = (PyObject *)NyMutNodeSet_New();
if (!(hv->static_types))
goto err;
/* Add standard and user-defined heap definitions */
if (hv_add_heapdefs_array(hv, NyStdTypes_HeapDef) == -1)
goto err;
if (hv_add_heapdefs_array(hv, NyHvTypes_HeapDef) == -1)
goto err;
if (hv_add_heapdefs_tuple(hv, heapdefs) == -1)
goto err;
return (PyObject *)hv;
err:
Py_DECREF(hv);
return 0;
}
static PyObject *
hv_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *heapdefs = NULL;
PyObject *root = NULL;
static char *kwlist[] = {"root", "heapdefs", 0};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO!:hv_new",kwlist,
&root,
&PyTuple_Type, &heapdefs))
return NULL;
return NyHeapView_SubTypeNew(type, root, (PyTupleObject *)heapdefs);
}
static void
hv_dealloc(PyObject *v)
{
PyObject_GC_UnTrack(v);
Py_TRASHCAN_BEGIN(v, hv_dealloc)
hv_gc_clear((NyHeapViewObject *)v);
Py_TYPE(v)->tp_free(v);
Py_TRASHCAN_END
}
PyDoc_STRVAR(hv_delete_extra_type_doc,
"HV.delete_extra_type(weakref)\n\
\n\
Delete extra type information. For internal use as a weak-ref callback.");
/* hv_delete_extra_type will be called by the weak type callback on its type.
I don't consider it time critical, because it wouldnt happen too often..
so make it simple, allow to take time in the order of the total number of
(extra) types.
*/
static PyObject *
hv_delete_extra_type(NyHeapViewObject *hv, PyObject *wr)
{
size_t i;
if (!PyWeakref_Check(wr)) {
PyErr_Format(PyExc_TypeError,
"delete_extra_type: argument must be a weak ref, got '%.50s'",
Py_TYPE(wr)->tp_name);
return 0;
}
for (i = 0; i < hv->xt_size; i++) {
ExtraType *xt, **xtp;
for (xtp = &hv->xt_table[i]; (xt = *xtp); xtp = &xt->xt_next) {
if (xt->xt_weak_type == wr) {
*xtp = xt->xt_next;
PyMem_Del(xt);
Py_DECREF(wr);
Py_INCREF(Py_None);
return Py_None;
}
}
}
PyErr_Format(PyExc_ValueError,
"delete_extra_type: reference object %p not found",
wr);
return 0;
}
#include "hv_cli.c"
typedef struct {
NyHeapViewObject *hv;
NyNodeSetObject *hs;
PyObject *arg;
int (*visit)(PyObject *, void *);
PyObject *to_visit;
} IterTravArg;
static int
iter_rec(PyObject *obj, IterTravArg *ta) {
int r;
if (Py_REFCNT(obj) > 1) {
r = NyNodeSet_setobj(ta->hs, obj);
if (r)
return r < 0 ? r : 0;
}
r = ta->visit(obj, ta->arg);
if (!r) {
r = PyList_Append(ta->to_visit, obj);
}
return r;
}
int
NyHeapView_iterate(NyHeapViewObject *hv, int (*visit)(PyObject *, void *),
void *arg)
{
IterTravArg ta;
int r;
ta.hv = hv;
ta.visit = visit;
ta.arg = arg;
ta.hs = hv_mutnodeset_new(hv);
ta.to_visit = PyList_New(0);
if (!(ta.hs && ta.to_visit))
goto err;
r = iter_rec(ta.hv->root, &ta);
while (PyList_Size(ta.to_visit)) {
PyObject *obj = hv_PyList_Pop(ta.to_visit);
if (!obj)
goto err;
if (hv_std_traverse(ta.hv, obj, (visitproc)iter_rec, &ta) == -1) {
Py_DECREF(obj);
goto err;
}
Py_DECREF(obj);
}
goto out;
err:
r = -1;
out:
Py_XDECREF(ta.to_visit);
Py_XDECREF(ta.hs);
return r;
}
PyDoc_STRVAR(hv_heap_doc,
"HV.heap() -> NodeSet\n\
\n\
Return a set containing all 'visible objects' in the heap view\n\
defined by HV. See also HeapView.__doc__.");
typedef struct {
NyHeapViewObject *hv;
NyNodeSetObject *visited;
PyObject *to_visit;
} HeapTravArg;
static int
hv_heap_rec(PyObject *obj, HeapTravArg *ta) {
int r;
if (hv_is_obj_hidden(ta->hv, obj) && Py_TYPE(obj) != &NyRootState_Type)
return 0;
r = NyNodeSet_setobj(ta->visited, obj);
if (r)
return r < 0 ? r : 0;
else
return PyList_Append(ta->to_visit, obj);
}
static int
hv_update_static_types_visitor(PyObject *obj, NyHeapViewObject *hv) {
if (PyType_Check(obj) &&
!(((PyTypeObject *)obj)->tp_flags & Py_TPFLAGS_HEAPTYPE))
return NyNodeSet_setobj((NyNodeSetObject *)(hv->static_types), obj);
return 0;
}
static int
hv_update_static_types(NyHeapViewObject *hv, PyObject *it)
{
return iterable_iterate(it, (visitproc)hv_update_static_types_visitor, hv);
}
static PyObject *
hv_heap(NyHeapViewObject *self, PyObject *args, PyObject *kwds)
{
HeapTravArg ta;
ta.hv = self;
ta.visited = hv_mutnodeset_new(self);
ta.to_visit = PyList_New(0);
if (!(ta.visited && ta.to_visit))
goto err;
if (hv_heap_rec(ta.hv->root, &ta) == -1)
goto err;
while (PyList_Size(ta.to_visit)) {
PyObject *obj = hv_PyList_Pop(ta.to_visit);
if (!obj)
goto err;
if (hv_std_traverse(ta.hv, obj, (visitproc)hv_heap_rec, &ta) == -1) {
Py_DECREF(obj);
goto err;
}
Py_DECREF(obj);
}
if (hv_cleanup_mutset(ta.hv, ta.visited) == -1)
goto err;
if (PyObject_Length(self->static_types) == 0) {
if (hv_update_static_types(self, (PyObject *)ta.visited) == -1)
goto err;
}
Py_XDECREF(ta.to_visit);
return (PyObject *)ta.visited;
err:
Py_XDECREF(ta.visited);
Py_XDECREF(ta.to_visit);
return 0;
}
typedef struct {
NyHeapViewObject *hv;
Py_ssize_t sum;
} SalArg;
static int
hv_indisize_sum_rec(PyObject *obj, SalArg *ta)
{
ta->sum += hv_std_size(ta->hv, obj);
return 0;
}
PyDoc_STRVAR(hv_indisize_sum_doc,
"HV.indisize_sum(S:iterable) -> int\n\
\n\
Return the sum of the 'individual size' of the objects in S.\n\
See also HeapView.__doc.");
static PyObject *
hv_indisize_sum(NyHeapViewObject *self, PyObject *arg)
{
SalArg ta;
ta.sum = 0;
ta.hv = self;
if (iterable_iterate(arg, (visitproc)hv_indisize_sum_rec, &ta) == -1)
return 0;
return PyLong_FromSsize_t(ta.sum);
}
typedef struct {
NyHeapRelate hr;
int err;
PyObject *relas[NYHR_LIMIT];
} hv_relate_visit_arg;
static int
hv_relate_visit(unsigned int relatype, PyObject *relator, NyHeapRelate *arg_)
{
hv_relate_visit_arg *arg = (void *)arg_;
arg->err = -1;
if (!relator) {
if (PyErr_Occurred())