Example #1
0
        public void running_a_scan_when_no_pointers_are_referenced_resets_the_arena()
        {
            var bump    = (Allocator.ArenaSize / 4) + 1; // three fit in each arena, with some spare
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Allocator(512, Mega.Bytes(1), mem);

            subject.Alloc(bump);
            var ar1 = subject.CurrentArena();

            subject.Alloc(bump); subject.Alloc(bump); // fill up first arena

            var x2  = subject.Alloc(bump).Value;
            var ar2 = subject.CurrentArena();

            Assert.That(ar1, Is.Not.EqualTo(ar2), "Failed to trigger a new arena");

            // Check that both arenas are non-empty:
            Assert.That(subject.GetArenaOccupation(ar1).Value, Is.Not.Zero);
            Assert.That(subject.GetArenaOccupation(ar2).Value, Is.Not.Zero);

            // Run the scan (only including the second arena)
            var list = new Vector <long>(subject, mem);

            list.Push(x2);
            subject.ScanAndSweep(list);

            // Check nothing has been cleared
            Assert.That(subject.GetArenaOccupation(ar1).Value, Is.Zero, "Unreferenced arena was not reset");
            Assert.That(subject.GetArenaOccupation(ar2).Value, Is.Not.Zero);
        }
Example #2
0
        public void running_a_scan_when_any_pointers_are_referenced_keeps_the_arena()
        {
            // scan takes a list of known references, and assumes anything
            // that's not in the list in not referenced. Any arena with nothing
            // referenced is reset.

            var mem     = new MemorySimulator(Mega.Bytes(1));
            var bump    = (Allocator.ArenaSize / 4) + 1; // three fit in each arena, with some spare
            var subject = new Allocator(512, Mega.Bytes(1), mem);

            var x1  = subject.Alloc(bump).Value;
            var ar1 = subject.CurrentArena();

            subject.Alloc(bump); subject.Alloc(bump); // fill up first arena

            var x2  = subject.Alloc(bump).Value;
            var ar2 = subject.CurrentArena();

            Assert.That(ar1, Is.Not.EqualTo(ar2), "Failed to trigger a new arena");

            // Check that both arenas are non-empty:
            Assert.That(subject.GetArenaOccupation(ar1).Value, Is.Not.Zero);
            Assert.That(subject.GetArenaOccupation(ar2).Value, Is.Not.Zero);

            // Run the scan (note we don't need all pointers, just one from each arena)
            var list = new Vector <long>(subject, mem);

            list.Push(x1);
            list.Push(x2);
            subject.ScanAndSweep(list);

            // Check nothing has been cleared
            Assert.That(subject.GetArenaOccupation(ar1).Value, Is.Not.Zero);
            Assert.That(subject.GetArenaOccupation(ar2).Value, Is.Not.Zero);
        }
Example #3
0
        public void allocating_enough_memory_changes_arena()
        {
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Allocator(100, Mega.Bytes(1), mem);

            int  first  = subject.CurrentArena();
            long ptr1   = subject.Alloc(Allocator.ArenaSize).Value;
            long ptr2   = subject.Alloc(Kilo.Bytes(1)).Value;
            int  second = subject.CurrentArena();

            Assert.That(ptr1, Is.Not.EqualTo(ptr2));
            Assert.That(first, Is.Not.EqualTo(second));
        }
Example #4
0
        public void can_directly_deallocate_a_pointer()
        {
            var mem     = new MemorySimulator(Mega.Bytes(1));
            var subject = new Allocator(100, Mega.Bytes(10), mem);

            long ptr = subject.Alloc(byteCount: 256).Value;

            subject.Deref(ptr);

            var ar   = subject.CurrentArena();
            var refs = subject.ArenaRefCount(ar);

            Assert.That(refs.Value, Is.Zero);
        }
Example #5
0
        [Test] // a stress test of sorts
        public void can_handle_large_allocation_spaces()
        {
            var mem     = new OffsetMemorySimulator(Mega.Bytes(1), Giga.Bytes(1)); // only need enough room for arena tables
            var subject = new Allocator(Giga.Bytes(1), Giga.Bytes(2), mem);        // big for an embedded system. 3GB total.

            var result = subject.Alloc(Allocator.ArenaSize / 2);

            Assert.That(result.Value - Giga.Bytes(1), Is.EqualTo(131072)); // allocated at bottom of given space, excluding arena tables

            for (int i = 0; i < 1000; i++)
            {
                result = subject.Alloc(Allocator.ArenaSize - 1);
            }

            Assert.That(subject.CurrentArena(), Is.GreaterThanOrEqualTo(1000));
            Assert.That(result.Value, Is.GreaterThan(Giga.Bytes(1)));
        }