Ejemplo n.º 1
0
            internal long AllocateAligned(long bytes, long alignment)
            {
                if (alignment <= 0)
                {
                    throw new System.ArgumentException("Invalid alignment: " + alignment + ". Alignment must be positive.");
                }
                long grabSize = Math.Min(GrabSize, ExpectedMaxMemory);

                if (bytes > GrabSize)
                {
                    // This is a huge allocation. Put it in its own grab and keep any existing grab at the head.
                    grabSize = bytes;
                    Grab nextGrab       = Head == null ? null : Head.next;
                    Grab allocationGrab = new Grab(nextGrab, grabSize, MemoryTracker);
                    if (!allocationGrab.CanAllocate(bytes, alignment))
                    {
                        allocationGrab.Free();
                        grabSize       = bytes + alignment;
                        allocationGrab = new Grab(nextGrab, grabSize, MemoryTracker);
                    }
                    long allocation = allocationGrab.Allocate(bytes, alignment);
                    Head = Head == null ? allocationGrab : Head.setNext(allocationGrab);
                    ExpectedMaxMemory -= bytes;
                    return(allocation);
                }

                if (Head == null || !Head.canAllocate(bytes, alignment))
                {
                    if (grabSize < bytes)
                    {
                        grabSize = bytes;
                        Grab grab = new Grab(Head, grabSize, MemoryTracker);
                        if (grab.CanAllocate(bytes, alignment))
                        {
                            ExpectedMaxMemory -= grabSize;
                            Head = grab;
                            return(Head.allocate(bytes, alignment));
                        }
                        grab.Free();
                        grabSize = bytes + alignment;
                    }
                    Head = new Grab(Head, grabSize, MemoryTracker);
                    ExpectedMaxMemory -= grabSize;
                }
                return(Head.allocate(bytes, alignment));
            }