blob: 4ac6f6525c91d1febde9b23b3c8d8fbadc4b5044 [file] [log] [blame]
Antoine Pitrouee58fa42008-08-19 18:22:14 +00001/* Memory view object. In Python this is available as "memoryview". */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002
3#ifndef Py_MEMORYOBJECT_H
4#define Py_MEMORYOBJECT_H
5#ifdef __cplusplus
6extern "C" {
7#endif
8
Stefan Krah9a2d99e2012-02-25 12:24:21 +01009#ifndef Py_LIMITED_API
10PyAPI_DATA(PyTypeObject) _PyManagedBuffer_Type;
11#endif
Travis E. Oliphantb99f7622007-08-18 11:21:56 +000012PyAPI_DATA(PyTypeObject) PyMemoryView_Type;
13
Antoine Pitrouee58fa42008-08-19 18:22:14 +000014#define PyMemoryView_Check(op) (Py_TYPE(op) == &PyMemoryView_Type)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +000015
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000016#ifndef Py_LIMITED_API
Stefan Krah9a2d99e2012-02-25 12:24:21 +010017/* Get a pointer to the memoryview's private copy of the exporter's buffer. */
Antoine Pitrouee58fa42008-08-19 18:22:14 +000018#define PyMemoryView_GET_BUFFER(op) (&((PyMemoryViewObject *)(op))->view)
Stefan Krah9a2d99e2012-02-25 12:24:21 +010019/* Get a pointer to the exporting object (this may be NULL!). */
Antoine Pitrouee58fa42008-08-19 18:22:14 +000020#define PyMemoryView_GET_BASE(op) (((PyMemoryViewObject *)(op))->view.obj)
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000021#endif
Antoine Pitrouee58fa42008-08-19 18:22:14 +000022
Travis E. Oliphantb99f7622007-08-18 11:21:56 +000023PyAPI_FUNC(PyObject *) PyMemoryView_FromObject(PyObject *base);
Stefan Krah9a2d99e2012-02-25 12:24:21 +010024PyAPI_FUNC(PyObject *) PyMemoryView_FromMemory(char *mem, Py_ssize_t size,
25 int flags);
Martin v. Löwisc83bc3c2011-01-06 19:15:47 +000026#ifndef Py_LIMITED_API
Antoine Pitrouee58fa42008-08-19 18:22:14 +000027PyAPI_FUNC(PyObject *) PyMemoryView_FromBuffer(Py_buffer *info);
Martin v. Löwisc83bc3c2011-01-06 19:15:47 +000028#endif
Stefan Krah9a2d99e2012-02-25 12:24:21 +010029PyAPI_FUNC(PyObject *) PyMemoryView_GetContiguous(PyObject *base,
30 int buffertype,
31 char order);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +000032
Antoine Pitrou35b7e832009-01-03 19:20:36 +000033
Stefan Krah9a2d99e2012-02-25 12:24:21 +010034/* The structs are declared here so that macros can work, but they shouldn't
35 be considered public. Don't access their fields directly, use the macros
Antoine Pitrouee58fa42008-08-19 18:22:14 +000036 and functions instead! */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000037#ifndef Py_LIMITED_API
Stefan Krah9a2d99e2012-02-25 12:24:21 +010038#define _Py_MANAGED_BUFFER_RELEASED 0x001 /* access to exporter blocked */
39#define _Py_MANAGED_BUFFER_FREE_FORMAT 0x002 /* free format */
Antoine Pitrouee58fa42008-08-19 18:22:14 +000040typedef struct {
Antoine Pitrou35b7e832009-01-03 19:20:36 +000041 PyObject_HEAD
Stefan Krah9a2d99e2012-02-25 12:24:21 +010042 int flags; /* state flags */
43 Py_ssize_t exports; /* number of direct memoryview exports */
44 Py_buffer master; /* snapshot buffer obtained from the original exporter */
45} _PyManagedBufferObject;
46
47
48/* static storage used for casting between formats */
49#define _Py_MEMORYVIEW_MAX_FORMAT 3 /* must be >= 3 */
50
51/* memoryview state flags */
52#define _Py_MEMORYVIEW_RELEASED 0x001 /* access to master buffer blocked */
53#define _Py_MEMORYVIEW_C 0x002 /* C-contiguous layout */
54#define _Py_MEMORYVIEW_FORTRAN 0x004 /* Fortran contiguous layout */
55#define _Py_MEMORYVIEW_SCALAR 0x008 /* scalar: ndim = 0 */
56#define _Py_MEMORYVIEW_PIL 0x010 /* PIL-style layout */
57
58typedef struct {
59 PyObject_VAR_HEAD
60 _PyManagedBufferObject *mbuf; /* managed buffer */
61 Py_hash_t hash; /* hash value for read-only views */
62 int flags; /* state flags */
63 Py_ssize_t exports; /* number of buffer re-exports */
64 Py_buffer view; /* private copy of the exporter's view */
65 char format[_Py_MEMORYVIEW_MAX_FORMAT]; /* used for casting */
66 Py_ssize_t ob_array[1]; /* shape, strides, suboffsets */
Antoine Pitrouee58fa42008-08-19 18:22:14 +000067} PyMemoryViewObject;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000068#endif
Antoine Pitrouee58fa42008-08-19 18:22:14 +000069
Travis E. Oliphantb99f7622007-08-18 11:21:56 +000070#ifdef __cplusplus
71}
72#endif
73#endif /* !Py_MEMORYOBJECT_H */