blob: d09f38c72f60153f87ec78972fcf7481116302c3 [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
13/*
14 * Core memory allocator
15 * =====================
16 */
17
18/* To make sure the interpreter is user-malloc friendly, all memory
19 APIs are implemented on top of this one.
20
21 The PyCore_* macros can be defined to make the interpreter use a
22 custom allocator. Note that they are for internal use only. Both
23 the core and extension modules should use the PyMem_* API.
24
25 See the comment block at the end of this file for two scenarios
26 showing how to use this to use a different allocator. */
27
28#ifndef PyCore_MALLOC_FUNC
29#undef PyCore_REALLOC_FUNC
30#undef PyCore_FREE_FUNC
31#define PyCore_MALLOC_FUNC malloc
32#define PyCore_REALLOC_FUNC realloc
33#define PyCore_FREE_FUNC free
34#endif
35
36#ifndef PyCore_MALLOC_PROTO
37#undef PyCore_REALLOC_PROTO
38#undef PyCore_FREE_PROTO
39#define PyCore_MALLOC_PROTO (size_t)
40#define PyCore_REALLOC_PROTO (void *, size_t)
41#define PyCore_FREE_PROTO (void *)
42#endif
43
44#ifdef NEED_TO_DECLARE_MALLOC_AND_FRIEND
45extern void *PyCore_MALLOC_FUNC PyCore_MALLOC_PROTO;
46extern void *PyCore_REALLOC_FUNC PyCore_REALLOC_PROTO;
47extern void PyCore_FREE_FUNC PyCore_FREE_PROTO;
48#endif
49
50#ifndef PyCore_MALLOC
51#undef PyCore_REALLOC
52#undef PyCore_FREE
53#define PyCore_MALLOC(n) PyCore_MALLOC_FUNC(n)
54#define PyCore_REALLOC(p, n) PyCore_REALLOC_FUNC((p), (n))
55#define PyCore_FREE(p) PyCore_FREE_FUNC(p)
56#endif
57
58/* BEWARE:
59
60 Each interface exports both functions and macros. Extension modules
61 should normally use the functions for ensuring binary compatibility
62 of the user's code across Python versions. Subsequently, if the
63 Python runtime switches to its own malloc (different from standard
64 malloc), no recompilation is required for the extensions.
65
66 The macro versions trade compatibility for speed. They can be used
67 whenever there is a performance problem, but their use implies
68 recompilation of the code for each new Python release. The Python
69 core uses the macros because it *is* compiled on every upgrade.
70 This might not be the case with 3rd party extensions in a custom
71 setup (for example, a customer does not always have access to the
72 source of 3rd party deliverables). You have been warned! */
73
74/*
75 * Raw memory interface
76 * ====================
77 */
78
79/* Functions */
80
81/* Function wrappers around PyCore_MALLOC and friends; useful if you
82 need to be sure that you are using the same memory allocator as
83 Python. Note that the wrappers make sure that allocating 0 bytes
84 returns a non-NULL pointer, even if the underlying malloc
85 doesn't. Returned pointers must be checked for NULL explicitly.
86 No action is performed on failure. */
87extern DL_IMPORT(void *) PyMem_Malloc(size_t);
88extern DL_IMPORT(void *) PyMem_Realloc(void *, size_t);
89extern DL_IMPORT(void) PyMem_Free(void *);
90
91/* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are
92 no longer supported. They used to call PyErr_NoMemory() on failure. */
93
94/* Macros */
95#define PyMem_MALLOC(n) PyCore_MALLOC(n)
96#define PyMem_REALLOC(p, n) PyCore_REALLOC((void *)(p), (n))
97#define PyMem_FREE(p) PyCore_FREE((void *)(p))
98
99/*
100 * Type-oriented memory interface
101 * ==============================
102 */
103
104/* Functions */
105#define PyMem_New(type, n) \
106 ( (type *) PyMem_Malloc((n) * sizeof(type)) )
107#define PyMem_Resize(p, type, n) \
Vladimir Marangozovdcb45c32000-08-13 11:59:08 +0000108 ( (p) = (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000109#define PyMem_Del(p) PyMem_Free(p)
110
111/* Macros */
112#define PyMem_NEW(type, n) \
113 ( (type *) PyMem_MALLOC(_PyMem_EXTRA + (n) * sizeof(type)) )
Tim Petersa5d78cc2002-03-02 08:43:19 +0000114
115/* See comment near MALLOC_ZERO_RETURNS_NULL in pyport.h. */
116#define PyMem_RESIZE(p, type, n) \
117 do { \
118 size_t _sum = (n) * sizeof(type); \
119 if (!_sum) \
120 _sum = 1; \
121 (p) = (type *)((p) ? \
122 PyMem_REALLOC(p, _sum) : \
123 PyMem_MALLOC(_sum)); \
124 } while (0)
125
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000126#define PyMem_DEL(p) PyMem_FREE(p)
127
128/* PyMem_XDEL is deprecated. To avoid the call when p is NULL,
129 it is recommended to write the test explicitly in the code.
130 Note that according to ANSI C, free(NULL) has no effect. */
131
132#ifdef __cplusplus
133}
134#endif
135
136/* SCENARIOS
137
138 Here are two scenarios by Vladimir Marangozov (the author of the
139 memory allocation redesign).
140
141 1) Scenario A
142
143 Suppose you want to use a debugging malloc library that collects info on
144 where the malloc calls originate from. Assume the interface is:
145
146 d_malloc(size_t n, char* src_file, unsigned long src_line) c.s.
147
Tim Peters76f373d2001-07-26 21:34:59 +0000148 In this case, you would define (for example in pyconfig.h) :
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000149
150 #define PyCore_MALLOC_FUNC d_malloc
151 ...
152 #define PyCore_MALLOC_PROTO (size_t, char *, unsigned long)
153 ...
154 #define NEED_TO_DECLARE_MALLOC_AND_FRIEND
155
156 #define PyCore_MALLOC(n) PyCore_MALLOC_FUNC((n), __FILE__, __LINE__)
157 ...
158
159 2) Scenario B
160
161 Suppose you want to use malloc hooks (defined & initialized in a 3rd party
162 malloc library) instead of malloc functions. In this case, you would
163 define:
164
165 #define PyCore_MALLOC_FUNC (*malloc_hook)
166 ...
167 #define NEED_TO_DECLARE_MALLOC_AND_FRIEND
168
169 and ignore the previous definitions about PyCore_MALLOC_FUNC, etc.
170
171
172*/
173
174
175#endif /* !Py_PYMEM_H */