]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/csharp/src/Apache.Arrow/ArrowBuffer.cs
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / csharp / src / Apache.Arrow / ArrowBuffer.cs
CommitLineData
1d09f67e
TL
1// Licensed to the Apache Software Foundation (ASF) under one or more
2// contributor license agreements. See the NOTICE file distributed with
3// this work for additional information regarding copyright ownership.
4// The ASF licenses this file to You under the Apache License, Version 2.0
5// (the "License"); you may not use this file except in compliance with
6// the License. You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16using System;
17using System.Buffers;
18using System.Runtime.CompilerServices;
19using Apache.Arrow.Memory;
20
21namespace Apache.Arrow
22{
23 public readonly partial struct ArrowBuffer : IEquatable<ArrowBuffer>, IDisposable
24 {
25 private readonly IMemoryOwner<byte> _memoryOwner;
26 private readonly ReadOnlyMemory<byte> _memory;
27
28 public static ArrowBuffer Empty => new ArrowBuffer(Memory<byte>.Empty);
29
30 public ArrowBuffer(ReadOnlyMemory<byte> data)
31 {
32 _memoryOwner = null;
33 _memory = data;
34 }
35
36 internal ArrowBuffer(IMemoryOwner<byte> memoryOwner)
37 {
38 // When wrapping an IMemoryOwner, don't cache the Memory<byte>
39 // since the owner may be disposed, and the cached Memory would
40 // be invalid.
41
42 _memoryOwner = memoryOwner;
43 _memory = Memory<byte>.Empty;
44 }
45
46 public ReadOnlyMemory<byte> Memory =>
47 _memoryOwner != null ? _memoryOwner.Memory : _memory;
48
49 public bool IsEmpty => Memory.IsEmpty;
50
51 public int Length => Memory.Length;
52
53 public ReadOnlySpan<byte> Span
54 {
55 [MethodImpl(MethodImplOptions.AggressiveInlining)]
56 get => Memory.Span;
57 }
58
59 public ArrowBuffer Clone(MemoryAllocator allocator = default)
60 {
61 return new Builder<byte>(Span.Length)
62 .Append(Span)
63 .Build(allocator);
64 }
65
66 public bool Equals(ArrowBuffer other)
67 {
68 return Span.SequenceEqual(other.Span);
69 }
70
71 public void Dispose()
72 {
73 _memoryOwner?.Dispose();
74 }
75 }
76}