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