blob: 3d6ab063d629f77fb5dd52d3f3fddb953ec2d91f [file] [log] [blame]
Guido van Rossuma3309961993-07-28 09:05:47 +00001#ifndef Py_MYMALLOC_H
2#define Py_MYMALLOC_H
Guido van Rossum1e28e5e1992-08-19 16:46:30 +00003/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00004Copyright (c) 2000, BeOpen.com.
5Copyright (c) 1995-2000, Corporation for National Research Initiatives.
6Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
7All rights reserved.
Guido van Rossum1e28e5e1992-08-19 16:46:30 +00008
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00009See the file "Misc/COPYRIGHT" for information on usage and
10redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000011******************************************************************/
12
13/* Lowest-level memory allocation interface */
14
15#ifdef macintosh
16#define ANY void
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000017#endif
18
19#ifdef __STDC__
20#define ANY void
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000021#endif
22
Guido van Rossum12d12c51993-10-26 17:58:25 +000023#ifdef __TURBOC__
24#define ANY void
Guido van Rossum12d12c51993-10-26 17:58:25 +000025#endif
26
27#ifdef __GNUC__
28#define ANY void
Guido van Rossum12d12c51993-10-26 17:58:25 +000029#endif
30
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000031#ifndef ANY
32#define ANY char
33#endif
34
Guido van Rossumb6775db1994-08-01 11:34:53 +000035#ifdef HAVE_STDLIB_H
36#include <stdlib.h>
Guido van Rossum6c1874f1995-01-10 17:43:33 +000037#endif
38
Guido van Rossumb18618d2000-05-03 23:44:39 +000039#include "myproto.h"
40
Guido van Rossum6c1874f1995-01-10 17:43:33 +000041#ifdef __cplusplus
Guido van Rossumd085e881997-08-05 01:59:22 +000042/* Move this down here since some C++ #include's don't like to be included
43 inside an extern "C" */
Guido van Rossum6c1874f1995-01-10 17:43:33 +000044extern "C" {
45#endif
46
Jack Jansenf9480ce1995-06-27 13:12:09 +000047#ifdef SYMANTEC__CFM68K__
Guido van Rossum0acd4b61995-02-18 14:50:12 +000048#pragma lib_export on
49#endif
50
Guido van Rossumb18618d2000-05-03 23:44:39 +000051#ifndef DL_IMPORT /* declarations for DLL import */
52#define DL_IMPORT(RTYPE) RTYPE
Guido van Rossumab589b91997-08-21 16:13:37 +000053#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000054
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000055#ifndef NULL
Guido van Rossumb6775db1994-08-01 11:34:53 +000056#define NULL ((ANY *)0)
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000057#endif
58
Guido van Rossum4b11c741997-07-10 22:40:54 +000059#ifdef MALLOC_ZERO_RETURNS_NULL
Guido van Rossum5f59d601992-12-14 16:59:51 +000060/* XXX Always allocate one extra byte, since some malloc's return NULL
61 XXX for malloc(0) or realloc(p, 0). */
Guido van Rossum4b11c741997-07-10 22:40:54 +000062#define _PyMem_EXTRA 1
63#else
64#define _PyMem_EXTRA 0
65#endif
66
Guido van Rossumb18618d2000-05-03 23:44:39 +000067/*
68 * Core memory allocator
69 * =====================
70 */
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000071
Guido van Rossumb18618d2000-05-03 23:44:39 +000072/* To make sure the interpreter is user-malloc friendly, all memory
73 APIs are implemented on top of this one.
Guido van Rossumd085e881997-08-05 01:59:22 +000074
Guido van Rossumb18618d2000-05-03 23:44:39 +000075 The PyCore_* macros can be defined to make the interpreter use a
76 custom allocator. Note that they are for internal use only. Both
Guido van Rossum3a03d4c2000-05-05 15:36:09 +000077 the core and extension modules should use the PyMem_* API.
78
79 See the comment block at the end of this file for two scenarios
80 showing how to use this to use a different allocator. */
Guido van Rossumb18618d2000-05-03 23:44:39 +000081
82#ifndef PyCore_MALLOC_FUNC
83#undef PyCore_REALLOC_FUNC
84#undef PyCore_FREE_FUNC
85#define PyCore_MALLOC_FUNC malloc
86#define PyCore_REALLOC_FUNC realloc
87#define PyCore_FREE_FUNC free
88#endif
89
90#ifndef PyCore_MALLOC_PROTO
91#undef PyCore_REALLOC_PROTO
92#undef PyCore_FREE_PROTO
93#define PyCore_MALLOC_PROTO Py_PROTO((size_t))
94#define PyCore_REALLOC_PROTO Py_PROTO((ANY *, size_t))
95#define PyCore_FREE_PROTO Py_PROTO((ANY *))
96#endif
97
98#ifdef NEED_TO_DECLARE_MALLOC_AND_FRIEND
99extern ANY *PyCore_MALLOC_FUNC PyCore_MALLOC_PROTO;
100extern ANY *PyCore_REALLOC_FUNC PyCore_REALLOC_PROTO;
101extern void PyCore_FREE_FUNC PyCore_FREE_PROTO;
102#endif
103
104#ifndef PyCore_MALLOC
105#undef PyCore_REALLOC
106#undef PyCore_FREE
107#define PyCore_MALLOC(n) PyCore_MALLOC_FUNC(n)
108#define PyCore_REALLOC(p, n) PyCore_REALLOC_FUNC((p), (n))
109#define PyCore_FREE(p) PyCore_FREE_FUNC(p)
110#endif
111
112/* BEWARE:
113
114 Each interface exports both functions and macros. Extension modules
115 should normally use the functions for ensuring binary compatibility
116 of the user's code across Python versions. Subsequently, if the
117 Python runtime switches to its own malloc (different from standard
118 malloc), no recompilation is required for the extensions.
119
120 The macro versions trade compatibility for speed. They can be used
121 whenever there is a performance problem, but their use implies
122 recompilation of the code for each new Python release. The Python
123 core uses the macros because it *is* compiled on every upgrade.
124 This might not be the case with 3rd party extensions in a custom
125 setup (for example, a customer does not always have access to the
126 source of 3rd party deliverables). You have been warned! */
127
128/*
129 * Raw memory interface
130 * ====================
131 */
132
133/* Functions */
134
135/* Function wrappers around PyCore_MALLOC and friends; useful if you
136 need to be sure that you are using the same memory allocator as
Guido van Rossumd085e881997-08-05 01:59:22 +0000137 Python. Note that the wrappers make sure that allocating 0 bytes
Guido van Rossumb18618d2000-05-03 23:44:39 +0000138 returns a non-NULL pointer, even if the underlying malloc
139 doesn't. Returned pointers must be checked for NULL explicitly.
140 No action is performed on failure. */
Guido van Rossum43466ec1998-12-04 18:48:25 +0000141extern DL_IMPORT(ANY *) PyMem_Malloc Py_PROTO((size_t));
142extern DL_IMPORT(ANY *) PyMem_Realloc Py_PROTO((ANY *, size_t));
143extern DL_IMPORT(void) PyMem_Free Py_PROTO((ANY *));
Guido van Rossumd085e881997-08-05 01:59:22 +0000144
Guido van Rossumb18618d2000-05-03 23:44:39 +0000145/* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are
146 no longer supported. They used to call PyErr_NoMemory() on failure. */
147
148/* Macros */
149#define PyMem_MALLOC(n) PyCore_MALLOC(n)
150#define PyMem_REALLOC(p, n) PyCore_REALLOC((ANY *)(p), (n))
151#define PyMem_FREE(p) PyCore_FREE((ANY *)(p))
152
153/*
154 * Type-oriented memory interface
155 * ==============================
156 */
157
158/* Functions */
159#define PyMem_New(type, n) \
160 ( (type *) PyMem_Malloc((n) * sizeof(type)) )
161#define PyMem_Resize(p, type, n) \
162 ( (p) = (type *) PyMem_Realloc((n) * sizeof(type)) )
163#define PyMem_Del(p) PyMem_Free(p)
164
165/* Macros */
166#define PyMem_NEW(type, n) \
167 ( (type *) PyMem_MALLOC(_PyMem_EXTRA + (n) * sizeof(type)) )
168#define PyMem_RESIZE(p, type, n) \
169 if ((p) == NULL) \
170 (p) = (type *)(PyMem_MALLOC( \
171 _PyMem_EXTRA + (n) * sizeof(type))); \
172 else \
173 (p) = (type *)(PyMem_REALLOC((p), \
174 _PyMem_EXTRA + (n) * sizeof(type)))
175#define PyMem_DEL(p) PyMem_FREE(p)
176
177/* PyMem_XDEL is deprecated. To avoid the call when p is NULL,
178 it is recommended to write the test explicitly in the code.
179 Note that according to ANSI C, free(NULL) has no effect. */
180
Guido van Rossuma3309961993-07-28 09:05:47 +0000181#ifdef __cplusplus
182}
183#endif
Guido van Rossumcaa63801995-01-12 11:45:45 +0000184
Guido van Rossum3a03d4c2000-05-05 15:36:09 +0000185/* SCENARIOS
186
187 Here are two scenarios by Vladimir Marangozov (the author of the
188 memory allocation redesign).
189
190 1) Scenario A
191
192 Suppose you want to use a debugging malloc library that collects info on
193 where the malloc calls originate from. Assume the interface is:
194
195 d_malloc(size_t n, char* src_file, unsigned long src_line) c.s.
196
197 In this case, you would define (for example in config.h) :
198
199 #define PyCore_MALLOC_FUNC d_malloc
200 ...
201 #define PyCore_MALLOC_PROTO Py_PROTO((size_t, char *, unsigned long))
202 ...
203 #define NEED_TO_DECLARE_MALLOC_AND_FRIEND
204
205 #define PyCore_MALLOC(n) PyCore_MALLOC_FUNC((n), __FILE__, __LINE__)
206 ...
207
208 2) Scenario B
209
210 Suppose you want to use malloc hooks (defined & initialized in a 3rd party
211 malloc library) instead of malloc functions. In this case, you would
212 define:
213
214 #define PyCore_MALLOC_FUNC (*malloc_hook)
215 ...
216 #define NEED_TO_DECLARE_MALLOC_AND_FRIEND
217
218 and ignore the previous definitions about PyCore_MALLOC_FUNC, etc.
219
220
221*/
222
223
Guido van Rossuma3309961993-07-28 09:05:47 +0000224#endif /* !Py_MYMALLOC_H */