blob: 2af58e450f23dd63e7e1d0ee8796470cf9f58074 [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
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +000013/***************************************
14THIS FILE IS OBSOLETE
15USE "pyport.h" INSTEAD
16***************************************/
17
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000018/* Lowest-level memory allocation interface */
19
Thomas Wouters334fb892000-07-25 12:56:38 +000020#define ANY void /* For API compatibility only. Obsolete, do not use. */
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000021
Guido van Rossumb6775db1994-08-01 11:34:53 +000022#ifdef HAVE_STDLIB_H
23#include <stdlib.h>
Guido van Rossum6c1874f1995-01-10 17:43:33 +000024#endif
25
Guido van Rossumb18618d2000-05-03 23:44:39 +000026#include "myproto.h"
27
Guido van Rossum6c1874f1995-01-10 17:43:33 +000028#ifdef __cplusplus
Guido van Rossumd085e881997-08-05 01:59:22 +000029/* Move this down here since some C++ #include's don't like to be included
30 inside an extern "C" */
Guido van Rossum6c1874f1995-01-10 17:43:33 +000031extern "C" {
32#endif
33
Guido van Rossumb18618d2000-05-03 23:44:39 +000034#ifndef DL_IMPORT /* declarations for DLL import */
35#define DL_IMPORT(RTYPE) RTYPE
Guido van Rossumab589b91997-08-21 16:13:37 +000036#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000037
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000038#ifndef NULL
Thomas Wouters334fb892000-07-25 12:56:38 +000039#define NULL ((void *)0)
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000040#endif
41
Guido van Rossum4b11c741997-07-10 22:40:54 +000042#ifdef MALLOC_ZERO_RETURNS_NULL
Guido van Rossum5f59d601992-12-14 16:59:51 +000043/* XXX Always allocate one extra byte, since some malloc's return NULL
44 XXX for malloc(0) or realloc(p, 0). */
Guido van Rossum4b11c741997-07-10 22:40:54 +000045#define _PyMem_EXTRA 1
46#else
47#define _PyMem_EXTRA 0
48#endif
49
Guido van Rossumb18618d2000-05-03 23:44:39 +000050/*
51 * Core memory allocator
52 * =====================
53 */
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000054
Guido van Rossumb18618d2000-05-03 23:44:39 +000055/* To make sure the interpreter is user-malloc friendly, all memory
56 APIs are implemented on top of this one.
Guido van Rossumd085e881997-08-05 01:59:22 +000057
Guido van Rossumb18618d2000-05-03 23:44:39 +000058 The PyCore_* macros can be defined to make the interpreter use a
59 custom allocator. Note that they are for internal use only. Both
Guido van Rossum3a03d4c2000-05-05 15:36:09 +000060 the core and extension modules should use the PyMem_* API.
61
62 See the comment block at the end of this file for two scenarios
63 showing how to use this to use a different allocator. */
Guido van Rossumb18618d2000-05-03 23:44:39 +000064
65#ifndef PyCore_MALLOC_FUNC
66#undef PyCore_REALLOC_FUNC
67#undef PyCore_FREE_FUNC
68#define PyCore_MALLOC_FUNC malloc
69#define PyCore_REALLOC_FUNC realloc
70#define PyCore_FREE_FUNC free
71#endif
72
73#ifndef PyCore_MALLOC_PROTO
74#undef PyCore_REALLOC_PROTO
75#undef PyCore_FREE_PROTO
Tim Petersdbd9ba62000-07-09 03:09:57 +000076#define PyCore_MALLOC_PROTO (size_t)
Thomas Wouters334fb892000-07-25 12:56:38 +000077#define PyCore_REALLOC_PROTO (void *, size_t)
78#define PyCore_FREE_PROTO (void *)
Guido van Rossumb18618d2000-05-03 23:44:39 +000079#endif
80
81#ifdef NEED_TO_DECLARE_MALLOC_AND_FRIEND
Thomas Wouters334fb892000-07-25 12:56:38 +000082extern void *PyCore_MALLOC_FUNC PyCore_MALLOC_PROTO;
83extern void *PyCore_REALLOC_FUNC PyCore_REALLOC_PROTO;
Guido van Rossumb18618d2000-05-03 23:44:39 +000084extern void PyCore_FREE_FUNC PyCore_FREE_PROTO;
85#endif
86
87#ifndef PyCore_MALLOC
88#undef PyCore_REALLOC
89#undef PyCore_FREE
90#define PyCore_MALLOC(n) PyCore_MALLOC_FUNC(n)
91#define PyCore_REALLOC(p, n) PyCore_REALLOC_FUNC((p), (n))
92#define PyCore_FREE(p) PyCore_FREE_FUNC(p)
93#endif
94
95/* BEWARE:
96
97 Each interface exports both functions and macros. Extension modules
98 should normally use the functions for ensuring binary compatibility
99 of the user's code across Python versions. Subsequently, if the
100 Python runtime switches to its own malloc (different from standard
101 malloc), no recompilation is required for the extensions.
102
103 The macro versions trade compatibility for speed. They can be used
104 whenever there is a performance problem, but their use implies
105 recompilation of the code for each new Python release. The Python
106 core uses the macros because it *is* compiled on every upgrade.
107 This might not be the case with 3rd party extensions in a custom
108 setup (for example, a customer does not always have access to the
109 source of 3rd party deliverables). You have been warned! */
110
111/*
112 * Raw memory interface
113 * ====================
114 */
115
116/* Functions */
117
118/* Function wrappers around PyCore_MALLOC and friends; useful if you
119 need to be sure that you are using the same memory allocator as
Guido van Rossumd085e881997-08-05 01:59:22 +0000120 Python. Note that the wrappers make sure that allocating 0 bytes
Guido van Rossumb18618d2000-05-03 23:44:39 +0000121 returns a non-NULL pointer, even if the underlying malloc
122 doesn't. Returned pointers must be checked for NULL explicitly.
123 No action is performed on failure. */
Thomas Wouters334fb892000-07-25 12:56:38 +0000124extern DL_IMPORT(void *) PyMem_Malloc(size_t);
125extern DL_IMPORT(void *) PyMem_Realloc(void *, size_t);
126extern DL_IMPORT(void) PyMem_Free(void *);
Guido van Rossumd085e881997-08-05 01:59:22 +0000127
Guido van Rossumb18618d2000-05-03 23:44:39 +0000128/* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are
129 no longer supported. They used to call PyErr_NoMemory() on failure. */
130
131/* Macros */
132#define PyMem_MALLOC(n) PyCore_MALLOC(n)
Thomas Wouters334fb892000-07-25 12:56:38 +0000133#define PyMem_REALLOC(p, n) PyCore_REALLOC((void *)(p), (n))
134#define PyMem_FREE(p) PyCore_FREE((void *)(p))
Guido van Rossumb18618d2000-05-03 23:44:39 +0000135
136/*
137 * Type-oriented memory interface
138 * ==============================
139 */
140
141/* Functions */
142#define PyMem_New(type, n) \
143 ( (type *) PyMem_Malloc((n) * sizeof(type)) )
144#define PyMem_Resize(p, type, n) \
145 ( (p) = (type *) PyMem_Realloc((n) * sizeof(type)) )
146#define PyMem_Del(p) PyMem_Free(p)
147
148/* Macros */
149#define PyMem_NEW(type, n) \
150 ( (type *) PyMem_MALLOC(_PyMem_EXTRA + (n) * sizeof(type)) )
151#define PyMem_RESIZE(p, type, n) \
152 if ((p) == NULL) \
153 (p) = (type *)(PyMem_MALLOC( \
154 _PyMem_EXTRA + (n) * sizeof(type))); \
155 else \
156 (p) = (type *)(PyMem_REALLOC((p), \
157 _PyMem_EXTRA + (n) * sizeof(type)))
158#define PyMem_DEL(p) PyMem_FREE(p)
159
160/* PyMem_XDEL is deprecated. To avoid the call when p is NULL,
161 it is recommended to write the test explicitly in the code.
162 Note that according to ANSI C, free(NULL) has no effect. */
163
Guido van Rossuma3309961993-07-28 09:05:47 +0000164#ifdef __cplusplus
165}
166#endif
Guido van Rossumcaa63801995-01-12 11:45:45 +0000167
Guido van Rossum3a03d4c2000-05-05 15:36:09 +0000168/* SCENARIOS
169
170 Here are two scenarios by Vladimir Marangozov (the author of the
171 memory allocation redesign).
172
173 1) Scenario A
174
175 Suppose you want to use a debugging malloc library that collects info on
176 where the malloc calls originate from. Assume the interface is:
177
178 d_malloc(size_t n, char* src_file, unsigned long src_line) c.s.
179
180 In this case, you would define (for example in config.h) :
181
182 #define PyCore_MALLOC_FUNC d_malloc
183 ...
Tim Petersdbd9ba62000-07-09 03:09:57 +0000184 #define PyCore_MALLOC_PROTO (size_t, char *, unsigned long)
Guido van Rossum3a03d4c2000-05-05 15:36:09 +0000185 ...
186 #define NEED_TO_DECLARE_MALLOC_AND_FRIEND
187
188 #define PyCore_MALLOC(n) PyCore_MALLOC_FUNC((n), __FILE__, __LINE__)
189 ...
190
191 2) Scenario B
192
193 Suppose you want to use malloc hooks (defined & initialized in a 3rd party
194 malloc library) instead of malloc functions. In this case, you would
195 define:
196
197 #define PyCore_MALLOC_FUNC (*malloc_hook)
198 ...
199 #define NEED_TO_DECLARE_MALLOC_AND_FRIEND
200
201 and ignore the previous definitions about PyCore_MALLOC_FUNC, etc.
202
203
204*/
205
206
Guido van Rossuma3309961993-07-28 09:05:47 +0000207#endif /* !Py_MYMALLOC_H */