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