blob: 2330366f483070d3ba00c92611e6ac1a91094444 [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. */
Tim Peters51e7f5c2002-04-22 02:33:27 +000055#ifdef PYMALLOC_DEBUG
56/* Redirect all memory operations to Python's debugging allocator. */
57#define PyMem_MALLOC PyObject_MALLOC
58#define PyMem_REALLOC PyObject_REALLOC
59#define PyMem_FREE PyObject_FREE
60
61#else /* ! PYMALLOC_DEBUG */
62
Tim Petersaf3e8de2002-04-12 07:22:56 +000063#ifdef MALLOC_ZERO_RETURNS_NULL
64#define PyMem_MALLOC(n) malloc((n) ? (n) : 1)
65#else
66#define PyMem_MALLOC malloc
Neil Schemenauer25f3dc22002-03-18 21:06:21 +000067#endif
Tim Petersaf3e8de2002-04-12 07:22:56 +000068/* Caution: whether MALLOC_ZERO_RETURNS_NULL is #defined has nothing to
69 do with whether platform realloc(non-NULL, 0) normally frees the memory
70 or returns NULL. Rather than introduce yet another config variation,
71 just make a realloc to 0 bytes act as if to 1 instead. */
72#define PyMem_REALLOC(p, n) realloc((p), (n) ? (n) : 1)
73
74#define PyMem_FREE free
Tim Peters51e7f5c2002-04-22 02:33:27 +000075#endif /* PYMALLOC_DEBUG */
Tim Petersaf3e8de2002-04-12 07:22:56 +000076
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000077/*
78 * Type-oriented memory interface
79 * ==============================
Tim Petersaf3e8de2002-04-12 07:22:56 +000080 *
81 * These are carried along for historical reasons. There's rarely a good
82 * reason to use them anymore.
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000083 */
84
85/* Functions */
86#define PyMem_New(type, n) \
87 ( (type *) PyMem_Malloc((n) * sizeof(type)) )
88#define PyMem_Resize(p, type, n) \
Vladimir Marangozovdcb45c32000-08-13 11:59:08 +000089 ( (p) = (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
Tim Petersaf3e8de2002-04-12 07:22:56 +000090
91/* In order to avoid breaking old code mixing PyObject_{New, NEW} with
92 PyMem_{Del, DEL} (there was no choice about this in 1.5.2), the latter
93 have to be redirected to the object allocator. */
Tim Petersaf3e8de2002-04-12 07:22:56 +000094#define PyMem_Del PyObject_Free
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000095
96/* Macros */
97#define PyMem_NEW(type, n) \
Tim Petersaf3e8de2002-04-12 07:22:56 +000098 ( (type *) PyMem_MALLOC((n) * sizeof(type)) )
99#define PyMem_RESIZE(p, type, n) \
100 ( (p) = (type *) PyMem_REALLOC((p), (n) * sizeof(type)) )
Tim Petersa5d78cc2002-03-02 08:43:19 +0000101
Tim Petersaf3e8de2002-04-12 07:22:56 +0000102#define PyMem_DEL PyObject_FREE
Tim Petersddea2082002-03-23 10:03:50 +0000103
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000104#ifdef __cplusplus
105}
106#endif
107
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000108#endif /* !Py_PYMEM_H */