| Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 1 | /* The PyMem_ family:  low-level memory allocation interfaces. | 
 | 2 |    See objimpl.h for the PyObject_ memory family. | 
 | 3 | */ | 
| Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 4 |  | 
 | 5 | #ifndef Py_PYMEM_H | 
 | 6 | #define Py_PYMEM_H | 
 | 7 |  | 
 | 8 | #include "pyport.h" | 
 | 9 |  | 
 | 10 | #ifdef __cplusplus | 
 | 11 | extern "C" { | 
 | 12 | #endif | 
 | 13 |  | 
| Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 14 | /* BEWARE: | 
 | 15 |  | 
| Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 16 |    Each interface exports both functions and macros.  Extension modules should | 
 | 17 |    use the functions, to ensure binary compatibility across Python versions. | 
 | 18 |    Because the Python implementation is free to change internal details, and | 
 | 19 |    the macros may (or may not) expose details for speed, if you do use the | 
 | 20 |    macros you must recompile your extensions with each Python release. | 
| Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 21 |  | 
| Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 22 |    Never mix calls to PyMem_ with calls to the platform malloc/realloc/ | 
 | 23 |    calloc/free.  For example, on Windows different DLLs may end up using | 
 | 24 |    different heaps, and if you use PyMem_Malloc you'll get the memory from the | 
 | 25 |    heap used by the Python DLL; it could be a disaster if you free()'ed that | 
 | 26 |    directly in your own extension.  Using PyMem_Free instead ensures Python | 
 | 27 |    can return the memory to the proper heap.  As another example, in | 
 | 28 |    PYMALLOC_DEBUG mode, Python wraps all calls to all PyMem_ and PyObject_ | 
 | 29 |    memory functions in special debugging wrappers that add additional | 
 | 30 |    debugging info to dynamic memory blocks.  The system routines have no idea | 
 | 31 |    what to do with that stuff, and the Python wrappers have no idea what to do | 
 | 32 |    with raw blocks obtained directly by the system routines then. | 
| Neal Norwitz | d83eb31 | 2007-05-02 04:47:55 +0000 | [diff] [blame] | 33 |  | 
 | 34 |    The GIL must be held when using these APIs. | 
| Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 35 | */ | 
| Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 36 |  | 
 | 37 | /* | 
 | 38 |  * Raw memory interface | 
 | 39 |  * ==================== | 
 | 40 |  */ | 
 | 41 |  | 
| Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 42 | /* Functions | 
| Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 43 |  | 
| Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 44 |    Functions supplying platform-independent semantics for malloc/realloc/ | 
 | 45 |    free.  These functions make sure that allocating 0 bytes returns a distinct | 
| Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 46 |    non-NULL pointer (whenever possible -- if we're flat out of memory, NULL | 
 | 47 |    may be returned), even if the platform malloc and realloc don't. | 
 | 48 |    Returned pointers must be checked for NULL explicitly.  No action is | 
| Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 49 |    performed on failure (no exception is set, no warning is printed, etc). | 
 | 50 | */ | 
| Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 51 |  | 
| Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 52 | PyAPI_FUNC(void *) PyMem_Malloc(size_t); | 
 | 53 | PyAPI_FUNC(void *) PyMem_Realloc(void *, size_t); | 
 | 54 | PyAPI_FUNC(void) PyMem_Free(void *); | 
| Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 55 |  | 
 | 56 | /* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are | 
 | 57 |    no longer supported. They used to call PyErr_NoMemory() on failure. */ | 
 | 58 |  | 
| Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 59 | /* Macros. */ | 
| Tim Peters | 51e7f5c | 2002-04-22 02:33:27 +0000 | [diff] [blame] | 60 | #ifdef PYMALLOC_DEBUG | 
 | 61 | /* Redirect all memory operations to Python's debugging allocator. */ | 
 | 62 | #define PyMem_MALLOC		PyObject_MALLOC | 
 | 63 | #define PyMem_REALLOC		PyObject_REALLOC | 
| Tim Peters | c9d78aa | 2006-03-26 23:27:58 +0000 | [diff] [blame] | 64 | #define PyMem_FREE		PyObject_FREE | 
| Tim Peters | 51e7f5c | 2002-04-22 02:33:27 +0000 | [diff] [blame] | 65 |  | 
 | 66 | #else	/* ! PYMALLOC_DEBUG */ | 
 | 67 |  | 
| Martin v. Löwis | 39f59b0 | 2002-11-23 09:13:40 +0000 | [diff] [blame] | 68 | /* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL | 
 | 69 |    for malloc(0), which would be treated as an error. Some platforms | 
 | 70 |    would return a pointer with no memory behind it, which would break | 
 | 71 |    pymalloc. To solve these problems, allocate an extra byte. */ | 
| Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 72 | #define PyMem_MALLOC(n)         malloc((n) ? (n) : 1) | 
| Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 73 | #define PyMem_REALLOC(p, n)     realloc((p), (n) ? (n) : 1) | 
| Tim Peters | c9d78aa | 2006-03-26 23:27:58 +0000 | [diff] [blame] | 74 | #define PyMem_FREE		free | 
| Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 75 |  | 
| Tim Peters | 51e7f5c | 2002-04-22 02:33:27 +0000 | [diff] [blame] | 76 | #endif	/* PYMALLOC_DEBUG */ | 
| Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 77 |  | 
| Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 78 | /* | 
 | 79 |  * Type-oriented memory interface | 
 | 80 |  * ============================== | 
| Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 81 |  * | 
 | 82 |  * These are carried along for historical reasons.  There's rarely a good | 
| Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 83 |  * reason to use them anymore (you can just as easily do the multiply and | 
 | 84 |  * cast yourself). | 
| Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 85 |  */ | 
 | 86 |  | 
| Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 87 | #define PyMem_New(type, n) \ | 
| Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 88 |   ( assert((n) <= PY_SIZE_MAX / sizeof(type)) , \ | 
 | 89 | 	( (type *) PyMem_Malloc((n) * sizeof(type)) ) ) | 
| Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 90 | #define PyMem_NEW(type, n) \ | 
| Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 91 |   ( assert((n) <= PY_SIZE_MAX / sizeof(type)) , \ | 
 | 92 | 	( (type *) PyMem_MALLOC((n) * sizeof(type)) ) ) | 
| Tim Peters | 8b078f9 | 2002-04-28 04:11:46 +0000 | [diff] [blame] | 93 |  | 
 | 94 | #define PyMem_Resize(p, type, n) \ | 
| Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 95 |   ( assert((n) <= PY_SIZE_MAX / sizeof(type)) , \ | 
 | 96 | 	( (p) = (type *) PyMem_Realloc((p), (n) * sizeof(type)) ) ) | 
| Tim Peters | af3e8de | 2002-04-12 07:22:56 +0000 | [diff] [blame] | 97 | #define PyMem_RESIZE(p, type, n) \ | 
| Gregory P. Smith | 9d53457 | 2008-06-11 07:41:16 +0000 | [diff] [blame] | 98 |   ( assert((n) <= PY_SIZE_MAX / sizeof(type)) , \ | 
 | 99 | 	( (p) = (type *) PyMem_REALLOC((p), (n) * sizeof(type)) ) ) | 
| Tim Peters | a5d78cc | 2002-03-02 08:43:19 +0000 | [diff] [blame] | 100 |  | 
| Tim Peters | c9d78aa | 2006-03-26 23:27:58 +0000 | [diff] [blame] | 101 | /* PyMem{Del,DEL} are left over from ancient days, and shouldn't be used | 
 | 102 |  * anymore.  They're just confusing aliases for PyMem_{Free,FREE} now. | 
 | 103 |  */ | 
 | 104 | #define PyMem_Del		PyMem_Free | 
 | 105 | #define PyMem_DEL		PyMem_FREE | 
| Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 106 |  | 
| Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 107 | #ifdef __cplusplus | 
 | 108 | } | 
 | 109 | #endif | 
 | 110 |  | 
| Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 111 | #endif /* !Py_PYMEM_H */ |