Skip to content

Commit 11f5be8

Browse files
committed
Simpilify PyCore_* macros by assuming the function prototypes for
malloc() and free() don't change.
1 parent f6eafc3 commit 11f5be8

File tree

3 files changed

+34
-154
lines changed

3 files changed

+34
-154
lines changed

Include/objimpl.h

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -77,40 +77,20 @@ recommended to use PyObject_{New, NewVar, Del}. */
7777
modules should use the PyObject_* API. */
7878

7979
#ifdef WITH_PYMALLOC
80-
#define PyCore_OBJECT_MALLOC_FUNC _PyCore_ObjectMalloc
81-
#define PyCore_OBJECT_REALLOC_FUNC _PyCore_ObjectRealloc
82-
#define PyCore_OBJECT_FREE_FUNC _PyCore_ObjectFree
83-
#define NEED_TO_DECLARE_OBJECT_MALLOC_AND_FRIEND
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
8486
#endif /* !WITH_PYMALLOC */
8587

86-
#ifndef PyCore_OBJECT_MALLOC_FUNC
87-
#undef PyCore_OBJECT_REALLOC_FUNC
88-
#undef PyCore_OBJECT_FREE_FUNC
89-
#define PyCore_OBJECT_MALLOC_FUNC PyCore_MALLOC_FUNC
90-
#define PyCore_OBJECT_REALLOC_FUNC PyCore_REALLOC_FUNC
91-
#define PyCore_OBJECT_FREE_FUNC PyCore_FREE_FUNC
92-
#endif
93-
94-
#ifndef PyCore_OBJECT_MALLOC_PROTO
95-
#undef PyCore_OBJECT_REALLOC_PROTO
96-
#undef PyCore_OBJECT_FREE_PROTO
97-
#define PyCore_OBJECT_MALLOC_PROTO PyCore_MALLOC_PROTO
98-
#define PyCore_OBJECT_REALLOC_PROTO PyCore_REALLOC_PROTO
99-
#define PyCore_OBJECT_FREE_PROTO PyCore_FREE_PROTO
100-
#endif
101-
102-
#ifdef NEED_TO_DECLARE_OBJECT_MALLOC_AND_FRIEND
103-
extern void *PyCore_OBJECT_MALLOC_FUNC PyCore_OBJECT_MALLOC_PROTO;
104-
extern void *PyCore_OBJECT_REALLOC_FUNC PyCore_OBJECT_REALLOC_PROTO;
105-
extern void PyCore_OBJECT_FREE_FUNC PyCore_OBJECT_FREE_PROTO;
106-
#endif
107-
10888
#ifndef PyCore_OBJECT_MALLOC
10989
#undef PyCore_OBJECT_REALLOC
11090
#undef PyCore_OBJECT_FREE
111-
#define PyCore_OBJECT_MALLOC(n) PyCore_OBJECT_MALLOC_FUNC(n)
112-
#define PyCore_OBJECT_REALLOC(p, n) PyCore_OBJECT_REALLOC_FUNC((p), (n))
113-
#define PyCore_OBJECT_FREE(p) PyCore_OBJECT_FREE_FUNC(p)
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)
11494
#endif
11595

11696
/*

Include/pymem.h

Lines changed: 4 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,14 @@ extern "C" {
2020
2121
The PyCore_* macros can be defined to make the interpreter use a
2222
custom allocator. Note that they are for internal use only. Both
23-
the core and extension modules should use the PyMem_* API.
24-
25-
See the comment block at the end of this file for two scenarios
26-
showing how to use this to use a different allocator. */
27-
28-
#ifndef PyCore_MALLOC_FUNC
29-
#undef PyCore_REALLOC_FUNC
30-
#undef PyCore_FREE_FUNC
31-
#define PyCore_MALLOC_FUNC malloc
32-
#define PyCore_REALLOC_FUNC realloc
33-
#define PyCore_FREE_FUNC free
34-
#endif
35-
36-
#ifndef PyCore_MALLOC_PROTO
37-
#undef PyCore_REALLOC_PROTO
38-
#undef PyCore_FREE_PROTO
39-
#define PyCore_MALLOC_PROTO (size_t)
40-
#define PyCore_REALLOC_PROTO (void *, size_t)
41-
#define PyCore_FREE_PROTO (void *)
42-
#endif
43-
44-
#ifdef NEED_TO_DECLARE_MALLOC_AND_FRIEND
45-
extern void *PyCore_MALLOC_FUNC PyCore_MALLOC_PROTO;
46-
extern void *PyCore_REALLOC_FUNC PyCore_REALLOC_PROTO;
47-
extern void PyCore_FREE_FUNC PyCore_FREE_PROTO;
48-
#endif
23+
the core and extension modules should use the PyMem_* API. */
4924

5025
#ifndef PyCore_MALLOC
5126
#undef PyCore_REALLOC
5227
#undef PyCore_FREE
53-
#define PyCore_MALLOC(n) PyCore_MALLOC_FUNC(n)
54-
#define PyCore_REALLOC(p, n) PyCore_REALLOC_FUNC((p), (n))
55-
#define PyCore_FREE(p) PyCore_FREE_FUNC(p)
28+
#define PyCore_MALLOC(n) malloc(n)
29+
#define PyCore_REALLOC(p, n) realloc((p), (n))
30+
#define PyCore_FREE(p) free(p)
5631
#endif
5732

5833
/* BEWARE:
@@ -133,43 +108,4 @@ extern DL_IMPORT(void) PyMem_Free(void *);
133108
}
134109
#endif
135110

136-
/* SCENARIOS
137-
138-
Here are two scenarios by Vladimir Marangozov (the author of the
139-
memory allocation redesign).
140-
141-
1) Scenario A
142-
143-
Suppose you want to use a debugging malloc library that collects info on
144-
where the malloc calls originate from. Assume the interface is:
145-
146-
d_malloc(size_t n, char* src_file, unsigned long src_line) c.s.
147-
148-
In this case, you would define (for example in pyconfig.h) :
149-
150-
#define PyCore_MALLOC_FUNC d_malloc
151-
...
152-
#define PyCore_MALLOC_PROTO (size_t, char *, unsigned long)
153-
...
154-
#define NEED_TO_DECLARE_MALLOC_AND_FRIEND
155-
156-
#define PyCore_MALLOC(n) PyCore_MALLOC_FUNC((n), __FILE__, __LINE__)
157-
...
158-
159-
2) Scenario B
160-
161-
Suppose you want to use malloc hooks (defined & initialized in a 3rd party
162-
malloc library) instead of malloc functions. In this case, you would
163-
define:
164-
165-
#define PyCore_MALLOC_FUNC (*malloc_hook)
166-
...
167-
#define NEED_TO_DECLARE_MALLOC_AND_FRIEND
168-
169-
and ignore the previous definitions about PyCore_MALLOC_FUNC, etc.
170-
171-
172-
*/
173-
174-
175111
#endif /* !Py_PYMEM_H */

Objects/obmalloc.c

Lines changed: 21 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -53,42 +53,6 @@
5353

5454
/*==========================================================================*/
5555

56-
/*
57-
* Public functions exported by this allocator.
58-
*
59-
* -- Define and use these names in your code to obtain or release memory --
60-
*/
61-
#define _THIS_MALLOC PyCore_OBJECT_MALLOC_FUNC
62-
#define _THIS_CALLOC /* unused */
63-
#define _THIS_REALLOC PyCore_OBJECT_REALLOC_FUNC
64-
#define _THIS_FREE PyCore_OBJECT_FREE_FUNC
65-
66-
/*
67-
* Underlying allocator's functions called by this allocator.
68-
* The underlying allocator is usually the one which comes with libc.
69-
*
70-
* -- Don't use these functions in your code (to avoid mixing allocators) --
71-
*
72-
* Redefine these __only__ if you are using a 3rd party general purpose
73-
* allocator which exports functions with names _other_ than the standard
74-
* malloc, calloc, realloc, free.
75-
*/
76-
#define _SYSTEM_MALLOC PyCore_MALLOC_FUNC
77-
#define _SYSTEM_CALLOC /* unused */
78-
#define _SYSTEM_REALLOC PyCore_REALLOC_FUNC
79-
#define _SYSTEM_FREE PyCore_FREE_FUNC
80-
81-
/*
82-
* If malloc hooks are needed, names of the hooks' set & fetch
83-
* functions exported by this allocator.
84-
*/
85-
#ifdef WITH_MALLOC_HOOKS
86-
#define _SET_HOOKS _PyCore_ObjectMalloc_SetHooks
87-
#define _FETCH_HOOKS _PyCore_ObjectMalloc_FetchHooks
88-
#endif
89-
90-
/*==========================================================================*/
91-
9256
/*
9357
* Allocation strategy abstract:
9458
*
@@ -385,7 +349,7 @@ static void (*free_hook)(void *) = NULL;
385349
*/
386350

387351
void *
388-
_THIS_MALLOC(size_t nbytes)
352+
_PyCore_ObjectMalloc(size_t nbytes)
389353
{
390354
block *bp;
391355
poolp pool;
@@ -515,7 +479,7 @@ _THIS_MALLOC(size_t nbytes)
515479
* With malloc, we can't avoid loosing one page address space
516480
* per arena due to the required alignment on page boundaries.
517481
*/
518-
bp = (block *)_SYSTEM_MALLOC(ARENA_SIZE + SYSTEM_PAGE_SIZE);
482+
bp = (block *)PyCore_MALLOC(ARENA_SIZE + SYSTEM_PAGE_SIZE);
519483
if (bp == NULL) {
520484
UNLOCK();
521485
goto redirect;
@@ -546,13 +510,13 @@ _THIS_MALLOC(size_t nbytes)
546510
* last chance to serve the request) or when the max memory limit
547511
* has been reached.
548512
*/
549-
return (void *)_SYSTEM_MALLOC(nbytes);
513+
return (void *)PyCore_MALLOC(nbytes);
550514
}
551515

552516
/* free */
553517

554518
void
555-
_THIS_FREE(void *p)
519+
_PyCore_ObjectFree(void *p)
556520
{
557521
poolp pool;
558522
poolp next, prev;
@@ -572,7 +536,7 @@ _THIS_FREE(void *p)
572536
offset = (off_t )p & POOL_SIZE_MASK;
573537
pool = (poolp )((block *)p - offset);
574538
if (pool->pooladdr != pool || pool->magic != (uint )POOL_MAGIC) {
575-
_SYSTEM_FREE(p);
539+
PyCore_FREE(p);
576540
return;
577541
}
578542

@@ -631,7 +595,7 @@ _THIS_FREE(void *p)
631595
/* realloc */
632596

633597
void *
634-
_THIS_REALLOC(void *p, size_t nbytes)
598+
_PyCore_ObjectRealloc(void *p, size_t nbytes)
635599
{
636600
block *bp;
637601
poolp pool;
@@ -643,7 +607,7 @@ _THIS_REALLOC(void *p, size_t nbytes)
643607
#endif
644608

645609
if (p == NULL)
646-
return _THIS_MALLOC(nbytes);
610+
return _PyCore_ObjectMalloc(nbytes);
647611

648612
/* realloc(p, 0) on big blocks is redirected. */
649613
pool = (poolp )((block *)p - ((off_t )p & POOL_SIZE_MASK));
@@ -654,7 +618,7 @@ _THIS_REALLOC(void *p, size_t nbytes)
654618
size = nbytes;
655619
goto malloc_copy_free;
656620
}
657-
bp = (block *)_SYSTEM_REALLOC(p, nbytes);
621+
bp = (block *)PyCore_REALLOC(p, nbytes);
658622
}
659623
else {
660624
/* We're in charge of this block */
@@ -663,7 +627,7 @@ _THIS_REALLOC(void *p, size_t nbytes)
663627
/* Don't bother if a smaller size was requested
664628
except for realloc(p, 0) == free(p), ret NULL */
665629
if (nbytes == 0) {
666-
_THIS_FREE(p);
630+
_PyCore_ObjectFree(p);
667631
bp = NULL;
668632
}
669633
else
@@ -673,10 +637,10 @@ _THIS_REALLOC(void *p, size_t nbytes)
673637

674638
malloc_copy_free:
675639

676-
bp = (block *)_THIS_MALLOC(nbytes);
640+
bp = (block *)_PyCore_ObjectMalloc(nbytes);
677641
if (bp != NULL) {
678642
memcpy(bp, p, size);
679-
_THIS_FREE(p);
643+
_PyCore_ObjectFree(p);
680644
}
681645
}
682646
}
@@ -687,7 +651,7 @@ _THIS_REALLOC(void *p, size_t nbytes)
687651

688652
/* -- unused --
689653
void *
690-
_THIS_CALLOC(size_t nbel, size_t elsz)
654+
_PyCore_ObjectCalloc(size_t nbel, size_t elsz)
691655
{
692656
void *p;
693657
size_t nbytes;
@@ -698,7 +662,7 @@ _THIS_CALLOC(size_t nbel, size_t elsz)
698662
#endif
699663
700664
nbytes = nbel * elsz;
701-
p = _THIS_MALLOC(nbytes);
665+
p = _PyCore_ObjectMalloc(nbytes);
702666
if (p != NULL)
703667
memset(p, 0, nbytes);
704668
return p;
@@ -714,10 +678,10 @@ _THIS_CALLOC(size_t nbel, size_t elsz)
714678
#ifdef WITH_MALLOC_HOOKS
715679

716680
void
717-
_SET_HOOKS( void *(*malloc_func)(size_t),
718-
void *(*calloc_func)(size_t, size_t),
719-
void *(*realloc_func)(void *, size_t),
720-
void (*free_func)(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 *) )
721685
{
722686
LOCK();
723687
malloc_hook = malloc_func;
@@ -728,10 +692,10 @@ _SET_HOOKS( void *(*malloc_func)(size_t),
728692
}
729693

730694
void
731-
_FETCH_HOOKS( void *(**malloc_funcp)(size_t),
732-
void *(**calloc_funcp)(size_t, size_t),
733-
void *(**realloc_funcp)(void *, size_t),
734-
void (**free_funcp)(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 *) )
735699
{
736700
LOCK();
737701
*malloc_funcp = malloc_hook;

0 commit comments

Comments
 (0)