HvpFindFreeCell

UINT64 __stdcall HvpFindFreeCell(
        ULONG_PTR Hive,
        UINT64 Index,
        UINT64 NewSize,
        _HSTORAGE_TYPE Type,
        _HCELL **pCell,
        _HV_GET_CELL_CONTEXT *pCellContext){
  _HHIVE *v6; 
  __int64 v8; 
  __int64 v9; 
  int v10; 
  unsigned int v11; 
  unsigned int v12; 
  int v13; 
  unsigned int i; 
  __int64 v15; 
  NTSTATUS SetBits; 
  int v17; 
  _HMAP_ENTRY *CellMap; 
  unsigned int v19; 
  unsigned __int64 PermanentBinAddress; 
  NTSTATUS FreeCellInBin; 
  __int64 v23; 
  int v24; 
  unsigned int NewSizea; 
  UINT64 CellIndex; 

  NewSizea = NewSize;
  v6 = *(_HHIVE **)&Hive;
  v8 = 632i64 * Type;
  v9 = (unsigned int)Index;
  v10 = (-1 << Index) & *(_DWORD *)(v8 + *(_QWORD *)&Hive + 896);
  if( !v10 )
    return(unsigned int)-1;
  _BitScanForward(&v11, v10);
  if( v11 != (_DWORD)Index )
    v9 = v11;
  v12 = -1;
  v13 = -1;
  for( i = *(_DWORD *)(v8 + 24 * v9 + *(_QWORD *)&Hive + 308); ; i = v24 )
  {
    v15 = v8 + 24 * v9;
    SetBits = RtlFindSetBits((INT64)&v6->Storage[0].FreeDisplay[0].Display + v15, 1ui64, i);
    *(unsigned int *)((char *)&v6->Storage[0].FreeDisplay[0].Hint + v15) = SetBits;
    v24 = SetBits + 1;
    if( v13 == SetBits )
    {
      v10 &= v10 - 1;
      if( !v10 )
        return v12;
      _BitScanForward((unsigned int *)&v9, v10);
      v23 = v8 + 24 * v9;
      SetBits = RtlFindSetBits(
                  (INT64)&v6->Storage[0].FreeDisplay[0].Display + v23,
                  1ui64,
                  *(unsigned int *)((char *)&v6->Storage[0].FreeDisplay[0].Hint + v23));
      *(unsigned int *)((char *)&v6->Storage[0].FreeDisplay[0].Hint + v23) = SetBits;
      v13 = SetBits;
      v24 = SetBits + 1;
    }
    v17 = SetBits;
    if( v13 != -1 )
      v17 = v13;
    v13 = v17;
    LODWORD(CellIndex) = (SetBits << 12) + (Type << 31);
    CellMap = HvpGetCellMap(v6, (unsigned int)CellIndex);
    if( !CellMap )
      KeBugCheckEx(0x51u, (PVOID)1, v6, (PVOID)v19, (PVOID)0x480);
    PermanentBinAddress = CellMap->PermanentBinAddress;
    if( (PermanentBinAddress & 2) == 0 && (PermanentBinAddress & 4) == 0 && (PermanentBinAddress & 1) != 0 )
      break;
LABEL_16:
    ;
  }
  FreeCellInBin = HvpFindFreeCellInBin(
                    v6,
                    NewSizea,
                    Type,
                    (_HBIN *)(PermanentBinAddress & 0xFFFFFFFFFFFFFFF0ui64),
                    &CellIndex,
                    pCell,
                    pCellContext);
  if( FreeCellInBin < 0 )
  {
    if( FreeCellInBin != -1073741275 )
      return v12;
    goto LABEL_16;
  }
  return(unsigned int)CellIndex;
}

Referenced by:

HvpDoAllocateCell