blob: 10b5bea5eb892914c9d2b647ddbb8a06d5585c88 [file] [log] [blame]
Tim Peters8b078f92002-04-28 04:11:46 +00001/* The PyMem_ family: low-level memory allocation interfaces.
2 See objimpl.h for the PyObject_ memory family.
3*/
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +00004
5#ifndef Py_PYMEM_H
6#define Py_PYMEM_H
7
8#include "pyport.h"
9
10#ifdef __cplusplus
11extern "C" {
12#endif
13
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000014/* BEWARE:
15
Tim Peters8b078f92002-04-28 04:11:46 +000016 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.
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000021
Tim Peters8b078f92002-04-28 04:11:46 +000022 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.
Guido van Rossum360e4b82007-05-14 22:51:27 +000033
34 The GIL must be held when using these APIs.
Tim Peters8b078f92002-04-28 04:11:46 +000035*/
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000036
37/*
38 * Raw memory interface
39 * ====================
40 */
41
Tim Peters8b078f92002-04-28 04:11:46 +000042/* Functions
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000043
Tim Peters8b078f92002-04-28 04:11:46 +000044 Functions supplying platform-independent semantics for malloc/realloc/
45 free. These functions make sure that allocating 0 bytes returns a distinct
Tim Petersaf3e8de2002-04-12 07:22:56 +000046 non-NULL pointer (whenever possible -- if we're flat out of memory, NULL
47 may be returned), even if the platform malloc and realloc don't.
48 Returned pointers must be checked for NULL explicitly. No action is
Tim Peters8b078f92002-04-28 04:11:46 +000049 performed on failure (no exception is set, no warning is printed, etc).
50*/
Tim Petersaf3e8de2002-04-12 07:22:56 +000051
Mark Hammond91a681d2002-08-12 07:21:58 +000052PyAPI_FUNC(void *) PyMem_Malloc(size_t);
53PyAPI_FUNC(void *) PyMem_Realloc(void *, size_t);
54PyAPI_FUNC(void) PyMem_Free(void *);
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000055
56/* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are
57 no longer supported. They used to call PyErr_NoMemory() on failure. */
58
Tim Petersaf3e8de2002-04-12 07:22:56 +000059/* Macros. */
Tim Peters51e7f5c2002-04-22 02:33:27 +000060#ifdef PYMALLOC_DEBUG
61/* Redirect all memory operations to Python's debugging allocator. */
Kristján Valur Jónssonae4cfb12009-09-28 13:45:02 +000062#define PyMem_MALLOC _PyMem_DebugMalloc
63#define PyMem_REALLOC _PyMem_DebugRealloc
64#define PyMem_FREE _PyMem_DebugFree
Tim Peters51e7f5c2002-04-22 02:33:27 +000065
66#else /* ! PYMALLOC_DEBUG */
67
Martin v. Löwis39f59b02002-11-23 09:13:40 +000068/* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL
69 for malloc(0), which would be treated as an error. Some platforms
70 would return a pointer with no memory behind it, which would break
71 pymalloc. To solve these problems, allocate an extra byte. */
Georg Brandld492ad82008-07-23 16:13:07 +000072/* Returns NULL to indicate error if a negative size or size larger than
73 Py_ssize_t can represent is supplied. Helps prevents security holes. */
Mark Dickinson629dfe22009-12-10 10:39:08 +000074#define PyMem_MALLOC(n) ((size_t)(n) > (size_t)PY_SSIZE_T_MAX ? NULL \
Georg Brandld492ad82008-07-23 16:13:07 +000075 : malloc((n) ? (n) : 1))
Mark Dickinson629dfe22009-12-10 10:39:08 +000076#define PyMem_REALLOC(p, n) ((size_t)(n) > (size_t)PY_SSIZE_T_MAX ? NULL \
Georg Brandld492ad82008-07-23 16:13:07 +000077 : realloc((p), (n) ? (n) : 1))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000078#define PyMem_FREE free
Tim Petersaf3e8de2002-04-12 07:22:56 +000079
Tim Peters51e7f5c2002-04-22 02:33:27 +000080#endif /* PYMALLOC_DEBUG */
Tim Petersaf3e8de2002-04-12 07:22:56 +000081
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000082/*
83 * Type-oriented memory interface
84 * ==============================
Tim Petersaf3e8de2002-04-12 07:22:56 +000085 *
Georg Brandld492ad82008-07-23 16:13:07 +000086 * Allocate memory for n objects of the given type. Returns a new pointer
87 * or NULL if the request was too large or memory allocation failed. Use
88 * these macros rather than doing the multiplication yourself so that proper
89 * overflow checking is always done.
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000090 */
91
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000092#define PyMem_New(type, n) \
Mark Dickinsonbbe63062010-02-14 14:08:54 +000093 ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000094 ( (type *) PyMem_Malloc((n) * sizeof(type)) ) )
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000095#define PyMem_NEW(type, n) \
Mark Dickinsonbbe63062010-02-14 14:08:54 +000096 ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000097 ( (type *) PyMem_MALLOC((n) * sizeof(type)) ) )
Tim Peters8b078f92002-04-28 04:11:46 +000098
Georg Brandld492ad82008-07-23 16:13:07 +000099/*
100 * The value of (p) is always clobbered by this macro regardless of success.
101 * The caller MUST check if (p) is NULL afterwards and deal with the memory
102 * error if so. This means the original value of (p) MUST be saved for the
103 * caller's memory error handler to not lose track of it.
104 */
Tim Peters8b078f92002-04-28 04:11:46 +0000105#define PyMem_Resize(p, type, n) \
Mark Dickinsonbbe63062010-02-14 14:08:54 +0000106 ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
Georg Brandld492ad82008-07-23 16:13:07 +0000107 (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
Tim Petersaf3e8de2002-04-12 07:22:56 +0000108#define PyMem_RESIZE(p, type, n) \
Mark Dickinsonbbe63062010-02-14 14:08:54 +0000109 ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
Georg Brandld492ad82008-07-23 16:13:07 +0000110 (type *) PyMem_REALLOC((p), (n) * sizeof(type)) )
Tim Petersa5d78cc2002-03-02 08:43:19 +0000111
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000112/* PyMem{Del,DEL} are left over from ancient days, and shouldn't be used
113 * anymore. They're just confusing aliases for PyMem_{Free,FREE} now.
114 */
115#define PyMem_Del PyMem_Free
116#define PyMem_DEL PyMem_FREE
Tim Petersddea2082002-03-23 10:03:50 +0000117
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000118#ifdef __cplusplus
119}
120#endif
121
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000122#endif /* !Py_PYMEM_H */