HvpFindFreeCell

UINT64 __stdcall HvpFindFreeCell(
        _HHIVE *Hive,
        UINT64 Index,
        UINT64 NewSize,
        HSTORAGE_TYPE Type,
        _HCELL **pCell,
        _HV_GET_CELL_CONTEXT *pCellContext){
  unsigned __int64 v8; 
  __int64 v9; 
  unsigned int v10; 
  unsigned int v11; 
  unsigned int v12; 
  int v13; 
  unsigned int i; 
  unsigned __int64 v15; 
  unsigned int SetBits; 
  int v17; 
  INT64 CellMap; 
  unsigned int v19; 
  __int64 v20; 
  NTSTATUS FreeCellInBin; 
  unsigned __int64 v23; 
  unsigned int v24; 
  unsigned int NewSizea; 
  UINT64 CellIndex; 
  NewSizea = NewSize;
  v8 = 632i64 * (int)Type;
  v9 = (unsigned int)Index;
  v10 = (-1 << Index) & Hive->Storage[v8 / 0x278].FreeSummary;
  if( !v10 )
    return(unsigned int)-1;
  _BitScanForward(&v11, v10);
  if( v11 != (_DWORD)Index )
    v9 = v11;
  v12 = -1;
  v13 = -1;
  for( i = *(unsigned int *)((char *)&Hive->Storage[0].FreeDisplay[v9].Hint + v8); ; i = v24 )
  {
    v15 = v8 + 24 * v9;
    SetBits = RtlFindSetBits((RTL_BITMAP *)((char *)&Hive->Storage[0].FreeDisplay[0].Display + v15), 1ui64, i);
    *(unsigned int *)((char *)&Hive->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(
                  (RTL_BITMAP *)((char *)&Hive->Storage[0].FreeDisplay[0].Display + v23),
                  1ui64,
                  *(unsigned int *)((char *)&Hive->Storage[0].FreeDisplay[0].Hint + v23));
      *(unsigned int *)((char *)&Hive->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((INT64)Hive, (unsigned int)CellIndex);
    if( !CellMap )
      KeBugCheckEx(0x51u, 1ui64, (ULONG_PTR)Hive, v19, 0x480ui64);
    v20 = *(_QWORD *)(CellMap + 8);
    if( (v20 & 2) == 0 && (v20 & 4) == 0 && (v20 & 1) != 0 )
      break;
LABEL_16:
    ;
  }
  FreeCellInBin = HvpFindFreeCellInBin(
                    Hive,
                    NewSizea,
                    Type,
                    (_HBIN *)(v20 & 0xFFFFFFFFFFFFFFF0ui64),
                    &CellIndex,
                    pCell,
                    pCellContext);
  if( FreeCellInBin < 0 )
  {
    if( FreeCellInBin != -1073741275 )
      return v12;
    goto LABEL_16;
  }
  return(unsigned int)CellIndex;
}

Referenced by:

HvpDoAllocateCell