blob: 558af9d29b17cedff582b135c994c39027059446 [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
60#ifdef __cplusplus
Guido van Rossumd085e881997-08-05 01:59:22 +000061/* Move this down here since some C++ #include's don't like to be included
62 inside an extern "C" */
Guido van Rossum6c1874f1995-01-10 17:43:33 +000063extern "C" {
64#endif
65
Jack Jansenf9480ce1995-06-27 13:12:09 +000066#ifdef SYMANTEC__CFM68K__
Guido van Rossum0acd4b61995-02-18 14:50:12 +000067#pragma lib_export on
68#endif
69
Guido van Rossumab589b91997-08-21 16:13:37 +000070/* The following should never be necessary */
71#ifdef NEED_TO_DECLARE_MALLOC_AND_FRIEND
Guido van Rossumcaa63801995-01-12 11:45:45 +000072extern ANY *malloc Py_PROTO((size_t));
73extern ANY *calloc Py_PROTO((size_t, size_t));
74extern ANY *realloc Py_PROTO((ANY *, size_t));
75extern void free Py_PROTO((ANY *)); /* XXX sometimes int on Unix old systems */
Guido van Rossumab589b91997-08-21 16:13:37 +000076#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000077
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000078#ifndef NULL
Guido van Rossumb6775db1994-08-01 11:34:53 +000079#define NULL ((ANY *)0)
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000080#endif
81
Guido van Rossum4b11c741997-07-10 22:40:54 +000082#ifdef MALLOC_ZERO_RETURNS_NULL
Guido van Rossum5f59d601992-12-14 16:59:51 +000083/* XXX Always allocate one extra byte, since some malloc's return NULL
84 XXX for malloc(0) or realloc(p, 0). */
Guido van Rossum4b11c741997-07-10 22:40:54 +000085#define _PyMem_EXTRA 1
86#else
87#define _PyMem_EXTRA 0
88#endif
89
90#define PyMem_NEW(type, n) \
91 ( (type *) malloc(_PyMem_EXTRA + (n) * sizeof(type)) )
Guido van Rossumcaa63801995-01-12 11:45:45 +000092#define PyMem_RESIZE(p, type, n) \
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000093 if ((p) == NULL) \
Guido van Rossum4b11c741997-07-10 22:40:54 +000094 (p) = (type *) malloc(_PyMem_EXTRA + (n) * sizeof(type)); \
Guido van Rossum1e28e5e1992-08-19 16:46:30 +000095 else \
Guido van Rossum4b11c741997-07-10 22:40:54 +000096 (p) = (type *) realloc((ANY *)(p), \
97 _PyMem_EXTRA + (n) * sizeof(type))
Guido van Rossumcaa63801995-01-12 11:45:45 +000098#define PyMem_DEL(p) free((ANY *)p)
99#define PyMem_XDEL(p) if ((p) == NULL) ; else PyMem_DEL(p)
Guido van Rossum1e28e5e1992-08-19 16:46:30 +0000100
Guido van Rossumd085e881997-08-05 01:59:22 +0000101
102/* Two sets of function wrappers around malloc and friends; useful if
103 you need to be sure that you are using the same memory allocator as
104 Python. Note that the wrappers make sure that allocating 0 bytes
105 returns a non-NULL pointer, even if the underlying malloc doesn't.
106 The Python interpreter continues to use PyMem_NEW etc. */
107
108/* These wrappers around malloc call PyErr_NoMemory() on failure */
Guido van Rossum43466ec1998-12-04 18:48:25 +0000109extern DL_IMPORT(ANY *) Py_Malloc Py_PROTO((size_t));
110extern DL_IMPORT(ANY *) Py_Realloc Py_PROTO((ANY *, size_t));
111extern DL_IMPORT(void) Py_Free Py_PROTO((ANY *));
Guido van Rossumd085e881997-08-05 01:59:22 +0000112
113/* These wrappers around malloc *don't* call anything on failure */
Guido van Rossum43466ec1998-12-04 18:48:25 +0000114extern DL_IMPORT(ANY *) PyMem_Malloc Py_PROTO((size_t));
115extern DL_IMPORT(ANY *) PyMem_Realloc Py_PROTO((ANY *, size_t));
116extern DL_IMPORT(void) PyMem_Free Py_PROTO((ANY *));
Guido van Rossumd085e881997-08-05 01:59:22 +0000117
Guido van Rossuma3309961993-07-28 09:05:47 +0000118#ifdef __cplusplus
119}
120#endif
Guido van Rossumcaa63801995-01-12 11:45:45 +0000121
Guido van Rossuma3309961993-07-28 09:05:47 +0000122#endif /* !Py_MYMALLOC_H */