Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 25f3dc2

Browse files
committed
Drop the PyCore_* memory API.
1 parent 08de92a commit 25f3dc2

4 files changed

Lines changed: 71 additions & 92 deletions

File tree

Include/objimpl.h

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -56,43 +56,6 @@ form of memory management you're using).
5656
Unless you have specific memory management requirements, it is
5757
recommended to use PyObject_{New, NewVar, Del}. */
5858

59-
/*
60-
* Core object memory allocator
61-
* ============================
62-
*/
63-
64-
/* The purpose of the object allocator is to make the distinction
65-
between "object memory" and the rest within the Python heap.
66-
67-
Object memory is the one allocated by PyObject_{New, NewVar}, i.e.
68-
the one that holds the object's representation defined by its C
69-
type structure, *excluding* any object-specific memory buffers that
70-
might be referenced by the structure (for type structures that have
71-
pointer fields). By default, the object memory allocator is
72-
implemented on top of the raw memory allocator.
73-
74-
The PyCore_* macros can be defined to make the interpreter use a
75-
custom object memory allocator. They are reserved for internal
76-
memory management purposes exclusively. Both the core and extension
77-
modules should use the PyObject_* API. */
78-
79-
#ifdef WITH_PYMALLOC
80-
void *_PyCore_ObjectMalloc(size_t nbytes);
81-
void *_PyCore_ObjectRealloc(void *p, size_t nbytes);
82-
void _PyCore_ObjectFree(void *p);
83-
#define PyCore_OBJECT_MALLOC _PyCore_ObjectMalloc
84-
#define PyCore_OBJECT_REALLOC _PyCore_ObjectRealloc
85-
#define PyCore_OBJECT_FREE _PyCore_ObjectFree
86-
#endif /* !WITH_PYMALLOC */
87-
88-
#ifndef PyCore_OBJECT_MALLOC
89-
#undef PyCore_OBJECT_REALLOC
90-
#undef PyCore_OBJECT_FREE
91-
#define PyCore_OBJECT_MALLOC(n) PyCore_MALLOC(n)
92-
#define PyCore_OBJECT_REALLOC(p, n) PyCore_REALLOC((p), (n))
93-
#define PyCore_OBJECT_FREE(p) PyCore_FREE(p)
94-
#endif
95-
9659
/*
9760
* Raw object memory interface
9861
* ===========================
@@ -111,19 +74,19 @@ void _PyCore_ObjectFree(void *p);
11174

11275
/* Functions */
11376

114-
/* Wrappers around PyCore_OBJECT_MALLOC and friends; useful if you
115-
need to be sure that you are using the same object memory allocator
116-
as Python. These wrappers *do not* make sure that allocating 0
117-
bytes returns a non-NULL pointer. Returned pointers must be checked
118-
for NULL explicitly; no action is performed on failure. */
77+
/* Wrappers that useful if you need to be sure that you are using the
78+
same object memory allocator as Python. These wrappers *do not* make
79+
sure that allocating 0 bytes returns a non-NULL pointer. Returned
80+
pointers must be checked for NULL explicitly; no action is performed
81+
on failure. */
11982
extern DL_IMPORT(void *) PyObject_Malloc(size_t);
12083
extern DL_IMPORT(void *) PyObject_Realloc(void *, size_t);
12184
extern DL_IMPORT(void) PyObject_Free(void *);
12285

12386
/* Macros */
124-
#define PyObject_MALLOC(n) PyCore_OBJECT_MALLOC(n)
125-
#define PyObject_REALLOC(op, n) PyCore_OBJECT_REALLOC((void *)(op), (n))
126-
#define PyObject_FREE(op) PyCore_OBJECT_FREE((void *)(op))
87+
#define PyObject_MALLOC(n) _PyMalloc_MALLOC(n)
88+
#define PyObject_REALLOC(op, n) _PyMalloc_REALLOC((void *)(op), (n))
89+
#define PyObject_FREE(op) _PyMalloc_FREE((void *)(op))
12790

12891
/*
12992
* Generic object allocator interface

Include/pymem.h

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,6 @@
1010
extern "C" {
1111
#endif
1212

13-
/*
14-
* Core memory allocator
15-
* =====================
16-
*/
17-
18-
/* To make sure the interpreter is user-malloc friendly, all memory
19-
APIs are implemented on top of this one.
20-
21-
The PyCore_* macros can be defined to make the interpreter use a
22-
custom allocator. Note that they are for internal use only. Both
23-
the core and extension modules should use the PyMem_* API. */
24-
25-
#ifndef PyCore_MALLOC
26-
#undef PyCore_REALLOC
27-
#undef PyCore_FREE
28-
#define PyCore_MALLOC(n) malloc(n)
29-
#define PyCore_REALLOC(p, n) realloc((p), (n))
30-
#define PyCore_FREE(p) free(p)
31-
#endif
32-
3313
/* BEWARE:
3414
3515
Each interface exports both functions and macros. Extension modules
@@ -51,9 +31,12 @@ extern "C" {
5131
* ====================
5232
*/
5333

34+
/* To make sure the interpreter is user-malloc friendly, all memory
35+
APIs are implemented on top of this one. */
36+
5437
/* Functions */
5538

56-
/* Function wrappers around PyCore_MALLOC and friends; useful if you
39+
/* Function wrappers around PyMem_MALLOC and friends; useful if you
5740
need to be sure that you are using the same memory allocator as
5841
Python. Note that the wrappers make sure that allocating 0 bytes
5942
returns a non-NULL pointer, even if the underlying malloc
@@ -66,10 +49,12 @@ extern DL_IMPORT(void) PyMem_Free(void *);
6649
/* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are
6750
no longer supported. They used to call PyErr_NoMemory() on failure. */
6851

69-
/* Macros */
70-
#define PyMem_MALLOC(n) PyCore_MALLOC(n)
71-
#define PyMem_REALLOC(p, n) PyCore_REALLOC((void *)(p), (n))
72-
#define PyMem_FREE(p) PyCore_FREE((void *)(p))
52+
/* Macros (override these if you want to a different malloc */
53+
#ifndef PyMem_MALLOC
54+
#define PyMem_MALLOC(n) malloc(n)
55+
#define PyMem_REALLOC(p, n) realloc((void *)(p), (n))
56+
#define PyMem_FREE(p) free((void *)(p))
57+
#endif
7358

7459
/*
7560
* Type-oriented memory interface
@@ -104,6 +89,22 @@ extern DL_IMPORT(void) PyMem_Free(void *);
10489
it is recommended to write the test explicitly in the code.
10590
Note that according to ANSI C, free(NULL) has no effect. */
10691

92+
93+
/* pymalloc (private to the interpreter) */
94+
#ifdef WITH_PYMALLOC
95+
void *_PyMalloc_Malloc(size_t nbytes);
96+
void *_PyMalloc_Realloc(void *p, size_t nbytes);
97+
void _PyMalloc_Free(void *p);
98+
#define _PyMalloc_MALLOC _PyMalloc_Malloc
99+
#define _PyMalloc_REALLOC _PyMalloc_Realloc
100+
#define _PyMalloc_FREE _PyMalloc_Free
101+
#else
102+
#define _PyMalloc_MALLOC PyMem_MALLOC
103+
#define _PyMalloc_REALLOC PyMem_REALLOC
104+
#define _PyMalloc_FREE PyMem_FREE
105+
#endif
106+
107+
107108
#ifdef __cplusplus
108109
}
109110
#endif

Objects/object.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2093,4 +2093,19 @@ _PyTrash_destroy_chain(void)
20932093

20942094
#ifdef WITH_PYMALLOC
20952095
#include "obmalloc.c"
2096-
#endif
2096+
#else
2097+
void *_PyMalloc_Malloc(size_t n)
2098+
{
2099+
return PyMem_MALLOC(n);
2100+
}
2101+
2102+
void *_PyMalloc_Realloc(void *p, size_t n)
2103+
{
2104+
return PyMem_REALLOC(p, n);
2105+
}
2106+
2107+
void _PyMalloc_Free(void *p)
2108+
{
2109+
PyMem_FREE(p);
2110+
}
2111+
#endif /* !WITH_PYMALLOC */

Objects/obmalloc.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ static void (*free_hook)(void *) = NULL;
349349
*/
350350

351351
void *
352-
_PyCore_ObjectMalloc(size_t nbytes)
352+
_PyMalloc_Malloc(size_t nbytes)
353353
{
354354
block *bp;
355355
poolp pool;
@@ -479,7 +479,7 @@ _PyCore_ObjectMalloc(size_t nbytes)
479479
* With malloc, we can't avoid loosing one page address space
480480
* per arena due to the required alignment on page boundaries.
481481
*/
482-
bp = (block *)PyCore_MALLOC(ARENA_SIZE + SYSTEM_PAGE_SIZE);
482+
bp = (block *)PyMem_MALLOC(ARENA_SIZE + SYSTEM_PAGE_SIZE);
483483
if (bp == NULL) {
484484
UNLOCK();
485485
goto redirect;
@@ -510,13 +510,13 @@ _PyCore_ObjectMalloc(size_t nbytes)
510510
* last chance to serve the request) or when the max memory limit
511511
* has been reached.
512512
*/
513-
return (void *)PyCore_MALLOC(nbytes);
513+
return (void *)PyMem_MALLOC(nbytes);
514514
}
515515

516516
/* free */
517517

518518
void
519-
_PyCore_ObjectFree(void *p)
519+
_PyMalloc_Free(void *p)
520520
{
521521
poolp pool;
522522
poolp next, prev;
@@ -536,7 +536,7 @@ _PyCore_ObjectFree(void *p)
536536
offset = (off_t )p & POOL_SIZE_MASK;
537537
pool = (poolp )((block *)p - offset);
538538
if (pool->pooladdr != pool || pool->magic != (uint )POOL_MAGIC) {
539-
PyCore_FREE(p);
539+
PyMem_FREE(p);
540540
return;
541541
}
542542

@@ -595,7 +595,7 @@ _PyCore_ObjectFree(void *p)
595595
/* realloc */
596596

597597
void *
598-
_PyCore_ObjectRealloc(void *p, size_t nbytes)
598+
_PyMalloc_Realloc(void *p, size_t nbytes)
599599
{
600600
block *bp;
601601
poolp pool;
@@ -607,7 +607,7 @@ _PyCore_ObjectRealloc(void *p, size_t nbytes)
607607
#endif
608608

609609
if (p == NULL)
610-
return _PyCore_ObjectMalloc(nbytes);
610+
return _PyMalloc_Malloc(nbytes);
611611

612612
/* realloc(p, 0) on big blocks is redirected. */
613613
pool = (poolp )((block *)p - ((off_t )p & POOL_SIZE_MASK));
@@ -618,7 +618,7 @@ _PyCore_ObjectRealloc(void *p, size_t nbytes)
618618
size = nbytes;
619619
goto malloc_copy_free;
620620
}
621-
bp = (block *)PyCore_REALLOC(p, nbytes);
621+
bp = (block *)PyMem_REALLOC(p, nbytes);
622622
}
623623
else {
624624
/* We're in charge of this block */
@@ -627,7 +627,7 @@ _PyCore_ObjectRealloc(void *p, size_t nbytes)
627627
/* Don't bother if a smaller size was requested
628628
except for realloc(p, 0) == free(p), ret NULL */
629629
if (nbytes == 0) {
630-
_PyCore_ObjectFree(p);
630+
_PyMalloc_Free(p);
631631
bp = NULL;
632632
}
633633
else
@@ -637,10 +637,10 @@ _PyCore_ObjectRealloc(void *p, size_t nbytes)
637637

638638
malloc_copy_free:
639639

640-
bp = (block *)_PyCore_ObjectMalloc(nbytes);
640+
bp = (block *)_PyMalloc_Malloc(nbytes);
641641
if (bp != NULL) {
642642
memcpy(bp, p, size);
643-
_PyCore_ObjectFree(p);
643+
_PyMalloc_Free(p);
644644
}
645645
}
646646
}
@@ -651,7 +651,7 @@ _PyCore_ObjectRealloc(void *p, size_t nbytes)
651651

652652
/* -- unused --
653653
void *
654-
_PyCore_ObjectCalloc(size_t nbel, size_t elsz)
654+
_PyMalloc_Calloc(size_t nbel, size_t elsz)
655655
{
656656
void *p;
657657
size_t nbytes;
@@ -662,7 +662,7 @@ _PyCore_ObjectCalloc(size_t nbel, size_t elsz)
662662
#endif
663663
664664
nbytes = nbel * elsz;
665-
p = _PyCore_ObjectMalloc(nbytes);
665+
p = _PyMalloc_Malloc(nbytes);
666666
if (p != NULL)
667667
memset(p, 0, nbytes);
668668
return p;
@@ -678,10 +678,10 @@ _PyCore_ObjectCalloc(size_t nbel, size_t elsz)
678678
#ifdef WITH_MALLOC_HOOKS
679679

680680
void
681-
_PyCore_ObjectMalloc_SetHooks( void *(*malloc_func)(size_t),
682-
void *(*calloc_func)(size_t, size_t),
683-
void *(*realloc_func)(void *, size_t),
684-
void (*free_func)(void *) )
681+
_PyMalloc_SetHooks( void *(*malloc_func)(size_t),
682+
void *(*calloc_func)(size_t, size_t),
683+
void *(*realloc_func)(void *, size_t),
684+
void (*free_func)(void *) )
685685
{
686686
LOCK();
687687
malloc_hook = malloc_func;
@@ -692,10 +692,10 @@ _PyCore_ObjectMalloc_SetHooks( void *(*malloc_func)(size_t),
692692
}
693693

694694
void
695-
_PyCore_ObjectMalloc_FetchHooks( void *(**malloc_funcp)(size_t),
696-
void *(**calloc_funcp)(size_t, size_t),
697-
void *(**realloc_funcp)(void *, size_t),
698-
void (**free_funcp)(void *) )
695+
_PyMalloc_FetchHooks( void *(**malloc_funcp)(size_t),
696+
void *(**calloc_funcp)(size_t, size_t),
697+
void *(**realloc_funcp)(void *, size_t),
698+
void (**free_funcp)(void *) )
699699
{
700700
LOCK();
701701
*malloc_funcp = malloc_hook;

0 commit comments

Comments
 (0)