| // |
| // SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| // SPDX-License-Identifier: Apache-2.0 |
| // |
| %define %mutable_buffer(TYPEMAP, SIZE) |
| %typemap(in) (TYPEMAP, SIZE) { |
| int res; void *buf = 0; size_t size = 0; |
| Py_buffer view; |
| res = PyObject_GetBuffer($input, &view, PyBUF_WRITABLE); |
| buf = view.buf; |
| size = view.len; |
| PyBuffer_Release(&view); |
| if (res < 0) { |
| PyErr_Clear(); |
| %argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum); |
| } |
| $1 = ($1_ltype) buf; |
| $2 = ($2_ltype) size; |
| } |
| |
| %typemap(typecheck) (TYPEMAP, SIZE) { |
| $1 = PyObject_CheckBuffer($input) || PyTuple_Check($input) ? 1 : 0; |
| } |
| %enddef |
| |
| %define %clear_mutable_buffer(TYPEMAP, SIZE) |
| %typemap(in) (TYPEMAP, SIZE); |
| %typemap(typecheck) (TYPEMAP, SIZE); |
| %enddef |
| |
| |
| %define %driver_buffer_out |
| %typemap(out) (char*) { |
| auto size = arg1->size(); |
| int readOnly = 0; |
| $result = PyMemoryView_FromMemory($1, size, readOnly); |
| } |
| %enddef |
| |
| %define %clear_driver_buffer_out |
| %typemap(out) (char*); |
| %enddef |