CurtHagenlocher commented on code in PR #304:
URL: https://github.com/apache/arrow-dotnet/pull/304#discussion_r3019269691


##########
src/Apache.Arrow/Memory/NativeBuffer.cs:
##########
@@ -0,0 +1,208 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+using System;
+using System.Buffers;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using System.Threading;
+
+namespace Apache.Arrow.Memory
+{
+    public sealed class NativeBuffer<TItem, TTracker> : IDisposable
+        where TItem : struct
+        where TTracker : struct, INativeAllocationTracker
+    {
+        private const int Alignment = MemoryAllocator.DefaultAlignment;
+
+        private MemoryManager _owner;
+        private int _byteLength;
+
+        /// <summary>Number of <typeparamref name="TItem"/> elements that fit 
in the buffer.</summary>
+        public int Length { get; private set; }
+
+        /// <summary>Creates a native buffer sized for <paramref 
name="elementCount"/> elements of <typeparamref name="TItem"/>.</summary>
+        /// <param name="elementCount">Number of elements.</param>
+        /// <param name="zeroFill">If true, the buffer is zeroed. Set to false 
if the caller will initialize the entire span itself.</param>
+        /// <param name="tracker">Allows native allocation sizes to be tracked 
and affect the GC.</param>
+        public NativeBuffer(int elementCount, bool zeroFill = true, TTracker 
tracker = default)
+        {
+            int elementSize = Unsafe.SizeOf<TItem>();
+            _byteLength = checked(elementCount * elementSize);
+            Length = elementCount;
+            _owner = new MemoryManager(_byteLength, tracker);
+
+            if (zeroFill)
+            {
+                Span.Clear();
+            }
+        }
+
+        /// <summary>Gets a <see cref="Span{T}"/> over the native 
buffer.</summary>
+        public Span<TItem> Span
+        {
+            get
+            {
+                var byteSpan = _owner!.Memory.Span;
+                return MemoryMarshal.Cast<byte, TItem>(byteSpan);
+            }
+        }
+
+        /// <summary>Gets a <see cref="Span{T}"/> over the raw bytes of the 
native buffer.</summary>
+        public Span<byte> ByteSpan => _owner!.Memory.Span.Slice(0, 
_byteLength);

Review Comment:
   I prefer to avoid the performance hit of the check.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to