memoryobject.h 2.7 KB
Newer Older
1
/* Memory view object. In Python this is available as "memoryview". */
2 3 4 5 6 7 8

#ifndef Py_MEMORYOBJECT_H
#define Py_MEMORYOBJECT_H
#ifdef __cplusplus
extern "C" {
#endif

9 10 11
#ifndef Py_LIMITED_API
PyAPI_DATA(PyTypeObject) _PyManagedBuffer_Type;
#endif
12 13
PyAPI_DATA(PyTypeObject) PyMemoryView_Type;

14 15
#define PyMemoryView_Check(op) (Py_TYPE(op) == &PyMemoryView_Type)

16
#ifndef Py_LIMITED_API
17
/* Get a pointer to the memoryview's private copy of the exporter's buffer. */
18
#define PyMemoryView_GET_BUFFER(op) (&((PyMemoryViewObject *)(op))->view)
19
/* Get a pointer to the exporting object (this may be NULL!). */
20
#define PyMemoryView_GET_BASE(op) (((PyMemoryViewObject *)(op))->view.obj)
21
#endif
22 23

PyAPI_FUNC(PyObject *) PyMemoryView_FromObject(PyObject *base);
24
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
25 26
PyAPI_FUNC(PyObject *) PyMemoryView_FromMemory(char *mem, Py_ssize_t size,
                                               int flags);
27
#endif
28
#ifndef Py_LIMITED_API
29
PyAPI_FUNC(PyObject *) PyMemoryView_FromBuffer(Py_buffer *info);
30
#endif
31 32 33
PyAPI_FUNC(PyObject *) PyMemoryView_GetContiguous(PyObject *base,
                                                  int buffertype,
                                                  char order);
34

35

36 37
/* The structs are declared here so that macros can work, but they shouldn't
   be considered public. Don't access their fields directly, use the macros
38
   and functions instead! */
39
#ifndef Py_LIMITED_API
40 41
#define _Py_MANAGED_BUFFER_RELEASED    0x001  /* access to exporter blocked */
#define _Py_MANAGED_BUFFER_FREE_FORMAT 0x002  /* free format */
42
typedef struct {
43
    PyObject_HEAD
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    int flags;          /* state flags */
    Py_ssize_t exports; /* number of direct memoryview exports */
    Py_buffer master; /* snapshot buffer obtained from the original exporter */
} _PyManagedBufferObject;


/* memoryview state flags */
#define _Py_MEMORYVIEW_RELEASED    0x001  /* access to master buffer blocked */
#define _Py_MEMORYVIEW_C           0x002  /* C-contiguous layout */
#define _Py_MEMORYVIEW_FORTRAN     0x004  /* Fortran contiguous layout */
#define _Py_MEMORYVIEW_SCALAR      0x008  /* scalar: ndim = 0 */
#define _Py_MEMORYVIEW_PIL         0x010  /* PIL-style layout */

typedef struct {
    PyObject_VAR_HEAD
    _PyManagedBufferObject *mbuf; /* managed buffer */
    Py_hash_t hash;               /* hash value for read-only views */
    int flags;                    /* state flags */
    Py_ssize_t exports;           /* number of buffer re-exports */
    Py_buffer view;               /* private copy of the exporter's view */
64
    PyObject *weakreflist;
65
    Py_ssize_t ob_array[1];       /* shape, strides, suboffsets */
66
} PyMemoryViewObject;
67
#endif
68

69 70 71 72
#ifdef __cplusplus
}
#endif
#endif /* !Py_MEMORYOBJECT_H */