public void deallocating_everything_in_an_arena_resets_it() { var mem = new MemorySimulator(Mega.Bytes(1)); var subject = new Allocator(100, Mega.Bytes(1), mem); long ptr1 = subject.Alloc(512).Value; long ptr2 = subject.Alloc(512).Value; long ptr3 = subject.Alloc(512).Value; subject.Deref(ptr1); subject.Deref(ptr2); long ptr4 = subject.Alloc(512).Value; subject.Deref(ptr3); subject.Deref(ptr4); // should be reset now, and next alloc goes back to start long ptrFinal = subject.Alloc(512).Value; Assert.That(ptrFinal, Is.EqualTo(ptr1)); }
public void deallocating_an_old_allocation_does_nothing() { // Older items just hang around until the entire arena is abandoned var mem = new MemorySimulator(Mega.Bytes(1)); var subject = new Allocator(100, Mega.Bytes(10), mem); long ptr1 = subject.Alloc(byteCount: 256).Value; long ptr2 = subject.Alloc(byteCount: 256).Value; subject.Deref(ptr1); long ptr3 = subject.Alloc(byteCount: 512).Value; Assert.That(ptr3, Is.GreaterThan(ptr2)); }
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); }
public void can_add_and_remove_current_referenced_pointers() { // This increments and decrements ref counts? // Free is the equivalent of directly setting ref count to zero? Or is it just a synonym for Deref? // We can keep an overall refcount for the arena and ignore the individual references (except for the head, as an optimisation) // We don't protect from double-free var mem = new MemorySimulator(Mega.Bytes(1)); var subject = new Allocator(100, Mega.Bytes(1), mem); long ptr = subject.Alloc(byteCount: 256).Value; subject.Reference(ptr); subject.Reference(ptr); subject.Deref(ptr); Assert.Pass(); }