blob: 3b9c0e2988370dff33a98da84b1deda6f627a852 [file] [log] [blame]
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +00001
2/* Lowest-level memory allocation interface */
3
4#ifndef Py_PYMEM_H
5#define Py_PYMEM_H
6
7#include "pyport.h"
8
9#ifdef __cplusplus
10extern "C" {
11#endif
12
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000013/* BEWARE:
14
15 Each interface exports both functions and macros. Extension modules
16 should normally use the functions for ensuring binary compatibility
17 of the user's code across Python versions. Subsequently, if the
18 Python runtime switches to its own malloc (different from standard
19 malloc), no recompilation is required for the extensions.
20
Tim Petersaf3e8de2002-04-12 07:22:56 +000021 The macro versions are free to trade compatibility for speed, although
22 there's no guarantee they're ever faster. Extensions shouldn't use the
23 macro versions, as they don't gurantee binary compatibility across
24 releases.
25
26 Do not mix calls to PyMem_xyz with calls to platform
27 malloc/realloc/calloc/free. */
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000028
29/*
30 * Raw memory interface
31 * ====================
32 */
33
34/* Functions */
35
Tim Petersaf3e8de2002-04-12 07:22:56 +000036/* Functions supplying platform-independent semantics for malloc/realloc/
37 free; useful if you need to be sure you're using the same memory
38 allocator as Python (this can be especially important on Windows, if
39 you need to make sure you're using the same MS malloc/free, and out of
40 the same heap, as the main Python DLL uses).
41 These functions make sure that allocating 0 bytes returns a distinct
42 non-NULL pointer (whenever possible -- if we're flat out of memory, NULL
43 may be returned), even if the platform malloc and realloc don't.
44 Returned pointers must be checked for NULL explicitly. No action is
45 performed on failure (no exception is set, no warning is printed, etc).` */
46
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000047extern DL_IMPORT(void *) PyMem_Malloc(size_t);
48extern DL_IMPORT(void *) PyMem_Realloc(void *, size_t);
49extern DL_IMPORT(void) PyMem_Free(void *);
50
51/* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are
52 no longer supported. They used to call PyErr_NoMemory() on failure. */
53
Tim Petersaf3e8de2002-04-12 07:22:56 +000054/* Macros. */
Neil Schemenauer25f3dc22002-03-18 21:06:21 +000055#ifndef PyMem_MALLOC
Tim Petersaf3e8de2002-04-12 07:22:56 +000056#ifdef MALLOC_ZERO_RETURNS_NULL
57#define PyMem_MALLOC(n) malloc((n) ? (n) : 1)
58#else
59#define PyMem_MALLOC malloc
Neil Schemenauer25f3dc22002-03-18 21:06:21 +000060#endif
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000061
Tim Petersaf3e8de2002-04-12 07:22:56 +000062/* Caution: whether MALLOC_ZERO_RETURNS_NULL is #defined has nothing to
63 do with whether platform realloc(non-NULL, 0) normally frees the memory
64 or returns NULL. Rather than introduce yet another config variation,
65 just make a realloc to 0 bytes act as if to 1 instead. */
66#define PyMem_REALLOC(p, n) realloc((p), (n) ? (n) : 1)
67
68#define PyMem_FREE free
69#endif /* PyMem_MALLOC */
70
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000071/*
72 * Type-oriented memory interface
73 * ==============================
Tim Petersaf3e8de2002-04-12 07:22:56 +000074 *
75 * These are carried along for historical reasons. There's rarely a good
76 * reason to use them anymore.
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000077 */
78
79/* Functions */
80#define PyMem_New(type, n) \
81 ( (type *) PyMem_Malloc((n) * sizeof(type)) )
82#define PyMem_Resize(p, type, n) \
Vladimir Marangozovdcb45c32000-08-13 11:59:08 +000083 ( (p) = (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
Tim Petersaf3e8de2002-04-12 07:22:56 +000084
85/* In order to avoid breaking old code mixing PyObject_{New, NEW} with
86 PyMem_{Del, DEL} (there was no choice about this in 1.5.2), the latter
87 have to be redirected to the object allocator. */
88/* XXX The parser module needs rework before this can be enabled. */
89#if 0
90#define PyMem_Del PyObject_Free
91#else
92#define PyMem_Del PyMem_Free
93#endif
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000094
95/* Macros */
96#define PyMem_NEW(type, n) \
Tim Petersaf3e8de2002-04-12 07:22:56 +000097 ( (type *) PyMem_MALLOC((n) * sizeof(type)) )
98#define PyMem_RESIZE(p, type, n) \
99 ( (p) = (type *) PyMem_REALLOC((p), (n) * sizeof(type)) )
Tim Petersa5d78cc2002-03-02 08:43:19 +0000100
Tim Petersaf3e8de2002-04-12 07:22:56 +0000101/* XXX The parser module needs rework before this can be enabled. */
102#if 0
103#define PyMem_DEL PyObject_FREE
104#else
105#define PyMem_DEL PyMem_FREE
106#endif
Tim Petersddea2082002-03-23 10:03:50 +0000107
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000108#ifdef __cplusplus
109}
110#endif
111
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000112#endif /* !Py_PYMEM_H */