Alloc() public static method

public static Alloc ( int size ) : void*
size int
return void*
Exemplo n.º 1
0
        public void ReadInvalidateTest()
        {
            BitStreamer bs  = new BitStreamer(false);
            IntPtr      ptr = Memory.Alloc(4);

            Assert.IsFalse(bs.ThrowsOnExceededBuffer);
            Assert.IsFalse(bs.IsValid);

            bs.ResetRead(ptr, 4);

            Assert.IsTrue(bs.IsValid);

            bs.ReadInt32();
            Assert.IsTrue(bs.IsValid);

            bs.ReadInt32();
            Assert.IsFalse(bs.IsValid);

            // Confirm offset hasn't increased.
            Assert.AreEqual(4, bs.ByteOffset);

            bs.ResetRead();
            Assert.IsTrue(bs.IsValid);

            Memory.Free(ptr);
        }
Exemplo n.º 2
0
        public void WriteInvalidateTest()
        {
            BitStreamer bs  = new BitStreamer(false);
            IntPtr      ptr = Memory.Alloc(8);

            Assert.IsFalse(bs.ThrowsOnExceededBuffer);
            Assert.IsFalse(bs.IsValid);

            bs.ResetWrite(ptr, 8);

            Assert.IsTrue(bs.IsValid);

            bs.WriteULong(123);
            Assert.IsTrue(bs.IsValid);

            bs.WriteInt32(321);
            Assert.IsFalse(bs.IsValid);

            // Confirm offset hasn't increased.
            Assert.AreEqual(8, bs.ByteOffset);

            bs.ResetWrite();
            Assert.IsTrue(bs.IsValid);

            Memory.Free(ptr);
        }
Exemplo n.º 3
0
        public void NonAlignBufferTest()
        {
            IntPtr ptr = Memory.Alloc(12);

            BitStreamer bs = new BitStreamer();

            bs.ResetWrite(ptr, 12);

            Assert.AreEqual(8, bs.ByteLength);

            bs.ResetRead();
            Assert.AreEqual(12, bs.ByteLength);

            bs.ResetWrite();
            Assert.AreEqual(8, bs.ByteLength);

            bs.WriteULong(123);

            // The write buffer should be rounded down to 8. So this must fail.
            Assert.Throws <InvalidOperationException>(() =>
            {
                bs.WriteByte(1);
            });

            Memory.Free(ptr);
        }
Exemplo n.º 4
0
        private unsafe NetPacket CreatePacket(int size)
        {
            NetPacket pack = new NetPacket();
            IntPtr    data = Memory.Alloc(size);

            pack.OnReceive(new ReadOnlySpan <byte>(data.ToPointer(), size));
            return(pack);
        }
Exemplo n.º 5
0
    internal static IntPtr AllocAndWrite(byte[] data)
    {
        IntPtr addr = Memory.Alloc((uint)data.Length);

        Memory.Write(addr, data);

        return(addr);
    }
Exemplo n.º 6
0
        private unsafe NetPacket CreatePacket(int size)
        {
            IntPtr    mem    = Memory.Alloc(size);
            var       span   = new ReadOnlySpan <byte>(mem.ToPointer(), size);
            NetPacket packet = new NetPacket();

            packet.OnReceive(span);

            return(packet);
        }
Exemplo n.º 7
0
        private byte *CreateArray()
        {
            var arr = (byte *)Memory.Alloc(8);

            for (int i = 0; i < 8; i++)
            {
                arr[i] = (byte)i;
            }

            return(arr);
        }
Exemplo n.º 8
0
        private void btnApply_Click(object sender, EventArgs e)
        {
            InitProcess();

            try
            {
                if (SelectedVersion.Version == 1)
                {
                    HotsOffsets_V1  CurrentVersion = SelectedVersion as HotsOffsets_V1;
                    FileVersionInfo ver            = Memory.CurrentProcess.MainModule.FileVersionInfo;

                    if (ver.FilePrivatePart != Offsets[cbVersions.SelectedIndex].Build)
                    {
                        MessageBox.Show("Wrong or incompatible build selected! Patch failed.");
                        return;
                    }

                    // Map patch
                    byte[] mapName = Encoding.ASCII.GetBytes(cbMapSelect.SelectedItem + "\0\0\0");

                    if (mapNamePtr == IntPtr.Zero)
                    {
                        mapNamePtr = Memory.Alloc(mapName.Length);
                    }

                    Memory.Write(mapNamePtr, mapName);

                    oldMapPtr1 = Memory.Read <IntPtr>(CurrentVersion.PatchMapOffset1);
                    oldMapPtr2 = Memory.Read <IntPtr>(CurrentVersion.PatchMapOffset2);

                    Memory.Write <IntPtr>(CurrentVersion.PatchMapOffset1, mapNamePtr);
                    Memory.Write <IntPtr>(CurrentVersion.PatchMapOffset2, mapNamePtr + mapName.Length - 3);
                }

                for (int i = 0; i < 10; ++i)
                {
                    Heroes[i] = (int)Enum.Parse(SelectedVersion.HeroIDEnum, (string)HeroComboBoxes[i].SelectedItem);
                }

                AlliedAI = (AIDifficulty)Enum.Parse(typeof(AIDifficulty), (string)cbAllyTeamAI.SelectedItem);
                EnemyAI  = (AIDifficulty)Enum.Parse(typeof(AIDifficulty), (string)cbEnemyTeamAI.SelectedItem);

                timer1.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
Exemplo n.º 9
0
        public void ResetWriteInvalid()
        {
            BitStreamer bs = new BitStreamer();

            IntPtr buff = Memory.Alloc(16);

            *(byte *)buff = 212;

            bs.ResetWrite(8);

            Assert.Throws <InvalidOperationException>(() =>
            {
                bs.ResetWrite(buff, 16);
            });
        }
Exemplo n.º 10
0
 /// <summary>
 /// Инициализирует новый экземпляр класса.
 /// </summary>
 /// <param name="itemSize">
 /// Размер одного элемента.
 /// </param>
 /// <param name="rowsCount">
 /// Количество строк.
 /// </param>
 /// <param name="columnsCount">
 /// Количество столбцов.
 /// </param>
 /// <exception cref="ArgumentOutOfRangeException">
 /// <para>В параметре <paramref name="itemSize"/> передано отрицательное или равное нулю значение.</para>
 /// <para>- или -</para>
 /// <para>В параметре <paramref name="rowsCount"/> передано отрицательное значение.</para>
 /// <para>- или -</para>
 /// <para>В параметре <paramref name="columnsCount"/> передано отрицательное значение.</para>
 /// </exception>
 /// <exception cref="OutOfMemoryException">
 /// Недостаточно памяти для выполнения запроса.
 /// </exception>
 public Matrix(int itemSize, int rowsCount, int columnsCount)
 {
     if (itemSize <= 0)
     {
         throw new ArgumentOutOfRangeException("itemSize", "Передано отрицательное или равное нулю значение.");
     }
     if (rowsCount < 0)
     {
         throw new ArgumentOutOfRangeException("rowsCount", "Передано отрицательное значение.");
     }
     if (columnsCount < 0)
     {
         throw new ArgumentOutOfRangeException("columnsCount", "Передано отрицательное значение.");
     }
     RowsCount    = rowsCount;
     ColumnsCount = columnsCount;
     Pointer      = Memory.Alloc(rowsCount * ((long)columnsCount) * itemSize);
 }
Exemplo n.º 11
0
        internal void Resize(int newSize)
        {
            if (newSize > Capacity)
            {
                var alignedSize = MathUtils.GetNextMultipleOf8(newSize);

                if (Data == null)
                {
                    Data = (byte *)Memory.Alloc(alignedSize);
                }
                else
                {
                    Data = (byte *)Memory.Realloc((IntPtr)Data, Capacity, alignedSize);
                }

                Capacity = alignedSize;
            }
        }
Exemplo n.º 12
0
        public void ResetWriteCopyBufferNull()
        {
            BitStreamer bs = new BitStreamer();

            IntPtr buff = Memory.Alloc(16);

            *(byte *)buff = 212;

            bs.ResetWrite(buff, 16, true);
            Assert.AreEqual(16, bs.ByteLength);
            Assert.AreEqual(16, bs.ByteOffset);

            bs.ResetRead();
            Assert.AreEqual(212, bs.ReadByte());
            Assert.IsTrue(bs.OwnsBuffer);

            Memory.Free(buff);
        }
Exemplo n.º 13
0
        private void WriteStringInternal(char *ptr, int charCount, Encoding encoding)
        {
            const int BUFFERSIZE = 256;

            int byteSize = encoding.GetByteCount(ptr, charCount);

            if (byteSize > ushort.MaxValue)
            {
                throw new ArgumentOutOfRangeException("value", "String is too large to be written.");
            }

            // Abort when the buffer doesn't have the required space.
            if (!EnsureWriteSize((byteSize + sizeof(ushort)) * 8))
            {
                return;
            }

            // Write the length of the string in bytes.
            WriteUnchecked((ushort)byteSize, 16);

            // Fast route, use stackalloc for small strings.
            if (byteSize <= BUFFERSIZE)
            {
                byte *buffer = stackalloc byte[byteSize];
                encoding.GetBytes(ptr, charCount, buffer, byteSize);
                WriteMemoryUnchecked(buffer, byteSize);
            }
            // Slow route, alloc mem for large strings.
            else
            {
                byte *buffer = (byte *)Memory.Alloc(byteSize);
                try
                {
                    encoding.GetBytes(ptr, charCount, buffer, byteSize);
                    WriteMemoryUnchecked(ptr, byteSize);
                }
                finally
                {
                    // Ensure we don't have a mem leak.
                    Memory.Free(buffer);
                }
            }
        }
Exemplo n.º 14
0
        public void OnReceiveResizeTest()
        {
            var pack = CreatePacket(8);

            Assert.AreEqual(8, pack.Capacity);
            Assert.AreEqual(8, pack.Size);

            var dataPtr = (ulong)pack.Data;

            // Must be larger than existing buffer.
            IntPtr ptr = Memory.Alloc(14);

            pack.OnReceive(new ReadOnlySpan <byte>(ptr.ToPointer(), 14));

            Assert.IsFalse(dataPtr == (ulong)pack.Data);
            Assert.AreEqual(16, pack.Capacity);
            Assert.AreEqual(14, pack.Size);

            pack.Dispose();
        }
Exemplo n.º 15
0
        public void NonAlignBufferSet()
        {
            IntPtr ptr = Memory.Alloc(12);

            BitStreamer bs = new BitStreamer();

            bs.ResetRead(ptr, 4);

            Assert.AreEqual(4, bs.ByteLength);
            bs.ReadFloat();

            // Should fail because we are exceeding the allowed size of 4.
            Assert.Throws <InvalidOperationException>(() =>
            {
                bs.ReadByte(1);
            });

            bs.ResetWrite();
            Assert.AreEqual(0, bs.ByteLength);
        }
Exemplo n.º 16
0
        public void OnReceiveTest()
        {
            var pack = CreatePacket(16);

            Assert.AreEqual(16, pack.Capacity);
            Assert.AreEqual(16, pack.Size);
            Assert.IsTrue(pack.IsValid);

            var dataPtr = (ulong)pack.Data;

            // Must be smaller than existing buffer.
            IntPtr ptr = Memory.Alloc(8);

            pack.OnReceive(new ReadOnlySpan <byte>(ptr.ToPointer(), 8));

            Assert.IsTrue(dataPtr == (ulong)pack.Data);
            Assert.AreEqual(16, pack.Capacity);
            Assert.AreEqual(8, pack.Size);

            pack.Dispose();
        }
Exemplo n.º 17
0
        /// <summary>
        /// Reads a string from the <see cref="NetPacket"/>.
        /// </summary>
        public string ReadString(Encoding encoding)
        {
            const int BUFFERSIZE = 256;
            ushort    byteSize   = ReadUShort();

            if (byteSize == 0)
            {
                return(string.Empty);
            }

            if (!EnsureReadSize(byteSize * 8))
            {
                return(string.Empty);
            }

            // Fast route, use stackalloc for small strings.
            if (byteSize <= BUFFERSIZE)
            {
                byte *buffer = stackalloc byte[byteSize];
                ReadMemoryUnchecked(buffer, byteSize);
                return(new string((sbyte *)buffer, 0, byteSize, encoding));
            }
            // Slow route, alloc mem for large strings.
            else
            {
                byte *buffer = (byte *)Memory.Alloc(byteSize);
                try
                {
                    ReadMemoryUnchecked(buffer, byteSize);
                    return(new string((sbyte *)buffer, 0, byteSize, encoding));
                }
                finally
                {
                    // Ensure we don't have a mem leak.
                    Memory.Free(buffer);
                }
            }
        }
Exemplo n.º 18
0
 public Model()
 {
     _forward  = (Node *)Memory.Alloc(sizeof(Node));
     _backward = (Node *)Memory.Alloc(sizeof(Node));
 }