blob: 2372b864a1d6da5a5ed45b7470a3790e96e18ac0 [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);
16PyAPI_FUNC(void *) PyMem_RawRealloc(void *ptr, size_t new_size);
17PyAPI_FUNC(void) PyMem_RawFree(void *ptr);
Martin v. Löwis1c0689c2014-01-03 21:36:49 +010018#endif
Victor Stinner0507bf52013-07-07 02:05:46 +020019
20
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000021/* BEWARE:
22
Tim Peters8b078f92002-04-28 04:11:46 +000023 Each interface exports both functions and macros. Extension modules should
24 use the functions, to ensure binary compatibility across Python versions.
25 Because the Python implementation is free to change internal details, and
26 the macros may (or may not) expose details for speed, if you do use the
27 macros you must recompile your extensions with each Python release.
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000028
Tim Peters8b078f92002-04-28 04:11:46 +000029 Never mix calls to PyMem_ with calls to the platform malloc/realloc/
30 calloc/free. For example, on Windows different DLLs may end up using
31 different heaps, and if you use PyMem_Malloc you'll get the memory from the
32 heap used by the Python DLL; it could be a disaster if you free()'ed that
33 directly in your own extension. Using PyMem_Free instead ensures Python
34 can return the memory to the proper heap. As another example, in
35 PYMALLOC_DEBUG mode, Python wraps all calls to all PyMem_ and PyObject_
36 memory functions in special debugging wrappers that add additional
37 debugging info to dynamic memory blocks. The system routines have no idea
38 what to do with that stuff, and the Python wrappers have no idea what to do
39 with raw blocks obtained directly by the system routines then.
Guido van Rossum360e4b82007-05-14 22:51:27 +000040
41 The GIL must be held when using these APIs.
Tim Peters8b078f92002-04-28 04:11:46 +000042*/
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000043
44/*
45 * Raw memory interface
46 * ====================
47 */
48
Tim Peters8b078f92002-04-28 04:11:46 +000049/* Functions
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000050
Tim Peters8b078f92002-04-28 04:11:46 +000051 Functions supplying platform-independent semantics for malloc/realloc/
52 free. These functions make sure that allocating 0 bytes returns a distinct
Tim Petersaf3e8de2002-04-12 07:22:56 +000053 non-NULL pointer (whenever possible -- if we're flat out of memory, NULL
54 may be returned), even if the platform malloc and realloc don't.
55 Returned pointers must be checked for NULL explicitly. No action is
Tim Peters8b078f92002-04-28 04:11:46 +000056 performed on failure (no exception is set, no warning is printed, etc).
57*/
Tim Petersaf3e8de2002-04-12 07:22:56 +000058
Victor Stinner0507bf52013-07-07 02:05:46 +020059PyAPI_FUNC(void *) PyMem_Malloc(size_t size);
60PyAPI_FUNC(void *) PyMem_Realloc(void *ptr, size_t new_size);
61PyAPI_FUNC(void) PyMem_Free(void *ptr);
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000062
Martin v. Löwis1c0689c2014-01-03 21:36:49 +010063#ifndef Py_LIMITED_API
Victor Stinner49fc8ec2013-07-07 23:30:24 +020064PyAPI_FUNC(char *) _PyMem_RawStrdup(const char *str);
65PyAPI_FUNC(char *) _PyMem_Strdup(const char *str);
Martin v. Löwis1c0689c2014-01-03 21:36:49 +010066#endif
Victor Stinner49fc8ec2013-07-07 23:30:24 +020067
Tim Petersaf3e8de2002-04-12 07:22:56 +000068/* Macros. */
Tim Peters51e7f5c2002-04-22 02:33:27 +000069
Martin v. Löwis39f59b02002-11-23 09:13:40 +000070/* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL
71 for malloc(0), which would be treated as an error. Some platforms
72 would return a pointer with no memory behind it, which would break
73 pymalloc. To solve these problems, allocate an extra byte. */
Georg Brandld492ad82008-07-23 16:13:07 +000074/* Returns NULL to indicate error if a negative size or size larger than
75 Py_ssize_t can represent is supplied. Helps prevents security holes. */
Victor Stinner0507bf52013-07-07 02:05:46 +020076#define PyMem_MALLOC(n) PyMem_Malloc(n)
77#define PyMem_REALLOC(p, n) PyMem_Realloc(p, n)
78#define PyMem_FREE(p) PyMem_Free(p)
Tim Petersaf3e8de2002-04-12 07:22:56 +000079
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000080/*
81 * Type-oriented memory interface
82 * ==============================
Tim Petersaf3e8de2002-04-12 07:22:56 +000083 *
Georg Brandld492ad82008-07-23 16:13:07 +000084 * Allocate memory for n objects of the given type. Returns a new pointer
85 * or NULL if the request was too large or memory allocation failed. Use
86 * these macros rather than doing the multiplication yourself so that proper
87 * overflow checking is always done.
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000088 */
89
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000090#define PyMem_New(type, n) \
Mark Dickinsonbbe63062010-02-14 14:08:54 +000091 ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000092 ( (type *) PyMem_Malloc((n) * sizeof(type)) ) )
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000093#define PyMem_NEW(type, n) \
Mark Dickinsonbbe63062010-02-14 14:08:54 +000094 ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +000095 ( (type *) PyMem_MALLOC((n) * sizeof(type)) ) )
Tim Peters8b078f92002-04-28 04:11:46 +000096
Georg Brandld492ad82008-07-23 16:13:07 +000097/*
98 * The value of (p) is always clobbered by this macro regardless of success.
99 * The caller MUST check if (p) is NULL afterwards and deal with the memory
100 * error if so. This means the original value of (p) MUST be saved for the
101 * caller's memory error handler to not lose track of it.
102 */
Tim Peters8b078f92002-04-28 04:11:46 +0000103#define PyMem_Resize(p, type, n) \
Mark Dickinsonbbe63062010-02-14 14:08:54 +0000104 ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
Georg Brandld492ad82008-07-23 16:13:07 +0000105 (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
Tim Petersaf3e8de2002-04-12 07:22:56 +0000106#define PyMem_RESIZE(p, type, n) \
Mark Dickinsonbbe63062010-02-14 14:08:54 +0000107 ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
Georg Brandld492ad82008-07-23 16:13:07 +0000108 (type *) PyMem_REALLOC((p), (n) * sizeof(type)) )
Tim Petersa5d78cc2002-03-02 08:43:19 +0000109
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000110/* PyMem{Del,DEL} are left over from ancient days, and shouldn't be used
111 * anymore. They're just confusing aliases for PyMem_{Free,FREE} now.
112 */
113#define PyMem_Del PyMem_Free
114#define PyMem_DEL PyMem_FREE
Tim Petersddea2082002-03-23 10:03:50 +0000115
Victor Stinner0507bf52013-07-07 02:05:46 +0200116#ifndef Py_LIMITED_API
117typedef enum {
118 /* PyMem_RawMalloc(), PyMem_RawRealloc() and PyMem_RawFree() */
119 PYMEM_DOMAIN_RAW,
120
121 /* PyMem_Malloc(), PyMem_Realloc() and PyMem_Free() */
122 PYMEM_DOMAIN_MEM,
123
124 /* PyObject_Malloc(), PyObject_Realloc() and PyObject_Free() */
125 PYMEM_DOMAIN_OBJ
126} PyMemAllocatorDomain;
127
128typedef struct {
129 /* user context passed as the first argument to the 3 functions */
130 void *ctx;
131
132 /* allocate a memory block */
133 void* (*malloc) (void *ctx, size_t size);
134
135 /* allocate or resize a memory block */
136 void* (*realloc) (void *ctx, void *ptr, size_t new_size);
137
138 /* release a memory block */
139 void (*free) (void *ctx, void *ptr);
140} PyMemAllocator;
141
142/* Get the memory block allocator of the specified domain. */
143PyAPI_FUNC(void) PyMem_GetAllocator(PyMemAllocatorDomain domain,
144 PyMemAllocator *allocator);
145
146/* Set the memory block allocator of the specified domain.
147
148 The new allocator must return a distinct non-NULL pointer when requesting
149 zero bytes.
150
151 For the PYMEM_DOMAIN_RAW domain, the allocator must be thread-safe: the GIL
152 is not held when the allocator is called.
153
154 If the new allocator is not a hook (don't call the previous allocator), the
155 PyMem_SetupDebugHooks() function must be called to reinstall the debug hooks
156 on top on the new allocator. */
157PyAPI_FUNC(void) PyMem_SetAllocator(PyMemAllocatorDomain domain,
158 PyMemAllocator *allocator);
159
160/* Setup hooks to detect bugs in the following Python memory allocator
161 functions:
162
163 - PyMem_RawMalloc(), PyMem_RawRealloc(), PyMem_RawFree()
164 - PyMem_Malloc(), PyMem_Realloc(), PyMem_Free()
165 - PyObject_Malloc(), PyObject_Realloc() and PyObject_Free()
166
167 Newly allocated memory is filled with the byte 0xCB, freed memory is filled
168 with the byte 0xDB. Additionnal checks:
169
170 - detect API violations, ex: PyObject_Free() called on a buffer allocated
171 by PyMem_Malloc()
172 - detect write before the start of the buffer (buffer underflow)
173 - detect write after the end of the buffer (buffer overflow)
174
175 The function does nothing if Python is not compiled is debug mode. */
176PyAPI_FUNC(void) PyMem_SetupDebugHooks(void);
177#endif
178
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000179#ifdef __cplusplus
180}
181#endif
182
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000183#endif /* !Py_PYMEM_H */