9
9
#include "pycore_pyerrors.h" // _PyErr_SetString()
10
10
#include "pycore_pyhash.h" // _Py_KeyedHash()
11
11
#include "pycore_pylifecycle.h"
12
- #include "pycore_pymem.h" // _PyMem_SetDefaultAllocator ()
12
+ #include "pycore_pymem.h" // _PyMem_DefaultRawFree ()
13
13
#include "pycore_pystate.h" // _PyInterpreterState_GET()
14
14
#include "pycore_sysmodule.h" // _PySys_Audit()
15
15
#include "marshal.h" // PyMarshal_ReadObjectFromString()
@@ -228,30 +228,23 @@ _PyImport_Init(void)
228
228
if (_PyRuntime .imports .inittab != NULL ) {
229
229
return _PyStatus_ERR ("global import state already initialized" );
230
230
}
231
- PyStatus status = _PyStatus_OK ();
232
231
233
232
size_t size ;
234
233
for (size = 0 ; PyImport_Inittab [size ].name != NULL ; size ++ )
235
234
;
236
235
size ++ ;
237
236
238
- /* Force default raw memory allocator to get a known allocator to be able
237
+ /* Use default raw memory allocator to get a known allocator to be able
239
238
to release the memory in _PyImport_Fini() */
240
- PyMemAllocatorEx old_alloc ;
241
- _PyMem_SetDefaultAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
242
239
243
240
/* Make the copy. */
244
- struct _inittab * copied = PyMem_RawMalloc (size * sizeof (struct _inittab ));
241
+ struct _inittab * copied = _PyMem_DefaultRawMalloc (size * sizeof (struct _inittab ));
245
242
if (copied == NULL ) {
246
- status = PyStatus_NoMemory ();
247
- goto done ;
243
+ return PyStatus_NoMemory ();
248
244
}
249
245
memcpy (copied , PyImport_Inittab , size * sizeof (struct _inittab ));
250
246
_PyRuntime .imports .inittab = copied ;
251
-
252
- done :
253
- PyMem_SetAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
254
- return status ;
247
+ return _PyStatus_OK ();
255
248
}
256
249
257
250
static inline void _extensions_cache_clear (void );
@@ -266,32 +259,22 @@ _PyImport_Fini(void)
266
259
}
267
260
268
261
/* Use the same memory allocator as _PyImport_Init(). */
269
- PyMemAllocatorEx old_alloc ;
270
- _PyMem_SetDefaultAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
271
-
272
262
/* Free memory allocated by _PyImport_Init() */
273
263
struct _inittab * inittab = _PyRuntime .imports .inittab ;
274
264
_PyRuntime .imports .inittab = NULL ;
275
- PyMem_RawFree (inittab );
276
-
277
- PyMem_SetAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
265
+ _PyMem_DefaultRawFree (inittab );
278
266
}
279
267
280
268
void
281
269
_PyImport_Fini2 (void )
282
270
{
283
271
/* Use the same memory allocator than PyImport_ExtendInittab(). */
284
- PyMemAllocatorEx old_alloc ;
285
- _PyMem_SetDefaultAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
286
-
287
272
// Reset PyImport_Inittab
288
273
PyImport_Inittab = _PyImport_Inittab ;
289
274
290
275
/* Free memory allocated by PyImport_ExtendInittab() */
291
- PyMem_RawFree (inittab_copy );
276
+ _PyMem_DefaultRawFree (inittab_copy );
292
277
inittab_copy = NULL ;
293
-
294
- PyMem_SetAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
295
278
}
296
279
297
280
/* Helper for sys */
@@ -2657,14 +2640,11 @@ PyImport_ExtendInittab(struct _inittab *newtab)
2657
2640
2658
2641
/* Force default raw memory allocator to get a known allocator to be able
2659
2642
to release the memory in _PyImport_Fini2() */
2660
- PyMemAllocatorEx old_alloc ;
2661
- _PyMem_SetDefaultAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
2662
-
2663
2643
/* Allocate new memory for the combined table */
2664
2644
p = NULL ;
2665
2645
if (i + n <= SIZE_MAX / sizeof (struct _inittab ) - 1 ) {
2666
2646
size_t size = sizeof (struct _inittab ) * (i + n + 1 );
2667
- p = PyMem_RawRealloc (inittab_copy , size );
2647
+ p = _PyMem_DefaultRawRealloc (inittab_copy , size );
2668
2648
}
2669
2649
if (p == NULL ) {
2670
2650
res = -1 ;
@@ -2678,9 +2658,7 @@ PyImport_ExtendInittab(struct _inittab *newtab)
2678
2658
}
2679
2659
memcpy (p + i , newtab , (n + 1 ) * sizeof (struct _inittab ));
2680
2660
PyImport_Inittab = inittab_copy = p ;
2681
-
2682
2661
done :
2683
- PyMem_SetAllocator (PYMEM_DOMAIN_RAW , & old_alloc );
2684
2662
return res ;
2685
2663
}
2686
2664
0 commit comments