Mercurial > cpython-lru_cache-weakref
annotate Include/pymem.h @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | 887ee6bd9284 |
| children | ef74764a6320 b6aff7a59803 |
| rev | line source |
|---|---|
| 23297 | 1 /* The PyMem_ family: low-level memory allocation interfaces. |
| 2 See objimpl.h for the PyObject_ memory family. | |
| 3 */ | |
|
13776
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
4 |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
5 #ifndef Py_PYMEM_H |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
6 #define Py_PYMEM_H |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
7 |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
8 #include "pyport.h" |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
9 |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
10 #ifdef __cplusplus |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
11 extern "C" { |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
12 #endif |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
13 |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
14 /* BEWARE: |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
15 |
| 23297 | 16 Each interface exports both functions and macros. Extension modules should |
| 17 use the functions, to ensure binary compatibility across Python versions. | |
| 18 Because the Python implementation is free to change internal details, and | |
| 19 the macros may (or may not) expose details for speed, if you do use the | |
| 20 macros you must recompile your extensions with each Python release. | |
|
13776
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
21 |
| 23297 | 22 Never mix calls to PyMem_ with calls to the platform malloc/realloc/ |
| 23 calloc/free. For example, on Windows different DLLs may end up using | |
| 24 different heaps, and if you use PyMem_Malloc you'll get the memory from the | |
| 25 heap used by the Python DLL; it could be a disaster if you free()'ed that | |
| 26 directly in your own extension. Using PyMem_Free instead ensures Python | |
| 27 can return the memory to the proper heap. As another example, in | |
| 28 PYMALLOC_DEBUG mode, Python wraps all calls to all PyMem_ and PyObject_ | |
| 29 memory functions in special debugging wrappers that add additional | |
| 30 debugging info to dynamic memory blocks. The system routines have no idea | |
| 31 what to do with that stuff, and the Python wrappers have no idea what to do | |
| 32 with raw blocks obtained directly by the system routines then. | |
|
41716
00c02c4488ce
Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
33 |
|
00c02c4488ce
Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
34 The GIL must be held when using these APIs. |
| 23297 | 35 */ |
|
13776
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
36 |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
37 /* |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
38 * Raw memory interface |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
39 * ==================== |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
40 */ |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
41 |
| 23297 | 42 /* Functions |
|
13776
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
43 |
| 23297 | 44 Functions supplying platform-independent semantics for malloc/realloc/ |
| 45 free. These functions make sure that allocating 0 bytes returns a distinct | |
|
22985
d9d82c1ef864
First stab at rationalizing the PyMem_ API. Mixing PyObject_xyz with
Tim Peters <tim.peters@gmail.com>
parents:
22971
diff
changeset
|
46 non-NULL pointer (whenever possible -- if we're flat out of memory, NULL |
|
d9d82c1ef864
First stab at rationalizing the PyMem_ API. Mixing PyObject_xyz with
Tim Peters <tim.peters@gmail.com>
parents:
22971
diff
changeset
|
47 may be returned), even if the platform malloc and realloc don't. |
|
d9d82c1ef864
First stab at rationalizing the PyMem_ API. Mixing PyObject_xyz with
Tim Peters <tim.peters@gmail.com>
parents:
22971
diff
changeset
|
48 Returned pointers must be checked for NULL explicitly. No action is |
| 23297 | 49 performed on failure (no exception is set, no warning is printed, etc). |
| 50 */ | |
|
22985
d9d82c1ef864
First stab at rationalizing the PyMem_ API. Mixing PyObject_xyz with
Tim Peters <tim.peters@gmail.com>
parents:
22971
diff
changeset
|
51 |
|
24752
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
23297
diff
changeset
|
52 PyAPI_FUNC(void *) PyMem_Malloc(size_t); |
|
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
23297
diff
changeset
|
53 PyAPI_FUNC(void *) PyMem_Realloc(void *, size_t); |
|
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
23297
diff
changeset
|
54 PyAPI_FUNC(void) PyMem_Free(void *); |
|
13776
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
55 |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
56 /* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
57 no longer supported. They used to call PyErr_NoMemory() on failure. */ |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
58 |
|
22985
d9d82c1ef864
First stab at rationalizing the PyMem_ API. Mixing PyObject_xyz with
Tim Peters <tim.peters@gmail.com>
parents:
22971
diff
changeset
|
59 /* Macros. */ |
| 23211 | 60 #ifdef PYMALLOC_DEBUG |
| 61 /* Redirect all memory operations to Python's debugging allocator. */ | |
| 62 #define PyMem_MALLOC PyObject_MALLOC | |
| 63 #define PyMem_REALLOC PyObject_REALLOC | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
26364
diff
changeset
|
64 #define PyMem_FREE PyObject_FREE |
| 23211 | 65 |
| 66 #else /* ! PYMALLOC_DEBUG */ | |
| 67 | |
|
26364
4f2d5174bd4a
Remove MALLOC_ZERO_RETURNS_NULL.
Martin v. Löwis <martin@v.loewis.de>
parents:
24752
diff
changeset
|
68 /* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL |
|
4f2d5174bd4a
Remove MALLOC_ZERO_RETURNS_NULL.
Martin v. Löwis <martin@v.loewis.de>
parents:
24752
diff
changeset
|
69 for malloc(0), which would be treated as an error. Some platforms |
|
4f2d5174bd4a
Remove MALLOC_ZERO_RETURNS_NULL.
Martin v. Löwis <martin@v.loewis.de>
parents:
24752
diff
changeset
|
70 would return a pointer with no memory behind it, which would break |
|
4f2d5174bd4a
Remove MALLOC_ZERO_RETURNS_NULL.
Martin v. Löwis <martin@v.loewis.de>
parents:
24752
diff
changeset
|
71 pymalloc. To solve these problems, allocate an extra byte. */ |
|
48565
887ee6bd9284
Merged revisions 65182 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48330
diff
changeset
|
72 /* Returns NULL to indicate error if a negative size or size larger than |
|
887ee6bd9284
Merged revisions 65182 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48330
diff
changeset
|
73 Py_ssize_t can represent is supplied. Helps prevents security holes. */ |
|
887ee6bd9284
Merged revisions 65182 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48330
diff
changeset
|
74 #define PyMem_MALLOC(n) (((n) < 0 || (n) > PY_SSIZE_T_MAX) ? NULL \ |
|
887ee6bd9284
Merged revisions 65182 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48330
diff
changeset
|
75 : malloc((n) ? (n) : 1)) |
|
887ee6bd9284
Merged revisions 65182 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48330
diff
changeset
|
76 #define PyMem_REALLOC(p, n) (((n) < 0 || (n) > PY_SSIZE_T_MAX) ? NULL \ |
|
887ee6bd9284
Merged revisions 65182 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48330
diff
changeset
|
77 : realloc((p), (n) ? (n) : 1)) |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
26364
diff
changeset
|
78 #define PyMem_FREE free |
|
22985
d9d82c1ef864
First stab at rationalizing the PyMem_ API. Mixing PyObject_xyz with
Tim Peters <tim.peters@gmail.com>
parents:
22971
diff
changeset
|
79 |
| 23211 | 80 #endif /* PYMALLOC_DEBUG */ |
|
22985
d9d82c1ef864
First stab at rationalizing the PyMem_ API. Mixing PyObject_xyz with
Tim Peters <tim.peters@gmail.com>
parents:
22971
diff
changeset
|
81 |
|
13776
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
82 /* |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
83 * Type-oriented memory interface |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
84 * ============================== |
|
22985
d9d82c1ef864
First stab at rationalizing the PyMem_ API. Mixing PyObject_xyz with
Tim Peters <tim.peters@gmail.com>
parents:
22971
diff
changeset
|
85 * |
|
48565
887ee6bd9284
Merged revisions 65182 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48330
diff
changeset
|
86 * Allocate memory for n objects of the given type. Returns a new pointer |
|
887ee6bd9284
Merged revisions 65182 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48330
diff
changeset
|
87 * or NULL if the request was too large or memory allocation failed. Use |
|
887ee6bd9284
Merged revisions 65182 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48330
diff
changeset
|
88 * these macros rather than doing the multiplication yourself so that proper |
|
887ee6bd9284
Merged revisions 65182 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48330
diff
changeset
|
89 * overflow checking is always done. |
|
13776
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
90 */ |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
91 |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
92 #define PyMem_New(type, n) \ |
|
48565
887ee6bd9284
Merged revisions 65182 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48330
diff
changeset
|
93 ( ((n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \ |
|
48330
ac854cde3135
Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41716
diff
changeset
|
94 ( (type *) PyMem_Malloc((n) * sizeof(type)) ) ) |
| 23297 | 95 #define PyMem_NEW(type, n) \ |
|
48565
887ee6bd9284
Merged revisions 65182 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48330
diff
changeset
|
96 ( ((n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \ |
|
48330
ac854cde3135
Merged revisions 64114 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
41716
diff
changeset
|
97 ( (type *) PyMem_MALLOC((n) * sizeof(type)) ) ) |
| 23297 | 98 |
|
48565
887ee6bd9284
Merged revisions 65182 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48330
diff
changeset
|
99 /* |
|
887ee6bd9284
Merged revisions 65182 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48330
diff
changeset
|
100 * The value of (p) is always clobbered by this macro regardless of success. |
|
887ee6bd9284
Merged revisions 65182 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48330
diff
changeset
|
101 * The caller MUST check if (p) is NULL afterwards and deal with the memory |
|
887ee6bd9284
Merged revisions 65182 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48330
diff
changeset
|
102 * error if so. This means the original value of (p) MUST be saved for the |
|
887ee6bd9284
Merged revisions 65182 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48330
diff
changeset
|
103 * caller's memory error handler to not lose track of it. |
|
887ee6bd9284
Merged revisions 65182 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48330
diff
changeset
|
104 */ |
|
13776
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
105 #define PyMem_Resize(p, type, n) \ |
|
48565
887ee6bd9284
Merged revisions 65182 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48330
diff
changeset
|
106 ( (p) = ((n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \ |
|
887ee6bd9284
Merged revisions 65182 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48330
diff
changeset
|
107 (type *) PyMem_Realloc((p), (n) * sizeof(type)) ) |
|
22985
d9d82c1ef864
First stab at rationalizing the PyMem_ API. Mixing PyObject_xyz with
Tim Peters <tim.peters@gmail.com>
parents:
22971
diff
changeset
|
108 #define PyMem_RESIZE(p, type, n) \ |
|
48565
887ee6bd9284
Merged revisions 65182 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48330
diff
changeset
|
109 ( (p) = ((n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \ |
|
887ee6bd9284
Merged revisions 65182 via svnmerge from
Georg Brandl <georg@python.org>
parents:
48330
diff
changeset
|
110 (type *) PyMem_REALLOC((p), (n) * sizeof(type)) ) |
|
22142
c25d6d780b1b
backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents:
18845
diff
changeset
|
111 |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
26364
diff
changeset
|
112 /* PyMem{Del,DEL} are left over from ancient days, and shouldn't be used |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
26364
diff
changeset
|
113 * anymore. They're just confusing aliases for PyMem_{Free,FREE} now. |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
26364
diff
changeset
|
114 */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
26364
diff
changeset
|
115 #define PyMem_Del PyMem_Free |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
26364
diff
changeset
|
116 #define PyMem_DEL PyMem_FREE |
|
22479
fb45791150d1
Give Python a debug-mode pymalloc, much as sketched on Python-Dev.
Tim Peters <tim.peters@gmail.com>
parents:
22416
diff
changeset
|
117 |
|
13776
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
118 #ifdef __cplusplus |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
119 } |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
120 #endif |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
121 |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
diff
changeset
|
122 #endif /* !Py_PYMEM_H */ |
