@@ -279,12 +279,13 @@ hashtable_compare_pointer_t(const void *key1, const void *key2)
279279static _Py_hashtable_t *
280280hashtable_new (size_t data_size ,
281281 _Py_hashtable_hash_func hash_func ,
282- _Py_hashtable_compare_func compare_func )
282+ _Py_hashtable_compare_func compare_func ,
283+ _Py_hashtable_value_destroy_func value_destroy_fun )
283284{
284285 _Py_hashtable_allocator_t hashtable_alloc = {malloc , free };
285286 return _Py_hashtable_new_full (data_size , 0 ,
286287 hash_func , compare_func ,
287- & hashtable_alloc );
288+ NULL , value_destroy_fun , & hashtable_alloc );
288289}
289290
290291
@@ -519,59 +520,41 @@ static int
519520tracemalloc_use_domain_cb (_Py_hashtable_t * old_traces ,
520521 _Py_hashtable_entry_t * entry , void * user_data )
521522{
522- uintptr_t ptr ;
523- pointer_t key ;
524- _Py_hashtable_t * new_traces = (_Py_hashtable_t * )user_data ;
525- const void * pdata = _Py_HASHTABLE_ENTRY_PDATA (old_traces , entry );
523+ return hashtable_new (sizeof (trace_t ),
524+ _Py_hashtable_hash_ptr ,
525+ _Py_hashtable_compare_direct ,
526+ NULL );
527+ }
526528
527529 _Py_HASHTABLE_ENTRY_READ_KEY (old_traces , entry , ptr );
528530 key .ptr = ptr ;
529531 key .domain = DEFAULT_DOMAIN ;
530532
531- return _Py_hashtable_set (new_traces ,
532- sizeof (key ), & key ,
533- old_traces -> data_size , pdata );
533+ static void
534+ tracemalloc_destroy_domain_table (_Py_hashtable_t * domains ,
535+ _Py_hashtable_entry_t * entry )
536+ {
537+ _Py_hashtable_t * traces ;
538+ _Py_HASHTABLE_ENTRY_READ_DATA (domains , entry , traces );
539+ _Py_hashtable_destroy (traces );
534540}
535541
536542
537- /* Convert tracemalloc_traces from compact key (uintptr_t) to pointer_t key.
538- * Return 0 on success, -1 on error. */
539- static int
540- tracemalloc_use_domain (void )
543+ static _Py_hashtable_t *
544+ tracemalloc_create_domains_table (void )
541545{
542- _Py_hashtable_t * new_traces = NULL ;
543-
544- assert (!tracemalloc_config .use_domain );
545-
546- new_traces = hashtable_new (sizeof (pointer_t ),
547- sizeof (trace_t ),
548- hashtable_hash_pointer_t ,
549- hashtable_compare_pointer_t );
550- if (new_traces == NULL ) {
551- return -1 ;
552- }
553-
554- if (_Py_hashtable_foreach (tracemalloc_traces , tracemalloc_use_domain_cb ,
555- new_traces ) < 0 )
556- {
557- _Py_hashtable_destroy (new_traces );
558- return -1 ;
559- }
560-
561- _Py_hashtable_destroy (tracemalloc_traces );
562- tracemalloc_traces = new_traces ;
563-
564- tracemalloc_config .use_domain = 1 ;
565-
566- return 0 ;
546+ return hashtable_new (sizeof (_Py_hashtable_t * ),
547+ hashtable_hash_uint ,
548+ _Py_hashtable_compare_direct ,
549+ tracemalloc_destroy_domain_table );
567550}
568551
569552
570553static void
571554tracemalloc_remove_trace (_PyTraceMalloc_domain_t domain , uintptr_t ptr )
572555{
573- trace_t trace ;
574- int removed ;
556+ _Py_hashtable_destroy ( domains ) ;
557+ }
575558
576559 assert (tracemalloc_config .tracing );
577560
@@ -1002,11 +985,13 @@ tracemalloc_init(void)
1002985
1003986 tracemalloc_filenames = hashtable_new (0 ,
1004987 hashtable_hash_pyobject ,
1005- hashtable_compare_unicode );
988+ hashtable_compare_unicode ,
989+ NULL );
1006990
1007991 tracemalloc_tracebacks = hashtable_new (0 ,
1008992 hashtable_hash_traceback ,
1009- hashtable_compare_traceback );
993+ hashtable_compare_traceback ,
994+ NULL );
1010995
1011996 if (tracemalloc_config .use_domain ) {
1012997 tracemalloc_traces = hashtable_new (sizeof (pointer_t ),
@@ -1324,14 +1309,30 @@ tracemalloc_get_traces_fill(_Py_hashtable_t *traces, _Py_hashtable_entry_t *entr
13241309
13251310
13261311static int
1312+ tracemalloc_get_traces_domain (_Py_hashtable_t * domains ,
1313+ _Py_hashtable_entry_t * entry ,
1314+ void * user_data )
1315+ {
1316+ get_traces_t * get_traces = user_data ;
1317+
1318+ unsigned int domain = (unsigned int )FROM_PTR (entry -> key );
1319+ _Py_hashtable_t * traces ;
1320+ _Py_HASHTABLE_ENTRY_READ_DATA (domains , entry , traces );
1321+
1322+ get_traces -> domain = domain ;
1323+ return _Py_hashtable_foreach (traces ,
1324+ tracemalloc_get_traces_fill ,
1325+ get_traces );
1326+ }
1327+
1328+
1329+ static void
13271330tracemalloc_pyobject_decref_cb (_Py_hashtable_t * tracebacks ,
1328- _Py_hashtable_entry_t * entry ,
1329- void * user_data )
1331+ _Py_hashtable_entry_t * entry )
13301332{
13311333 PyObject * obj ;
13321334 _Py_HASHTABLE_ENTRY_READ_DATA (tracebacks , entry , obj );
13331335 Py_DECREF (obj );
1334- return 0 ;
13351336}
13361337
13371338
@@ -1364,7 +1365,8 @@ py_tracemalloc_get_traces(PyObject *self, PyObject *obj)
13641365 get_traces .tracebacks = hashtable_new (sizeof (traceback_t * ),
13651366 sizeof (PyObject * ),
13661367 _Py_hashtable_hash_ptr ,
1367- _Py_hashtable_compare_direct );
1368+ _Py_hashtable_compare_direct ,
1369+ tracemalloc_pyobject_decref_cb );
13681370 if (get_traces .tracebacks == NULL ) {
13691371 PyErr_NoMemory ();
13701372 goto error ;
@@ -1393,8 +1395,6 @@ py_tracemalloc_get_traces(PyObject *self, PyObject *obj)
13931395
13941396finally :
13951397 if (get_traces .tracebacks != NULL ) {
1396- _Py_hashtable_foreach (get_traces .tracebacks ,
1397- tracemalloc_pyobject_decref_cb , NULL );
13981398 _Py_hashtable_destroy (get_traces .tracebacks );
13991399 }
14001400 if (get_traces .traces != NULL ) {
0 commit comments