mem.pxd 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_Malloc(size_t n)
  28. # Allocates n bytes and returns a pointer of type void* to the
  29. # allocated memory, or NULL if the request fails. Requesting zero
  30. # bytes returns a distinct non-NULL pointer if possible, as if
  31. # PyMem_Malloc(1) had been called instead. The memory will not
  32. # have been initialized in any way.
  33. void* PyMem_Realloc(void *p, size_t n)
  34. # Resizes the memory block pointed to by p to n bytes. The
  35. # contents will be unchanged to the minimum of the old and the new
  36. # sizes. If p is NULL, the call is equivalent to PyMem_Malloc(n);
  37. # else if n is equal to zero, the memory block is resized but is
  38. # not freed, and the returned pointer is non-NULL. Unless p is
  39. # NULL, it must have been returned by a previous call to
  40. # PyMem_Malloc() or PyMem_Realloc().
  41. void PyMem_Free(void *p)
  42. # Frees the memory block pointed to by p, which must have been
  43. # returned by a previous call to PyMem_Malloc() or
  44. # PyMem_Realloc(). Otherwise, or if PyMem_Free(p) has been called
  45. # before, undefined behavior occurs. If p is NULL, no operation is
  46. # performed.
  47. # The following type-oriented macros are provided for
  48. # convenience. Note that TYPE refers to any C type.
  49. # TYPE* PyMem_New(TYPE, size_t n)
  50. # Same as PyMem_Malloc(), but allocates (n * sizeof(TYPE)) bytes
  51. # of memory. Returns a pointer cast to TYPE*. The memory will not
  52. # have been initialized in any way.
  53. # TYPE* PyMem_Resize(void *p, TYPE, size_t n)
  54. # Same as PyMem_Realloc(), but the memory block is resized to (n *
  55. # sizeof(TYPE)) bytes. Returns a pointer cast to TYPE*.
  56. void PyMem_Del(void *p)
  57. # Same as PyMem_Free().
  58. # In addition, the following macro sets are provided for calling
  59. # the Python memory allocator directly, without involving the C
  60. # API functions listed above. However, note that their use does
  61. # not preserve binary compatibility across Python versions and is
  62. # therefore deprecated in extension modules.
  63. # PyMem_MALLOC(), PyMem_REALLOC(), PyMem_FREE().
  64. # PyMem_NEW(), PyMem_RESIZE(), PyMem_DEL().
  65. #####################################################################
  66. # Raw object memory interface
  67. #####################################################################
  68. # Functions to call the same malloc/realloc/free as used by Python's
  69. # object allocator. If WITH_PYMALLOC is enabled, these may differ from
  70. # the platform malloc/realloc/free. The Python object allocator is
  71. # designed for fast, cache-conscious allocation of many "small" objects,
  72. # and with low hidden memory overhead.
  73. #
  74. # PyObject_Malloc(0) returns a unique non-NULL pointer if possible.
  75. #
  76. # PyObject_Realloc(NULL, n) acts like PyObject_Malloc(n).
  77. # PyObject_Realloc(p != NULL, 0) does not return NULL, or free the memory
  78. # at p.
  79. #
  80. # Returned pointers must be checked for NULL explicitly; no action is
  81. # performed on failure other than to return NULL (no warning it printed, no
  82. # exception is set, etc).
  83. #
  84. # For allocating objects, use PyObject_{New, NewVar} instead whenever
  85. # possible. The PyObject_{Malloc, Realloc, Free} family is exposed
  86. # so that you can exploit Python's small-block allocator for non-object
  87. # uses. If you must use these routines to allocate object memory, make sure
  88. # the object gets initialized via PyObject_{Init, InitVar} after obtaining
  89. # the raw memory.
  90. void* PyObject_Malloc(size_t size)
  91. void* PyObject_Calloc(size_t nelem, size_t elsize)
  92. void* PyObject_Realloc(void *ptr, size_t new_size)
  93. void PyObject_Free(void *ptr)