blob: e00ef24e5429068ee909a39344ddda67b0beb11d [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 Rossum5799b521995-01-04 19:06:22 +00004Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
5The Netherlands.
Guido van Rossum1e28e5e1992-08-19 16:46:30 +00006
7 All Rights Reserved
8
Guido van Rossumd266eb41996-10-25 14:44:06 +00009Permission to use, copy, modify, and distribute this software and its
10documentation for any purpose and without fee is hereby granted,
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000011provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000012both that copyright notice and this permission notice appear in
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000013supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000014Centrum or CWI or Corporation for National Research Initiatives or
15CNRI not be used in advertising or publicity pertaining to
16distribution of the software without specific, written prior
17permission.
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000018
Guido van Rossumd266eb41996-10-25 14:44:06 +000019While CWI is the initial source for this software, a modified version
20is made available by the Corporation for National Research Initiatives
21(CNRI) at the Internet address ftp://ftp.python.org.
22
23STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
24REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
25MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
26CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
27DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
28PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
29TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
30PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000031
32******************************************************************/
33
34/* Lowest-level memory allocation interface */
35
36#ifdef macintosh
37#define ANY void
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000038#endif
39
40#ifdef __STDC__
41#define ANY void
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000042#endif
43
Guido van Rossum12d12c51993-10-26 17:58:25 +000044#ifdef __TURBOC__
45#define ANY void
Guido van Rossum12d12c51993-10-26 17:58:25 +000046#endif
47
48#ifdef __GNUC__
49#define ANY void
Guido van Rossum12d12c51993-10-26 17:58:25 +000050#endif
51
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000052#ifndef ANY
53#define ANY char
54#endif
55
Guido van Rossumb6775db1994-08-01 11:34:53 +000056#ifdef HAVE_STDLIB_H
57#include <stdlib.h>
Guido van Rossum6c1874f1995-01-10 17:43:33 +000058#endif
59
Guido van Rossumb18618d2000-05-03 23:44:39 +000060#include "myproto.h"
61
Guido van Rossum6c1874f1995-01-10 17:43:33 +000062#ifdef __cplusplus
Guido van Rossumd085e881997-08-05 01:59:22 +000063/* Move this down here since some C++ #include's don't like to be included
64 inside an extern "C" */
Guido van Rossum6c1874f1995-01-10 17:43:33 +000065extern "C" {
66#endif
67
Jack Jansenf9480ce1995-06-27 13:12:09 +000068#ifdef SYMANTEC__CFM68K__
Guido van Rossum0acd4b61995-02-18 14:50:12 +000069#pragma lib_export on
70#endif
71
Guido van Rossumb18618d2000-05-03 23:44:39 +000072#ifndef DL_IMPORT /* declarations for DLL import */
73#define DL_IMPORT(RTYPE) RTYPE
Guido van Rossumab589b91997-08-21 16:13:37 +000074#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000075
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000076#ifndef NULL
Guido van Rossumb6775db1994-08-01 11:34:53 +000077#define NULL ((ANY *)0)
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000078#endif
79
Guido van Rossum4b11c741997-07-10 22:40:54 +000080#ifdef MALLOC_ZERO_RETURNS_NULL
Guido van Rossum5f59d601992-12-14 16:59:51 +000081/* XXX Always allocate one extra byte, since some malloc's return NULL
82 XXX for malloc(0) or realloc(p, 0). */
Guido van Rossum4b11c741997-07-10 22:40:54 +000083#define _PyMem_EXTRA 1
84#else
85#define _PyMem_EXTRA 0
86#endif
87
Guido van Rossumb18618d2000-05-03 23:44:39 +000088/*
89 * Core memory allocator
90 * =====================
91 */
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000092
Guido van Rossumb18618d2000-05-03 23:44:39 +000093/* To make sure the interpreter is user-malloc friendly, all memory
94 APIs are implemented on top of this one.
Guido van Rossumd085e881997-08-05 01:59:22 +000095
Guido van Rossumb18618d2000-05-03 23:44:39 +000096 The PyCore_* macros can be defined to make the interpreter use a
97 custom allocator. Note that they are for internal use only. Both
Guido van Rossum3a03d4c2000-05-05 15:36:09 +000098 the core and extension modules should use the PyMem_* API.
99
100 See the comment block at the end of this file for two scenarios
101 showing how to use this to use a different allocator. */
Guido van Rossumb18618d2000-05-03 23:44:39 +0000102
103#ifndef PyCore_MALLOC_FUNC
104#undef PyCore_REALLOC_FUNC
105#undef PyCore_FREE_FUNC
106#define PyCore_MALLOC_FUNC malloc
107#define PyCore_REALLOC_FUNC realloc
108#define PyCore_FREE_FUNC free
109#endif
110
111#ifndef PyCore_MALLOC_PROTO
112#undef PyCore_REALLOC_PROTO
113#undef PyCore_FREE_PROTO
114#define PyCore_MALLOC_PROTO Py_PROTO((size_t))
115#define PyCore_REALLOC_PROTO Py_PROTO((ANY *, size_t))
116#define PyCore_FREE_PROTO Py_PROTO((ANY *))
117#endif
118
119#ifdef NEED_TO_DECLARE_MALLOC_AND_FRIEND
120extern ANY *PyCore_MALLOC_FUNC PyCore_MALLOC_PROTO;
121extern ANY *PyCore_REALLOC_FUNC PyCore_REALLOC_PROTO;
122extern void PyCore_FREE_FUNC PyCore_FREE_PROTO;
123#endif
124
125#ifndef PyCore_MALLOC
126#undef PyCore_REALLOC
127#undef PyCore_FREE
128#define PyCore_MALLOC(n) PyCore_MALLOC_FUNC(n)
129#define PyCore_REALLOC(p, n) PyCore_REALLOC_FUNC((p), (n))
130#define PyCore_FREE(p) PyCore_FREE_FUNC(p)
131#endif
132
133/* BEWARE:
134
135 Each interface exports both functions and macros. Extension modules
136 should normally use the functions for ensuring binary compatibility
137 of the user's code across Python versions. Subsequently, if the
138 Python runtime switches to its own malloc (different from standard
139 malloc), no recompilation is required for the extensions.
140
141 The macro versions trade compatibility for speed. They can be used
142 whenever there is a performance problem, but their use implies
143 recompilation of the code for each new Python release. The Python
144 core uses the macros because it *is* compiled on every upgrade.
145 This might not be the case with 3rd party extensions in a custom
146 setup (for example, a customer does not always have access to the
147 source of 3rd party deliverables). You have been warned! */
148
149/*
150 * Raw memory interface
151 * ====================
152 */
153
154/* Functions */
155
156/* Function wrappers around PyCore_MALLOC and friends; useful if you
157 need to be sure that you are using the same memory allocator as
Guido van Rossumd085e881997-08-05 01:59:22 +0000158 Python. Note that the wrappers make sure that allocating 0 bytes
Guido van Rossumb18618d2000-05-03 23:44:39 +0000159 returns a non-NULL pointer, even if the underlying malloc
160 doesn't. Returned pointers must be checked for NULL explicitly.
161 No action is performed on failure. */
Guido van Rossum43466ec1998-12-04 18:48:25 +0000162extern DL_IMPORT(ANY *) PyMem_Malloc Py_PROTO((size_t));
163extern DL_IMPORT(ANY *) PyMem_Realloc Py_PROTO((ANY *, size_t));
164extern DL_IMPORT(void) PyMem_Free Py_PROTO((ANY *));
Guido van Rossumd085e881997-08-05 01:59:22 +0000165
Guido van Rossumb18618d2000-05-03 23:44:39 +0000166/* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are
167 no longer supported. They used to call PyErr_NoMemory() on failure. */
168
169/* Macros */
170#define PyMem_MALLOC(n) PyCore_MALLOC(n)
171#define PyMem_REALLOC(p, n) PyCore_REALLOC((ANY *)(p), (n))
172#define PyMem_FREE(p) PyCore_FREE((ANY *)(p))
173
174/*
175 * Type-oriented memory interface
176 * ==============================
177 */
178
179/* Functions */
180#define PyMem_New(type, n) \
181 ( (type *) PyMem_Malloc((n) * sizeof(type)) )
182#define PyMem_Resize(p, type, n) \
183 ( (p) = (type *) PyMem_Realloc((n) * sizeof(type)) )
184#define PyMem_Del(p) PyMem_Free(p)
185
186/* Macros */
187#define PyMem_NEW(type, n) \
188 ( (type *) PyMem_MALLOC(_PyMem_EXTRA + (n) * sizeof(type)) )
189#define PyMem_RESIZE(p, type, n) \
190 if ((p) == NULL) \
191 (p) = (type *)(PyMem_MALLOC( \
192 _PyMem_EXTRA + (n) * sizeof(type))); \
193 else \
194 (p) = (type *)(PyMem_REALLOC((p), \
195 _PyMem_EXTRA + (n) * sizeof(type)))
196#define PyMem_DEL(p) PyMem_FREE(p)
197
198/* PyMem_XDEL is deprecated. To avoid the call when p is NULL,
199 it is recommended to write the test explicitly in the code.
200 Note that according to ANSI C, free(NULL) has no effect. */
201
Guido van Rossuma3309961993-07-28 09:05:47 +0000202#ifdef __cplusplus
203}
204#endif
Guido van Rossumcaa63801995-01-12 11:45:45 +0000205
Guido van Rossum3a03d4c2000-05-05 15:36:09 +0000206/* SCENARIOS
207
208 Here are two scenarios by Vladimir Marangozov (the author of the
209 memory allocation redesign).
210
211 1) Scenario A
212
213 Suppose you want to use a debugging malloc library that collects info on
214 where the malloc calls originate from. Assume the interface is:
215
216 d_malloc(size_t n, char* src_file, unsigned long src_line) c.s.
217
218 In this case, you would define (for example in config.h) :
219
220 #define PyCore_MALLOC_FUNC d_malloc
221 ...
222 #define PyCore_MALLOC_PROTO Py_PROTO((size_t, char *, unsigned long))
223 ...
224 #define NEED_TO_DECLARE_MALLOC_AND_FRIEND
225
226 #define PyCore_MALLOC(n) PyCore_MALLOC_FUNC((n), __FILE__, __LINE__)
227 ...
228
229 2) Scenario B
230
231 Suppose you want to use malloc hooks (defined & initialized in a 3rd party
232 malloc library) instead of malloc functions. In this case, you would
233 define:
234
235 #define PyCore_MALLOC_FUNC (*malloc_hook)
236 ...
237 #define NEED_TO_DECLARE_MALLOC_AND_FRIEND
238
239 and ignore the previous definitions about PyCore_MALLOC_FUNC, etc.
240
241
242*/
243
244
Guido van Rossuma3309961993-07-28 09:05:47 +0000245#endif /* !Py_MYMALLOC_H */