mem.pxd 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. cdef extern from "Python.h":
  2. #####################################################################
  3. # 9.2 Memory Interface
  4. #####################################################################
  5. # You are definitely *supposed* to use these: "In most situations,
  6. # however, it is recommended to allocate memory from the Python
  7. # heap specifically because the latter is under control of the
  8. # Python memory manager. For example, this is required when the
  9. # interpreter is extended with new object types written in
  10. # C. Another reason for using the Python heap is the desire to
  11. # inform the Python memory manager about the memory needs of the
  12. # extension module. Even when the requested memory is used
  13. # exclusively for internal, highly-specific purposes, delegating
  14. # all memory requests to the Python memory manager causes the
  15. # interpreter to have a more accurate image of its memory
  16. # footprint as a whole. Consequently, under certain circumstances,
  17. # the Python memory manager may or may not trigger appropriate
  18. # actions, like garbage collection, memory compaction or other
  19. # preventive procedures. Note that by using the C library
  20. # allocator as shown in the previous example, the allocated memory
  21. # for the I/O buffer escapes completely the Python memory
  22. # manager."
  23. # The following function sets, modeled after the ANSI C standard,
  24. # but specifying behavior when requesting zero bytes, are
  25. # available for allocating and releasing memory from the Python
  26. # heap:
  27. void* PyMem_RawMalloc(size_t n) nogil
  28. void* PyMem_Malloc(size_t n)
  29. # Allocates n bytes and returns a pointer of type void* to the
  30. # allocated memory, or NULL if the request fails. Requesting zero
  31. # bytes returns a distinct non-NULL pointer if possible, as if
  32. # PyMem_Malloc(1) had been called instead. The memory will not
  33. # have been initialized in any way.
  34. void* PyMem_RawRealloc(void *p, size_t n) nogil
  35. void* PyMem_Realloc(void *p, size_t n)
  36. # Resizes the memory block pointed to by p to n bytes. The
  37. # contents will be unchanged to the minimum of the old and the new
  38. # sizes. If p is NULL, the call is equivalent to PyMem_Malloc(n);
  39. # else if n is equal to zero, the memory block is resized but is
  40. # not freed, and the returned pointer is non-NULL. Unless p is
  41. # NULL, it must have been returned by a previous call to
  42. # PyMem_Malloc() or PyMem_Realloc().
  43. void PyMem_RawFree(void *p) nogil
  44. void PyMem_Free(void *p)
  45. # Frees the memory block pointed to by p, which must have been
  46. # returned by a previous call to PyMem_Malloc() or
  47. # PyMem_Realloc(). Otherwise, or if PyMem_Free(p) has been called
  48. # before, undefined behavior occurs. If p is NULL, no operation is
  49. # performed.
  50. # The following type-oriented macros are provided for
  51. # convenience. Note that TYPE refers to any C type.
  52. # TYPE* PyMem_New(TYPE, size_t n)
  53. # Same as PyMem_Malloc(), but allocates (n * sizeof(TYPE)) bytes
  54. # of memory. Returns a pointer cast to TYPE*. The memory will not
  55. # have been initialized in any way.
  56. # TYPE* PyMem_Resize(void *p, TYPE, size_t n)
  57. # Same as PyMem_Realloc(), but the memory block is resized to (n *
  58. # sizeof(TYPE)) bytes. Returns a pointer cast to TYPE*.
  59. void PyMem_Del(void *p)
  60. # Same as PyMem_Free().
  61. # In addition, the following macro sets are provided for calling
  62. # the Python memory allocator directly, without involving the C
  63. # API functions listed above. However, note that their use does
  64. # not preserve binary compatibility across Python versions and is
  65. # therefore deprecated in extension modules.
  66. # PyMem_MALLOC(), PyMem_REALLOC(), PyMem_FREE().
  67. # PyMem_NEW(), PyMem_RESIZE(), PyMem_DEL().
  68. #####################################################################
  69. # Raw object memory interface
  70. #####################################################################
  71. # Functions to call the same malloc/realloc/free as used by Python's
  72. # object allocator. If WITH_PYMALLOC is enabled, these may differ from
  73. # the platform malloc/realloc/free. The Python object allocator is
  74. # designed for fast, cache-conscious allocation of many "small" objects,
  75. # and with low hidden memory overhead.
  76. #
  77. # PyObject_Malloc(0) returns a unique non-NULL pointer if possible.
  78. #
  79. # PyObject_Realloc(NULL, n) acts like PyObject_Malloc(n).
  80. # PyObject_Realloc(p != NULL, 0) does not return NULL, or free the memory
  81. # at p.
  82. #
  83. # Returned pointers must be checked for NULL explicitly; no action is
  84. # performed on failure other than to return NULL (no warning it printed, no
  85. # exception is set, etc).
  86. #
  87. # For allocating objects, use PyObject_{New, NewVar} instead whenever
  88. # possible. The PyObject_{Malloc, Realloc, Free} family is exposed
  89. # so that you can exploit Python's small-block allocator for non-object
  90. # uses. If you must use these routines to allocate object memory, make sure
  91. # the object gets initialized via PyObject_{Init, InitVar} after obtaining
  92. # the raw memory.
  93. void* PyObject_Malloc(size_t size)
  94. void* PyObject_Calloc(size_t nelem, size_t elsize)
  95. void* PyObject_Realloc(void *ptr, size_t new_size)
  96. void PyObject_Free(void *ptr)