private void CheckCapacity()
 {
     if (_queues[0].Count <= 5)
     {
         // -------- Multiply by 2 the capacity
         LinkedIndexPoolEntry *olds = _entries;
         int newCapacity            = _capacity * 2;
         _entries = (LinkedIndexPoolEntry *)HeapAllocator.New(sizeof(LinkedIndexPoolEntry) * newCapacity);
         MemoryHelper.Copy((byte *)olds, (byte *)_entries, sizeof(LinkedIndexPoolEntry) * _capacity);
         for (int i = _capacity; i < newCapacity; i++)
         {
             _entries[i].Previous = i - 1;
             _entries[i].Next     = i + 1;
             _entries[i].Index    = i;
             _entries[i].Queue    = 0;
         }
         _entries[newCapacity - 1].Next = -1;
         // -------- Fix 0 queue :
         _entries[_queues[0].Last].Next = _capacity;
         _entries[_capacity].Previous   = _queues[0].Last;
         _queues[0].Last   = newCapacity - 1;
         _queues[0].Count += _capacity;
         // --------
         _capacity = newCapacity;
         HeapAllocator.Free((byte *)olds);
         CheckCoherency();
     }
 }
 public StackedIndexPoolUnsafe(int capacity, int idOffset)
 {
     _idOffset = idOffset;
     _entries  = (int *)HeapAllocator.New(capacity * sizeof(int));
     for (int i = 0; i < capacity; i++)
     {
         _entries[i] = i + idOffset;
     }
     _bottom   = 0;
     _capacity = capacity;
 }
 private void EnsureCpacity(int n)
 {
     if (n >= _capacity)
     {
         int  newCapacity = _capacity * 2;
         int *newEntries  = (int *)HeapAllocator.New(newCapacity * sizeof(int));
         MemoryHelper.Copy((byte *)_entries, (byte *)newEntries, _capacity * sizeof(int));
         HeapAllocator.Free((byte *)_entries);
         for (int i = _capacity; i < newCapacity; i++)
         {
             newEntries[i] = i + _idOffset;
         }
         _entries  = newEntries;
         _capacity = newCapacity;
     }
 }
        /********************************************************************************/
        // -------- Queues
        public int GetFreeQueue()
        {
            int tmp = _queueStack.Pop();

            if (_queueStack.Capacity > _queueCount)
            {
                int newQueueCount = _queueStack.Capacity;
                LinkedIndexPoolQueue *_oldQueues = _queues;
                _queues = (LinkedIndexPoolQueue *)HeapAllocator.New(sizeof(LinkedIndexPoolQueue) * newQueueCount);
                for (int i = 0; i < _queueCount; i++)
                {
                    _queues[i] = _oldQueues[i];
                }
                for (int i = _queueCount; i < newQueueCount; i++)
                {
                    _queues[i].First = _queues[i].Last = -1;
                    _queues[i].Index = i;
                    _queues[i].Count = 0;
                }
            }
            return(tmp);
        }
 public LinkedIndexPool(int capacity, int queueCount, bool check = false)
 {
     if (capacity < 8)
     {
         capacity = 8;
     }
     if (queueCount < 2)
     {
         queueCount = 2;
     }
     // -------- Initialize entries
     _entries  = (LinkedIndexPoolEntry *)HeapAllocator.New(sizeof(LinkedIndexPoolEntry) * capacity);
     _capacity = capacity;
     for (int i = 0; i < capacity; i++)
     {
         _entries[i].Previous = i - 1;
         _entries[i].Next     = i + 1;
         _entries[i].Index    = i;
         _entries[i].Queue    = 0;
     }
     _entries[0].Previous        = -1;
     _entries[capacity - 1].Next = -1;
     // -------- Initialize queues
     _queues     = (LinkedIndexPoolQueue *)HeapAllocator.New(sizeof(LinkedIndexPoolQueue) * queueCount);
     _queueCount = queueCount;
     for (int i = 0; i < queueCount; i++)
     {
         _queues[i].First = _queues[i].Last = -1;
         _queues[i].Index = i;
         _queues[i].Count = 0;
     }
     _queues[0].First = 0;
     _queues[0].Last  = capacity - 1;
     _queues[0].Count = capacity;
     // -------- Initialize queues stack
     _queueStack = new StackedIndexPoolUnsafe(queueCount, 2);
     // -------- All is ok...
     _checkIt = check;
 }