blob: 6b1105ccf720a39deff835fcd5df6e5f24b69de7 [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.
33*/
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000034
35/*
36 * Raw memory interface
37 * ====================
38 */
39
Tim Peters8b078f92002-04-28 04:11:46 +000040/* Functions
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000041
Tim Peters8b078f92002-04-28 04:11:46 +000042 Functions supplying platform-independent semantics for malloc/realloc/
43 free. These functions make sure that allocating 0 bytes returns a distinct
Tim Petersaf3e8de2002-04-12 07:22:56 +000044 non-NULL pointer (whenever possible -- if we're flat out of memory, NULL
45 may be returned), even if the platform malloc and realloc don't.
46 Returned pointers must be checked for NULL explicitly. No action is
Tim Peters8b078f92002-04-28 04:11:46 +000047 performed on failure (no exception is set, no warning is printed, etc).
48*/
Tim Petersaf3e8de2002-04-12 07:22:56 +000049
Mark Hammond91a681d2002-08-12 07:21:58 +000050PyAPI_FUNC(void *) PyMem_Malloc(size_t);
51PyAPI_FUNC(void *) PyMem_Realloc(void *, size_t);
52PyAPI_FUNC(void) PyMem_Free(void *);
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000053
54/* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are
55 no longer supported. They used to call PyErr_NoMemory() on failure. */
56
Tim Petersaf3e8de2002-04-12 07:22:56 +000057/* Macros. */
Tim Peters51e7f5c2002-04-22 02:33:27 +000058#ifdef PYMALLOC_DEBUG
59/* Redirect all memory operations to Python's debugging allocator. */
60#define PyMem_MALLOC PyObject_MALLOC
61#define PyMem_REALLOC PyObject_REALLOC
Tim Peters51e7f5c2002-04-22 02:33:27 +000062
63#else /* ! PYMALLOC_DEBUG */
64
Tim Petersaf3e8de2002-04-12 07:22:56 +000065#ifdef MALLOC_ZERO_RETURNS_NULL
66#define PyMem_MALLOC(n) malloc((n) ? (n) : 1)
67#else
68#define PyMem_MALLOC malloc
Neil Schemenauer25f3dc22002-03-18 21:06:21 +000069#endif
Tim Peters8b078f92002-04-28 04:11:46 +000070
Tim Petersaf3e8de2002-04-12 07:22:56 +000071/* Caution: whether MALLOC_ZERO_RETURNS_NULL is #defined has nothing to
72 do with whether platform realloc(non-NULL, 0) normally frees the memory
73 or returns NULL. Rather than introduce yet another config variation,
74 just make a realloc to 0 bytes act as if to 1 instead. */
75#define PyMem_REALLOC(p, n) realloc((p), (n) ? (n) : 1)
76
Tim Peters51e7f5c2002-04-22 02:33:27 +000077#endif /* PYMALLOC_DEBUG */
Tim Petersaf3e8de2002-04-12 07:22:56 +000078
Tim Peters8b078f92002-04-28 04:11:46 +000079/* In order to avoid breaking old code mixing PyObject_{New, NEW} with
80 PyMem_{Del, DEL} and PyMem_{Free, FREE}, the PyMem "release memory"
81 functions have to be redirected to the object deallocator. */
82#define PyMem_FREE PyObject_FREE
83
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000084/*
85 * Type-oriented memory interface
86 * ==============================
Tim Petersaf3e8de2002-04-12 07:22:56 +000087 *
88 * These are carried along for historical reasons. There's rarely a good
Tim Peters8b078f92002-04-28 04:11:46 +000089 * reason to use them anymore (you can just as easily do the multiply and
90 * cast yourself).
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000091 */
92
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000093#define PyMem_New(type, n) \
94 ( (type *) PyMem_Malloc((n) * sizeof(type)) )
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000095#define PyMem_NEW(type, n) \
Tim Petersaf3e8de2002-04-12 07:22:56 +000096 ( (type *) PyMem_MALLOC((n) * sizeof(type)) )
Tim Peters8b078f92002-04-28 04:11:46 +000097
98#define PyMem_Resize(p, type, n) \
99 ( (p) = (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
Tim Petersaf3e8de2002-04-12 07:22:56 +0000100#define PyMem_RESIZE(p, type, n) \
101 ( (p) = (type *) PyMem_REALLOC((p), (n) * sizeof(type)) )
Tim Petersa5d78cc2002-03-02 08:43:19 +0000102
Tim Peters8b078f92002-04-28 04:11:46 +0000103/* In order to avoid breaking old code mixing PyObject_{New, NEW} with
104 PyMem_{Del, DEL} and PyMem_{Free, FREE}, the PyMem "release memory"
105 functions have to be redirected to the object deallocator. */
106#define PyMem_Del PyObject_Free
107#define PyMem_DEL PyObject_FREE
Tim Petersddea2082002-03-23 10:03:50 +0000108
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000109#ifdef __cplusplus
110}
111#endif
112
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000113#endif /* !Py_PYMEM_H */