blob: 043db64deb89df70d875712e61308d7ea8196697 [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
Martin v. Löwis1c0689c2014-01-03 21:36:49 +010014#ifndef Py_LIMITED_API
Victor Stinner0507bf52013-07-07 02:05:46 +020015PyAPI_FUNC(void *) PyMem_RawMalloc(size_t size);
Victor Stinnerdb067af2014-05-02 22:31:14 +020016PyAPI_FUNC(void *) PyMem_RawCalloc(size_t nelem, size_t elsize);
Victor Stinner0507bf52013-07-07 02:05:46 +020017PyAPI_FUNC(void *) PyMem_RawRealloc(void *ptr, size_t new_size);
18PyAPI_FUNC(void) PyMem_RawFree(void *ptr);
Martin v. Löwis1c0689c2014-01-03 21:36:49 +010019#endif
Victor Stinner0507bf52013-07-07 02:05:46 +020020
21
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000022/* BEWARE:
23
Tim Peters8b078f92002-04-28 04:11:46 +000024 Each interface exports both functions and macros. Extension modules should
25 use the functions, to ensure binary compatibility across Python versions.
26 Because the Python implementation is free to change internal details, and
27 the macros may (or may not) expose details for speed, if you do use the
28 macros you must recompile your extensions with each Python release.
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000029
Tim Peters8b078f92002-04-28 04:11:46 +000030 Never mix calls to PyMem_ with calls to the platform malloc/realloc/
31 calloc/free. For example, on Windows different DLLs may end up using
32 different heaps, and if you use PyMem_Malloc you'll get the memory from the
33 heap used by the Python DLL; it could be a disaster if you free()'ed that
34 directly in your own extension. Using PyMem_Free instead ensures Python
35 can return the memory to the proper heap. As another example, in
36 PYMALLOC_DEBUG mode, Python wraps all calls to all PyMem_ and PyObject_
37 memory functions in special debugging wrappers that add additional
38 debugging info to dynamic memory blocks. The system routines have no idea
39 what to do with that stuff, and the Python wrappers have no idea what to do
40 with raw blocks obtained directly by the system routines then.
Guido van Rossum360e4b82007-05-14 22:51:27 +000041
42 The GIL must be held when using these APIs.
Tim Peters8b078f92002-04-28 04:11:46 +000043*/
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000044
45/*
46 * Raw memory interface
47 * ====================
48 */
49
Tim Peters8b078f92002-04-28 04:11:46 +000050/* Functions
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000051
Tim Peters8b078f92002-04-28 04:11:46 +000052 Functions supplying platform-independent semantics for malloc/realloc/
53 free. These functions make sure that allocating 0 bytes returns a distinct
Tim Petersaf3e8de2002-04-12 07:22:56 +000054 non-NULL pointer (whenever possible -- if we're flat out of memory, NULL
55 may be returned), even if the platform malloc and realloc don't.
56 Returned pointers must be checked for NULL explicitly. No action is
Tim Peters8b078f92002-04-28 04:11:46 +000057 performed on failure (no exception is set, no warning is printed, etc).
58*/
Tim Petersaf3e8de2002-04-12 07:22:56 +000059
Victor Stinner0507bf52013-07-07 02:05:46 +020060PyAPI_FUNC(void *) PyMem_Malloc(size_t size);
Victor Stinnerdb067af2014-05-02 22:31:14 +020061PyAPI_FUNC(void *) PyMem_Calloc(size_t nelem, size_t elsize);
Victor Stinner0507bf52013-07-07 02:05:46 +020062PyAPI_FUNC(void *) PyMem_Realloc(void *ptr, size_t new_size);
63PyAPI_FUNC(void) PyMem_Free(void *ptr);
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000064
Martin v. Löwis1c0689c2014-01-03 21:36:49 +010065#ifndef Py_LIMITED_API
Victor Stinner49fc8ec2013-07-07 23:30:24 +020066PyAPI_FUNC(char *) _PyMem_RawStrdup(const char *str);
67PyAPI_FUNC(char *) _PyMem_Strdup(const char *str);
Martin v. Löwis1c0689c2014-01-03 21:36:49 +010068#endif
Victor Stinner49fc8ec2013-07-07 23:30:24 +020069
Tim Petersaf3e8de2002-04-12 07:22:56 +000070/* Macros. */
Tim Peters51e7f5c2002-04-22 02:33:27 +000071
Martin v. Löwis39f59b02002-11-23 09:13:40 +000072/* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL
73 for malloc(0), which would be treated as an error. Some platforms
74 would return a pointer with no memory behind it, which would break
75 pymalloc. To solve these problems, allocate an extra byte. */
Georg Brandld492ad82008-07-23 16:13:07 +000076/* Returns NULL to indicate error if a negative size or size larger than
77 Py_ssize_t can represent is supplied. Helps prevents security holes. */
Victor Stinner0507bf52013-07-07 02:05:46 +020078#define PyMem_MALLOC(n) PyMem_Malloc(n)
79#define PyMem_REALLOC(p, n) PyMem_Realloc(p, n)
80#define PyMem_FREE(p) PyMem_Free(p)
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
Victor Stinner0507bf52013-07-07 02:05:46 +0200118#ifndef Py_LIMITED_API
119typedef enum {
120 /* PyMem_RawMalloc(), PyMem_RawRealloc() and PyMem_RawFree() */
121 PYMEM_DOMAIN_RAW,
122
123 /* PyMem_Malloc(), PyMem_Realloc() and PyMem_Free() */
124 PYMEM_DOMAIN_MEM,
125
126 /* PyObject_Malloc(), PyObject_Realloc() and PyObject_Free() */
127 PYMEM_DOMAIN_OBJ
128} PyMemAllocatorDomain;
129
130typedef struct {
Victor Stinnerd8f0d922014-06-02 21:57:10 +0200131 /* user context passed as the first argument to the 4 functions */
Victor Stinner0507bf52013-07-07 02:05:46 +0200132 void *ctx;
133
134 /* allocate a memory block */
135 void* (*malloc) (void *ctx, size_t size);
136
Victor Stinnerdb067af2014-05-02 22:31:14 +0200137 /* allocate a memory block initialized by zeros */
138 void* (*calloc) (void *ctx, size_t nelem, size_t elsize);
139
Victor Stinner0507bf52013-07-07 02:05:46 +0200140 /* allocate or resize a memory block */
141 void* (*realloc) (void *ctx, void *ptr, size_t new_size);
142
143 /* release a memory block */
144 void (*free) (void *ctx, void *ptr);
Victor Stinnerd8f0d922014-06-02 21:57:10 +0200145} PyMemAllocatorEx;
Victor Stinner0507bf52013-07-07 02:05:46 +0200146
147/* Get the memory block allocator of the specified domain. */
148PyAPI_FUNC(void) PyMem_GetAllocator(PyMemAllocatorDomain domain,
Victor Stinnerd8f0d922014-06-02 21:57:10 +0200149 PyMemAllocatorEx *allocator);
Victor Stinner0507bf52013-07-07 02:05:46 +0200150
151/* Set the memory block allocator of the specified domain.
152
153 The new allocator must return a distinct non-NULL pointer when requesting
154 zero bytes.
155
156 For the PYMEM_DOMAIN_RAW domain, the allocator must be thread-safe: the GIL
157 is not held when the allocator is called.
158
159 If the new allocator is not a hook (don't call the previous allocator), the
160 PyMem_SetupDebugHooks() function must be called to reinstall the debug hooks
161 on top on the new allocator. */
162PyAPI_FUNC(void) PyMem_SetAllocator(PyMemAllocatorDomain domain,
Victor Stinnerd8f0d922014-06-02 21:57:10 +0200163 PyMemAllocatorEx *allocator);
Victor Stinner0507bf52013-07-07 02:05:46 +0200164
165/* Setup hooks to detect bugs in the following Python memory allocator
166 functions:
167
168 - PyMem_RawMalloc(), PyMem_RawRealloc(), PyMem_RawFree()
169 - PyMem_Malloc(), PyMem_Realloc(), PyMem_Free()
170 - PyObject_Malloc(), PyObject_Realloc() and PyObject_Free()
171
172 Newly allocated memory is filled with the byte 0xCB, freed memory is filled
173 with the byte 0xDB. Additionnal checks:
174
175 - detect API violations, ex: PyObject_Free() called on a buffer allocated
176 by PyMem_Malloc()
177 - detect write before the start of the buffer (buffer underflow)
178 - detect write after the end of the buffer (buffer overflow)
179
180 The function does nothing if Python is not compiled is debug mode. */
181PyAPI_FUNC(void) PyMem_SetupDebugHooks(void);
182#endif
183
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000184#ifdef __cplusplus
185}
186#endif
187
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000188#endif /* !Py_PYMEM_H */