Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-19 09:50:54

0001 // The PyMem_ family:  low-level memory allocation interfaces.
0002 // See objimpl.h for the PyObject_ memory family.
0003 
0004 #ifndef Py_PYMEM_H
0005 #define Py_PYMEM_H
0006 #ifdef __cplusplus
0007 extern "C" {
0008 #endif
0009 
0010 /* BEWARE:
0011 
0012    Each interface exports both functions and macros.  Extension modules should
0013    use the functions, to ensure binary compatibility across Python versions.
0014    Because the Python implementation is free to change internal details, and
0015    the macros may (or may not) expose details for speed, if you do use the
0016    macros you must recompile your extensions with each Python release.
0017 
0018    Never mix calls to PyMem_ with calls to the platform malloc/realloc/
0019    calloc/free.  For example, on Windows different DLLs may end up using
0020    different heaps, and if you use PyMem_Malloc you'll get the memory from the
0021    heap used by the Python DLL; it could be a disaster if you free()'ed that
0022    directly in your own extension.  Using PyMem_Free instead ensures Python
0023    can return the memory to the proper heap.  As another example, in
0024    a debug build (Py_DEBUG macro), Python wraps all calls to all PyMem_ and
0025    PyObject_ memory functions in special debugging wrappers that add additional
0026    debugging info to dynamic memory blocks.  The system routines have no idea
0027    what to do with that stuff, and the Python wrappers have no idea what to do
0028    with raw blocks obtained directly by the system routines then.
0029 
0030    The GIL must be held when using these APIs.
0031 */
0032 
0033 /*
0034  * Raw memory interface
0035  * ====================
0036  */
0037 
0038 /* Functions
0039 
0040    Functions supplying platform-independent semantics for malloc/realloc/
0041    free.  These functions make sure that allocating 0 bytes returns a distinct
0042    non-NULL pointer (whenever possible -- if we're flat out of memory, NULL
0043    may be returned), even if the platform malloc and realloc don't.
0044    Returned pointers must be checked for NULL explicitly.  No action is
0045    performed on failure (no exception is set, no warning is printed, etc).
0046 */
0047 
0048 PyAPI_FUNC(void *) PyMem_Malloc(size_t size);
0049 PyAPI_FUNC(void *) PyMem_Calloc(size_t nelem, size_t elsize);
0050 PyAPI_FUNC(void *) PyMem_Realloc(void *ptr, size_t new_size);
0051 PyAPI_FUNC(void) PyMem_Free(void *ptr);
0052 
0053 /*
0054  * Type-oriented memory interface
0055  * ==============================
0056  *
0057  * Allocate memory for n objects of the given type.  Returns a new pointer
0058  * or NULL if the request was too large or memory allocation failed.  Use
0059  * these macros rather than doing the multiplication yourself so that proper
0060  * overflow checking is always done.
0061  */
0062 
0063 #define PyMem_New(type, n) \
0064   ( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL :      \
0065         ( (type *) PyMem_Malloc((n) * sizeof(type)) ) )
0066 
0067 /*
0068  * The value of (p) is always clobbered by this macro regardless of success.
0069  * The caller MUST check if (p) is NULL afterwards and deal with the memory
0070  * error if so.  This means the original value of (p) MUST be saved for the
0071  * caller's memory error handler to not lose track of it.
0072  */
0073 #define PyMem_Resize(p, type, n) \
0074   ( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL :        \
0075         (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
0076 
0077 
0078 // Deprecated aliases only kept for backward compatibility.
0079 // PyMem_Del and PyMem_DEL are defined with no parameter to be able to use
0080 // them as function pointers (ex: dealloc = PyMem_Del).
0081 #define PyMem_MALLOC(n)           PyMem_Malloc((n))
0082 #define PyMem_NEW(type, n)        PyMem_New(type, (n))
0083 #define PyMem_REALLOC(p, n)       PyMem_Realloc((p), (n))
0084 #define PyMem_RESIZE(p, type, n)  PyMem_Resize((p), type, (n))
0085 #define PyMem_FREE(p)             PyMem_Free((p))
0086 #define PyMem_Del(p)              PyMem_Free((p))
0087 #define PyMem_DEL(p)              PyMem_Free((p))
0088 
0089 
0090 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000
0091 // Memory allocator which doesn't require the GIL to be held.
0092 // Usually, it's just a thin wrapper to functions of the standard C library:
0093 // malloc(), calloc(), realloc() and free(). The difference is that
0094 // tracemalloc can track these memory allocations.
0095 PyAPI_FUNC(void *) PyMem_RawMalloc(size_t size);
0096 PyAPI_FUNC(void *) PyMem_RawCalloc(size_t nelem, size_t elsize);
0097 PyAPI_FUNC(void *) PyMem_RawRealloc(void *ptr, size_t new_size);
0098 PyAPI_FUNC(void) PyMem_RawFree(void *ptr);
0099 #endif
0100 
0101 #ifndef Py_LIMITED_API
0102 #  define Py_CPYTHON_PYMEM_H
0103 #  include "cpython/pymem.h"
0104 #  undef Py_CPYTHON_PYMEM_H
0105 #endif
0106 
0107 #ifdef __cplusplus
0108 }
0109 #endif
0110 #endif   // !Py_PYMEM_H