Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 1 | #ifndef Py_MYMALLOC_H |
| 2 | #define Py_MYMALLOC_H |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 3 | /*********************************************************** |
Guido van Rossum | 5799b52 | 1995-01-04 19:06:22 +0000 | [diff] [blame] | 4 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 5 | The Netherlands. |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 6 | |
| 7 | All Rights Reserved |
| 8 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 9 | Permission to use, copy, modify, and distribute this software and its |
| 10 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 11 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 13 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 14 | Centrum or CWI or Corporation for National Research Initiatives or |
| 15 | CNRI not be used in advertising or publicity pertaining to |
| 16 | distribution of the software without specific, written prior |
| 17 | permission. |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 19 | While CWI is the initial source for this software, a modified version |
| 20 | is made available by the Corporation for National Research Initiatives |
| 21 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 22 | |
| 23 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 24 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 25 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 26 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 27 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 28 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 29 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 30 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 31 | |
| 32 | ******************************************************************/ |
| 33 | |
| 34 | /* Lowest-level memory allocation interface */ |
| 35 | |
| 36 | #ifdef macintosh |
| 37 | #define ANY void |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 38 | #endif |
| 39 | |
| 40 | #ifdef __STDC__ |
| 41 | #define ANY void |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 42 | #endif |
| 43 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 44 | #ifdef __TURBOC__ |
| 45 | #define ANY void |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 46 | #endif |
| 47 | |
| 48 | #ifdef __GNUC__ |
| 49 | #define ANY void |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 50 | #endif |
| 51 | |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 52 | #ifndef ANY |
| 53 | #define ANY char |
| 54 | #endif |
| 55 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 56 | #ifdef HAVE_STDLIB_H |
| 57 | #include <stdlib.h> |
Guido van Rossum | 6c1874f | 1995-01-10 17:43:33 +0000 | [diff] [blame] | 58 | #endif |
| 59 | |
| 60 | #ifdef __cplusplus |
Guido van Rossum | d085e88 | 1997-08-05 01:59:22 +0000 | [diff] [blame] | 61 | /* Move this down here since some C++ #include's don't like to be included |
| 62 | inside an extern "C" */ |
Guido van Rossum | 6c1874f | 1995-01-10 17:43:33 +0000 | [diff] [blame] | 63 | extern "C" { |
| 64 | #endif |
| 65 | |
Jack Jansen | f9480ce | 1995-06-27 13:12:09 +0000 | [diff] [blame] | 66 | #ifdef SYMANTEC__CFM68K__ |
Guido van Rossum | 0acd4b6 | 1995-02-18 14:50:12 +0000 | [diff] [blame] | 67 | #pragma lib_export on |
| 68 | #endif |
| 69 | |
Guido van Rossum | ab589b9 | 1997-08-21 16:13:37 +0000 | [diff] [blame] | 70 | /* The following should never be necessary */ |
| 71 | #ifdef NEED_TO_DECLARE_MALLOC_AND_FRIEND |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 72 | extern ANY *malloc Py_PROTO((size_t)); |
| 73 | extern ANY *calloc Py_PROTO((size_t, size_t)); |
| 74 | extern ANY *realloc Py_PROTO((ANY *, size_t)); |
| 75 | extern void free Py_PROTO((ANY *)); /* XXX sometimes int on Unix old systems */ |
Guido van Rossum | ab589b9 | 1997-08-21 16:13:37 +0000 | [diff] [blame] | 76 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 78 | #ifndef NULL |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 79 | #define NULL ((ANY *)0) |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 80 | #endif |
| 81 | |
Guido van Rossum | 4b11c74 | 1997-07-10 22:40:54 +0000 | [diff] [blame] | 82 | #ifdef MALLOC_ZERO_RETURNS_NULL |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 83 | /* XXX Always allocate one extra byte, since some malloc's return NULL |
| 84 | XXX for malloc(0) or realloc(p, 0). */ |
Guido van Rossum | 4b11c74 | 1997-07-10 22:40:54 +0000 | [diff] [blame] | 85 | #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 Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 92 | #define PyMem_RESIZE(p, type, n) \ |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 93 | if ((p) == NULL) \ |
Guido van Rossum | 4b11c74 | 1997-07-10 22:40:54 +0000 | [diff] [blame] | 94 | (p) = (type *) malloc(_PyMem_EXTRA + (n) * sizeof(type)); \ |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 95 | else \ |
Guido van Rossum | 4b11c74 | 1997-07-10 22:40:54 +0000 | [diff] [blame] | 96 | (p) = (type *) realloc((ANY *)(p), \ |
| 97 | _PyMem_EXTRA + (n) * sizeof(type)) |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 98 | #define PyMem_DEL(p) free((ANY *)p) |
| 99 | #define PyMem_XDEL(p) if ((p) == NULL) ; else PyMem_DEL(p) |
Guido van Rossum | 1e28e5e | 1992-08-19 16:46:30 +0000 | [diff] [blame] | 100 | |
Guido van Rossum | d085e88 | 1997-08-05 01:59:22 +0000 | [diff] [blame] | 101 | |
| 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 */ |
| 109 | extern ANY *Py_Malloc Py_PROTO((size_t)); |
| 110 | extern ANY *Py_Realloc Py_PROTO((ANY *, size_t)); |
| 111 | extern void Py_Free Py_PROTO((ANY *)); |
| 112 | |
| 113 | /* These wrappers around malloc *don't* call anything on failure */ |
| 114 | extern ANY *PyMem_Malloc Py_PROTO((size_t)); |
| 115 | extern ANY *PyMem_Realloc Py_PROTO((ANY *, size_t)); |
| 116 | extern void PyMem_Free Py_PROTO((ANY *)); |
| 117 | |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 118 | #ifdef __cplusplus |
| 119 | } |
| 120 | #endif |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 121 | |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 122 | #endif /* !Py_MYMALLOC_H */ |