/// <summary> /// Tries to allocate an entry in the <see cref="EntryTable{TValue}"/>. Returns <see langword="true"/> if /// success; otherwise returns <see langword="false"/>. /// </summary> /// <param name="index">Index of entry allocated in the table</param> /// <returns><see langword="true"/> if success; otherwise <see langword="false"/></returns> public bool TryAllocate(out int index) { lock (_allocated) { if (_allocated.IsSet(_freeHint)) { _freeHint = _allocated.FindFirstUnset(); } if (_freeHint < _table.Length) { index = checked (_freeHint++); _allocated.Set(index); return(true); } } index = 0; return(false); }
/// <summary> /// Allocates an entry in the <see cref="EntryTable{TEntry}"/>. /// </summary> /// <returns>Index of entry allocated in the table</returns> /// <exception cref="ObjectDisposedException"><see cref="EntryTable{TEntry}"/> instance was disposed</exception> public int Allocate() { if (_disposed) { throw new ObjectDisposedException(null); } lock (_allocated) { if (_allocated.IsSet(_freeHint)) { _freeHint = _allocated.FindFirstUnset(); } int index = _freeHint++; var page = GetPage(index); _allocated.Set(index); GetValue(page, index) = default; return(index); } }