pymemcompat.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* The idea of this file is that you bundle it with your extension,
  2. #include it, program to Python 2.3's memory API and have your
  3. extension build with any version of Python from 1.5.2 through to
  4. 2.3 (and hopefully beyond). */
  5. #ifndef Py_PYMEMCOMPAT_H
  6. #define Py_PYMEMCOMPAT_H
  7. #include "Python.h"
  8. /* There are three "families" of memory API: the "raw memory", "object
  9. memory" and "object" families. (This is ignoring the matter of the
  10. cycle collector, about which more is said below).
  11. Raw Memory:
  12. PyMem_Malloc, PyMem_Realloc, PyMem_Free
  13. Object Memory:
  14. PyObject_Malloc, PyObject_Realloc, PyObject_Free
  15. Object:
  16. PyObject_New, PyObject_NewVar, PyObject_Del
  17. The raw memory and object memory allocators both mimic the
  18. malloc/realloc/free interface from ANSI C, but the object memory
  19. allocator can (and, since 2.3, does by default) use a different
  20. allocation strategy biased towards lots of lots of "small"
  21. allocations.
  22. The object family is used for allocating Python objects, and the
  23. initializers take care of some basic initialization (setting the
  24. refcount to 1 and filling out the ob_type field) as well as having
  25. a somewhat different interface.
  26. Do not mix the families! E.g. do not allocate memory with
  27. PyMem_Malloc and free it with PyObject_Free. You may get away with
  28. it quite a lot of the time, but there *are* scenarios where this
  29. will break. You Have Been Warned.
  30. Also, in many versions of Python there are an insane amount of
  31. memory interfaces to choose from. Use the ones described above. */
  32. #if PY_VERSION_HEX < 0x01060000
  33. /* raw memory interface already present */
  34. /* there is no object memory interface in 1.5.2 */
  35. #define PyObject_Malloc PyMem_Malloc
  36. #define PyObject_Realloc PyMem_Realloc
  37. #define PyObject_Free PyMem_Free
  38. /* the object interface is there, but the names have changed */
  39. #define PyObject_New PyObject_NEW
  40. #define PyObject_NewVar PyObject_NEW_VAR
  41. #define PyObject_Del PyMem_Free
  42. #endif
  43. /* If your object is a container you probably want to support the
  44. cycle collector, which was new in Python 2.0.
  45. Unfortunately, the interface to the collector that was present in
  46. Python 2.0 and 2.1 proved to be tricky to use, and so changed in
  47. 2.2 -- in a way that can't easily be papered over with macros.
  48. This file contains macros that let you program to the 2.2 GC API.
  49. Your module will compile against any Python since version 1.5.2,
  50. but the type will only participate in the GC in versions 2.2 and
  51. up. Some work is still necessary on your part to only fill out the
  52. tp_traverse and tp_clear fields when they exist and set tp_flags
  53. appropriately.
  54. It is possible to support both the 2.0 and 2.2 GC APIs, but it's
  55. not pretty and this comment block is too narrow to contain a
  56. desciption of what's required... */
  57. #if PY_VERSION_HEX < 0x020200B1
  58. #define PyObject_GC_New PyObject_New
  59. #define PyObject_GC_NewVar PyObject_NewVar
  60. #define PyObject_GC_Del PyObject_Del
  61. #define PyObject_GC_Track(op)
  62. #define PyObject_GC_UnTrack(op)
  63. #endif
  64. #endif /* !Py_PYMEMCOMPAT_H */