home *** CD-ROM | disk | FTP | other *** search
/ Freelog 33 / Freelog033.iso / Progr / Python-2.2.1.exe / OBJIMPL.H < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-11  |  13.1 KB  |  344 lines

  1.  
  2. #ifndef Py_OBJIMPL_H
  3. #define Py_OBJIMPL_H
  4.  
  5. #include "pymem.h"
  6.  
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10.  
  11. /*
  12. Functions and macros for modules that implement new object types.
  13. You must first include "object.h".
  14.  
  15.  - PyObject_New(type, typeobj) allocates memory for a new object of
  16.    the given type; here 'type' must be the C structure type used to
  17.    represent the object and 'typeobj' the address of the corresponding
  18.    type object.  Reference count and type pointer are filled in; the
  19.    rest of the bytes of the object are *undefined*!  The resulting
  20.    expression type is 'type *'.  The size of the object is actually
  21.    determined by the tp_basicsize field of the type object.
  22.  
  23.  - PyObject_NewVar(type, typeobj, n) is similar but allocates a
  24.    variable-size object with n extra items.  The size is computed as
  25.    tp_basicsize plus n * tp_itemsize.  This fills in the ob_size field
  26.    as well.
  27.  
  28.  - PyObject_Del(op) releases the memory allocated for an object.
  29.  
  30.  - PyObject_Init(op, typeobj) and PyObject_InitVar(op, typeobj, n) are
  31.    similar to PyObject_{New, NewVar} except that they don't allocate
  32.    the memory needed for an object. Instead of the 'type' parameter,
  33.    they accept the pointer of a new object (allocated by an arbitrary
  34.    allocator) and initialize its object header fields.
  35.  
  36. Note that objects created with PyObject_{New, NewVar} are allocated
  37. within the Python heap by an object allocator, the latter being
  38. implemented (by default) on top of the Python raw memory
  39. allocator. This ensures that Python keeps control on the user's
  40. objects regarding their memory management; for instance, they may be
  41. subject to automatic garbage collection.
  42.  
  43. In case a specific form of memory management is needed, implying that
  44. the objects would not reside in the Python heap (for example standard
  45. malloc heap(s) are mandatory, use of shared memory, C++ local storage
  46. or operator new), you must first allocate the object with your custom
  47. allocator, then pass its pointer to PyObject_{Init, InitVar} for
  48. filling in its Python-specific fields: reference count, type pointer,
  49. possibly others. You should be aware that Python has very limited
  50. control over these objects because they don't cooperate with the
  51. Python memory manager. Such objects may not be eligible for automatic
  52. garbage collection and you have to make sure that they are released
  53. accordingly whenever their destructor gets called (cf. the specific
  54. form of memory management you're using).
  55.  
  56. Unless you have specific memory management requirements, it is
  57. recommended to use PyObject_{New, NewVar, Del}. */
  58.  
  59. /*
  60.  * Core object memory allocator
  61.  * ============================
  62.  */
  63.  
  64. /* The purpose of the object allocator is to make the distinction
  65.    between "object memory" and the rest within the Python heap.
  66.  
  67.    Object memory is the one allocated by PyObject_{New, NewVar}, i.e.
  68.    the one that holds the object's representation defined by its C
  69.    type structure, *excluding* any object-specific memory buffers that
  70.    might be referenced by the structure (for type structures that have
  71.    pointer fields).  By default, the object memory allocator is
  72.    implemented on top of the raw memory allocator.
  73.  
  74.    The PyCore_* macros can be defined to make the interpreter use a
  75.    custom object memory allocator. They are reserved for internal
  76.    memory management purposes exclusively. Both the core and extension
  77.    modules should use the PyObject_* API. */
  78.  
  79. #ifdef WITH_PYMALLOC
  80. #define PyCore_OBJECT_MALLOC_FUNC    _PyCore_ObjectMalloc
  81. #define PyCore_OBJECT_REALLOC_FUNC   _PyCore_ObjectRealloc
  82. #define PyCore_OBJECT_FREE_FUNC      _PyCore_ObjectFree
  83. #define NEED_TO_DECLARE_OBJECT_MALLOC_AND_FRIEND
  84. #endif /* !WITH_PYMALLOC */
  85.  
  86. #ifndef PyCore_OBJECT_MALLOC_FUNC
  87. #undef PyCore_OBJECT_REALLOC_FUNC
  88. #undef PyCore_OBJECT_FREE_FUNC
  89. #define PyCore_OBJECT_MALLOC_FUNC    PyCore_MALLOC_FUNC
  90. #define PyCore_OBJECT_REALLOC_FUNC   PyCore_REALLOC_FUNC
  91. #define PyCore_OBJECT_FREE_FUNC      PyCore_FREE_FUNC
  92. #endif
  93.  
  94. #ifndef PyCore_OBJECT_MALLOC_PROTO
  95. #undef PyCore_OBJECT_REALLOC_PROTO
  96. #undef PyCore_OBJECT_FREE_PROTO
  97. #define PyCore_OBJECT_MALLOC_PROTO   PyCore_MALLOC_PROTO
  98. #define PyCore_OBJECT_REALLOC_PROTO  PyCore_REALLOC_PROTO
  99. #define PyCore_OBJECT_FREE_PROTO     PyCore_FREE_PROTO
  100. #endif
  101.  
  102. #ifdef NEED_TO_DECLARE_OBJECT_MALLOC_AND_FRIEND
  103. extern void *PyCore_OBJECT_MALLOC_FUNC PyCore_OBJECT_MALLOC_PROTO;
  104. extern void *PyCore_OBJECT_REALLOC_FUNC PyCore_OBJECT_REALLOC_PROTO;
  105. extern void PyCore_OBJECT_FREE_FUNC PyCore_OBJECT_FREE_PROTO;
  106. #endif
  107.  
  108. #ifndef PyCore_OBJECT_MALLOC
  109. #undef PyCore_OBJECT_REALLOC
  110. #undef PyCore_OBJECT_FREE
  111. #define PyCore_OBJECT_MALLOC(n)      PyCore_OBJECT_MALLOC_FUNC(n)
  112. #define PyCore_OBJECT_REALLOC(p, n)  PyCore_OBJECT_REALLOC_FUNC((p), (n))
  113. #define PyCore_OBJECT_FREE(p)        PyCore_OBJECT_FREE_FUNC(p)
  114. #endif
  115.  
  116. /*
  117.  * Raw object memory interface
  118.  * ===========================
  119.  */
  120.  
  121. /* The use of this API should be avoided, unless a builtin object
  122.    constructor inlines PyObject_{New, NewVar}, either because the
  123.    latter functions cannot allocate the exact amount of needed memory,
  124.    either for speed. This situation is exceptional, but occurs for
  125.    some object constructors (PyBuffer_New, PyList_New...).  Inlining
  126.    PyObject_{New, NewVar} for objects that are supposed to belong to
  127.    the Python heap is discouraged. If you really have to, make sure
  128.    the object is initialized with PyObject_{Init, InitVar}. Do *not*
  129.    inline PyObject_{Init, InitVar} for user-extension types or you
  130.    might seriously interfere with Python's memory management. */
  131.  
  132. /* Functions */
  133.  
  134. /* Wrappers around PyCore_OBJECT_MALLOC and friends; useful if you
  135.    need to be sure that you are using the same object memory allocator
  136.    as Python. These wrappers *do not* make sure that allocating 0
  137.    bytes returns a non-NULL pointer. Returned pointers must be checked
  138.    for NULL explicitly; no action is performed on failure. */
  139. extern DL_IMPORT(void *) PyObject_Malloc(size_t);
  140. extern DL_IMPORT(void *) PyObject_Realloc(void *, size_t);
  141. extern DL_IMPORT(void) PyObject_Free(void *);
  142.  
  143. /* Macros */
  144. #define PyObject_MALLOC(n)           PyCore_OBJECT_MALLOC(n)
  145. #define PyObject_REALLOC(op, n)      PyCore_OBJECT_REALLOC((void *)(op), (n))
  146. #define PyObject_FREE(op)            PyCore_OBJECT_FREE((void *)(op))
  147.  
  148. /*
  149.  * Generic object allocator interface
  150.  * ==================================
  151.  */
  152.  
  153. /* Functions */
  154. extern DL_IMPORT(PyObject *) PyObject_Init(PyObject *, PyTypeObject *);
  155. extern DL_IMPORT(PyVarObject *) PyObject_InitVar(PyVarObject *,
  156.                                                  PyTypeObject *, int);
  157. extern DL_IMPORT(PyObject *) _PyObject_New(PyTypeObject *);
  158. extern DL_IMPORT(PyVarObject *) _PyObject_NewVar(PyTypeObject *, int);
  159. extern DL_IMPORT(void) _PyObject_Del(PyObject *);
  160.  
  161. #define PyObject_New(type, typeobj) \
  162.         ( (type *) _PyObject_New(typeobj) )
  163. #define PyObject_NewVar(type, typeobj, n) \
  164.         ( (type *) _PyObject_NewVar((typeobj), (n)) )
  165. #define PyObject_Del(op) _PyObject_Del((PyObject *)(op))
  166.  
  167. /* Macros trading binary compatibility for speed. See also pymem.h.
  168.    Note that these macros expect non-NULL object pointers.*/
  169. #define PyObject_INIT(op, typeobj) \
  170.     ( (op)->ob_type = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
  171. #define PyObject_INIT_VAR(op, typeobj, size) \
  172.     ( (op)->ob_size = (size), PyObject_INIT((op), (typeobj)) )
  173.  
  174. #define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
  175.  
  176. /* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a
  177.    vrbl-size object with nitems items, exclusive of gc overhead (if any).  The
  178.    value is rounded up to the closest multiple of sizeof(void *), in order to
  179.    ensure that pointer fields at the end of the object are correctly aligned
  180.    for the platform (this is of special importance for subclasses of, e.g.,
  181.    str or long, so that pointers can be stored after the embedded data).
  182.  
  183.    Note that there's no memory wastage in doing this, as malloc has to
  184.    return (at worst) pointer-aligned memory anyway.
  185. */
  186. #if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0
  187. #   error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2"
  188. #endif
  189.  
  190. #define _PyObject_VAR_SIZE(typeobj, nitems)    \
  191.     (size_t)                \
  192.     ( ( (typeobj)->tp_basicsize +        \
  193.         (nitems)*(typeobj)->tp_itemsize +    \
  194.         (SIZEOF_VOID_P - 1)            \
  195.       ) & ~(SIZEOF_VOID_P - 1)        \
  196.     )
  197.  
  198. #define PyObject_NEW(type, typeobj) \
  199. ( (type *) PyObject_Init( \
  200.     (PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) )
  201.  
  202. #define PyObject_NEW_VAR(type, typeobj, n) \
  203. ( (type *) PyObject_InitVar( \
  204.       (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj),(n)) ),\
  205.       (typeobj), (n)) )
  206.  
  207. #define PyObject_DEL(op) PyObject_FREE(op)
  208.  
  209. /* This example code implements an object constructor with a custom
  210.    allocator, where PyObject_New is inlined, and shows the important
  211.    distinction between two steps (at least):
  212.        1) the actual allocation of the object storage;
  213.        2) the initialization of the Python specific fields
  214.           in this storage with PyObject_{Init, InitVar}.
  215.  
  216.    PyObject *
  217.    YourObject_New(...)
  218.    {
  219.        PyObject *op;
  220.  
  221.        op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
  222.        if (op == NULL)
  223.            return PyErr_NoMemory();
  224.  
  225.        op = PyObject_Init(op, &YourTypeStruct);
  226.        if (op == NULL)
  227.            return NULL;
  228.  
  229.        op->ob_field = value;
  230.        ...
  231.        return op;
  232.    }
  233.  
  234.    Note that in C++, the use of the new operator usually implies that
  235.    the 1st step is performed automatically for you, so in a C++ class
  236.    constructor you would start directly with PyObject_Init/InitVar. */
  237.  
  238. /*
  239.  * Garbage Collection Support
  240.  * ==========================
  241.  *
  242.  * Some of the functions and macros below are always defined; when
  243.  * WITH_CYCLE_GC is undefined, they simply don't do anything different
  244.  * than their non-GC counterparts.
  245.  */
  246.  
  247. /* Test if a type has a GC head */
  248. #define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)
  249.  
  250. /* Test if an object has a GC head */
  251. #define PyObject_IS_GC(o) (PyType_IS_GC((o)->ob_type) && \
  252.     ((o)->ob_type->tp_is_gc == NULL || (o)->ob_type->tp_is_gc(o)))
  253.  
  254. extern DL_IMPORT(PyObject *) _PyObject_GC_Malloc(PyTypeObject *, int);
  255. extern DL_IMPORT(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, int);
  256.  
  257. #define PyObject_GC_Resize(type, op, n) \
  258.         ( (type *) _PyObject_GC_Resize((PyVarObject *)(op), (n)) )
  259.  
  260. extern DL_IMPORT(PyObject *) _PyObject_GC_New(PyTypeObject *);
  261. extern DL_IMPORT(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, int);
  262. extern DL_IMPORT(void) _PyObject_GC_Del(PyObject *);
  263. extern DL_IMPORT(void) _PyObject_GC_Track(PyObject *);
  264. extern DL_IMPORT(void) _PyObject_GC_UnTrack(PyObject *);
  265.  
  266. #ifdef WITH_CYCLE_GC
  267.  
  268. /* GC information is stored BEFORE the object structure */
  269. typedef union _gc_head {
  270.     struct {
  271.         union _gc_head *gc_next; /* not NULL if object is tracked */
  272.         union _gc_head *gc_prev;
  273.         int gc_refs;
  274.     } gc;
  275.     double dummy;  /* force worst-case alignment */
  276. } PyGC_Head;
  277.  
  278. extern PyGC_Head _PyGC_generation0;
  279.  
  280. /* Tell the GC to track this object.  NB: While the object is tracked the
  281.  * collector it must be safe to call the ob_traverse method. */
  282. #define _PyObject_GC_TRACK(o) do { \
  283.     PyGC_Head *g = (PyGC_Head *)(o)-1; \
  284.     if (g->gc.gc_next != NULL) \
  285.         Py_FatalError("GC object already in linked list"); \
  286.     g->gc.gc_next = &_PyGC_generation0; \
  287.     g->gc.gc_prev = _PyGC_generation0.gc.gc_prev; \
  288.     g->gc.gc_prev->gc.gc_next = g; \
  289.     _PyGC_generation0.gc.gc_prev = g; \
  290.     } while (0);
  291.  
  292. /* Tell the GC to stop tracking this object. */
  293. #define _PyObject_GC_UNTRACK(o) do { \
  294.     PyGC_Head *g = (PyGC_Head *)(o)-1; \
  295.     g->gc.gc_prev->gc.gc_next = g->gc.gc_next; \
  296.     g->gc.gc_next->gc.gc_prev = g->gc.gc_prev; \
  297.     g->gc.gc_next = NULL; \
  298.     } while (0);
  299.  
  300. #define PyObject_GC_Track(op) _PyObject_GC_Track((PyObject *)op)
  301. #define PyObject_GC_UnTrack(op) _PyObject_GC_UnTrack((PyObject *)op)
  302.  
  303.  
  304. #define PyObject_GC_New(type, typeobj) \
  305.         ( (type *) _PyObject_GC_New(typeobj) )
  306. #define PyObject_GC_NewVar(type, typeobj, n) \
  307.         ( (type *) _PyObject_GC_NewVar((typeobj), (n)) )
  308. #define PyObject_GC_Del(op) _PyObject_GC_Del((PyObject *)(op))
  309.  
  310. #else /* !WITH_CYCLE_GC */
  311.  
  312. #define PyObject_GC_New PyObject_New
  313. #define PyObject_GC_NewVar PyObject_NewVar
  314. #define PyObject_GC_Del     PyObject_Del
  315. #define _PyObject_GC_TRACK(op)
  316. #define _PyObject_GC_UNTRACK(op)
  317. #define PyObject_GC_Track(op)
  318. #define PyObject_GC_UnTrack(op)
  319.  
  320. #endif
  321.  
  322. /* This is here for the sake of backwards compatibility.  Extensions that
  323.  * use the old GC API will still compile but the objects will not be
  324.  * tracked by the GC. */
  325. #define PyGC_HEAD_SIZE 0
  326. #define PyObject_GC_Init(op)
  327. #define PyObject_GC_Fini(op)
  328. #define PyObject_AS_GC(op) (op)
  329. #define PyObject_FROM_GC(op) (op)
  330.  
  331.  
  332. /* Test if a type supports weak references */
  333. #define PyType_SUPPORTS_WEAKREFS(t) \
  334.         (PyType_HasFeature((t), Py_TPFLAGS_HAVE_WEAKREFS) \
  335.          && ((t)->tp_weaklistoffset > 0))
  336.  
  337. #define PyObject_GET_WEAKREFS_LISTPTR(o) \
  338.     ((PyObject **) (((char *) (o)) + (o)->ob_type->tp_weaklistoffset))
  339.  
  340. #ifdef __cplusplus
  341. }
  342. #endif
  343. #endif /* !Py_OBJIMPL_H */
  344.