Example #1
0
        ushort AllocHandle(ushort growSize)
        {
            // Do we have any free handles?
            if (_freeHandles.Count == 0)
            {
                // No, allocate a fixed block of memory for the handles
                // Each handle in the block points to the actual location of the allocation elsewhere in the heap
                int handlesPerBlock = 32;

                // Allocate range
                var r = _allocator.Alloc(handlesPerBlock * 2, false, true);
                if (r == null)
                {
                    GrowHeap(handlesPerBlock * 2 + growSize + 1024);

                    r = _allocator.Alloc(handlesPerBlock * 2, false, true);
                    if (r == null)
                    {
                        return(0);
                    }
                }

                r.User = new LocalAllocation()
                {
                    flags       = Win16.LMEM_FIXED,
                    handleOrPtr = 0,
                    range       = r,
                };

                // Create handles
                for (int i = 0; i < handlesPerBlock; i++)
                {
                    _freeHandles.Add((ushort)(r.Position + i * 2));
                }
            }

            // Remove from list
            ushort handle = _freeHandles[0];

            _freeHandles.RemoveAt(0);
            return(handle);
        }
Example #2
0
        public Selector AllocSelector(string name, ushort pages = 1)
        {
            // Create the allocation entry
            var sel = new Selector()
            {
                name = name,
            };

            // Multi segment
            sel.selectorIndex = (ushort)(_selectorAllocator.Alloc(pages, false, false).Position);

            // Update the high memory page map for all selectors
            for (int i = 0; i < pages; i++)
            {
                _pageMap[(sel.selectorIndex + i)] = sel;
            }

            if (_machine.logGlobalAllocations)
            {
                Log.WriteLine("Allocated selector: 0x{0:X4} ({1} pages)", sel.selector, pages);
            }

            return(sel);
        }
Example #3
0
        public LocalHeap(GlobalHeap globalHeap, ushort globalHandle, ushort baseOffset, ushort maxSize)
        {
            _globalHeap   = globalHeap;
            _globalHandle = globalHandle;

            // Dont allocate at zero
            if (baseOffset == 0)
            {
                baseOffset = 16;
            }

            // Work out top of heap
            uint topOfHeap = _globalHeap.Size(globalHandle);
            uint heapLimit = baseOffset + (uint)(maxSize == 0 ? 0x10000 : maxSize);

            if (heapLimit > topOfHeap)
            {
                heapLimit = topOfHeap;
            }

            _allocator      = new RangeAllocator <LocalAllocation>((int)heapLimit);
            _freeHandles    = new List <ushort>();
            _handleOrPtrMap = new Dictionary <ushort, LocalAllocation>();

            var reservedPage = _allocator.Alloc(baseOffset, false, false);

            reservedPage.User = new LocalAllocation()
            {
                flags       = Win16.LMEM_FIXED,
                handleOrPtr = 0,
                range       = reservedPage,
            };

            _allocator.MoveListener = this;
            _baseOffset             = baseOffset;
        }