MCPcopy Create free account
hub / github.com/apache/arrow / ImportBuffer

Function ImportBuffer

cpp/src/arrow/c/dlpack.cc:445–472  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

443}
444
445Result<std::shared_ptr<Buffer>> ImportBuffer(CppDLTensor&& dl) {
446 ARROW_ASSIGN_OR_RAISE(const int64_t nbytes, dl.ComputeNumBytes());
447
448 // DLPack mandates a null data pointer when the tensor holds no element, so there is
449 // neither anything to share nor to copy.
450 uint8_t* data =
451 (nbytes == 0) ? nullptr : dl.data_as<uint8_t>() + dl.tensor().byte_offset;
452
453 std::shared_ptr<Buffer> buffer = nullptr;
454 if (nbytes == 0) {
455 // DLPack data pointer may be null on empty tensors
456 buffer = std::make_shared<Buffer>(data, nbytes);
457 } else if (const auto byte_offset = dl.tensor().byte_offset; dl.is_readonly()) {
458 auto get_data = [byte_offset](auto& d) {
459 return d.template data_as<const uint8_t>() + byte_offset;
460 };
461 buffer = Buffer::TakeOwnership(std::move(dl), nbytes, get_data);
462 ARROW_DCHECK(!buffer->is_mutable());
463 } else {
464 auto get_data = [byte_offset](auto& d) {
465 return d.template data_as<uint8_t>() + byte_offset;
466 };
467 buffer = Buffer::TakeOwnership(std::move(dl), nbytes, get_data);
468 ARROW_DCHECK(buffer->is_mutable());
469 }
470
471 return buffer;
472}
473
474} // namespace
475

Callers 6

ImportBinaryViewMethod · 0.85
ImportBitsBufferMethod · 0.85
ImportFixedSizeBufferMethod · 0.85
ImportOffsetsBufferMethod · 0.85
ImportSizesBufferMethod · 0.85

Calls 2

TakeOwnershipFunction · 0.85
is_readonlyMethod · 0.80

Tested by

no test coverage detected