<commit_before>﻿using BepuUtilities.Collections;
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

#if !DEBUG
[module: SkipLocalsInit]
#endif

namespace BepuUtilities.Memory
{
    /// <summary>
    /// Span over an unmanaged memory region.
    /// </summary>
    /// <typeparam name="T">Type of the memory exposed by the span.</typeparam>
    [StructLayout(LayoutKind.Sequential)]
    public unsafe struct Buffer<T> where T : unmanaged
    {
        /// <summary>
        /// Pointer to the beginning of the memory backing this buffer.
        /// </summary>
        public T* Memory;
        internal int length;
        //We're primarily interested in x64, so memory + length is 12 bytes. This struct would/should get padded to 16 bytes for alignment reasons anyway, 
        //so making use of the last 4 bytes to speed up the case where the raw buffer is taken from a pool (which is basically always) is a good option.

        /// <summary>
        /// Implementation specific identifier of the raw buffer set by its source. If taken from a BufferPool, Id includes the index in the power pool from which it was taken.
        /// </summary>
        public int Id;

        /// <summary>
        /// Creates a new buffer.
        /// </summary>
        /// <param name="memory">Memory to back the buffer.</param>
        /// <param name="length">Length of the buffer in terms of the specified type.</param>
        /// <param name="id">Id of the buffer.</param>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public Buffer(void* memory, int length, int id = -1)
        {
            Memory = (T*)memory;
            this.length = length;
            Id = id;
        }

        /// <summary><commit_msg>Added a Buffer helperconstructor to match Quick* collections.<commit_after>﻿using BepuUtilities.Collections;
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

#if !DEBUG
[module: SkipLocalsInit]
#endif

namespace BepuUtilities.Memory
{
    /// <summary>
    /// Span over an unmanaged memory region.
    /// </summary>
    /// <typeparam name="T">Type of the memory exposed by the span.</typeparam>
    [StructLayout(LayoutKind.Sequential)]
    public unsafe struct Buffer<T> where T : unmanaged
    {
        /// <summary>
        /// Pointer to the beginning of the memory backing this buffer.
        /// </summary>
        public T* Memory;
        internal int length;
        //We're primarily interested in x64, so memory + length is 12 bytes. This struct would/should get padded to 16 bytes for alignment reasons anyway, 
        //so making use of the last 4 bytes to speed up the case where the raw buffer is taken from a pool (which is basically always) is a good option.

        /// <summary>
        /// Implementation specific identifier of the raw buffer set by its source. If taken from a BufferPool, Id includes the index in the power pool from which it was taken.
        /// </summary>
        public int Id;

        /// <summary>
        /// Creates a new buffer.
        /// </summary>
        /// <param name="memory">Memory to back the buffer.</param>
        /// <param name="length">Length of the buffer in terms of the specified type.</param>
        /// <param name="id">Id of the buffer.</param>
        public Buffer(void* memory, int length, int id = -1)
        {
            Memory = (T*)memory;
            this.length = length;
            Id = id;
        }

        /// <summary>
        /// Allocates a new buffer from a pool.
        /// </summary>
        /// <param name="length">Length of the buffer in terms of elements of type T</param>
        /// <param name="pool">Pool to allocate from.</param>
        public Buffer(int length, IUnmanagedMemoryPool pool)
        {
            pool.Take(length, out this);
        }

        /// <summary>
        /// Returns a buffer to a pool. This should only be used if the specified pool is the same as the one used to allocate the buffer.
        /// </summary>
        /// <param name="pool">Pool to return the buffer to.</param>
        public void Dispose(IUnmanagedMemoryPool pool)
        {
            pool.Return(ref this);
        }

        /// <summary>
{'fraction_non_alphanumeric': 0.10588507877664505, 'fraction_numerical': 0.0037071362372567192, 'mean_word_length': 2.151094890510949, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5083574', 'n_tokens_mistral': 1121, 'n_tokens_neox': 1099, 'n_words': 462}