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

Skip to content

Commit 25f6894

Browse files
author
Peter Schneider-Kamp
committed
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
1 parent b88b0bc commit 25f6894

10 files changed

Lines changed: 221 additions & 177 deletions

File tree

Include/Python.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
5454

5555
#include "pyport.h"
5656

57+
#include "pymem.h"
58+
5759
#include "object.h"
5860
#include "objimpl.h"
5961

Include/mymalloc.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#ifndef Py_MYMALLOC_H
2-
#define Py_MYMALLOC_H
31
/***********************************************************
42
Copyright (c) 2000, BeOpen.com.
53
Copyright (c) 1995-2000, Corporation for National Research Initiatives.
@@ -15,6 +13,9 @@ THIS FILE IS OBSOLETE
1513
USE "pyport.h" INSTEAD
1614
***************************************/
1715

16+
#ifndef Py_MYMALLOC_H
17+
#define Py_MYMALLOC_H
18+
1819
/* Lowest-level memory allocation interface */
1920

2021
#define ANY void /* For API compatibility only. Obsolete, do not use. */

Include/mymath.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/***********************************************************
2+
Copyright (c) 2000, BeOpen.com.
3+
Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4+
Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5+
All rights reserved.
6+
7+
See the file "Misc/COPYRIGHT" for information on usage and
8+
redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9+
******************************************************************/
10+
111
/***************************************
212
THIS FILE IS OBSOLETE
313
USE "pyport.h" INSTEAD

Include/myproto.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
#ifndef Py_PROTO_H
2-
#define Py_PROTO_H
3-
#ifdef __cplusplus
4-
extern "C" {
5-
#endif
6-
71
/***********************************************************
82
Copyright (c) 2000, BeOpen.com.
93
Copyright (c) 1995-2000, Corporation for National Research Initiatives.
@@ -19,6 +13,12 @@ THIS FILE IS OBSOLETE
1913
DON'T USE Py_PROTO or Py_FPROTO anymore.
2014
***************************************/
2115

16+
#ifndef Py_PROTO_H
17+
#define Py_PROTO_H
18+
#ifdef __cplusplus
19+
extern "C" {
20+
#endif
21+
2222
#ifdef HAVE_PROTOTYPES
2323
#define Py_PROTO(x) x
2424
#else

Include/myselect.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
#ifndef Py_MYSELECT_H
2-
#define Py_MYSELECT_H
3-
#ifdef __cplusplus
4-
extern "C" {
5-
#endif
6-
71
/***********************************************************
82
Copyright (c) 2000, BeOpen.com.
93
Copyright (c) 1995-2000, Corporation for National Research Initiatives.
@@ -19,6 +13,12 @@ THIS FILE IS OBSOLETE
1913
USE "pyport.h" INSTEAD
2014
***************************************/
2115

16+
#ifndef Py_MYSELECT_H
17+
#define Py_MYSELECT_H
18+
#ifdef __cplusplus
19+
extern "C" {
20+
#endif
21+
2222
/* Include file for users of select() */
2323

2424
/* NB caller must include <sys/types.h> */

Include/mytime.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
#ifndef Py_MYTIME_H
2-
#define Py_MYTIME_H
3-
#ifdef __cplusplus
4-
extern "C" {
5-
#endif
6-
71
/***********************************************************
82
Copyright (c) 2000, BeOpen.com.
93
Copyright (c) 1995-2000, Corporation for National Research Initiatives.
@@ -19,6 +13,12 @@ THIS FILE IS OBSOLETE
1913
USE "pyport.h" INSTEAD
2014
***************************************/
2115

16+
#ifndef Py_MYTIME_H
17+
#define Py_MYTIME_H
18+
#ifdef __cplusplus
19+
extern "C" {
20+
#endif
21+
2222
/* Include file instead of <time.h> and/or <sys/time.h> */
2323

2424
#ifdef TIME_WITH_SYS_TIME

Include/objimpl.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
1010

1111
#ifndef Py_OBJIMPL_H
1212
#define Py_OBJIMPL_H
13+
14+
#include "pymem.h"
15+
1316
#ifdef __cplusplus
1417
extern "C" {
1518
#endif
1619

17-
#include "pyport.h"
18-
1920
/*
2021
Functions and macros for modules that implement new object types.
2122
You must first include "object.h".

Include/pgenheaders.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
3030
#include <stdlib.h>
3131
#endif
3232

33-
#include "pyport.h"
33+
#include "pymem.h"
3434

3535
#include "pydebug.h"
3636

Include/pymem.h

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/***********************************************************
2+
Copyright (c) 2000, BeOpen.com.
3+
Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4+
Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5+
All rights reserved.
6+
7+
See the file "Misc/COPYRIGHT" for information on usage and
8+
redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9+
******************************************************************/
10+
11+
/* Lowest-level memory allocation interface */
12+
13+
#ifndef Py_PYMEM_H
14+
#define Py_PYMEM_H
15+
16+
#include "pyport.h"
17+
18+
#ifdef __cplusplus
19+
extern "C" {
20+
#endif
21+
22+
/*
23+
* Core memory allocator
24+
* =====================
25+
*/
26+
27+
/* To make sure the interpreter is user-malloc friendly, all memory
28+
APIs are implemented on top of this one.
29+
30+
The PyCore_* macros can be defined to make the interpreter use a
31+
custom allocator. Note that they are for internal use only. Both
32+
the core and extension modules should use the PyMem_* API.
33+
34+
See the comment block at the end of this file for two scenarios
35+
showing how to use this to use a different allocator. */
36+
37+
#ifndef PyCore_MALLOC_FUNC
38+
#undef PyCore_REALLOC_FUNC
39+
#undef PyCore_FREE_FUNC
40+
#define PyCore_MALLOC_FUNC malloc
41+
#define PyCore_REALLOC_FUNC realloc
42+
#define PyCore_FREE_FUNC free
43+
#endif
44+
45+
#ifndef PyCore_MALLOC_PROTO
46+
#undef PyCore_REALLOC_PROTO
47+
#undef PyCore_FREE_PROTO
48+
#define PyCore_MALLOC_PROTO (size_t)
49+
#define PyCore_REALLOC_PROTO (void *, size_t)
50+
#define PyCore_FREE_PROTO (void *)
51+
#endif
52+
53+
#ifdef NEED_TO_DECLARE_MALLOC_AND_FRIEND
54+
extern void *PyCore_MALLOC_FUNC PyCore_MALLOC_PROTO;
55+
extern void *PyCore_REALLOC_FUNC PyCore_REALLOC_PROTO;
56+
extern void PyCore_FREE_FUNC PyCore_FREE_PROTO;
57+
#endif
58+
59+
#ifndef PyCore_MALLOC
60+
#undef PyCore_REALLOC
61+
#undef PyCore_FREE
62+
#define PyCore_MALLOC(n) PyCore_MALLOC_FUNC(n)
63+
#define PyCore_REALLOC(p, n) PyCore_REALLOC_FUNC((p), (n))
64+
#define PyCore_FREE(p) PyCore_FREE_FUNC(p)
65+
#endif
66+
67+
/* BEWARE:
68+
69+
Each interface exports both functions and macros. Extension modules
70+
should normally use the functions for ensuring binary compatibility
71+
of the user's code across Python versions. Subsequently, if the
72+
Python runtime switches to its own malloc (different from standard
73+
malloc), no recompilation is required for the extensions.
74+
75+
The macro versions trade compatibility for speed. They can be used
76+
whenever there is a performance problem, but their use implies
77+
recompilation of the code for each new Python release. The Python
78+
core uses the macros because it *is* compiled on every upgrade.
79+
This might not be the case with 3rd party extensions in a custom
80+
setup (for example, a customer does not always have access to the
81+
source of 3rd party deliverables). You have been warned! */
82+
83+
/*
84+
* Raw memory interface
85+
* ====================
86+
*/
87+
88+
/* Functions */
89+
90+
/* Function wrappers around PyCore_MALLOC and friends; useful if you
91+
need to be sure that you are using the same memory allocator as
92+
Python. Note that the wrappers make sure that allocating 0 bytes
93+
returns a non-NULL pointer, even if the underlying malloc
94+
doesn't. Returned pointers must be checked for NULL explicitly.
95+
No action is performed on failure. */
96+
extern DL_IMPORT(void *) PyMem_Malloc(size_t);
97+
extern DL_IMPORT(void *) PyMem_Realloc(void *, size_t);
98+
extern DL_IMPORT(void) PyMem_Free(void *);
99+
100+
/* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are
101+
no longer supported. They used to call PyErr_NoMemory() on failure. */
102+
103+
/* Macros */
104+
#define PyMem_MALLOC(n) PyCore_MALLOC(n)
105+
#define PyMem_REALLOC(p, n) PyCore_REALLOC((void *)(p), (n))
106+
#define PyMem_FREE(p) PyCore_FREE((void *)(p))
107+
108+
/*
109+
* Type-oriented memory interface
110+
* ==============================
111+
*/
112+
113+
/* Functions */
114+
#define PyMem_New(type, n) \
115+
( (type *) PyMem_Malloc((n) * sizeof(type)) )
116+
#define PyMem_Resize(p, type, n) \
117+
( (p) = (type *) PyMem_Realloc((n) * sizeof(type)) )
118+
#define PyMem_Del(p) PyMem_Free(p)
119+
120+
/* Macros */
121+
#define PyMem_NEW(type, n) \
122+
( (type *) PyMem_MALLOC(_PyMem_EXTRA + (n) * sizeof(type)) )
123+
#define PyMem_RESIZE(p, type, n) \
124+
if ((p) == NULL) \
125+
(p) = (type *)(PyMem_MALLOC( \
126+
_PyMem_EXTRA + (n) * sizeof(type))); \
127+
else \
128+
(p) = (type *)(PyMem_REALLOC((p), \
129+
_PyMem_EXTRA + (n) * sizeof(type)))
130+
#define PyMem_DEL(p) PyMem_FREE(p)
131+
132+
/* PyMem_XDEL is deprecated. To avoid the call when p is NULL,
133+
it is recommended to write the test explicitly in the code.
134+
Note that according to ANSI C, free(NULL) has no effect. */
135+
136+
#ifdef __cplusplus
137+
}
138+
#endif
139+
140+
/* SCENARIOS
141+
142+
Here are two scenarios by Vladimir Marangozov (the author of the
143+
memory allocation redesign).
144+
145+
1) Scenario A
146+
147+
Suppose you want to use a debugging malloc library that collects info on
148+
where the malloc calls originate from. Assume the interface is:
149+
150+
d_malloc(size_t n, char* src_file, unsigned long src_line) c.s.
151+
152+
In this case, you would define (for example in config.h) :
153+
154+
#define PyCore_MALLOC_FUNC d_malloc
155+
...
156+
#define PyCore_MALLOC_PROTO (size_t, char *, unsigned long)
157+
...
158+
#define NEED_TO_DECLARE_MALLOC_AND_FRIEND
159+
160+
#define PyCore_MALLOC(n) PyCore_MALLOC_FUNC((n), __FILE__, __LINE__)
161+
...
162+
163+
2) Scenario B
164+
165+
Suppose you want to use malloc hooks (defined & initialized in a 3rd party
166+
malloc library) instead of malloc functions. In this case, you would
167+
define:
168+
169+
#define PyCore_MALLOC_FUNC (*malloc_hook)
170+
...
171+
#define NEED_TO_DECLARE_MALLOC_AND_FRIEND
172+
173+
and ignore the previous definitions about PyCore_MALLOC_FUNC, etc.
174+
175+
176+
*/
177+
178+
179+
#endif /* !Py_PYMEM_H */

0 commit comments

Comments
 (0)