Changeset 391 for python/trunk/Doc/c-api/memory.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/c-api/memory.rst
r2 r391 48 48 49 49 To avoid memory corruption, extension writers should never try to operate on 50 Python objects with the functions exported by the C library: :c func:`malloc`,51 :c func:`calloc`, :cfunc:`realloc` and :cfunc:`free`. This will result in mixed50 Python objects with the functions exported by the C library: :c:func:`malloc`, 51 :c:func:`calloc`, :c:func:`realloc` and :c:func:`free`. This will result in mixed 52 52 calls between the C allocator and the Python memory manager with fatal 53 53 consequences, because they implement different algorithms and operate on … … 95 95 96 96 97 .. c function:: void* PyMem_Malloc(size_t n)98 99 Allocates *n* bytes and returns a pointer of type :c type:`void\*` to the97 .. c:function:: void* PyMem_Malloc(size_t n) 98 99 Allocates *n* bytes and returns a pointer of type :c:type:`void\*` to the 100 100 allocated memory, or *NULL* if the request fails. Requesting zero bytes returns 101 a distinct non-*NULL* pointer if possible, as if :cfunc:`PyMem_Malloc(1)` had101 a distinct non-*NULL* pointer if possible, as if ``PyMem_Malloc(1)`` had 102 102 been called instead. The memory will not have been initialized in any way. 103 103 104 104 105 .. c function:: void* PyMem_Realloc(void *p, size_t n)105 .. c:function:: void* PyMem_Realloc(void *p, size_t n) 106 106 107 107 Resizes the memory block pointed to by *p* to *n* bytes. The contents will be 108 108 unchanged to the minimum of the old and the new sizes. If *p* is *NULL*, the 109 call is equivalent to :cfunc:`PyMem_Malloc(n)`; else if *n* is equal to zero,109 call is equivalent to ``PyMem_Malloc(n)``; else if *n* is equal to zero, 110 110 the memory block is resized but is not freed, and the returned pointer is 111 111 non-*NULL*. Unless *p* is *NULL*, it must have been returned by a previous call 112 to :c func:`PyMem_Malloc` or :cfunc:`PyMem_Realloc`. If the request fails,113 :c func:`PyMem_Realloc` returns *NULL* and *p* remains a valid pointer to the112 to :c:func:`PyMem_Malloc` or :c:func:`PyMem_Realloc`. If the request fails, 113 :c:func:`PyMem_Realloc` returns *NULL* and *p* remains a valid pointer to the 114 114 previous memory area. 115 115 116 116 117 .. c function:: void PyMem_Free(void *p)117 .. c:function:: void PyMem_Free(void *p) 118 118 119 119 Frees the memory block pointed to by *p*, which must have been returned by a 120 previous call to :c func:`PyMem_Malloc` or :cfunc:`PyMem_Realloc`. Otherwise, or121 if :cfunc:`PyMem_Free(p)` has been called before, undefined behavior occurs. If120 previous call to :c:func:`PyMem_Malloc` or :c:func:`PyMem_Realloc`. Otherwise, or 121 if ``PyMem_Free(p)`` has been called before, undefined behavior occurs. If 122 122 *p* is *NULL*, no operation is performed. 123 123 … … 126 126 127 127 128 .. c function:: TYPE* PyMem_New(TYPE, size_t n)129 130 Same as :c func:`PyMem_Malloc`, but allocates ``(n * sizeof(TYPE))`` bytes of131 memory. Returns a pointer cast to :c type:`TYPE\*`. The memory will not have128 .. c:function:: TYPE* PyMem_New(TYPE, size_t n) 129 130 Same as :c:func:`PyMem_Malloc`, but allocates ``(n * sizeof(TYPE))`` bytes of 131 memory. Returns a pointer cast to :c:type:`TYPE\*`. The memory will not have 132 132 been initialized in any way. 133 133 134 134 135 .. c function:: TYPE* PyMem_Resize(void *p, TYPE, size_t n)136 137 Same as :c func:`PyMem_Realloc`, but the memory block is resized to ``(n *138 sizeof(TYPE))`` bytes. Returns a pointer cast to :c type:`TYPE\*`. On return,135 .. c:function:: TYPE* PyMem_Resize(void *p, TYPE, size_t n) 136 137 Same as :c:func:`PyMem_Realloc`, but the memory block is resized to ``(n * 138 sizeof(TYPE))`` bytes. Returns a pointer cast to :c:type:`TYPE\*`. On return, 139 139 *p* will be a pointer to the new memory area, or *NULL* in the event of 140 140 failure. This is a C preprocessor macro; p is always reassigned. Save … … 142 142 143 143 144 .. c function:: void PyMem_Del(void *p)145 146 Same as :c func:`PyMem_Free`.144 .. c:function:: void PyMem_Del(void *p) 145 146 Same as :c:func:`PyMem_Free`. 147 147 148 148 In addition, the following macro sets are provided for calling the Python memory … … 151 151 versions and is therefore deprecated in extension modules. 152 152 153 :c func:`PyMem_MALLOC`, :cfunc:`PyMem_REALLOC`, :cfunc:`PyMem_FREE`.154 155 :c func:`PyMem_NEW`, :cfunc:`PyMem_RESIZE`, :cfunc:`PyMem_DEL`.153 :c:func:`PyMem_MALLOC`, :c:func:`PyMem_REALLOC`, :c:func:`PyMem_FREE`. 154 155 :c:func:`PyMem_NEW`, :c:func:`PyMem_RESIZE`, :c:func:`PyMem_DEL`. 156 156 157 157 … … 202 202 203 203 In addition to the functions aimed at handling raw memory blocks from the Python 204 heap, objects in Python are allocated and released with :c func:`PyObject_New`,205 :c func:`PyObject_NewVar` and :cfunc:`PyObject_Del`.204 heap, objects in Python are allocated and released with :c:func:`PyObject_New`, 205 :c:func:`PyObject_NewVar` and :c:func:`PyObject_Del`. 206 206 207 207 These will be explained in the next chapter on defining and implementing new
Note:
See TracChangeset
for help on using the changeset viewer.