blob: 8dadb66a7902648983903e96cfad16904bb7ae2a [file] [log] [blame]
Guido van Rossum1e28e5e1992-08-19 16:46:30 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossum1e28e5e1992-08-19 16:46:30 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum1e28e5e1992-08-19 16:46:30 +00009******************************************************************/
10
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +000011/***************************************
12THIS FILE IS OBSOLETE
13USE "pyport.h" INSTEAD
14***************************************/
15
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000016#ifndef Py_MYMALLOC_H
17#define Py_MYMALLOC_H
18
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000019/* Lowest-level memory allocation interface */
20
Thomas Wouters334fb892000-07-25 12:56:38 +000021#define ANY void /* For API compatibility only. Obsolete, do not use. */
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000022
Guido van Rossumb6775db1994-08-01 11:34:53 +000023#ifdef HAVE_STDLIB_H
24#include <stdlib.h>
Guido van Rossum6c1874f1995-01-10 17:43:33 +000025#endif
26
Guido van Rossumb18618d2000-05-03 23:44:39 +000027#include "myproto.h"
28
Guido van Rossum6c1874f1995-01-10 17:43:33 +000029#ifdef __cplusplus
Guido van Rossumd085e881997-08-05 01:59:22 +000030/* Move this down here since some C++ #include's don't like to be included
31 inside an extern "C" */
Guido van Rossum6c1874f1995-01-10 17:43:33 +000032extern "C" {
33#endif
34
Guido van Rossumb18618d2000-05-03 23:44:39 +000035#ifndef DL_IMPORT /* declarations for DLL import */
36#define DL_IMPORT(RTYPE) RTYPE
Guido van Rossumab589b91997-08-21 16:13:37 +000037#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000038
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000039#ifndef NULL
Thomas Wouters334fb892000-07-25 12:56:38 +000040#define NULL ((void *)0)
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000041#endif
42
Guido van Rossum4b11c741997-07-10 22:40:54 +000043#ifdef MALLOC_ZERO_RETURNS_NULL
Guido van Rossum5f59d601992-12-14 16:59:51 +000044/* XXX Always allocate one extra byte, since some malloc's return NULL
45 XXX for malloc(0) or realloc(p, 0). */
Guido van Rossum4b11c741997-07-10 22:40:54 +000046#define _PyMem_EXTRA 1
47#else
48#define _PyMem_EXTRA 0
49#endif
50
Guido van Rossumb18618d2000-05-03 23:44:39 +000051/*
52 * Core memory allocator
53 * =====================
54 */
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000055
Guido van Rossumb18618d2000-05-03 23:44:39 +000056/* To make sure the interpreter is user-malloc friendly, all memory
57 APIs are implemented on top of this one.
Guido van Rossumd085e881997-08-05 01:59:22 +000058
Guido van Rossumb18618d2000-05-03 23:44:39 +000059 The PyCore_* macros can be defined to make the interpreter use a
60 custom allocator. Note that they are for internal use only. Both
Guido van Rossum3a03d4c2000-05-05 15:36:09 +000061 the core and extension modules should use the PyMem_* API.
62
63 See the comment block at the end of this file for two scenarios
64 showing how to use this to use a different allocator. */
Guido van Rossumb18618d2000-05-03 23:44:39 +000065
66#ifndef PyCore_MALLOC_FUNC
67#undef PyCore_REALLOC_FUNC
68#undef PyCore_FREE_FUNC
69#define PyCore_MALLOC_FUNC malloc
70#define PyCore_REALLOC_FUNC realloc
71#define PyCore_FREE_FUNC free
72#endif
73
74#ifndef PyCore_MALLOC_PROTO
75#undef PyCore_REALLOC_PROTO
76#undef PyCore_FREE_PROTO
Tim Petersdbd9ba62000-07-09 03:09:57 +000077#define PyCore_MALLOC_PROTO (size_t)
Thomas Wouters334fb892000-07-25 12:56:38 +000078#define PyCore_REALLOC_PROTO (void *, size_t)
79#define PyCore_FREE_PROTO (void *)
Guido van Rossumb18618d2000-05-03 23:44:39 +000080#endif
81
82#ifdef NEED_TO_DECLARE_MALLOC_AND_FRIEND
Thomas Wouters334fb892000-07-25 12:56:38 +000083extern void *PyCore_MALLOC_FUNC PyCore_MALLOC_PROTO;
84extern void *PyCore_REALLOC_FUNC PyCore_REALLOC_PROTO;
Guido van Rossumb18618d2000-05-03 23:44:39 +000085extern void PyCore_FREE_FUNC PyCore_FREE_PROTO;
86#endif
87
88#ifndef PyCore_MALLOC
89#undef PyCore_REALLOC
90#undef PyCore_FREE
91#define PyCore_MALLOC(n) PyCore_MALLOC_FUNC(n)
92#define PyCore_REALLOC(p, n) PyCore_REALLOC_FUNC((p), (n))
93#define PyCore_FREE(p) PyCore_FREE_FUNC(p)
94#endif
95
96/* BEWARE:
97
98 Each interface exports both functions and macros. Extension modules
99 should normally use the functions for ensuring binary compatibility
100 of the user's code across Python versions. Subsequently, if the
101 Python runtime switches to its own malloc (different from standard
102 malloc), no recompilation is required for the extensions.
103
104 The macro versions trade compatibility for speed. They can be used
105 whenever there is a performance problem, but their use implies
106 recompilation of the code for each new Python release. The Python
107 core uses the macros because it *is* compiled on every upgrade.
108 This might not be the case with 3rd party extensions in a custom
109 setup (for example, a customer does not always have access to the
110 source of 3rd party deliverables). You have been warned! */
111
112/*
113 * Raw memory interface
114 * ====================
115 */
116
117/* Functions */
118
119/* Function wrappers around PyCore_MALLOC and friends; useful if you
120 need to be sure that you are using the same memory allocator as
Guido van Rossumd085e881997-08-05 01:59:22 +0000121 Python. Note that the wrappers make sure that allocating 0 bytes
Guido van Rossumb18618d2000-05-03 23:44:39 +0000122 returns a non-NULL pointer, even if the underlying malloc
123 doesn't. Returned pointers must be checked for NULL explicitly.
124 No action is performed on failure. */
Thomas Wouters334fb892000-07-25 12:56:38 +0000125extern DL_IMPORT(void *) PyMem_Malloc(size_t);
126extern DL_IMPORT(void *) PyMem_Realloc(void *, size_t);
127extern DL_IMPORT(void) PyMem_Free(void *);
Guido van Rossumd085e881997-08-05 01:59:22 +0000128
Guido van Rossumb18618d2000-05-03 23:44:39 +0000129/* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are
130 no longer supported. They used to call PyErr_NoMemory() on failure. */
131
132/* Macros */
133#define PyMem_MALLOC(n) PyCore_MALLOC(n)
Thomas Wouters334fb892000-07-25 12:56:38 +0000134#define PyMem_REALLOC(p, n) PyCore_REALLOC((void *)(p), (n))
135#define PyMem_FREE(p) PyCore_FREE((void *)(p))
Guido van Rossumb18618d2000-05-03 23:44:39 +0000136
137/*
138 * Type-oriented memory interface
139 * ==============================
140 */
141
142/* Functions */
143#define PyMem_New(type, n) \
144 ( (type *) PyMem_Malloc((n) * sizeof(type)) )
145#define PyMem_Resize(p, type, n) \
146 ( (p) = (type *) PyMem_Realloc((n) * sizeof(type)) )
147#define PyMem_Del(p) PyMem_Free(p)
148
149/* Macros */
150#define PyMem_NEW(type, n) \
151 ( (type *) PyMem_MALLOC(_PyMem_EXTRA + (n) * sizeof(type)) )
152#define PyMem_RESIZE(p, type, n) \
153 if ((p) == NULL) \
154 (p) = (type *)(PyMem_MALLOC( \
155 _PyMem_EXTRA + (n) * sizeof(type))); \
156 else \
157 (p) = (type *)(PyMem_REALLOC((p), \
158 _PyMem_EXTRA + (n) * sizeof(type)))
159#define PyMem_DEL(p) PyMem_FREE(p)
160
161/* PyMem_XDEL is deprecated. To avoid the call when p is NULL,
162 it is recommended to write the test explicitly in the code.
163 Note that according to ANSI C, free(NULL) has no effect. */
164
Guido van Rossuma3309961993-07-28 09:05:47 +0000165#ifdef __cplusplus
166}
167#endif
Guido van Rossumcaa63801995-01-12 11:45:45 +0000168
Guido van Rossum3a03d4c2000-05-05 15:36:09 +0000169/* SCENARIOS
170
171 Here are two scenarios by Vladimir Marangozov (the author of the
172 memory allocation redesign).
173
174 1) Scenario A
175
176 Suppose you want to use a debugging malloc library that collects info on
177 where the malloc calls originate from. Assume the interface is:
178
179 d_malloc(size_t n, char* src_file, unsigned long src_line) c.s.
180
181 In this case, you would define (for example in config.h) :
182
183 #define PyCore_MALLOC_FUNC d_malloc
184 ...
Tim Petersdbd9ba62000-07-09 03:09:57 +0000185 #define PyCore_MALLOC_PROTO (size_t, char *, unsigned long)
Guido van Rossum3a03d4c2000-05-05 15:36:09 +0000186 ...
187 #define NEED_TO_DECLARE_MALLOC_AND_FRIEND
188
189 #define PyCore_MALLOC(n) PyCore_MALLOC_FUNC((n), __FILE__, __LINE__)
190 ...
191
192 2) Scenario B
193
194 Suppose you want to use malloc hooks (defined & initialized in a 3rd party
195 malloc library) instead of malloc functions. In this case, you would
196 define:
197
198 #define PyCore_MALLOC_FUNC (*malloc_hook)
199 ...
200 #define NEED_TO_DECLARE_MALLOC_AND_FRIEND
201
202 and ignore the previous definitions about PyCore_MALLOC_FUNC, etc.
203
204
205*/
206
207
Guido van Rossuma3309961993-07-28 09:05:47 +0000208#endif /* !Py_MYMALLOC_H */