Ejemplo n.º 1
0
        unsafe public void ArenaAllocatorAlloc()
        {
            byte *buffer = arenaAllocator.Allocate <byte>(Bytes);

            for (int i = 0; i < Bytes; i++)
            {
                unchecked
                {
                    buffer[i] = (byte)i;
                }
            }
            arenaAllocator.Free(buffer);
        }
        public void ArenaAllocatorTest()
        {
            ArenaAllocator allocator = new ArenaAllocator(1000);

            Assert.AreSame(allocator, Allocator.GetAllocatorByID(allocator.ID));
            Assert.IsTrue(Allocator.IsCached(allocator));

            int *p = allocator.Allocate <int>(4);

            for (int i = 0; i < 4; i++)
            {
                p[i] = i + 1;
            }

            Assert.AreEqual(1, p[0]);
            Assert.AreEqual(2, p[1]);
            Assert.AreEqual(3, p[2]);
            Assert.AreEqual(4, p[3]);

            p = allocator.Reallocate(p, 6);

            Assert.AreEqual(1, p[0]);
            Assert.AreEqual(2, p[1]);
            Assert.AreEqual(3, p[2]);
            Assert.AreEqual(4, p[3]);
            Assert.AreEqual(0, p[4]);
            Assert.AreEqual(0, p[5]);

            allocator.Free(p);
            allocator.Dispose();
            Assert.IsFalse(Allocator.IsCached(allocator));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Allocates the memory for the given type in the shared memory region.
        /// </summary>
        /// <typeparam name="T">CodegenProxyType of the allocated object.</typeparam>
        /// <param name="allocator">Allocator instance.</param>
        /// <returns></returns>
        public static T Allocate <T>(this ArenaAllocator allocator)
            where T : ICodegenProxy, new()
        {
            AllocationEntry allocationEntry = allocator.Allocate(default(T).CodegenTypeSize());

            T codegenProxy = new T {
                Buffer = allocationEntry.Buffer + (int)default(AllocationEntry).CodegenTypeSize()
            };

            return(codegenProxy);
        }