| 70 | } |
| 71 | |
| 72 | void Buffer::toMATLAB(libmexclass::proxy::method::Context& context) { |
| 73 | namespace mda = ::matlab::data; |
| 74 | |
| 75 | // If buffer->is_cpu() returns false, invoking buffer->data() may cause a crash. |
| 76 | // Avoid this potential crash by first invoking ViewOrCopy(buffer, |
| 77 | // memory_manager_device). This function tries to create a no-copy view of the buffer on |
| 78 | // the given memory manager device. If not possible, then ViewOrCopy copies the buffer's |
| 79 | // contents. |
| 80 | MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT( |
| 81 | auto cpu_buffer, |
| 82 | arrow::Buffer::ViewOrCopy(buffer, arrow::default_cpu_memory_manager()), context, |
| 83 | error::BUFFER_VIEW_OR_COPY_FAILED); |
| 84 | |
| 85 | const auto* data_begin = cpu_buffer->data(); |
| 86 | const auto num_bytes = cpu_buffer->size(); |
| 87 | // data_begin is a uint8_t*, so num_bytes is equal to the number of elements |
| 88 | const auto* data_end = data_begin + num_bytes; |
| 89 | |
| 90 | mda::ArrayFactory factory; |
| 91 | context.outputs[0] = factory.createArray<uint8_t>({static_cast<size_t>(num_bytes), 1}, |
| 92 | data_begin, data_end); |
| 93 | } |
| 94 | |
| 95 | } // namespace arrow::matlab::buffer::proxy |