blob: df3113ba66c05a244f685a2224b43c664e4f580d [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 Rossumd266eb41996-10-25 14:44:06 +00009Permission to use, copy, modify, and distribute this software and its
10documentation for any purpose and without fee is hereby granted,
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000011provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000012both that copyright notice and this permission notice appear in
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000013supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000014Centrum or CWI or Corporation for National Research Initiatives or
15CNRI not be used in advertising or publicity pertaining to
16distribution of the software without specific, written prior
17permission.
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000018
Guido van Rossumd266eb41996-10-25 14:44:06 +000019While CWI is the initial source for this software, a modified version
20is made available by the Corporation for National Research Initiatives
21(CNRI) at the Internet address ftp://ftp.python.org.
22
23STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
24REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
25MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
26CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
27DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
28PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
29TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
30PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000031
32******************************************************************/
33
34/* Lowest-level memory allocation interface */
35
36#ifdef macintosh
37#define ANY void
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000038#endif
39
40#ifdef __STDC__
41#define ANY void
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000042#endif
43
Guido van Rossum12d12c51993-10-26 17:58:25 +000044#ifdef __TURBOC__
45#define ANY void
Guido van Rossum12d12c51993-10-26 17:58:25 +000046#endif
47
48#ifdef __GNUC__
49#define ANY void
Guido van Rossum12d12c51993-10-26 17:58:25 +000050#endif
51
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000052#ifndef ANY
53#define ANY char
54#endif
55
Guido van Rossumb6775db1994-08-01 11:34:53 +000056#ifdef HAVE_STDLIB_H
57#include <stdlib.h>
Guido van Rossum6c1874f1995-01-10 17:43:33 +000058#endif
59
Guido van Rossumb18618d2000-05-03 23:44:39 +000060#include "myproto.h"
61
Guido van Rossum6c1874f1995-01-10 17:43:33 +000062#ifdef __cplusplus
Guido van Rossumd085e881997-08-05 01:59:22 +000063/* Move this down here since some C++ #include's don't like to be included
64 inside an extern "C" */
Guido van Rossum6c1874f1995-01-10 17:43:33 +000065extern "C" {
66#endif
67
Jack Jansenf9480ce1995-06-27 13:12:09 +000068#ifdef SYMANTEC__CFM68K__
Guido van Rossum0acd4b61995-02-18 14:50:12 +000069#pragma lib_export on
70#endif
71
Guido van Rossumb18618d2000-05-03 23:44:39 +000072#ifndef DL_IMPORT /* declarations for DLL import */
73#define DL_IMPORT(RTYPE) RTYPE
Guido van Rossumab589b91997-08-21 16:13:37 +000074#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000075
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000076#ifndef NULL
Guido van Rossumb6775db1994-08-01 11:34:53 +000077#define NULL ((ANY *)0)
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000078#endif
79
Guido van Rossum4b11c741997-07-10 22:40:54 +000080#ifdef MALLOC_ZERO_RETURNS_NULL
Guido van Rossum5f59d601992-12-14 16:59:51 +000081/* XXX Always allocate one extra byte, since some malloc's return NULL
82 XXX for malloc(0) or realloc(p, 0). */
Guido van Rossum4b11c741997-07-10 22:40:54 +000083#define _PyMem_EXTRA 1
84#else
85#define _PyMem_EXTRA 0
86#endif
87
Guido van Rossumb18618d2000-05-03 23:44:39 +000088/*
89 * Core memory allocator
90 * =====================
91 */
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000092
Guido van Rossumb18618d2000-05-03 23:44:39 +000093/* To make sure the interpreter is user-malloc friendly, all memory
94 APIs are implemented on top of this one.
Guido van Rossumd085e881997-08-05 01:59:22 +000095
Guido van Rossumb18618d2000-05-03 23:44:39 +000096 The PyCore_* macros can be defined to make the interpreter use a
97 custom allocator. Note that they are for internal use only. Both
98 the core and extension modules should use the PyMem_* API. */
99
100#ifndef PyCore_MALLOC_FUNC
101#undef PyCore_REALLOC_FUNC
102#undef PyCore_FREE_FUNC
103#define PyCore_MALLOC_FUNC malloc
104#define PyCore_REALLOC_FUNC realloc
105#define PyCore_FREE_FUNC free
106#endif
107
108#ifndef PyCore_MALLOC_PROTO
109#undef PyCore_REALLOC_PROTO
110#undef PyCore_FREE_PROTO
111#define PyCore_MALLOC_PROTO Py_PROTO((size_t))
112#define PyCore_REALLOC_PROTO Py_PROTO((ANY *, size_t))
113#define PyCore_FREE_PROTO Py_PROTO((ANY *))
114#endif
115
116#ifdef NEED_TO_DECLARE_MALLOC_AND_FRIEND
117extern ANY *PyCore_MALLOC_FUNC PyCore_MALLOC_PROTO;
118extern ANY *PyCore_REALLOC_FUNC PyCore_REALLOC_PROTO;
119extern void PyCore_FREE_FUNC PyCore_FREE_PROTO;
120#endif
121
122#ifndef PyCore_MALLOC
123#undef PyCore_REALLOC
124#undef PyCore_FREE
125#define PyCore_MALLOC(n) PyCore_MALLOC_FUNC(n)
126#define PyCore_REALLOC(p, n) PyCore_REALLOC_FUNC((p), (n))
127#define PyCore_FREE(p) PyCore_FREE_FUNC(p)
128#endif
129
130/* BEWARE:
131
132 Each interface exports both functions and macros. Extension modules
133 should normally use the functions for ensuring binary compatibility
134 of the user's code across Python versions. Subsequently, if the
135 Python runtime switches to its own malloc (different from standard
136 malloc), no recompilation is required for the extensions.
137
138 The macro versions trade compatibility for speed. They can be used
139 whenever there is a performance problem, but their use implies
140 recompilation of the code for each new Python release. The Python
141 core uses the macros because it *is* compiled on every upgrade.
142 This might not be the case with 3rd party extensions in a custom
143 setup (for example, a customer does not always have access to the
144 source of 3rd party deliverables). You have been warned! */
145
146/*
147 * Raw memory interface
148 * ====================
149 */
150
151/* Functions */
152
153/* Function wrappers around PyCore_MALLOC and friends; useful if you
154 need to be sure that you are using the same memory allocator as
Guido van Rossumd085e881997-08-05 01:59:22 +0000155 Python. Note that the wrappers make sure that allocating 0 bytes
Guido van Rossumb18618d2000-05-03 23:44:39 +0000156 returns a non-NULL pointer, even if the underlying malloc
157 doesn't. Returned pointers must be checked for NULL explicitly.
158 No action is performed on failure. */
Guido van Rossum43466ec1998-12-04 18:48:25 +0000159extern DL_IMPORT(ANY *) PyMem_Malloc Py_PROTO((size_t));
160extern DL_IMPORT(ANY *) PyMem_Realloc Py_PROTO((ANY *, size_t));
161extern DL_IMPORT(void) PyMem_Free Py_PROTO((ANY *));
Guido van Rossumd085e881997-08-05 01:59:22 +0000162
Guido van Rossumb18618d2000-05-03 23:44:39 +0000163/* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are
164 no longer supported. They used to call PyErr_NoMemory() on failure. */
165
166/* Macros */
167#define PyMem_MALLOC(n) PyCore_MALLOC(n)
168#define PyMem_REALLOC(p, n) PyCore_REALLOC((ANY *)(p), (n))
169#define PyMem_FREE(p) PyCore_FREE((ANY *)(p))
170
171/*
172 * Type-oriented memory interface
173 * ==============================
174 */
175
176/* Functions */
177#define PyMem_New(type, n) \
178 ( (type *) PyMem_Malloc((n) * sizeof(type)) )
179#define PyMem_Resize(p, type, n) \
180 ( (p) = (type *) PyMem_Realloc((n) * sizeof(type)) )
181#define PyMem_Del(p) PyMem_Free(p)
182
183/* Macros */
184#define PyMem_NEW(type, n) \
185 ( (type *) PyMem_MALLOC(_PyMem_EXTRA + (n) * sizeof(type)) )
186#define PyMem_RESIZE(p, type, n) \
187 if ((p) == NULL) \
188 (p) = (type *)(PyMem_MALLOC( \
189 _PyMem_EXTRA + (n) * sizeof(type))); \
190 else \
191 (p) = (type *)(PyMem_REALLOC((p), \
192 _PyMem_EXTRA + (n) * sizeof(type)))
193#define PyMem_DEL(p) PyMem_FREE(p)
194
195/* PyMem_XDEL is deprecated. To avoid the call when p is NULL,
196 it is recommended to write the test explicitly in the code.
197 Note that according to ANSI C, free(NULL) has no effect. */
198
Guido van Rossuma3309961993-07-28 09:05:47 +0000199#ifdef __cplusplus
200}
201#endif
Guido van Rossumcaa63801995-01-12 11:45:45 +0000202
Guido van Rossuma3309961993-07-28 09:05:47 +0000203#endif /* !Py_MYMALLOC_H */