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