Kshitij Sisodia | f9efe0d | 2022-09-30 16:42:50 +0100 | [diff] [blame] | 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | %define %mutable_buffer(TYPEMAP, SIZE) |
| 6 | %typemap(in) (TYPEMAP, SIZE) { |
| 7 | int res; void *buf = 0; size_t size = 0; |
| 8 | Py_buffer view; |
| 9 | res = PyObject_GetBuffer($input, &view, PyBUF_WRITABLE); |
| 10 | buf = view.buf; |
| 11 | size = view.len; |
| 12 | PyBuffer_Release(&view); |
| 13 | if (res < 0) { |
| 14 | PyErr_Clear(); |
| 15 | %argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum); |
| 16 | } |
| 17 | $1 = ($1_ltype) buf; |
| 18 | $2 = ($2_ltype) size; |
| 19 | } |
| 20 | |
| 21 | %typemap(typecheck) (TYPEMAP, SIZE) { |
| 22 | $1 = PyObject_CheckBuffer($input) || PyTuple_Check($input) ? 1 : 0; |
| 23 | } |
| 24 | %enddef |
| 25 | |
| 26 | %define %clear_mutable_buffer(TYPEMAP, SIZE) |
| 27 | %typemap(in) (TYPEMAP, SIZE); |
| 28 | %typemap(typecheck) (TYPEMAP, SIZE); |
| 29 | %enddef |
| 30 | |
| 31 | |
| 32 | %define %driver_buffer_out |
| 33 | %typemap(out) (char*) { |
| 34 | auto size = arg1->size(); |
| 35 | int readOnly = 0; |
| 36 | $result = PyMemoryView_FromMemory($1, size, readOnly); |
| 37 | } |
| 38 | %enddef |
| 39 | |
| 40 | %define %clear_driver_buffer_out |
| 41 | %typemap(out) (char*); |
| 42 | %enddef |