summaryrefslogtreecommitdiffstats
path: root/Include/mymalloc.h
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2000-05-03 23:44:39 (GMT)
committerGuido van Rossum <guido@python.org>2000-05-03 23:44:39 (GMT)
commitb18618dab7b6b85bb05b084693706e59211fa180 (patch)
tree785d51f6677da8366be2ad4b4296a62f53161276 /Include/mymalloc.h
parent2808b744e8d94459f189e1d89c97072d6a1f53b6 (diff)
downloadcpython-b18618dab7b6b85bb05b084693706e59211fa180.zip
cpython-b18618dab7b6b85bb05b084693706e59211fa180.tar.gz
cpython-b18618dab7b6b85bb05b084693706e59211fa180.tar.bz2
Vladimir Marangozov's long-awaited malloc restructuring.
For more comments, read the patches@python.org archives. For documentation read the comments in mymalloc.h and objimpl.h. (This is not exactly what Vladimir posted to the patches list; I've made a few changes, and Vladimir sent me a fix in private email for a problem that only occurs in debug mode. I'm also holding back on his change to main.c, which seems unnecessary to me.)
Diffstat (limited to 'Include/mymalloc.h')
-rw-r--r--Include/mymalloc.h133
1 files changed, 107 insertions, 26 deletions
diff --git a/Include/mymalloc.h b/Include/mymalloc.h
index 558af9d..df3113b 100644
--- a/Include/mymalloc.h
+++ b/Include/mymalloc.h
@@ -57,6 +57,8 @@ PERFORMANCE OF THIS SOFTWARE.
#include <stdlib.h>
#endif
+#include "myproto.h"
+
#ifdef __cplusplus
/* Move this down here since some C++ #include's don't like to be included
inside an extern "C" */
@@ -67,12 +69,8 @@ extern "C" {
#pragma lib_export on
#endif
-/* The following should never be necessary */
-#ifdef NEED_TO_DECLARE_MALLOC_AND_FRIEND
-extern ANY *malloc Py_PROTO((size_t));
-extern ANY *calloc Py_PROTO((size_t, size_t));
-extern ANY *realloc Py_PROTO((ANY *, size_t));
-extern void free Py_PROTO((ANY *)); /* XXX sometimes int on Unix old systems */
+#ifndef DL_IMPORT /* declarations for DLL import */
+#define DL_IMPORT(RTYPE) RTYPE
#endif
#ifndef NULL
@@ -87,34 +85,117 @@ extern void free Py_PROTO((ANY *)); /* XXX sometimes int on Unix old systems */
#define _PyMem_EXTRA 0
#endif
-#define PyMem_NEW(type, n) \
- ( (type *) malloc(_PyMem_EXTRA + (n) * sizeof(type)) )
-#define PyMem_RESIZE(p, type, n) \
- if ((p) == NULL) \
- (p) = (type *) malloc(_PyMem_EXTRA + (n) * sizeof(type)); \
- else \
- (p) = (type *) realloc((ANY *)(p), \
- _PyMem_EXTRA + (n) * sizeof(type))
-#define PyMem_DEL(p) free((ANY *)p)
-#define PyMem_XDEL(p) if ((p) == NULL) ; else PyMem_DEL(p)
+/*
+ * Core memory allocator
+ * =====================
+ */
+/* To make sure the interpreter is user-malloc friendly, all memory
+ APIs are implemented on top of this one.
-/* Two sets of function wrappers around malloc and friends; useful if
- you need to be sure that you are using the same memory allocator as
- Python. Note that the wrappers make sure that allocating 0 bytes
- returns a non-NULL pointer, even if the underlying malloc doesn't.
- The Python interpreter continues to use PyMem_NEW etc. */
+ The PyCore_* macros can be defined to make the interpreter use a
+ custom allocator. Note that they are for internal use only. Both
+ the core and extension modules should use the PyMem_* API. */
+
+#ifndef PyCore_MALLOC_FUNC
+#undef PyCore_REALLOC_FUNC
+#undef PyCore_FREE_FUNC
+#define PyCore_MALLOC_FUNC malloc
+#define PyCore_REALLOC_FUNC realloc
+#define PyCore_FREE_FUNC free
+#endif
+
+#ifndef PyCore_MALLOC_PROTO
+#undef PyCore_REALLOC_PROTO
+#undef PyCore_FREE_PROTO
+#define PyCore_MALLOC_PROTO Py_PROTO((size_t))
+#define PyCore_REALLOC_PROTO Py_PROTO((ANY *, size_t))
+#define PyCore_FREE_PROTO Py_PROTO((ANY *))
+#endif
+
+#ifdef NEED_TO_DECLARE_MALLOC_AND_FRIEND
+extern ANY *PyCore_MALLOC_FUNC PyCore_MALLOC_PROTO;
+extern ANY *PyCore_REALLOC_FUNC PyCore_REALLOC_PROTO;
+extern void PyCore_FREE_FUNC PyCore_FREE_PROTO;
+#endif
+
+#ifndef PyCore_MALLOC
+#undef PyCore_REALLOC
+#undef PyCore_FREE
+#define PyCore_MALLOC(n) PyCore_MALLOC_FUNC(n)
+#define PyCore_REALLOC(p, n) PyCore_REALLOC_FUNC((p), (n))
+#define PyCore_FREE(p) PyCore_FREE_FUNC(p)
+#endif
-/* These wrappers around malloc call PyErr_NoMemory() on failure */
-extern DL_IMPORT(ANY *) Py_Malloc Py_PROTO((size_t));
-extern DL_IMPORT(ANY *) Py_Realloc Py_PROTO((ANY *, size_t));
-extern DL_IMPORT(void) Py_Free Py_PROTO((ANY *));
+/* BEWARE:
-/* These wrappers around malloc *don't* call anything on failure */
+ Each interface exports both functions and macros. Extension modules
+ should normally use the functions for ensuring binary compatibility
+ of the user's code across Python versions. Subsequently, if the
+ Python runtime switches to its own malloc (different from standard
+ malloc), no recompilation is required for the extensions.
+
+ The macro versions trade compatibility for speed. They can be used
+ whenever there is a performance problem, but their use implies
+ recompilation of the code for each new Python release. The Python
+ core uses the macros because it *is* compiled on every upgrade.
+ This might not be the case with 3rd party extensions in a custom
+ setup (for example, a customer does not always have access to the
+ source of 3rd party deliverables). You have been warned! */
+
+/*
+ * Raw memory interface
+ * ====================
+ */
+
+/* Functions */
+
+/* Function wrappers around PyCore_MALLOC and friends; useful if you
+ need to be sure that you are using the same memory allocator as
+ Python. Note that the wrappers make sure that allocating 0 bytes
+ returns a non-NULL pointer, even if the underlying malloc
+ doesn't. Returned pointers must be checked for NULL explicitly.
+ No action is performed on failure. */
extern DL_IMPORT(ANY *) PyMem_Malloc Py_PROTO((size_t));
extern DL_IMPORT(ANY *) PyMem_Realloc Py_PROTO((ANY *, size_t));
extern DL_IMPORT(void) PyMem_Free Py_PROTO((ANY *));
+/* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are
+ no longer supported. They used to call PyErr_NoMemory() on failure. */
+
+/* Macros */
+#define PyMem_MALLOC(n) PyCore_MALLOC(n)
+#define PyMem_REALLOC(p, n) PyCore_REALLOC((ANY *)(p), (n))
+#define PyMem_FREE(p) PyCore_FREE((ANY *)(p))
+
+/*
+ * Type-oriented memory interface
+ * ==============================
+ */
+
+/* Functions */
+#define PyMem_New(type, n) \
+ ( (type *) PyMem_Malloc((n) * sizeof(type)) )
+#define PyMem_Resize(p, type, n) \
+ ( (p) = (type *) PyMem_Realloc((n) * sizeof(type)) )
+#define PyMem_Del(p) PyMem_Free(p)
+
+/* Macros */
+#define PyMem_NEW(type, n) \
+ ( (type *) PyMem_MALLOC(_PyMem_EXTRA + (n) * sizeof(type)) )
+#define PyMem_RESIZE(p, type, n) \
+ if ((p) == NULL) \
+ (p) = (type *)(PyMem_MALLOC( \
+ _PyMem_EXTRA + (n) * sizeof(type))); \
+ else \
+ (p) = (type *)(PyMem_REALLOC((p), \
+ _PyMem_EXTRA + (n) * sizeof(type)))
+#define PyMem_DEL(p) PyMem_FREE(p)
+
+/* PyMem_XDEL is deprecated. To avoid the call when p is NULL,
+ it is recommended to write the test explicitly in the code.
+ Note that according to ANSI C, free(NULL) has no effect. */
+
#ifdef __cplusplus
}
#endif