blob: 34d93186a393fcae0a713a1b4b672ac7d835b950 [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 Stinner4d705622013-06-15 00:37:46 +020014typedef struct {
15 /* user context passed as the first argument to the 3 functions */
16 void *ctx;
17
18 /* allocate memory */
19 void* (*malloc) (void *ctx, size_t size);
20
21 /* allocate memory or resize a memory buffer */
22 void* (*realloc) (void *ctx, void *ptr, size_t new_size);
23
24 /* release memory */
25 void (*free) (void *ctx, void *ptr);
26} PyMemAllocators;
27
28/* Raw memory allocators, system functions: malloc(), realloc(), free().
29
30 These functions are thread-safe, the GIL does not need to be held. */
31
32/* Get internal functions of PyMem_RawMalloc(), PyMem_RawRealloc() and
33 PyMem_RawFree(). *ctx_p is an arbitrary user value. */
34PyAPI_FUNC(void) PyMem_GetRawAllocators(PyMemAllocators *allocators);
35
36/* Set internal functions of PyMem_RawMalloc(), PyMem_RawRealloc() and
37 PyMem_RawFree(). ctx is an arbitrary user value.
38
39 PyMem_SetupDebugHooks() should be called to reinstall debug hooks if new
40 functions do no call original functions anymore. */
41PyAPI_FUNC(void) PyMem_SetRawAllocators(PyMemAllocators *allocators);
42
43PyAPI_FUNC(void *) PyMem_RawMalloc(size_t size);
44PyAPI_FUNC(void *) PyMem_RawRealloc(void *ptr, size_t new_size);
45PyAPI_FUNC(void) PyMem_RawFree(void *ptr);
46
47
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000048/* BEWARE:
49
Tim Peters8b078f92002-04-28 04:11:46 +000050 Each interface exports both functions and macros. Extension modules should
51 use the functions, to ensure binary compatibility across Python versions.
52 Because the Python implementation is free to change internal details, and
53 the macros may (or may not) expose details for speed, if you do use the
54 macros you must recompile your extensions with each Python release.
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000055
Tim Peters8b078f92002-04-28 04:11:46 +000056 Never mix calls to PyMem_ with calls to the platform malloc/realloc/
57 calloc/free. For example, on Windows different DLLs may end up using
58 different heaps, and if you use PyMem_Malloc you'll get the memory from the
59 heap used by the Python DLL; it could be a disaster if you free()'ed that
60 directly in your own extension. Using PyMem_Free instead ensures Python
61 can return the memory to the proper heap. As another example, in
62 PYMALLOC_DEBUG mode, Python wraps all calls to all PyMem_ and PyObject_
63 memory functions in special debugging wrappers that add additional
64 debugging info to dynamic memory blocks. The system routines have no idea
65 what to do with that stuff, and the Python wrappers have no idea what to do
66 with raw blocks obtained directly by the system routines then.
Guido van Rossum360e4b82007-05-14 22:51:27 +000067
68 The GIL must be held when using these APIs.
Tim Peters8b078f92002-04-28 04:11:46 +000069*/
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000070
71/*
72 * Raw memory interface
73 * ====================
74 */
75
Tim Peters8b078f92002-04-28 04:11:46 +000076/* Functions
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000077
Tim Peters8b078f92002-04-28 04:11:46 +000078 Functions supplying platform-independent semantics for malloc/realloc/
79 free. These functions make sure that allocating 0 bytes returns a distinct
Tim Petersaf3e8de2002-04-12 07:22:56 +000080 non-NULL pointer (whenever possible -- if we're flat out of memory, NULL
81 may be returned), even if the platform malloc and realloc don't.
82 Returned pointers must be checked for NULL explicitly. No action is
Tim Peters8b078f92002-04-28 04:11:46 +000083 performed on failure (no exception is set, no warning is printed, etc).
84*/
Tim Petersaf3e8de2002-04-12 07:22:56 +000085
Victor Stinner4d705622013-06-15 00:37:46 +020086PyAPI_FUNC(void *) PyMem_Malloc(size_t size);
87PyAPI_FUNC(void *) PyMem_Realloc(void *ptr, size_t new_size);
88PyAPI_FUNC(void) PyMem_Free(void *ptr);
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000089
Tim Petersaf3e8de2002-04-12 07:22:56 +000090/* Macros. */
Tim Peters51e7f5c2002-04-22 02:33:27 +000091
Martin v. Löwis39f59b02002-11-23 09:13:40 +000092/* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL
93 for malloc(0), which would be treated as an error. Some platforms
94 would return a pointer with no memory behind it, which would break
95 pymalloc. To solve these problems, allocate an extra byte. */
Georg Brandld492ad82008-07-23 16:13:07 +000096/* Returns NULL to indicate error if a negative size or size larger than
97 Py_ssize_t can represent is supplied. Helps prevents security holes. */
Victor Stinner4d705622013-06-15 00:37:46 +020098#define PyMem_MALLOC(n) PyMem_Malloc(n)
99#define PyMem_REALLOC(p, n) PyMem_Realloc(p, n)
100#define PyMem_FREE(p) PyMem_Free(p)
Tim Petersaf3e8de2002-04-12 07:22:56 +0000101
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000102/*
103 * Type-oriented memory interface
104 * ==============================
Tim Petersaf3e8de2002-04-12 07:22:56 +0000105 *
Georg Brandld492ad82008-07-23 16:13:07 +0000106 * Allocate memory for n objects of the given type. Returns a new pointer
107 * or NULL if the request was too large or memory allocation failed. Use
108 * these macros rather than doing the multiplication yourself so that proper
109 * overflow checking is always done.
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000110 */
111
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000112#define PyMem_New(type, n) \
Mark Dickinsonbbe63062010-02-14 14:08:54 +0000113 ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000114 ( (type *) PyMem_Malloc((n) * sizeof(type)) ) )
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000115#define PyMem_NEW(type, n) \
Mark Dickinsonbbe63062010-02-14 14:08:54 +0000116 ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000117 ( (type *) PyMem_MALLOC((n) * sizeof(type)) ) )
Tim Peters8b078f92002-04-28 04:11:46 +0000118
Georg Brandld492ad82008-07-23 16:13:07 +0000119/*
120 * The value of (p) is always clobbered by this macro regardless of success.
121 * The caller MUST check if (p) is NULL afterwards and deal with the memory
122 * error if so. This means the original value of (p) MUST be saved for the
123 * caller's memory error handler to not lose track of it.
124 */
Tim Peters8b078f92002-04-28 04:11:46 +0000125#define PyMem_Resize(p, type, n) \
Mark Dickinsonbbe63062010-02-14 14:08:54 +0000126 ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
Georg Brandld492ad82008-07-23 16:13:07 +0000127 (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
Tim Petersaf3e8de2002-04-12 07:22:56 +0000128#define PyMem_RESIZE(p, type, n) \
Mark Dickinsonbbe63062010-02-14 14:08:54 +0000129 ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
Georg Brandld492ad82008-07-23 16:13:07 +0000130 (type *) PyMem_REALLOC((p), (n) * sizeof(type)) )
Tim Petersa5d78cc2002-03-02 08:43:19 +0000131
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000132/* PyMem{Del,DEL} are left over from ancient days, and shouldn't be used
133 * anymore. They're just confusing aliases for PyMem_{Free,FREE} now.
134 */
135#define PyMem_Del PyMem_Free
136#define PyMem_DEL PyMem_FREE
Tim Petersddea2082002-03-23 10:03:50 +0000137
Victor Stinner4d705622013-06-15 00:37:46 +0200138/* Get internal functions of PyMem_Malloc(), PyMem_Realloc()
139 and PyMem_Free() */
140PyAPI_FUNC(void) PyMem_GetAllocators(PyMemAllocators *allocators);
141
142/* Set internal functions of PyMem_Malloc(), PyMem_Realloc() and PyMem_Free().
143
144 malloc(ctx, 0) and realloc(ctx, ptr, 0) must not return NULL: it would be
145 treated as an error.
146
147 PyMem_SetupDebugHooks() should be called to reinstall debug hooks if new
148 functions do no call original functions anymore. */
149PyAPI_FUNC(void) PyMem_SetAllocators(PyMemAllocators *allocators);
150
151/* Setup hooks to detect bugs in the following Python memory allocator
152 functions:
153
154 - PyMem_RawMalloc(), PyMem_RawRealloc(), PyMem_RawFree()
155 - PyMem_Malloc(), PyMem_Realloc(), PyMem_Free()
156 - PyObject_Malloc(), PyObject_Realloc() and PyObject_Free()
157
158 Newly allocated memory is filled with the byte 0xCB, freed memory is filled
159 with the byte 0xDB. Additionnal checks:
160
161 - detect API violations, ex: PyObject_Free() called on a buffer allocated
162 by PyMem_Malloc()
163 - detect write before the start of the buffer (buffer underflow)
164 - detect write after the end of the buffer (buffer overflow)
165
166 The function does nothing if Python is not compiled is debug mode. */
167PyAPI_FUNC(void) PyMem_SetupDebugHooks(void);
168
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000169#ifdef __cplusplus
170}
171#endif
172
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000173#endif /* !Py_PYMEM_H */