diff --git a/Doc/data/python3.12.abi b/Doc/data/python3.12.abi index ab5190f6966c02..fca912823a74ed 100644 --- a/Doc/data/python3.12.abi +++ b/Doc/data/python3.12.abi @@ -1710,7 +1710,7 @@ - + @@ -2072,7 +2072,7 @@ - + @@ -2524,7 +2524,7 @@ - + @@ -2548,39 +2548,39 @@ - + - + - + - + - - + + - - + + - - - + + + - - - + + + - - - + + + @@ -3679,7 +3679,7 @@ - + @@ -3698,7 +3698,7 @@ - + @@ -3724,65 +3724,68 @@ - - + + - + - - - - + + + + - + - - + - - + + - + + + + + - - + + - + - + - - + + - - - - + + + + - - - + + + - - + + - - + + @@ -3896,22 +3899,22 @@ - - - + + + - - + + - - + + - - - + + + @@ -4033,7 +4036,7 @@ - + @@ -4089,22 +4092,22 @@ - + - + - + - + @@ -4750,7 +4753,7 @@ - + @@ -4908,27 +4911,27 @@ - + - + - + - + - + @@ -5189,7 +5192,7 @@ - + @@ -5374,7 +5377,7 @@ - + @@ -5411,12 +5414,12 @@ - + - + @@ -5640,7 +5643,7 @@ - + @@ -5672,21 +5675,21 @@ - + - + - + - + @@ -5820,12 +5823,12 @@ - + - + @@ -6136,7 +6139,7 @@ - + @@ -6169,11 +6172,11 @@ - + - + @@ -6590,11 +6593,11 @@ - + - + @@ -6875,9 +6878,9 @@ - - - + + + @@ -7125,41 +7128,41 @@ - - + + - - - - + + + + - - - - + + + + - - - - + + + + - - - + + + - - - + + + - - - + + + @@ -7414,7 +7417,7 @@ - + @@ -7460,7 +7463,7 @@ - + @@ -7484,23 +7487,23 @@ - + - + - + - + - + @@ -7537,60 +7540,60 @@ - - + + - - - - + + + + - - + + - - - - + + + + - - - - + + + + - + - + - + - + - + - + - + @@ -7648,15 +7651,15 @@ - + - + - + @@ -7717,7 +7720,7 @@ - + @@ -7807,42 +7810,42 @@ - - - - - + + + + + - - - + + + - - - + + + - - - + + + - - - + + + - + - - + + - - + + @@ -7925,33 +7928,33 @@ - + - + - + - + - + - + - + - + - + - + @@ -7993,7 +7996,7 @@ - + @@ -8154,7 +8157,7 @@ - + @@ -8217,12 +8220,18 @@ + + + + + + - - + + @@ -8426,13 +8435,18 @@ - + + + - + + + + - + @@ -8473,63 +8487,63 @@ - - - + + + - - - + + + - - + + - + - + - - - + + + - - - + + + - - + + - - + + - - - + + + - - + + - - + + - - + + - - + + @@ -8616,21 +8630,21 @@ - - + + - - + + - - - + + + - - + + @@ -8663,40 +8677,40 @@ - + - + - + - - + + - + - + - + - + - + - + - + @@ -8837,16 +8851,16 @@ - + - - - + + + @@ -9062,7 +9076,7 @@ - + @@ -9663,50 +9677,50 @@ - - - + + + - - - + + + - - - - + + + + - - - + + + - - - - - + + + + + - - - - + + + + - - - + + + - - + + - + @@ -10841,7 +10855,7 @@ - + @@ -11974,28 +11988,28 @@ - - - - - + + + + + - + - + - + - + - + - + @@ -12064,10 +12078,10 @@ - + - + @@ -13232,42 +13246,42 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -16098,7 +16112,7 @@ - + @@ -16211,112 +16225,112 @@ - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + @@ -16489,7 +16503,7 @@ - + @@ -16497,34 +16511,34 @@ - + - + - + - + - + - + - + - + - + @@ -16532,22 +16546,22 @@ - - + + - + - + - + @@ -16558,34 +16572,34 @@ - + - + - + - + - + - + - + - + - + - + @@ -16593,7 +16607,7 @@ - + @@ -16601,9 +16615,9 @@ - + - + @@ -16612,8 +16626,8 @@ - - + + @@ -16633,7 +16647,7 @@ - + @@ -16642,22 +16656,22 @@ - + - + - + - + - + @@ -16669,7 +16683,7 @@ - + @@ -16681,10 +16695,10 @@ - + - + @@ -16693,7 +16707,7 @@ - + @@ -16705,7 +16719,7 @@ - + @@ -16714,10 +16728,10 @@ - + - + @@ -16732,10 +16746,10 @@ - + - + @@ -16749,20 +16763,20 @@ - + - + - + - + @@ -16777,7 +16791,7 @@ - + @@ -16785,15 +16799,15 @@ - + - + - + @@ -16801,9 +16815,9 @@ - + - + @@ -16812,7 +16826,7 @@ - + @@ -16826,7 +16840,7 @@ - + @@ -16840,12 +16854,12 @@ - + - + - + @@ -16860,7 +16874,7 @@ - + @@ -16872,13 +16886,13 @@ - + - + @@ -16897,7 +16911,7 @@ - + @@ -16913,12 +16927,12 @@ - + - + @@ -16938,13 +16952,13 @@ - + - + @@ -16953,17 +16967,17 @@ - - + + - + - + - - + + @@ -16971,12 +16985,12 @@ - + - + - + @@ -16990,7 +17004,7 @@ - + @@ -17000,13 +17014,13 @@ - + - + - + @@ -17027,11 +17041,11 @@ - - + + - - + + @@ -17045,18 +17059,18 @@ - - + + - + - + - + @@ -17064,8 +17078,8 @@ - - + + @@ -17073,11 +17087,11 @@ - + - + - + @@ -17086,10 +17100,10 @@ - - + + - + @@ -17098,10 +17112,10 @@ - + - + @@ -17113,21 +17127,21 @@ - + - + - + - - - - - - - + + + + + + + @@ -17150,24 +17164,24 @@ - + - + - + - + - - + + - + - + @@ -17185,25 +17199,25 @@ - - - - - - + + + + + + - - + + - - + + @@ -17211,9 +17225,9 @@ - - - + + + @@ -17227,36 +17241,36 @@ - + - + - + - + - + - + - + - + - + - + @@ -17264,7 +17278,7 @@ - + @@ -17272,10 +17286,10 @@ - + - + @@ -17283,10 +17297,10 @@ - + - + @@ -17303,7 +17317,7 @@ - + @@ -17311,18 +17325,18 @@ - + - + - + - + - + @@ -17330,7 +17344,7 @@ - + @@ -17338,7 +17352,7 @@ - + @@ -17349,8 +17363,8 @@ - - + + @@ -17361,32 +17375,32 @@ - - - + + + - + - + - + - - - - + + + + @@ -17396,71 +17410,72 @@ - + - - + + - - + + - + + - - - + + + - - - - - - - - - - - + + + + + + + + + + + - + - - - - - + + + + + - - - - - - - - - - - - + + + + + + + + + + + + @@ -17483,7 +17498,7 @@ - + @@ -17491,7 +17506,7 @@ - + @@ -17542,163 +17557,163 @@ - + - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -17706,8 +17721,8 @@ - - + + @@ -17720,17 +17735,17 @@ - - + + - + - - + + @@ -17738,8 +17753,8 @@ - - + + @@ -17747,8 +17762,8 @@ - - + + @@ -17756,35 +17771,35 @@ - - + + - + - - + + - + - - + + - - + + @@ -17792,8 +17807,8 @@ - - + + @@ -17801,29 +17816,29 @@ - - + + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + @@ -17861,7 +17876,7 @@ - + @@ -17891,22 +17906,22 @@ - + - + - + - + - + @@ -17957,13 +17972,13 @@ - + - + @@ -18253,7 +18268,7 @@ - + @@ -18284,7 +18299,7 @@ - + @@ -18335,7 +18350,7 @@ - + @@ -18345,7 +18360,7 @@ - + @@ -18356,7 +18371,7 @@ - + @@ -18366,7 +18381,7 @@ - + @@ -18429,7 +18444,7 @@ - + @@ -18450,7 +18465,7 @@ - + @@ -18593,27 +18608,27 @@ - + - + - + - + @@ -18653,76 +18668,76 @@ - + - + - + - + - + - - + + - - + + - + - + - + - + - + - - + + - + - + - + - + - - + + - + - - + + - + @@ -18731,104 +18746,104 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - + - + - + - + - - - + + + - + - + - - + + @@ -18837,43 +18852,43 @@ - + - + - + - + - + - + - + - + - + - + - + - + @@ -18881,22 +18896,22 @@ - + - + - + - + @@ -18906,39 +18921,39 @@ - + - + - + - - - + + + - + - + - + - + - + @@ -18947,60 +18962,60 @@ - + - + - + - - + + - + - + - + - + - + - + - + - + - + @@ -19008,15 +19023,15 @@ - - + + - + @@ -19028,7 +19043,7 @@ - + @@ -19038,22 +19053,22 @@ - + - + - + - - - - + + + + @@ -19067,32 +19082,32 @@ - - + + - + - - + + - + - + - + - - - + + + - + @@ -19100,7 +19115,7 @@ - + @@ -19108,189 +19123,189 @@ - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - + - + - + - + - + - + - + - + - - + + - + - + - + - - - + + + - + - + - + - + - - + + - + - - + + @@ -19310,43 +19325,43 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -19361,16 +19376,16 @@ - + - + - + @@ -19388,22 +19403,22 @@ - + - + - + - + - + @@ -19442,17 +19457,17 @@ - - - + + + - + - + @@ -19461,7 +19476,7 @@ - + @@ -19481,7 +19496,7 @@ - + @@ -19489,25 +19504,25 @@ - + - - - + + + - - + + - - + + @@ -19523,20 +19538,20 @@ - - + + - - - - - - + + + + + + @@ -19544,11 +19559,11 @@ - + - - + + @@ -19556,11 +19571,11 @@ - + - - + + @@ -19568,11 +19583,11 @@ - + - - + + @@ -19580,11 +19595,11 @@ - + - - + + @@ -19614,12 +19629,12 @@ - + - + - + @@ -19634,42 +19649,42 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -19681,51 +19696,51 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -19733,7 +19748,7 @@ - + @@ -19744,15 +19759,15 @@ - + - + - + @@ -19760,7 +19775,7 @@ - + @@ -19771,7 +19786,7 @@ - + @@ -19779,20 +19794,20 @@ - + - + - + - + @@ -19800,7 +19815,7 @@ - + @@ -19808,7 +19823,7 @@ - + @@ -19819,7 +19834,7 @@ - + @@ -19830,7 +19845,7 @@ - + @@ -19841,12 +19856,12 @@ - + - + @@ -19854,7 +19869,7 @@ - + @@ -19865,7 +19880,7 @@ - + @@ -19876,7 +19891,7 @@ - + @@ -19884,7 +19899,7 @@ - + @@ -19892,7 +19907,7 @@ - + @@ -19900,7 +19915,7 @@ - + @@ -19911,7 +19926,7 @@ - + @@ -19925,7 +19940,7 @@ - + @@ -19948,7 +19963,7 @@ - + @@ -19971,7 +19986,7 @@ - + @@ -19991,9 +20006,9 @@ - - - + + + @@ -20007,23 +20022,23 @@ - + - + - + - + - + @@ -20031,43 +20046,43 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + @@ -20102,24 +20117,24 @@ - - - + + + - - - - - - - - + + + + + + + + - - - + + + @@ -20157,10 +20172,10 @@ - + - + @@ -20169,31 +20184,31 @@ - + - + - + - + - + - + @@ -20205,11 +20220,11 @@ - + - + @@ -20220,11 +20235,11 @@ - + - - + + @@ -20247,14 +20262,14 @@ - + - - + + @@ -20262,10 +20277,10 @@ - - + + - + @@ -20274,7 +20289,7 @@ - + @@ -20282,13 +20297,13 @@ - - + + - + - + @@ -20303,7 +20318,7 @@ - + @@ -20339,7 +20354,7 @@ - + @@ -20354,19 +20369,19 @@ - - + + - + - + @@ -20392,14 +20407,14 @@ - + - + - + @@ -20444,8 +20459,8 @@ - - + + @@ -20483,7 +20498,7 @@ - + @@ -20513,22 +20528,22 @@ - + - + - + - + - + @@ -20579,13 +20594,13 @@ - + - + @@ -20600,82 +20615,82 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - - - - - - + + + + + + + + - - + + + - + - - - - - - - - - + + + + + + + + + + - - - - - - - - - + + + + + + + + - - - - - - - + + + + + + + - - - - - + + + + + - - + + - - - - + + + + @@ -20762,25 +20777,25 @@ - + - + - + - + @@ -20879,19 +20894,19 @@ - + - + - + - + @@ -20899,7 +20914,7 @@ - + @@ -20910,12 +20925,12 @@ - + - + @@ -20923,143 +20938,143 @@ - + - + - + - + - + - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -21077,7 +21092,7 @@ - + @@ -21183,14 +21198,14 @@ - - + + - + - + @@ -21208,9 +21223,9 @@ - - + + @@ -21267,7 +21282,7 @@ - + @@ -21307,7 +21322,7 @@ - + @@ -21360,7 +21375,7 @@ - + @@ -21376,12 +21391,17 @@ + + + + + - + @@ -21489,7 +21509,7 @@ - + @@ -21571,7 +21591,7 @@ - + @@ -21673,11 +21693,11 @@ - + - + @@ -21704,11 +21724,11 @@ - + - + @@ -21789,7 +21809,7 @@ - + @@ -21814,21 +21834,21 @@ - + - + - + - + @@ -21944,7 +21964,7 @@ - + @@ -22010,35 +22030,35 @@ - - + + - + - - + + - - + + - - + + - - + + - + - + @@ -22046,33 +22066,33 @@ - - + + - + - + - - - - - + + + + + - - - - - + + + + + - + - + @@ -22110,38 +22130,38 @@ - - + + - + - + - + - + - + - - + + - - + + @@ -22200,7 +22220,7 @@ - + @@ -22301,16 +22321,16 @@ - - + + - + - + - + @@ -22324,17 +22344,17 @@ - - + + - + - - + + @@ -22348,8 +22368,8 @@ - - + + @@ -22360,8 +22380,8 @@ - - + + @@ -22369,16 +22389,16 @@ - + - + - - + + - + @@ -22396,8 +22416,8 @@ - - + + @@ -22435,8 +22455,8 @@ - - + + @@ -22444,46 +22464,46 @@ - + - + - + - - + + - - + + - + - + - - + + - + - + - + - + - + @@ -22519,23 +22539,23 @@ - - + + - + - + - + - + - - + + @@ -22545,8 +22565,8 @@ - - + + @@ -22554,16 +22574,16 @@ - - + + - + - + @@ -22581,7 +22601,7 @@ - + @@ -22590,7 +22610,7 @@ - + @@ -22613,7 +22633,7 @@ - + @@ -22631,7 +22651,7 @@ - + @@ -22682,23 +22702,23 @@ - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + @@ -22714,38 +22734,38 @@ - - + + - + - + - - + + - + - + - + - + - + @@ -22755,56 +22775,56 @@ - + - + - + - + - + - - - + + + - + - + - + - + - + @@ -22813,7 +22833,7 @@ - + @@ -22837,11 +22857,11 @@ - + - + @@ -22964,7 +22984,7 @@ - + @@ -23119,15 +23139,15 @@ - + - + - + @@ -23135,11 +23155,11 @@ - - - - - + + + + + @@ -23184,7 +23204,7 @@ - + @@ -23267,7 +23287,7 @@ - + @@ -23341,15 +23361,15 @@ - + - + - + @@ -23367,7 +23387,7 @@ - + @@ -23384,7 +23404,7 @@ - + @@ -23392,15 +23412,15 @@ - - - + - - - - - + + + + + + + @@ -23431,50 +23451,50 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -23486,7 +23506,7 @@ - + @@ -23659,8 +23679,8 @@ - - + + @@ -23672,8 +23692,8 @@ - - + + @@ -23698,7 +23718,7 @@ - + @@ -23711,7 +23731,7 @@ - + @@ -23720,7 +23740,7 @@ - + @@ -23728,19 +23748,14 @@ - + - - - - - - + @@ -23912,7 +23927,7 @@ - + @@ -23923,11 +23938,11 @@ - + - - + + @@ -23947,24 +23962,24 @@ - - - - - - - - - - - + + + + + + + + + + + - - + + - - + + @@ -24006,13 +24021,13 @@ - + - - + + @@ -24037,30 +24052,30 @@ - + - + - + - - + + - + - + @@ -24074,7 +24089,7 @@ - + @@ -24083,7 +24098,7 @@ - + @@ -24103,6 +24118,10 @@ + + + + @@ -24125,130 +24144,130 @@ - + - + - + - - + + - - + + - - + + - - + + - - + + - - + + - - - + + + - - - - + + + + - - - + + + - - - + + + - - + + - - - + + + - + - + - - - + + + - - + + - - + + - - - - + + + + - - - - + + + + - - + + - - - + + + - - - - + + + + - - - - - + + + + + - - + + - + @@ -24257,25 +24276,25 @@ - + - + - + - + - - - - - - - + + + + + + + @@ -24291,8 +24310,8 @@ - - + + @@ -24319,12 +24338,12 @@ - + - + @@ -24332,7 +24351,7 @@ - + @@ -24341,7 +24360,7 @@ - + @@ -24351,18 +24370,18 @@ - + - + - + - + @@ -24380,34 +24399,34 @@ - + - + - + - + - + - + - + - + - - - - + + + + @@ -24581,7 +24600,7 @@ - + @@ -24613,55 +24632,55 @@ - + - + - + - + - + - - - - + + + + - + - + - + - + - + - + - + - + - + @@ -24669,7 +24688,7 @@ - + @@ -24677,26 +24696,26 @@ - + - + - + - + - - + + - + @@ -24708,22 +24727,22 @@ - - - - - + + + + + - + - + - + @@ -24746,8 +24765,8 @@ - - + + @@ -24758,38 +24777,38 @@ - - + + - + - + - + - + - - - - + + + + - - - - - - + + + + - + - - + + + + @@ -24809,22 +24828,25 @@ - + + + + - + - + - - + + @@ -24934,17 +24956,17 @@ - + - - + + - + @@ -24966,7 +24988,7 @@ - + @@ -24982,6 +25004,10 @@ + + + + @@ -25167,7 +25193,7 @@ - + @@ -25259,7 +25285,7 @@ - + @@ -25307,15 +25333,12 @@ - - - - + @@ -25324,8 +25347,8 @@ - + @@ -25347,126 +25370,126 @@ - + - + - + - - + + - - - + + + - - - - + + + + - - - - + + + + - - + + - - + + - + - + - + - - - + + + - + - - + + - - - + + + - - + + - - - + + + - - + + - - + + - - - + + + - - - + + + - - - + + + - - - - + + + + - - - + + + - + - + - + @@ -25481,19 +25504,23 @@ - + - + - + + + + + @@ -25522,161 +25549,161 @@ - + - - + + - - + + - - + + - - + + - - + + - - - + + + - - + + - - + + - - + + - - + + - - + + - - + + - + - - + + - - + + - - + + - - - + + + - - - + + + - + - + - + - + - - - - - - + + + + + + - - - - - - + + + + + + - - - + + + - - + + - - - + + + - - + + - - + + - - + + - - - + + + - - + + - - + + - - + + - - - + + + - - + + @@ -25818,133 +25845,133 @@ - - - - - + + + + + - - - - + + + + - - - + + + - - - - - - - - + + + + + + + + - - - - - - + + + + + + - - - + + + - - - - + + + + - - - - + + + + - - - - - - + + + + + + - - - - - - - + + + + + + + - - - - - - - + + + + + + + - - - + + + - - - - + + + + - - - - - + + + + + - - + + - - - - + + + + - - - - - + + + + + - - - + + + - - - + + + - + @@ -25958,16 +25985,16 @@ - - + + - + - + @@ -26005,25 +26032,25 @@ - - - - - - - + + + + + + + - + - + - - + + - - + + @@ -26058,21 +26085,21 @@ - + - - + + - - + + @@ -26088,30 +26115,30 @@ - + - + - - + + - + - + - + - + @@ -26128,26 +26155,26 @@ - + - - + + - - + + - + @@ -26164,21 +26191,21 @@ - - + + - - + + - - + + @@ -26220,12 +26247,12 @@ - + - + @@ -26247,55 +26274,55 @@ - + - - + + - + - + - + - + - - + + - + - - - - - - - - - - - - + + + + + + + + + + - + + + - - - + + + @@ -26311,39 +26338,39 @@ - + - + - + - + - - + + - + - + - + @@ -26361,31 +26388,31 @@ - + - + - + - + - + - + @@ -26421,41 +26448,41 @@ - - - + + + - - - - - + + + + + - + - - + + - - + + - - - + + + - - + + - + - + @@ -26469,7 +26496,7 @@ - + diff --git a/Include/internal/pycore_interp.h b/Include/internal/pycore_interp.h index 37cc88ed081b72..a0ef5990259e29 100644 --- a/Include/internal/pycore_interp.h +++ b/Include/internal/pycore_interp.h @@ -178,7 +178,17 @@ struct _is { struct _warnings_runtime_state warnings; struct atexit_state atexit; - struct _obmalloc_state obmalloc; + // Per-interpreter state for the obmalloc allocator. For the main + // interpreter and for all interpreters that don't have their + // own obmalloc state, this points to the static structure in + // obmalloc.c obmalloc_state_main. For other interpreters, it is + // heap allocated by _PyMem_init_obmalloc() and freed when the + // interpreter structure is freed. In the case of a heap allocated + // obmalloc state, it is not safe to hold on to or use memory after + // the interpreter is freed. The obmalloc state corresponding to + // that allocated memory is gone. See free_obmalloc_arenas() for + // more comments. + struct _obmalloc_state *obmalloc; PyObject *audit_hooks; PyType_WatchCallback type_watchers[TYPE_MAX_WATCHERS]; diff --git a/Include/internal/pycore_obmalloc.h b/Include/internal/pycore_obmalloc.h index b1c00654ac1c5d..38427e194956ac 100644 --- a/Include/internal/pycore_obmalloc.h +++ b/Include/internal/pycore_obmalloc.h @@ -686,6 +686,8 @@ extern Py_ssize_t _Py_GetGlobalAllocatedBlocks(void); _Py_GetGlobalAllocatedBlocks() extern Py_ssize_t _PyInterpreterState_GetAllocatedBlocks(PyInterpreterState *); extern void _PyInterpreterState_FinalizeAllocatedBlocks(PyInterpreterState *); +extern int _PyMem_init_obmalloc(PyInterpreterState *interp); +extern bool _PyMem_obmalloc_state_on_heap(PyInterpreterState *interp); #ifdef WITH_PYMALLOC diff --git a/Include/internal/pycore_obmalloc_init.h b/Include/internal/pycore_obmalloc_init.h index 8ee72ff2d4126f..e6811b7aeca73c 100644 --- a/Include/internal/pycore_obmalloc_init.h +++ b/Include/internal/pycore_obmalloc_init.h @@ -59,13 +59,6 @@ extern "C" { .dump_debug_stats = -1, \ } -#define _obmalloc_state_INIT(obmalloc) \ - { \ - .pools = { \ - .used = _obmalloc_pools_INIT(obmalloc.pools), \ - }, \ - } - #ifdef __cplusplus } diff --git a/Include/internal/pycore_runtime_init.h b/Include/internal/pycore_runtime_init.h index e5f9e17efff24b..d3a64b3d4a7895 100644 --- a/Include/internal/pycore_runtime_init.h +++ b/Include/internal/pycore_runtime_init.h @@ -88,7 +88,6 @@ extern PyTypeObject _PyExc_MemoryError; { \ .id_refcount = -1, \ .imports = IMPORTS_INIT, \ - .obmalloc = _obmalloc_state_INIT(INTERP.obmalloc), \ .ceval = { \ .recursion_limit = Py_DEFAULT_RECURSION_LIMIT, \ }, \ diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-12-22-13-21-39.gh-issue-113055.47xBMF.rst b/Misc/NEWS.d/next/Core and Builtins/2023-12-22-13-21-39.gh-issue-113055.47xBMF.rst new file mode 100644 index 00000000000000..90f49272218c96 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-12-22-13-21-39.gh-issue-113055.47xBMF.rst @@ -0,0 +1,5 @@ +Make interp->obmalloc a pointer. For interpreters that share state with the +main interpreter, this points to the same static memory structure. For +interpreters with their own obmalloc state, it is heap allocated. Add +free_obmalloc_arenas() which will free the obmalloc arenas and radix tree +structures for interpreters with their own obmalloc state. diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c index 9620a8fbb44cac..acbefef614195c 100644 --- a/Objects/obmalloc.c +++ b/Objects/obmalloc.c @@ -3,6 +3,7 @@ #include "Python.h" #include "pycore_code.h" // stats #include "pycore_pystate.h" // _PyInterpreterState_GET +#include "pycore_obmalloc_init.h" #include "pycore_obmalloc.h" #include "pycore_pymem.h" @@ -852,6 +853,13 @@ static int running_on_valgrind = -1; typedef struct _obmalloc_state OMState; +/* obmalloc state for main interpreter and shared by all interpreters without + * their own obmalloc state. By not explicitly initalizing this structure, it + * will be allocated in the BSS which is a small performance win. The radix + * tree arrays are fairly large but are sparsely used. */ +static struct _obmalloc_state obmalloc_state_main; +static bool obmalloc_state_initialized; + static inline int has_own_state(PyInterpreterState *interp) { @@ -864,10 +872,8 @@ static inline OMState * get_state(void) { PyInterpreterState *interp = _PyInterpreterState_GET(); - if (!has_own_state(interp)) { - interp = _PyInterpreterState_Main(); - } - return &interp->obmalloc; + assert(interp->obmalloc != NULL); // otherwise not initialized or freed + return interp->obmalloc; } // These macros all rely on a local "state" variable. @@ -893,7 +899,11 @@ _PyInterpreterState_GetAllocatedBlocks(PyInterpreterState *interp) "the interpreter doesn't have its own allocator"); } #endif - OMState *state = &interp->obmalloc; + OMState *state = interp->obmalloc; + + if (state == NULL) { + return 0; + } Py_ssize_t n = raw_allocated_blocks; /* add up allocated blocks for used pools */ @@ -915,13 +925,25 @@ _PyInterpreterState_GetAllocatedBlocks(PyInterpreterState *interp) return n; } +static void free_obmalloc_arenas(PyInterpreterState *interp); + void _PyInterpreterState_FinalizeAllocatedBlocks(PyInterpreterState *interp) { - if (has_own_state(interp)) { + if (has_own_state(interp) && interp->obmalloc != NULL) { Py_ssize_t leaked = _PyInterpreterState_GetAllocatedBlocks(interp); assert(has_own_state(interp) || leaked == 0); interp->runtime->obmalloc.interpreter_leaks += leaked; + if (_PyMem_obmalloc_state_on_heap(interp) && leaked == 0) { + // free the obmalloc arenas and radix tree nodes. If leaked > 0 + // then some of the memory allocated by obmalloc has not been + // freed. It might be safe to free the arenas in that case but + // it's possible that extension modules are still using that + // memory. So, it is safer to not free and to leak. Perhaps there + // should be warning when this happens. It should be possible to + // use a tool like "-fsanitize=address" to track down these leaks. + free_obmalloc_arenas(interp); + } } } @@ -2511,9 +2533,96 @@ _PyDebugAllocatorStats(FILE *out, (void)printone(out, buf2, num_blocks * sizeof_block); } +// Return true if the obmalloc state structure is heap allocated, +// by PyMem_RawCalloc(). For the main interpreter, this structure +// allocated in the BSS. Allocating that way gives some memory savings +// and a small performance win (at least on a demand paged OS). On +// 64-bit platforms, the obmalloc structure is 256 kB. Most of that +// memory is for the arena_map_top array. Since normally only one entry +// of that array is used, only one page of resident memory is actually +// used, rather than the full 256 kB. +bool _PyMem_obmalloc_state_on_heap(PyInterpreterState *interp) +{ +#if WITH_PYMALLOC + return interp->obmalloc && interp->obmalloc != &obmalloc_state_main; +#else + return false; +#endif +} + +#ifdef WITH_PYMALLOC +static void +init_obmalloc_pools(PyInterpreterState *interp) +{ + // initialize the obmalloc->pools structure. This must be done + // before the obmalloc alloc/free functions can be called. + poolp temp[OBMALLOC_USED_POOLS_SIZE] = + _obmalloc_pools_INIT(interp->obmalloc->pools); + memcpy(&interp->obmalloc->pools.used, temp, sizeof(temp)); +} +#endif /* WITH_PYMALLOC */ + +int _PyMem_init_obmalloc(PyInterpreterState *interp) +{ +#ifdef WITH_PYMALLOC + /* Initialize obmalloc, but only for subinterpreters, + since the main interpreter is initialized statically. */ + if (_Py_IsMainInterpreter(interp) + || _PyInterpreterState_HasFeature(interp, + Py_RTFLAGS_USE_MAIN_OBMALLOC)) { + interp->obmalloc = &obmalloc_state_main; + if (!obmalloc_state_initialized) { + init_obmalloc_pools(interp); + obmalloc_state_initialized = true; + } + } else { + interp->obmalloc = PyMem_RawCalloc(1, sizeof(struct _obmalloc_state)); + if (interp->obmalloc == NULL) { + return -1; + } + init_obmalloc_pools(interp); + } +#endif /* WITH_PYMALLOC */ + return 0; // success +} + #ifdef WITH_PYMALLOC +static void +free_obmalloc_arenas(PyInterpreterState *interp) +{ + OMState *state = interp->obmalloc; + for (uint i = 0; i < maxarenas; ++i) { + // free each obmalloc memory arena + struct arena_object *ao = &allarenas[i]; + _PyObject_Arena.free(_PyObject_Arena.ctx, + (void *)ao->address, ARENA_SIZE); + } + // free the array containing pointers to all arenas + PyMem_RawFree(allarenas); +#if WITH_PYMALLOC_RADIX_TREE +#ifdef USE_INTERIOR_NODES + // Free the middle and bottom nodes of the radix tree. These are allocated + // by arena_map_mark_used() but not freed when arenas are freed. + for (int i1 = 0; i1 < MAP_TOP_LENGTH; i1++) { + arena_map_mid_t *mid = arena_map_root.ptrs[i1]; + if (mid == NULL) { + continue; + } + for (int i2 = 0; i2 < MAP_MID_LENGTH; i2++) { + arena_map_bot_t *bot = arena_map_root.ptrs[i1]->ptrs[i2]; + if (bot == NULL) { + continue; + } + PyMem_RawFree(bot); + } + PyMem_RawFree(mid); + } +#endif +#endif +} + #ifdef Py_DEBUG /* Is target in the list? The list is traversed via the nextpool pointers. * The list may be NULL-terminated, or circular. Return 1 if target is in diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index a0130fde15d574..31a24d4a65aebf 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -28,6 +28,7 @@ #include "pycore_typeobject.h" // _PyTypes_InitTypes() #include "pycore_typevarobject.h" // _Py_clear_generic_types() #include "pycore_unicodeobject.h" // _PyUnicode_InitTypes() +#include "pycore_obmalloc.h" // _PyMem_init_obmalloc() #include "opcode.h" #include // setlocale() @@ -650,6 +651,13 @@ pycore_create_interpreter(_PyRuntimeState *runtime, return status; } + // initialize the interp->obmalloc state. This must be done after + // the settings are loaded (so that feature_flags are set) but before + // any calls are made to obmalloc functions. + if (_PyMem_init_obmalloc(interp) < 0) { + return _PyStatus_NO_MEMORY(); + } + PyThreadState *tstate = _PyThreadState_New(interp); if (tstate == NULL) { return _PyStatus_ERR("can't make first thread"); @@ -2094,6 +2102,14 @@ new_interpreter(PyThreadState **tstate_p, const PyInterpreterConfig *config) goto error; } + // initialize the interp->obmalloc state. This must be done after + // the settings are loaded (so that feature_flags are set) but before + // any calls are made to obmalloc functions. + if (_PyMem_init_obmalloc(interp) < 0) { + status = _PyStatus_NO_MEMORY(); + goto error; + } + status = init_interp_create_gil(tstate, config->gil); if (_PyStatus_EXCEPTION(status)) { goto error; diff --git a/Python/pystate.c b/Python/pystate.c index 1337516aa59cbc..a25c3dcf9d09ea 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -14,6 +14,7 @@ #include "pycore_pystate.h" #include "pycore_runtime_init.h" // _PyRuntimeState_INIT #include "pycore_sysmodule.h" +#include "pycore_obmalloc.h" // _PyMem_obmalloc_state_on_heap() /* -------------------------------------------------------------------------- CAUTION @@ -636,6 +637,11 @@ free_interpreter(PyInterpreterState *interp) // The main interpreter is statically allocated so // should not be freed. if (interp != &_PyRuntime._main_interpreter) { + if (_PyMem_obmalloc_state_on_heap(interp)) { + // interpreter has its own obmalloc state, free it + PyMem_RawFree(interp->obmalloc); + interp->obmalloc = NULL; + } PyMem_RawFree(interp); } } @@ -679,13 +685,6 @@ init_interpreter(PyInterpreterState *interp, assert(next != NULL || (interp == runtime->interpreters.main)); interp->next = next; - /* Initialize obmalloc, but only for subinterpreters, - since the main interpreter is initialized statically. */ - if (interp != &runtime->_main_interpreter) { - poolp temp[OBMALLOC_USED_POOLS_SIZE] = \ - _obmalloc_pools_INIT(interp->obmalloc.pools); - memcpy(&interp->obmalloc.pools.used, temp, sizeof(temp)); - } _PyObject_InitState(interp); _PyEval_InitState(interp, pending_lock); diff --git a/Tools/c-analyzer/cpython/ignored.tsv b/Tools/c-analyzer/cpython/ignored.tsv index 9f36c47ca7ea03..7bcca27ecc32f6 100644 --- a/Tools/c-analyzer/cpython/ignored.tsv +++ b/Tools/c-analyzer/cpython/ignored.tsv @@ -318,7 +318,8 @@ Objects/obmalloc.c - _PyMem_Debug - Objects/obmalloc.c - _PyMem_Raw - Objects/obmalloc.c - _PyObject - Objects/obmalloc.c - last_final_leaks - -Objects/obmalloc.c - usedpools - +Objects/obmalloc.c - obmalloc_state_main - +Objects/obmalloc.c - obmalloc_state_initialized - Objects/typeobject.c - name_op - Objects/typeobject.c - slotdefs - Objects/unicodeobject.c - stripfuncnames -