FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpython/Objects/obmalloc.c at main · python/cpython · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
python
/
cpython
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
35.3k
Star
74.9k
Code
Issues
5k+
Pull requests
2.6k
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpython
/
Objects
/
obmalloc.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
3874 lines (3406 loc) · 117 KB
Breadcrumbs
cpython
/
Objects
/
obmalloc.c
Copy path
File metadata and controls
3874 lines (3406 loc) · 117 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/* Python's malloc wrappers (see pymem.h) */
#include
"Python.h"
#include
"pycore_interp.h"
// _PyInterpreterState_HasFeature
#include
"pycore_mmap.h"
// _PyAnnotateMemoryMap()
#include
"pycore_object.h"
// _PyDebugAllocatorStats() definition
#include
"pycore_obmalloc.h"
#include
"pycore_obmalloc_init.h"
#include
"pycore_pyerrors.h"
// _Py_FatalErrorFormat()
#include
"pycore_pymem.h"
#include
"pycore_pystate.h"
// _PyInterpreterState_GET
#include
"pycore_stats.h"
// OBJECT_STAT_INC_COND()
#include
<stdlib.h>
// malloc()
#include
<stdbool.h>
#include
<stdio.h>
// fopen(), fgets(), sscanf()
#include
<errno.h>
// errno
#ifdef
WITH_MIMALLOC
// Forward declarations of functions used in our mimalloc modifications
static
void
_PyMem_mi_page_clear_qsbr
(
mi_page_t
*
page
);
static
bool
_PyMem_mi_page_is_safe_to_free
(
mi_page_t
*
page
);
static
bool
_PyMem_mi_page_maybe_free
(
mi_page_t
*
page
,
mi_page_queue_t
*
pq
,
bool
force
);
static
void
_PyMem_mi_page_reclaimed
(
mi_page_t
*
page
);
static
void
_PyMem_mi_heap_collect_qsbr
(
mi_heap_t
*
heap
);
# include
"pycore_mimalloc.h"
# include
"mimalloc/static.c"
# include
"mimalloc/internal.h"
// for stats
#endif
#if
defined(
Py_GIL_DISABLED
)
&&
!defined(
WITH_MIMALLOC
)
# error
"Py_GIL_DISABLED requires WITH_MIMALLOC"
#endif
#undef
uint
#define
uint
pymem_uint
/* Defined in tracemalloc.c */
extern
void
_PyMem_DumpTraceback
(
int
fd
,
const
void
*
ptr
);
static
void
_PyObject_DebugDumpAddress
(
const
void
*
p
);
static
void
_PyMem_DebugCheckAddress
(
const
char
*
func
,
char
api_id
,
const
void
*
p
);
static
void
set_up_debug_hooks_domain_unlocked
(
PyMemAllocatorDomain
domain
);
static
void
set_up_debug_hooks_unlocked
(
void
);
static
void
get_allocator_unlocked
(
PyMemAllocatorDomain
,
PyMemAllocatorEx
*
);
static
void
set_allocator_unlocked
(
PyMemAllocatorDomain
,
PyMemAllocatorEx
*
);
/***************************************/
/* low-level allocator implementations */
/***************************************/
/* the default raw allocator (wraps malloc) */
void
*
_PyMem_RawMalloc
(
void
*
Py_UNUSED
(
ctx
),
size_t
size
)
{
/* PyMem_RawMalloc(0) means malloc(1). Some systems would return NULL
for malloc(0), which would be treated as an error. Some platforms would
return a pointer with no memory behind it, which would break pymalloc.
To solve these problems, allocate an extra byte. */
if
(
size
==
0
)
size
=
1
;
return
malloc
(
size
);
}
void
*
_PyMem_RawCalloc
(
void
*
Py_UNUSED
(
ctx
),
size_t
nelem
,
size_t
elsize
)
{
/* PyMem_RawCalloc(0, 0) means calloc(1, 1). Some systems would return NULL
for calloc(0, 0), which would be treated as an error. Some platforms
would return a pointer with no memory behind it, which would break
pymalloc. To solve these problems, allocate an extra byte. */
if
(
nelem
==
0
||
elsize
==
0
) {
nelem
=
1
;
elsize
=
1
;
}
return
calloc
(
nelem
,
elsize
);
}
void
*
_PyMem_RawRealloc
(
void
*
Py_UNUSED
(
ctx
),
void
*
ptr
,
size_t
size
)
{
if
(
size
==
0
)
size
=
1
;
return
realloc
(
ptr
,
size
);
}
void
_PyMem_RawFree
(
void
*
Py_UNUSED
(
ctx
),
void
*
ptr
)
{
free
(
ptr
);
}
#ifdef
WITH_MIMALLOC
static
void
_PyMem_mi_page_clear_qsbr
(
mi_page_t
*
page
)
{
#ifdef
Py_GIL_DISABLED
// Clear the QSBR goal and remove the page from the QSBR linked list.
page
->
qsbr_goal
=
0
;
if
(
page
->
qsbr_node
.
next
!=
NULL
) {
llist_remove
(
&
page
->
qsbr_node
);
}
#endif
}
// Check if an empty, newly reclaimed page is safe to free now.
static
bool
_PyMem_mi_page_is_safe_to_free
(
mi_page_t
*
page
)
{
assert
(
mi_page_all_free
(
page
));
#ifdef
Py_GIL_DISABLED
assert
(
page
->
qsbr_node
.
next
==
NULL
);
if
(
page
->
use_qsbr
&&
page
->
qsbr_goal
!=
0
) {
_PyThreadStateImpl
*
tstate
=
(
_PyThreadStateImpl
*
)
_PyThreadState_GET
();
if
(
tstate
==
NULL
) {
return
false;
}
return
_Py_qbsr_goal_reached
(
tstate
->
qsbr
,
page
->
qsbr_goal
);
}
#endif
return
true;
}
#ifdef
Py_GIL_DISABLED
// If we are deferring collection of more than this amount of memory for
// mimalloc pages, advance the write sequence. Advancing allows these
// pages to be re-used in a different thread or for a different size class.
#define
QSBR_PAGE_MEM_LIMIT
4096*20
// Return true if the global write sequence should be advanced for a mimalloc
// page that is deferred from collection.
static
bool
should_advance_qsbr_for_page
(
struct
_qsbr_thread_state
*
qsbr
,
mi_page_t
*
page
)
{
size_t
bsize
=
mi_page_block_size
(
page
);
size_t
page_size
=
page
->
capacity
*
bsize
;
if
(
page_size
>
QSBR_PAGE_MEM_LIMIT
) {
qsbr
->
deferred_page_memory
=
0
;
return
true;
}
qsbr
->
deferred_page_memory
+=
page_size
;
if
(
qsbr
->
deferred_page_memory
>
QSBR_PAGE_MEM_LIMIT
) {
qsbr
->
deferred_page_memory
=
0
;
return
true;
}
return
false;
}
static
_PyThreadStateImpl
*
tstate_from_heap
(
mi_heap_t
*
heap
)
{
return
_Py_CONTAINER_OF
(
heap
->
tld
,
_PyThreadStateImpl
,
mimalloc
.
tld
);
}
#endif
static
bool
_PyMem_mi_page_maybe_free
(
mi_page_t
*
page
,
mi_page_queue_t
*
pq
,
bool
force
)
{
#ifdef
Py_GIL_DISABLED
assert
(
mi_page_all_free
(
page
));
if
(
page
->
use_qsbr
) {
struct
_qsbr_thread_state
*
qsbr
=
((
_PyThreadStateImpl
*
)
PyThreadState_GET
())
->
qsbr
;
if
(
page
->
qsbr_goal
!=
0
&&
_Py_qbsr_goal_reached
(
qsbr
,
page
->
qsbr_goal
)) {
_PyMem_mi_page_clear_qsbr
(
page
);
_mi_page_free
(
page
,
pq
,
force
);
return
true;
}
// gh-145615: since we are not freeing this page yet, we want to
// make it available for allocations. Note that the QSBR goal and
// linked list node remain set even if the page is later used for
// an allocation. We only detect and clear the QSBR goal when the
// page becomes empty again (used == 0).
if
(
mi_page_is_in_full
(
page
)) {
_mi_page_unfull
(
page
);
}
_PyMem_mi_page_clear_qsbr
(
page
);
page
->
retire_expire
=
0
;
if
(
should_advance_qsbr_for_page
(
qsbr
,
page
)) {
page
->
qsbr_goal
=
_Py_qsbr_advance
(
qsbr
->
shared
);
}
else
{
page
->
qsbr_goal
=
_Py_qsbr_shared_next
(
qsbr
->
shared
);
}
// We may be freeing a page belonging to a different thread during a
// stop-the-world event. Find the _PyThreadStateImpl for the page.
_PyThreadStateImpl
*
tstate
=
tstate_from_heap
(
mi_page_heap
(
page
));
llist_insert_tail
(
&
tstate
->
mimalloc
.
page_list
,
&
page
->
qsbr_node
);
return
false;
}
#endif
_mi_page_free
(
page
,
pq
,
force
);
return
true;
}
static
void
_PyMem_mi_page_reclaimed
(
mi_page_t
*
page
)
{
#ifdef
Py_GIL_DISABLED
assert
(
page
->
qsbr_node
.
next
==
NULL
);
if
(
page
->
qsbr_goal
!=
0
) {
if
(
mi_page_all_free
(
page
)) {
assert
(
page
->
qsbr_node
.
next
==
NULL
);
_PyThreadStateImpl
*
tstate
=
tstate_from_heap
(
mi_page_heap
(
page
));
assert
(
tstate
==
(
_PyThreadStateImpl
*
)
_PyThreadState_GET
());
page
->
retire_expire
=
0
;
llist_insert_tail
(
&
tstate
->
mimalloc
.
page_list
,
&
page
->
qsbr_node
);
}
else
{
page
->
qsbr_goal
=
0
;
}
}
#endif
}
static
void
_PyMem_mi_heap_collect_qsbr
(
mi_heap_t
*
heap
)
{
#ifdef
Py_GIL_DISABLED
if
(!
heap
->
page_use_qsbr
) {
return
;
}
_PyThreadStateImpl
*
tstate
=
(
_PyThreadStateImpl
*
)
_PyThreadState_GET
();
struct
llist_node
*
head
=
&
tstate
->
mimalloc
.
page_list
;
if
(
llist_empty
(
head
)) {
return
;
}
struct
llist_node
*
node
;
llist_for_each_safe
(
node
,
head
) {
mi_page_t
*
page
=
llist_data
(
node
,
mi_page_t
,
qsbr_node
);
if
(!
mi_page_all_free
(
page
)) {
// We allocated from this page some point after the delayed free
_PyMem_mi_page_clear_qsbr
(
page
);
continue
;
}
if
(!
_Py_qsbr_poll
(
tstate
->
qsbr
,
page
->
qsbr_goal
)) {
return
;
}
_PyMem_mi_page_clear_qsbr
(
page
);
_mi_page_free
(
page
,
mi_page_queue_of
(
page
), false);
}
#endif
}
void
*
_PyMem_MiMalloc
(
void
*
ctx
,
size_t
size
)
{
#ifdef
Py_GIL_DISABLED
_PyThreadStateImpl
*
tstate
=
(
_PyThreadStateImpl
*
)
_PyThreadState_GET
();
mi_heap_t
*
heap
=
&
tstate
->
mimalloc
.
heaps
[
_Py_MIMALLOC_HEAP_MEM
];
return
mi_heap_malloc
(
heap
,
size
);
#else
return
mi_malloc
(
size
);
#endif
}
void
*
_PyMem_MiCalloc
(
void
*
ctx
,
size_t
nelem
,
size_t
elsize
)
{
#ifdef
Py_GIL_DISABLED
_PyThreadStateImpl
*
tstate
=
(
_PyThreadStateImpl
*
)
_PyThreadState_GET
();
mi_heap_t
*
heap
=
&
tstate
->
mimalloc
.
heaps
[
_Py_MIMALLOC_HEAP_MEM
];
return
mi_heap_calloc
(
heap
,
nelem
,
elsize
);
#else
return
mi_calloc
(
nelem
,
elsize
);
#endif
}
void
*
_PyMem_MiRealloc
(
void
*
ctx
,
void
*
ptr
,
size_t
size
)
{
#ifdef
Py_GIL_DISABLED
_PyThreadStateImpl
*
tstate
=
(
_PyThreadStateImpl
*
)
_PyThreadState_GET
();
mi_heap_t
*
heap
=
&
tstate
->
mimalloc
.
heaps
[
_Py_MIMALLOC_HEAP_MEM
];
return
mi_heap_realloc
(
heap
,
ptr
,
size
);
#else
return
mi_realloc
(
ptr
,
size
);
#endif
}
void
_PyMem_MiFree
(
void
*
ctx
,
void
*
ptr
)
{
mi_free
(
ptr
);
}
void
*
_PyObject_MiMalloc
(
void
*
ctx
,
size_t
nbytes
)
{
#ifdef
Py_GIL_DISABLED
_PyThreadStateImpl
*
tstate
=
(
_PyThreadStateImpl
*
)
_PyThreadState_GET
();
mi_heap_t
*
heap
=
tstate
->
mimalloc
.
current_object_heap
;
return
mi_heap_malloc
(
heap
,
nbytes
);
#else
return
mi_malloc
(
nbytes
);
#endif
}
void
*
_PyObject_MiCalloc
(
void
*
ctx
,
size_t
nelem
,
size_t
elsize
)
{
#ifdef
Py_GIL_DISABLED
_PyThreadStateImpl
*
tstate
=
(
_PyThreadStateImpl
*
)
_PyThreadState_GET
();
mi_heap_t
*
heap
=
tstate
->
mimalloc
.
current_object_heap
;
return
mi_heap_calloc
(
heap
,
nelem
,
elsize
);
#else
return
mi_calloc
(
nelem
,
elsize
);
#endif
}
void
*
_PyObject_MiRealloc
(
void
*
ctx
,
void
*
ptr
,
size_t
nbytes
)
{
#ifdef
Py_GIL_DISABLED
_PyThreadStateImpl
*
tstate
=
(
_PyThreadStateImpl
*
)
_PyThreadState_GET
();
// Implement our own realloc logic so that we can copy PyObject header
// in a thread-safe way.
size_t
size
=
mi_usable_size
(
ptr
);
if
(
nbytes
<=
size
&&
nbytes
>= (
size
/
2
)
&&
nbytes
>
0
) {
return
ptr
;
}
mi_heap_t
*
heap
=
tstate
->
mimalloc
.
current_object_heap
;
void
*
newp
=
mi_heap_malloc
(
heap
,
nbytes
);
if
(
newp
==
NULL
) {
return
NULL
;
}
// Free threaded Python allows access from other threads to the PyObject reference count
// fields for a period of time after the object is freed (see InternalDocs/qsbr.md).
// These fields are typically initialized by PyObject_Init() using relaxed
// atomic stores. We need to copy these fields in a thread-safe way here.
// We use the "debug_offset" to determine how many bytes to copy -- it
// includes the PyObject header and plus any extra pre-headers.
size_t
offset
=
heap
->
debug_offset
;
assert
(
offset
%
sizeof
(
void
*
)
==
0
);
size_t
copy_size
=
(
size
<
nbytes
?
size
:
nbytes
);
if
(
copy_size
>=
offset
) {
for
(
size_t
i
=
0
;
i
!=
offset
;
i
+=
sizeof
(
void
*
)) {
// Use memcpy to avoid strict-aliasing issues. However, we probably
// still have unavoidable strict-aliasing issues with
// _Py_atomic_store_ptr_relaxed here.
void
*
word
;
memcpy
(
&
word
, (
char
*
)
ptr
+
i
,
sizeof
(
void
*
));
_Py_atomic_store_ptr_relaxed
((
void
*
*
)((
char
*
)
newp
+
i
),
word
);
}
_mi_memcpy
((
char
*
)
newp
+
offset
, (
char
*
)
ptr
+
offset
,
copy_size
-
offset
);
}
else
{
// memcpy(dst, NULL, 0) is undefined behavior. See gh-151297.
if
mi_likely
(
ptr
) {
_mi_memcpy
(
newp
,
ptr
,
copy_size
);
}
}
mi_free
(
ptr
);
return
newp
;
#else
return
mi_realloc
(
ptr
,
nbytes
);
#endif
}
void
_PyObject_MiFree
(
void
*
ctx
,
void
*
ptr
)
{
mi_free
(
ptr
);
}
void
*
_PyMem_MiRawMalloc
(
void
*
ctx
,
size_t
size
)
{
return
mi_malloc
(
size
);
}
void
*
_PyMem_MiRawCalloc
(
void
*
ctx
,
size_t
nelem
,
size_t
elsize
)
{
return
mi_calloc
(
nelem
,
elsize
);
}
void
*
_PyMem_MiRawRealloc
(
void
*
ctx
,
void
*
ptr
,
size_t
size
)
{
return
mi_realloc
(
ptr
,
size
);
}
void
_PyMem_MiRawFree
(
void
*
ctx
,
void
*
ptr
)
{
mi_free
(
ptr
);
}
#endif
// WITH_MIMALLOC
#define
MALLOC_ALLOC
{NULL, _PyMem_RawMalloc, _PyMem_RawCalloc, _PyMem_RawRealloc, _PyMem_RawFree}
#ifdef
WITH_MIMALLOC
# define
MIMALLOC_ALLOC
{NULL, _PyMem_MiMalloc, _PyMem_MiCalloc, _PyMem_MiRealloc, _PyMem_MiFree}
# define
MIMALLOC_RAWALLOC
{NULL, _PyMem_MiRawMalloc, _PyMem_MiRawCalloc, _PyMem_MiRawRealloc, _PyMem_MiRawFree}
# define
MIMALLOC_OBJALLOC
{NULL, _PyObject_MiMalloc, _PyObject_MiCalloc, _PyObject_MiRealloc, _PyObject_MiFree}
#endif
/* the pymalloc allocator */
// The actual implementation is further down.
#if
defined(
WITH_PYMALLOC
)
void
*
_PyObject_Malloc
(
void
*
ctx
,
size_t
size
);
void
*
_PyObject_Calloc
(
void
*
ctx
,
size_t
nelem
,
size_t
elsize
);
void
_PyObject_Free
(
void
*
ctx
,
void
*
p
);
void
*
_PyObject_Realloc
(
void
*
ctx
,
void
*
ptr
,
size_t
size
);
# define
PYMALLOC_ALLOC
{NULL, _PyObject_Malloc, _PyObject_Calloc, _PyObject_Realloc, _PyObject_Free}
#endif
// WITH_PYMALLOC
#if
defined(
Py_GIL_DISABLED
)
// Py_GIL_DISABLED requires using mimalloc for "mem" and "obj" domains.
# define
PYRAW_ALLOC
MIMALLOC_RAWALLOC
# define
PYMEM_ALLOC
MIMALLOC_ALLOC
# define
PYOBJ_ALLOC
MIMALLOC_OBJALLOC
#elif
defined(
WITH_PYMALLOC
)
# define
PYRAW_ALLOC
MALLOC_ALLOC
# define
PYMEM_ALLOC
PYMALLOC_ALLOC
# define
PYOBJ_ALLOC
PYMALLOC_ALLOC
#else
# define
PYRAW_ALLOC
MALLOC_ALLOC
# define
PYMEM_ALLOC
MALLOC_ALLOC
# define
PYOBJ_ALLOC
MALLOC_ALLOC
#endif
/* the default debug allocators */
// The actual implementation is further down.
void
*
_PyMem_DebugRawMalloc
(
void
*
ctx
,
size_t
size
);
void
*
_PyMem_DebugRawCalloc
(
void
*
ctx
,
size_t
nelem
,
size_t
elsize
);
void
*
_PyMem_DebugRawRealloc
(
void
*
ctx
,
void
*
ptr
,
size_t
size
);
void
_PyMem_DebugRawFree
(
void
*
ctx
,
void
*
ptr
);
void
*
_PyMem_DebugMalloc
(
void
*
ctx
,
size_t
size
);
void
*
_PyMem_DebugCalloc
(
void
*
ctx
,
size_t
nelem
,
size_t
elsize
);
void
*
_PyMem_DebugRealloc
(
void
*
ctx
,
void
*
ptr
,
size_t
size
);
void
_PyMem_DebugFree
(
void
*
ctx
,
void
*
p
);
#define
PYDBGRAW_ALLOC
\
{&_PyRuntime.allocators.debug.raw, _PyMem_DebugRawMalloc, _PyMem_DebugRawCalloc, _PyMem_DebugRawRealloc, _PyMem_DebugRawFree}
#define
PYDBGMEM_ALLOC
\
{&_PyRuntime.allocators.debug.mem, _PyMem_DebugMalloc, _PyMem_DebugCalloc, _PyMem_DebugRealloc, _PyMem_DebugFree}
#define
PYDBGOBJ_ALLOC
\
{&_PyRuntime.allocators.debug.obj, _PyMem_DebugMalloc, _PyMem_DebugCalloc, _PyMem_DebugRealloc, _PyMem_DebugFree}
/* default raw allocator (not swappable) */
void
*
_PyMem_DefaultRawMalloc
(
size_t
size
)
{
#ifdef
Py_DEBUG
return
_PyMem_DebugRawMalloc
(
&
_PyRuntime
.
allocators
.
debug
.
raw
,
size
);
#else
return
_PyMem_RawMalloc
(
NULL
,
size
);
#endif
}
void
*
_PyMem_DefaultRawCalloc
(
size_t
nelem
,
size_t
elsize
)
{
#ifdef
Py_DEBUG
return
_PyMem_DebugRawCalloc
(
&
_PyRuntime
.
allocators
.
debug
.
raw
,
nelem
,
elsize
);
#else
return
_PyMem_RawCalloc
(
NULL
,
nelem
,
elsize
);
#endif
}
void
*
_PyMem_DefaultRawRealloc
(
void
*
ptr
,
size_t
size
)
{
#ifdef
Py_DEBUG
return
_PyMem_DebugRawRealloc
(
&
_PyRuntime
.
allocators
.
debug
.
raw
,
ptr
,
size
);
#else
return
_PyMem_RawRealloc
(
NULL
,
ptr
,
size
);
#endif
}
void
_PyMem_DefaultRawFree
(
void
*
ptr
)
{
#ifdef
Py_DEBUG
_PyMem_DebugRawFree
(
&
_PyRuntime
.
allocators
.
debug
.
raw
,
ptr
);
#else
_PyMem_RawFree
(
NULL
,
ptr
);
#endif
}
wchar_t
*
_PyMem_DefaultRawWcsdup
(
const
wchar_t
*
str
)
{
assert
(
str
!=
NULL
);
size_t
len
=
wcslen
(
str
);
if
(
len
>
(
size_t
)
PY_SSIZE_T_MAX
/
sizeof
(
wchar_t
)
-
1
) {
return
NULL
;
}
size_t
size
=
(
len
+
1
)
*
sizeof
(
wchar_t
);
wchar_t
*
str2
=
_PyMem_DefaultRawMalloc
(
size
);
if
(
str2
==
NULL
) {
return
NULL
;
}
memcpy
(
str2
,
str
,
size
);
return
str2
;
}
/* the low-level virtual memory allocator */
#ifdef
WITH_PYMALLOC
# ifdef
MS_WINDOWS
# include
<windows.h>
# elif
defined(
HAVE_MMAP
)
# include
<sys/mman.h>
# ifdef
MAP_ANONYMOUS
# define
ARENAS_USE_MMAP
# endif
# endif
#endif
/* Return the system's default huge page size in bytes, or 0 if it
* cannot be determined. The result is cached after the first call.
*
* This is Linux-only (/proc/meminfo). On other systems that define
* MAP_HUGETLB the caller should skip huge pages gracefully. */
#if
defined(
PYMALLOC_USE_HUGEPAGES
)
&&
defined(
ARENAS_USE_MMAP
)
&&
defined(
MAP_HUGETLB
)
static
size_t
_pymalloc_system_hugepage_size
(
void
)
{
static
size_t
hp_size
=
0
;
static
int
initialized
=
0
;
if
(
initialized
) {
return
hp_size
;
}
#ifdef
__linux__
FILE
*
f
=
fopen
(
"/proc/meminfo"
,
"r"
);
if
(
f
!=
NULL
) {
char
line
[
256
];
while
(
fgets
(
line
,
sizeof
(
line
),
f
)) {
unsigned long
size_kb
;
if
(
sscanf
(
line
,
"Hugepagesize: %lu kB"
,
&
size_kb
)
==
1
) {
hp_size
=
(
size_t
)
size_kb
*
1024
;
break
;
}
}
fclose
(
f
);
}
#endif
initialized
=
1
;
return
hp_size
;
}
#endif
#if
(defined(
MS_WINDOWS
)
&&
defined(
PYMALLOC_USE_HUGEPAGES
))
||
\
(defined(
PYMALLOC_USE_HUGEPAGES
)
&&
defined(
ARENAS_USE_MMAP
)
&&
defined(
MAP_HUGETLB
))
static
size_t
_pymalloc_round_up_to_multiple
(
size_t
size
,
size_t
multiple
)
{
if
(
multiple
==
0
||
size
==
0
) {
return
size
;
}
size_t
remainder
=
size
%
multiple
;
if
(
remainder
==
0
) {
return
size
;
}
size_t
padding
=
multiple
-
remainder
;
if
(
size
>
SIZE_MAX
-
padding
) {
return
0
;
}
return
size
+
padding
;
}
#endif
static
size_t
_pymalloc_virtual_alloc_size
(
size_t
size
)
{
#if
defined(
MS_WINDOWS
)
&&
defined(
PYMALLOC_USE_HUGEPAGES
)
if
(
_PyRuntime
.
allocators
.
use_hugepages
) {
SIZE_T
large_page_size
=
GetLargePageMinimum
();
if
(
large_page_size
>
0
) {
return
_pymalloc_round_up_to_multiple
(
size
, (
size_t
)
large_page_size
);
}
}
#elif
defined(
PYMALLOC_USE_HUGEPAGES
)
&&
defined(
ARENAS_USE_MMAP
)
&&
defined(
MAP_HUGETLB
)
if
(
_PyRuntime
.
allocators
.
use_hugepages
) {
size_t
hp_size
=
_pymalloc_system_hugepage_size
();
if
(
hp_size
>
0
) {
return
_pymalloc_round_up_to_multiple
(
size
,
hp_size
);
}
}
#endif
return
size
;
}
void
*
_PyMem_ArenaAlloc
(
void
*
Py_UNUSED
(
ctx
),
size_t
size
)
{
#ifdef
MS_WINDOWS
# ifdef
PYMALLOC_USE_HUGEPAGES
if
(
_PyRuntime
.
allocators
.
use_hugepages
) {
SIZE_T
lp_size
=
GetLargePageMinimum
();
if
(
lp_size
>
0
&&
size
%
lp_size
==
0
) {
void
*
ptr
=
VirtualAlloc
(
NULL
,
size
,
MEM_COMMIT
|
MEM_RESERVE
|
MEM_LARGE_PAGES
,
PAGE_READWRITE
);
if
(
ptr
!=
NULL
)
return
ptr
;
}
}
/* Fall back to regular pages */
# endif
return
VirtualAlloc
(
NULL
,
size
,
MEM_COMMIT
|
MEM_RESERVE
,
PAGE_READWRITE
);
#elif
defined(
ARENAS_USE_MMAP
)
void
*
ptr
;
# ifdef
PYMALLOC_USE_HUGEPAGES
# ifdef
MAP_HUGETLB
if
(
_PyRuntime
.
allocators
.
use_hugepages
) {
size_t
hp_size
=
_pymalloc_system_hugepage_size
();
/* Only use huge pages if the arena size is a multiple of the
* system's default huge page size. When the arena is smaller
* than the huge page, mmap still succeeds but silently
* allocates an entire huge page; the subsequent munmap with
* the smaller arena size then fails with EINVAL, leaking
* all of that memory. */
if
(
hp_size
>
0
&&
size
%
hp_size
==
0
) {
ptr
=
mmap
(
NULL
,
size
,
PROT_READ
|
PROT_WRITE
,
MAP_PRIVATE
|
MAP_ANONYMOUS
|
MAP_HUGETLB
,
-1
,
0
);
if
(
ptr
!=
MAP_FAILED
) {
assert
(
ptr
!=
NULL
);
(
void
)
_PyAnnotateMemoryMap
(
ptr
,
size
,
"cpython:pymalloc:hugepage"
);
return
ptr
;
}
}
}
/* Fall back to regular pages */
# endif
# endif
ptr
=
mmap
(
NULL
,
size
,
PROT_READ
|
PROT_WRITE
,
MAP_PRIVATE
|
MAP_ANONYMOUS
,
-1
,
0
);
if
(
ptr
==
MAP_FAILED
)
return
NULL
;
assert
(
ptr
!=
NULL
);
#ifdef
MADV_HUGEPAGE
(
void
)
madvise
(
ptr
,
size
,
MADV_HUGEPAGE
);
#endif
(
void
)
_PyAnnotateMemoryMap
(
ptr
,
size
,
"cpython:pymalloc"
);
return
ptr
;
#else
return
malloc
(
size
);
#endif
}
void
_PyMem_ArenaFree
(
void
*
Py_UNUSED
(
ctx
),
void
*
ptr
,
#if
defined(
ARENAS_USE_MMAP
)
size_t
size
#else
size_t
Py_UNUSED
(
size
)
#endif
)
{
#ifdef
MS_WINDOWS
/* Unlike free(), VirtualFree() does not special-case NULL to noop. */
if
(
ptr
==
NULL
) {
return
;
}
VirtualFree
(
ptr
,
0
,
MEM_RELEASE
);
#elif
defined(
ARENAS_USE_MMAP
)
/* Unlike free(), munmap() does not special-case NULL to noop. */
if
(
ptr
==
NULL
) {
return
;
}
if
(
munmap
(
ptr
,
size
)
<
0
) {
_Py_FatalErrorFormat
(
__func__
,
"munmap(%p, %zu) failed with errno %d"
,
ptr
,
size
,
errno
);
}
#else
free
(
ptr
);
#endif
}
/*******************************************/
/* end low-level allocator implementations */
/*******************************************/
#define
ALLOCATORS_MUTEX
(_PyRuntime.allocators.mutex)
#define
_PyMem_Raw
(_PyRuntime.allocators.standard.raw)
#define
_PyMem
(_PyRuntime.allocators.standard.mem)
#define
_PyObject
(_PyRuntime.allocators.standard.obj)
#define
_PyMem_Debug
(_PyRuntime.allocators.debug)
#define
_PyObject_Arena
(_PyRuntime.allocators.obj_arena)
/***************************/
/* managing the allocators */
/***************************/
static
int
set_default_allocator_unlocked
(
PyMemAllocatorDomain
domain
,
int
debug
,
PyMemAllocatorEx
*
old_alloc
)
{
if
(
old_alloc
!=
NULL
) {
get_allocator_unlocked
(
domain
,
old_alloc
);
}
PyMemAllocatorEx
new_alloc
;
switch
(
domain
)
{
case
PYMEM_DOMAIN_RAW
:
new_alloc
=
(
PyMemAllocatorEx
)
PYRAW_ALLOC
;
break
;
case
PYMEM_DOMAIN_MEM
:
new_alloc
=
(
PyMemAllocatorEx
)
PYMEM_ALLOC
;
break
;
case
PYMEM_DOMAIN_OBJ
:
new_alloc
=
(
PyMemAllocatorEx
)
PYOBJ_ALLOC
;
break
;
default
:
/* unknown domain */
return
-1
;
}
set_allocator_unlocked
(
domain
,
&
new_alloc
);
if
(
debug
) {
set_up_debug_hooks_domain_unlocked
(
domain
);
}
return
0
;
}
#ifdef
Py_DEBUG
static
const
int
pydebug
=
1
;
#else
static
const
int
pydebug
=
0
;
#endif
int
_PyMem_GetAllocatorName
(
const
char
*
name
,
PyMemAllocatorName
*
allocator
)
{
if
(
name
==
NULL
||
*
name
==
'\0'
) {
/* PYTHONMALLOC is empty or is not set or ignored (-E/-I command line
nameions): use default memory allocators */
*
allocator
=
PYMEM_ALLOCATOR_DEFAULT
;
}
else
if
(
strcmp
(
name
,
"default"
)
==
0
) {
*
allocator
=
PYMEM_ALLOCATOR_DEFAULT
;
}
else
if
(
strcmp
(
name
,
"debug"
)
==
0
) {
*
allocator
=
PYMEM_ALLOCATOR_DEBUG
;
}
#if
defined(
WITH_PYMALLOC
)
&&
!defined(
Py_GIL_DISABLED
)
else
if
(
strcmp
(
name
, "
pymalloc
")
==
0
) {
*
allocator
=
PYMEM_ALLOCATOR_PYMALLOC
;
}
else
if
(
strcmp
(
name
, "
pymalloc_debug
")
==
0
) {
*
allocator
=
PYMEM_ALLOCATOR_PYMALLOC_DEBUG
;
}
#endif
#ifdef
WITH_MIMALLOC
else
if
(
strcmp
(
name
, "
mimalloc
")
==
0
) {
*
allocator
=
PYMEM_ALLOCATOR_MIMALLOC
;
}
else
if
(
strcmp
(
name
,
"mimalloc_debug"
)
==
0
) {
*
allocator
=
PYMEM_ALLOCATOR_MIMALLOC_DEBUG
;
}
#endif
#ifndef
Py_GIL_DISABLED
else
if
(
strcmp
(
name
, "
malloc
")
==
0
) {
*
allocator
=
PYMEM_ALLOCATOR_MALLOC
;
}
else
if
(
strcmp
(
name
,
"malloc_debug"
)
==
0
) {
*
allocator
=
PYMEM_ALLOCATOR_MALLOC_DEBUG
;
}
#endif
else
{
/* unknown allocator */
return
-1
;
}
return
0
;
}
static
int
set_up_allocators_unlocked
(
PyMemAllocatorName
allocator
)
{
switch
(
allocator
) {
case
PYMEM_ALLOCATOR_NOT_SET
:
/* do nothing */
break
;
case
PYMEM_ALLOCATOR_DEFAULT
:
(
void
)
set_default_allocator_unlocked
(
PYMEM_DOMAIN_RAW
,
pydebug
,
NULL
);
(
void
)
set_default_allocator_unlocked
(
PYMEM_DOMAIN_MEM
,
pydebug
,
NULL
);
(
void
)
set_default_allocator_unlocked
(
PYMEM_DOMAIN_OBJ
,
pydebug
,
NULL
);
_PyRuntime
.
allocators
.
is_debug_enabled
=
pydebug
;
break
;
case
PYMEM_ALLOCATOR_DEBUG
:
(
void
)
set_default_allocator_unlocked
(
PYMEM_DOMAIN_RAW
,
1
,
NULL
);
(
void
)
set_default_allocator_unlocked
(
PYMEM_DOMAIN_MEM
,
1
,
NULL
);
(
void
)
set_default_allocator_unlocked
(
PYMEM_DOMAIN_OBJ
,
1
,
NULL
);
_PyRuntime
.
allocators
.
is_debug_enabled
=
1
;
break
;
#ifdef
WITH_PYMALLOC
case
PYMEM_ALLOCATOR_PYMALLOC
:
case
PYMEM_ALLOCATOR_PYMALLOC_DEBUG
:
{
PyMemAllocatorEx
malloc_alloc
=
MALLOC_ALLOC
;
set_allocator_unlocked
(
PYMEM_DOMAIN_RAW
,
&
malloc_alloc
);
PyMemAllocatorEx
pymalloc
=
PYMALLOC_ALLOC
;
set_allocator_unlocked
(
PYMEM_DOMAIN_MEM
,
&
pymalloc
);
set_allocator_unlocked
(
PYMEM_DOMAIN_OBJ
,
&
pymalloc
);
int
is_debug
=
(
allocator
==
PYMEM_ALLOCATOR_PYMALLOC_DEBUG
);
_PyRuntime
.
allocators
.
is_debug_enabled
=
is_debug
;
if
(
is_debug
) {
set_up_debug_hooks_unlocked
();
}
break
;
}
#endif
#ifdef
WITH_MIMALLOC
case
PYMEM_ALLOCATOR_MIMALLOC
:
case
PYMEM_ALLOCATOR_MIMALLOC_DEBUG
:
{
PyMemAllocatorEx
malloc_alloc
=
MIMALLOC_RAWALLOC
;
set_allocator_unlocked
(
PYMEM_DOMAIN_RAW
,
&
malloc_alloc
);
PyMemAllocatorEx
pymalloc
=
MIMALLOC_ALLOC
;
set_allocator_unlocked
(
PYMEM_DOMAIN_MEM
,
&
pymalloc
);
PyMemAllocatorEx
objmalloc
=
MIMALLOC_OBJALLOC
;
set_allocator_unlocked
(
PYMEM_DOMAIN_OBJ
,
&
objmalloc
);
int
is_debug
=
(
allocator
==
PYMEM_ALLOCATOR_MIMALLOC_DEBUG
);
_PyRuntime
.
allocators
.
is_debug_enabled
=
is_debug
;
if
(
is_debug
) {
set_up_debug_hooks_unlocked
();
}
break
;
}
#endif
case
PYMEM_ALLOCATOR_MALLOC
:
case
PYMEM_ALLOCATOR_MALLOC_DEBUG
:
{
PyMemAllocatorEx
malloc_alloc
=
MALLOC_ALLOC
;
set_allocator_unlocked
(
PYMEM_DOMAIN_RAW
,
&
malloc_alloc
);
set_allocator_unlocked
(
PYMEM_DOMAIN_MEM
,
&
malloc_alloc
);
set_allocator_unlocked
(
PYMEM_DOMAIN_OBJ
,
&
malloc_alloc
);
int
is_debug
=
(
allocator
==
PYMEM_ALLOCATOR_MALLOC_DEBUG
);
_PyRuntime
.
allocators
.
is_debug_enabled
=
is_debug
;
if
(
is_debug
) {
set_up_debug_hooks_unlocked
();
}
break
;
}
default
:
/* unknown allocator */
return
-1
;
}
return
0
;
}
int
_PyMem_SetupAllocators
(
PyMemAllocatorName
allocator
)
{
PyMutex_Lock
(
&
ALLOCATORS_MUTEX
);
int
res
=
set_up_allocators_unlocked
(
allocator
);
PyMutex_Unlock
(
&
ALLOCATORS_MUTEX
);
return
res
;
}
static
int
pymemallocator_eq
(
PyMemAllocatorEx
*
a
,
PyMemAllocatorEx
*
b
)
{
return
(
memcmp
(
a
,
b
,
sizeof
(
PyMemAllocatorEx
))
==
0
);
}
static
const
char
*
get_current_allocator_name_unlocked
(
void
)
{
PyMemAllocatorEx
malloc_alloc
=
MALLOC_ALLOC
;
#ifdef
WITH_PYMALLOC
PyMemAllocatorEx
pymalloc
=
PYMALLOC_ALLOC
;
#endif
#ifdef
WITH_MIMALLOC
PyMemAllocatorEx
mimalloc
=
MIMALLOC_ALLOC
;
PyMemAllocatorEx
mimalloc_obj
=
MIMALLOC_OBJALLOC
;
PyMemAllocatorEx
mimalloc_raw
=
MIMALLOC_RAWALLOC
;
#endif
if
(
pymemallocator_eq
(
&
_PyMem_Raw
,
&
malloc_alloc
)
&&
pymemallocator_eq
(
&
_PyMem
,
&
malloc_alloc
)
&&
pymemallocator_eq
(
&
_PyObject
,
&
malloc_alloc
))
{
return
"malloc"
;
}
#ifdef
WITH_PYMALLOC
if
(
pymemallocator_eq
(
&
_PyMem_Raw
,
&
malloc_alloc
)
&&
pymemallocator_eq
(
&
_PyMem
,
&
pymalloc
)
&&
pymemallocator_eq
(
&
_PyObject
,
&
pymalloc
))
{
return
"pymalloc"
;
}
#endif
#ifdef
WITH_MIMALLOC
if
(
pymemallocator_eq
(
&
_PyMem_Raw
,
&
mimalloc_raw
)
&&
pymemallocator_eq
(
&
_PyMem
,
&
mimalloc
)
&&
pymemallocator_eq
(
&
_PyObject
,
&
mimalloc_obj
))
{
return
"mimalloc"
;
}
#endif
PyMemAllocatorEx
dbg_raw
=
PYDBGRAW_ALLOC
;
PyMemAllocatorEx
dbg_mem
=
PYDBGMEM_ALLOC
;
PyMemAllocatorEx
dbg_obj
=
PYDBGOBJ_ALLOC
;
if
(
pymemallocator_eq
(
&
_PyMem_Raw
,
&
dbg_raw
)
&&
pymemallocator_eq
(
&
_PyMem
,
&
dbg_mem
)
&&
pymemallocator_eq
(
&
_PyObject
,
&
dbg_obj
))
{
/* Debug hooks installed */
if
(
pymemallocator_eq
(
&
_PyMem_Debug
.
raw
.
alloc
,
&
malloc_alloc
)
&&
pymemallocator_eq
(
&
_PyMem_Debug
.
mem
.
alloc
,
&
malloc_alloc
)
&&
pymemallocator_eq
(
&
_PyMem_Debug
.
obj
.
alloc
,
&
malloc_alloc
))
{
return
"malloc_debug"
;
}
#ifdef
WITH_PYMALLOC
if
(
pymemallocator_eq
(
&
_PyMem_Debug
.
raw
.
alloc
,
&
malloc_alloc
)
&&
pymemallocator_eq
(
&
_PyMem_Debug
.
mem
.
alloc
,
&
pymalloc
)
&&
pymemallocator_eq
(
&
_PyMem_Debug
.
obj
.
alloc
,
&
pymalloc
))
{
return
"pymalloc_debug"
;
}
#endif
#ifdef
WITH_MIMALLOC
if
(
pymemallocator_eq
(
&
_PyMem_Debug
.
raw
.
alloc
,
&
mimalloc_raw
)
&&
pymemallocator_eq
(
&
_PyMem_Debug
.
mem
.
alloc
,
&
mimalloc
)
&&
pymemallocator_eq
(
&
_PyMem_Debug
.
obj
.
alloc
,
&
mimalloc_obj
))
{
return
"mimalloc_debug"
;
}
#endif
}
return
NULL
;
}
const
char
*
_PyMem_GetCurrentAllocatorName
(
void
)
{
PyMutex_Lock
(
&
ALLOCATORS_MUTEX
);
const
char
*
name
=
get_current_allocator_name_unlocked
();
PyMutex_Unlock
(
&
ALLOCATORS_MUTEX
);
return
name
;
}
int
_PyMem_DebugEnabled
(
void
)
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL