blob: 88faf9031c0aa93717e64812cf4fb4f7a422bb8c [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
Guido van Rossumb18618d2000-05-03 23:44:39 +000047#ifndef DL_IMPORT /* declarations for DLL import */
48#define DL_IMPORT(RTYPE) RTYPE
Guido van Rossumab589b91997-08-21 16:13:37 +000049#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000050
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000051#ifndef NULL
Guido van Rossumb6775db1994-08-01 11:34:53 +000052#define NULL ((ANY *)0)
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000053#endif
54
Guido van Rossum4b11c741997-07-10 22:40:54 +000055#ifdef MALLOC_ZERO_RETURNS_NULL
Guido van Rossum5f59d601992-12-14 16:59:51 +000056/* XXX Always allocate one extra byte, since some malloc's return NULL
57 XXX for malloc(0) or realloc(p, 0). */
Guido van Rossum4b11c741997-07-10 22:40:54 +000058#define _PyMem_EXTRA 1
59#else
60#define _PyMem_EXTRA 0
61#endif
62
Guido van Rossumb18618d2000-05-03 23:44:39 +000063/*
64 * Core memory allocator
65 * =====================
66 */
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000067
Guido van Rossumb18618d2000-05-03 23:44:39 +000068/* To make sure the interpreter is user-malloc friendly, all memory
69 APIs are implemented on top of this one.
Guido van Rossumd085e881997-08-05 01:59:22 +000070
Guido van Rossumb18618d2000-05-03 23:44:39 +000071 The PyCore_* macros can be defined to make the interpreter use a
72 custom allocator. Note that they are for internal use only. Both
Guido van Rossum3a03d4c2000-05-05 15:36:09 +000073 the core and extension modules should use the PyMem_* API.
74
75 See the comment block at the end of this file for two scenarios
76 showing how to use this to use a different allocator. */
Guido van Rossumb18618d2000-05-03 23:44:39 +000077
78#ifndef PyCore_MALLOC_FUNC
79#undef PyCore_REALLOC_FUNC
80#undef PyCore_FREE_FUNC
81#define PyCore_MALLOC_FUNC malloc
82#define PyCore_REALLOC_FUNC realloc
83#define PyCore_FREE_FUNC free
84#endif
85
86#ifndef PyCore_MALLOC_PROTO
87#undef PyCore_REALLOC_PROTO
88#undef PyCore_FREE_PROTO
Tim Petersdbd9ba62000-07-09 03:09:57 +000089#define PyCore_MALLOC_PROTO (size_t)
90#define PyCore_REALLOC_PROTO (ANY *, size_t)
91#define PyCore_FREE_PROTO (ANY *)
Guido van Rossumb18618d2000-05-03 23:44:39 +000092#endif
93
94#ifdef NEED_TO_DECLARE_MALLOC_AND_FRIEND
95extern ANY *PyCore_MALLOC_FUNC PyCore_MALLOC_PROTO;
96extern ANY *PyCore_REALLOC_FUNC PyCore_REALLOC_PROTO;
97extern void PyCore_FREE_FUNC PyCore_FREE_PROTO;
98#endif
99
100#ifndef PyCore_MALLOC
101#undef PyCore_REALLOC
102#undef PyCore_FREE
103#define PyCore_MALLOC(n) PyCore_MALLOC_FUNC(n)
104#define PyCore_REALLOC(p, n) PyCore_REALLOC_FUNC((p), (n))
105#define PyCore_FREE(p) PyCore_FREE_FUNC(p)
106#endif
107
108/* BEWARE:
109
110 Each interface exports both functions and macros. Extension modules
111 should normally use the functions for ensuring binary compatibility
112 of the user's code across Python versions. Subsequently, if the
113 Python runtime switches to its own malloc (different from standard
114 malloc), no recompilation is required for the extensions.
115
116 The macro versions trade compatibility for speed. They can be used
117 whenever there is a performance problem, but their use implies
118 recompilation of the code for each new Python release. The Python
119 core uses the macros because it *is* compiled on every upgrade.
120 This might not be the case with 3rd party extensions in a custom
121 setup (for example, a customer does not always have access to the
122 source of 3rd party deliverables). You have been warned! */
123
124/*
125 * Raw memory interface
126 * ====================
127 */
128
129/* Functions */
130
131/* Function wrappers around PyCore_MALLOC and friends; useful if you
132 need to be sure that you are using the same memory allocator as
Guido van Rossumd085e881997-08-05 01:59:22 +0000133 Python. Note that the wrappers make sure that allocating 0 bytes
Guido van Rossumb18618d2000-05-03 23:44:39 +0000134 returns a non-NULL pointer, even if the underlying malloc
135 doesn't. Returned pointers must be checked for NULL explicitly.
136 No action is performed on failure. */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000137extern DL_IMPORT(ANY *) PyMem_Malloc(size_t);
138extern DL_IMPORT(ANY *) PyMem_Realloc(ANY *, size_t);
139extern DL_IMPORT(void) PyMem_Free(ANY *);
Guido van Rossumd085e881997-08-05 01:59:22 +0000140
Guido van Rossumb18618d2000-05-03 23:44:39 +0000141/* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are
142 no longer supported. They used to call PyErr_NoMemory() on failure. */
143
144/* Macros */
145#define PyMem_MALLOC(n) PyCore_MALLOC(n)
146#define PyMem_REALLOC(p, n) PyCore_REALLOC((ANY *)(p), (n))
147#define PyMem_FREE(p) PyCore_FREE((ANY *)(p))
148
149/*
150 * Type-oriented memory interface
151 * ==============================
152 */
153
154/* Functions */
155#define PyMem_New(type, n) \
156 ( (type *) PyMem_Malloc((n) * sizeof(type)) )
157#define PyMem_Resize(p, type, n) \
158 ( (p) = (type *) PyMem_Realloc((n) * sizeof(type)) )
159#define PyMem_Del(p) PyMem_Free(p)
160
161/* Macros */
162#define PyMem_NEW(type, n) \
163 ( (type *) PyMem_MALLOC(_PyMem_EXTRA + (n) * sizeof(type)) )
164#define PyMem_RESIZE(p, type, n) \
165 if ((p) == NULL) \
166 (p) = (type *)(PyMem_MALLOC( \
167 _PyMem_EXTRA + (n) * sizeof(type))); \
168 else \
169 (p) = (type *)(PyMem_REALLOC((p), \
170 _PyMem_EXTRA + (n) * sizeof(type)))
171#define PyMem_DEL(p) PyMem_FREE(p)
172
173/* PyMem_XDEL is deprecated. To avoid the call when p is NULL,
174 it is recommended to write the test explicitly in the code.
175 Note that according to ANSI C, free(NULL) has no effect. */
176
Guido van Rossuma3309961993-07-28 09:05:47 +0000177#ifdef __cplusplus
178}
179#endif
Guido van Rossumcaa63801995-01-12 11:45:45 +0000180
Guido van Rossum3a03d4c2000-05-05 15:36:09 +0000181/* SCENARIOS
182
183 Here are two scenarios by Vladimir Marangozov (the author of the
184 memory allocation redesign).
185
186 1) Scenario A
187
188 Suppose you want to use a debugging malloc library that collects info on
189 where the malloc calls originate from. Assume the interface is:
190
191 d_malloc(size_t n, char* src_file, unsigned long src_line) c.s.
192
193 In this case, you would define (for example in config.h) :
194
195 #define PyCore_MALLOC_FUNC d_malloc
196 ...
Tim Petersdbd9ba62000-07-09 03:09:57 +0000197 #define PyCore_MALLOC_PROTO (size_t, char *, unsigned long)
Guido van Rossum3a03d4c2000-05-05 15:36:09 +0000198 ...
199 #define NEED_TO_DECLARE_MALLOC_AND_FRIEND
200
201 #define PyCore_MALLOC(n) PyCore_MALLOC_FUNC((n), __FILE__, __LINE__)
202 ...
203
204 2) Scenario B
205
206 Suppose you want to use malloc hooks (defined & initialized in a 3rd party
207 malloc library) instead of malloc functions. In this case, you would
208 define:
209
210 #define PyCore_MALLOC_FUNC (*malloc_hook)
211 ...
212 #define NEED_TO_DECLARE_MALLOC_AND_FRIEND
213
214 and ignore the previous definitions about PyCore_MALLOC_FUNC, etc.
215
216
217*/
218
219
Guido van Rossuma3309961993-07-28 09:05:47 +0000220#endif /* !Py_MYMALLOC_H */