blob: 92cd5369589edb6a269daf7c036c079dc53c824c [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
Victor Stinner0507bf52013-07-07 02:05:46 +020052PyAPI_FUNC(void *) PyMem_Malloc(size_t size);
53PyAPI_FUNC(void *) PyMem_Realloc(void *ptr, size_t new_size);
54PyAPI_FUNC(void) PyMem_Free(void *ptr);
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000055
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000056/*
57 * Type-oriented memory interface
58 * ==============================
Tim Petersaf3e8de2002-04-12 07:22:56 +000059 *
Georg Brandld492ad82008-07-23 16:13:07 +000060 * Allocate memory for n objects of the given type. Returns a new pointer
61 * or NULL if the request was too large or memory allocation failed. Use
62 * these macros rather than doing the multiplication yourself so that proper
63 * overflow checking is always done.
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000064 */
65
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000066#define PyMem_New(type, n) \
luzpaza5293b42017-11-05 07:37:50 -060067 ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
68 ( (type *) PyMem_Malloc((n) * sizeof(type)) ) )
Tim Peters8b078f92002-04-28 04:11:46 +000069
Georg Brandld492ad82008-07-23 16:13:07 +000070/*
71 * The value of (p) is always clobbered by this macro regardless of success.
72 * The caller MUST check if (p) is NULL afterwards and deal with the memory
73 * error if so. This means the original value of (p) MUST be saved for the
74 * caller's memory error handler to not lose track of it.
75 */
Tim Peters8b078f92002-04-28 04:11:46 +000076#define PyMem_Resize(p, type, n) \
luzpaza5293b42017-11-05 07:37:50 -060077 ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
78 (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
Tim Petersa5d78cc2002-03-02 08:43:19 +000079
Victor Stinner00d7abd2020-12-01 09:56:42 +010080
81// Deprecated aliases only kept for backward compatibility.
Victor Stinner32bd68c2020-12-01 10:37:39 +010082// PyMem_Del and PyMem_DEL are defined with no parameter to be able to use
83// them as function pointers (ex: dealloc = PyMem_Del).
Victor Stinner00d7abd2020-12-01 09:56:42 +010084#define PyMem_MALLOC(n) PyMem_Malloc(n)
85#define PyMem_NEW(type, n) PyMem_New(type, n)
86#define PyMem_REALLOC(p, n) PyMem_Realloc(p, n)
87#define PyMem_RESIZE(p, type, n) PyMem_Resize(p, type, n)
88#define PyMem_FREE(p) PyMem_Free(p)
Victor Stinner32bd68c2020-12-01 10:37:39 +010089#define PyMem_Del PyMem_Free
90#define PyMem_DEL PyMem_Free
Tim Petersddea2082002-03-23 10:03:50 +000091
Victor Stinner9820c072019-04-15 17:00:19 +020092
93#ifndef Py_LIMITED_API
94# define Py_CPYTHON_PYMEM_H
95# include "cpython/pymem.h"
96# undef Py_CPYTHON_PYMEM_H
97#endif
98
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000099#ifdef __cplusplus
100}
101#endif
102
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000103#endif /* !Py_PYMEM_H */