Beispiel #1
0
        /// <summary>
        /// Gets memory from existing pointer.
        /// </summary>
        /// <param name="memPtr">Cross-platform memory pointer.</param>
        /// <returns>Memory.</returns>
        public IPlatformMemory Get(long memPtr)
        {
            int flags = PlatformMemoryUtils.GetFlags(memPtr);

            return(PlatformMemoryUtils.IsExternal(flags) ? GetExternalMemory(memPtr)
                : PlatformMemoryUtils.IsPooled(flags) ? Pool().Get(memPtr) : new PlatformUnpooledMemory(memPtr));
        }
        /// <summary>
        /// Allocate memory chunk, optionally pooling it.
        /// </summary>
        /// <param name="cap">Minimum capacity.</param>
        /// <returns>Memory chunk</returns>
        public PlatformMemory Allocate(int cap)
        {
            var memPtr = PlatformMemoryUtils.AllocatePooled(handle.ToInt64(), cap);

            // memPtr == 0 means that we failed to acquire thread-local memory chunk, so fallback to unpooled memory.
            return(memPtr != 0 ? Get(memPtr) : new PlatformUnpooledMemory(PlatformMemoryUtils.AllocateUnpooled(cap)));
        }
        /** <inheritdoc /> */
        protected override bool ReleaseHandle()
        {
            PlatformMemoryUtils.ReleasePool(handle.ToInt64());

            handle = new IntPtr(-1);

            return(true);
        }
        /** <inheritdoc /> */
        public byte[] ArrayCopy()
        {
            byte[] res = new byte[_mem.Length];

            fixed(byte *res0 = res)
            {
                PlatformMemoryUtils.CopyMemory(Data, res0, res.Length);
            }

            return(res);
        }
        /** <inheritdoc /> */

        public byte[] ReadByteArray(int cnt)
        {
            int curPos = EnsureReadCapacityAndShift(cnt);

            byte[] res = new byte[cnt];

            fixed(byte *res0 = res)
            {
                PlatformMemoryUtils.CopyMemory(Data + curPos, res0, cnt);
            }

            return(res);
        }
        /// <summary>
        /// Copy (write) some data from source and shift the stream forward.
        /// </summary>
        /// <param name="src">Source.</param>
        /// <param name="cnt">Bytes count.</param>
        private void CopyFromAndShift(byte *src, int cnt)
        {
            int curPos = EnsureWriteCapacityAndShift(cnt);

            PlatformMemoryUtils.CopyMemory(src, Data + curPos, cnt);
        }
        /// <summary>
        /// Copy (read) some data into destination and shift the stream forward.
        /// </summary>
        /// <param name="dest">Destination.</param>
        /// <param name="cnt">Bytes count.</param>
        private void CopyToAndShift(byte *dest, int cnt)
        {
            int curPos = EnsureReadCapacityAndShift(cnt);

            PlatformMemoryUtils.CopyMemory(Data + curPos, dest, cnt);
        }
 /// <summary>
 /// Release pooled memory chunk.
 /// </summary>
 /// <param name="memPtr">Memory pointer.</param>
 public static void Release(long memPtr)
 {
     PlatformMemoryUtils.ReleasePooled(memPtr);
 }
 /// <summary>
 /// Re-allocate existing pool memory chunk.
 /// </summary>
 /// <param name="memPtr">Memory pointer.</param>
 /// <param name="cap">Minimum capacity.</param>
 public static void Reallocate(long memPtr, int cap)
 {
     PlatformMemoryUtils.ReallocatePooled(memPtr, cap);
 }
 /// <summary>
 /// Constructor.
 /// </summary>
 public PlatformMemoryPool() : base(true)
 {
     handle = (IntPtr)PlatformMemoryUtils.AllocatePool();
 }