blob: 306028f4b225d8bbd0f0d443969b696cce991fed [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
Dong-hee Nad905df72020-02-14 02:37:17 +090014#define PyMemoryView_Check(op) Py_IS_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);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +020024#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
Stefan Krah9a2d99e2012-02-25 12:24:21 +010025PyAPI_FUNC(PyObject *) PyMemoryView_FromMemory(char *mem, Py_ssize_t size,
26 int flags);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +020027#endif
Martin v. Löwisc83bc3c2011-01-06 19:15:47 +000028#ifndef Py_LIMITED_API
Antoine Pitrouee58fa42008-08-19 18:22:14 +000029PyAPI_FUNC(PyObject *) PyMemoryView_FromBuffer(Py_buffer *info);
Martin v. Löwisc83bc3c2011-01-06 19:15:47 +000030#endif
Stefan Krah9a2d99e2012-02-25 12:24:21 +010031PyAPI_FUNC(PyObject *) PyMemoryView_GetContiguous(PyObject *base,
32 int buffertype,
33 char order);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +000034
Antoine Pitrou35b7e832009-01-03 19:20:36 +000035
Stefan Krah9a2d99e2012-02-25 12:24:21 +010036/* The structs are declared here so that macros can work, but they shouldn't
37 be considered public. Don't access their fields directly, use the macros
Antoine Pitrouee58fa42008-08-19 18:22:14 +000038 and functions instead! */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000039#ifndef Py_LIMITED_API
Stefan Krah9a2d99e2012-02-25 12:24:21 +010040#define _Py_MANAGED_BUFFER_RELEASED 0x001 /* access to exporter blocked */
41#define _Py_MANAGED_BUFFER_FREE_FORMAT 0x002 /* free format */
Antoine Pitrouee58fa42008-08-19 18:22:14 +000042typedef struct {
Antoine Pitrou35b7e832009-01-03 19:20:36 +000043 PyObject_HEAD
Stefan Krah9a2d99e2012-02-25 12:24:21 +010044 int flags; /* state flags */
45 Py_ssize_t exports; /* number of direct memoryview exports */
46 Py_buffer master; /* snapshot buffer obtained from the original exporter */
47} _PyManagedBufferObject;
48
49
Stefan Krah9a2d99e2012-02-25 12:24:21 +010050/* memoryview state flags */
51#define _Py_MEMORYVIEW_RELEASED 0x001 /* access to master buffer blocked */
52#define _Py_MEMORYVIEW_C 0x002 /* C-contiguous layout */
53#define _Py_MEMORYVIEW_FORTRAN 0x004 /* Fortran contiguous layout */
54#define _Py_MEMORYVIEW_SCALAR 0x008 /* scalar: ndim = 0 */
55#define _Py_MEMORYVIEW_PIL 0x010 /* PIL-style layout */
56
57typedef struct {
58 PyObject_VAR_HEAD
59 _PyManagedBufferObject *mbuf; /* managed buffer */
60 Py_hash_t hash; /* hash value for read-only views */
61 int flags; /* state flags */
62 Py_ssize_t exports; /* number of buffer re-exports */
63 Py_buffer view; /* private copy of the exporter's view */
Richard Oudkerk3e0a1eb2012-05-28 21:35:09 +010064 PyObject *weakreflist;
Stefan Krah9a2d99e2012-02-25 12:24:21 +010065 Py_ssize_t ob_array[1]; /* shape, strides, suboffsets */
Antoine Pitrouee58fa42008-08-19 18:22:14 +000066} PyMemoryViewObject;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000067#endif
Antoine Pitrouee58fa42008-08-19 18:22:14 +000068
Travis E. Oliphantb99f7622007-08-18 11:21:56 +000069#ifdef __cplusplus
70}
71#endif
72#endif /* !Py_MEMORYOBJECT_H */