MiFindPageFileWriteCluster

VOID __stdcall MiFindPageFileWriteCluster(
        _MMPAGING_FILE *PagingFile,
        UINT64 *StartingIndex,
        UINT64 *ClusterSizeInOut,
        UINT64 LockHeld,
        UINT64 a5){
  unsigned int v5; 
  int v7; 
  __int64 v9; 
  __int64 v10; 
  _MI_PARTITION *Partition; 
  int v12; 
  NTSTATUS FreePageFileSpace; 
  INT64 v14; 
  unsigned __int64 Long; 
  unsigned __int64 v16; 
  _DWORD *v17; 
  unsigned int RefreshAllocationCluster; 
  unsigned int LargestAllocationCluster; 
  INT64 v20; 
  unsigned int v21; 
  _MI_PAGING_FILE_SPACE_BITMAPS *Bitmaps; 
  _RTL_BITMAP *p_BitMapHeader; 
  _DWORD *p_AllocationBitmapHint; 
  unsigned int LongestRunClearCapped; 
  bool v26; 
  _MI_PAGING_FILE_SPACE_BITMAPS *v27; 
  _RTL_BITMAP BitMapHeader; 
  _MI_PAGING_FILE_SPACE_BITMAPS ReferencedBitmaps; 
  INT64 v30; 
  _BYTE savedregs[56]; 
  _MMPTE PageFileInfo; 
  UINT64 *v33; 
  UINT64 StartingIndexa; 

  v33 = ClusterSizeInOut;
  v5 = *(_DWORD *)ClusterSizeInOut;
  v7 = LockHeld;
  v9 = (unsigned __int64)*(unsigned int *)StartingIndex << 32;
  memset(&ReferencedBitmaps, 0, sizeof(ReferencedBitmaps));
  BitMapHeader = 0i64;
  v10 = MiSwizzleInvalidPte(v9);
  Partition = PagingFile->Partition;
  PageFileInfo.u.Long = ((unsigned __int64)(PagingFile->_bf_cc & 0xF) << 12) | v10 & 0xFFFFFFFFFFFF0FFFui64;
  FreePageFileSpace = MiFindFreePageFileSpace(Partition, &PageFileInfo, v5, v12 != 0 ? 38 : 32);
  if( FreePageFileSpace )
  {
    Long = PageFileInfo.u.Long;
    if( *(&stru_140C4DB30 + 42) && (PageFileInfo.u.Long & 0x10) == 0 )
      Long = PageFileInfo.u.Long & ~*(&stru_140C4DB30 + 42);
    v16 = HIDWORD(Long);
  }
  else
  {
    if( v7 )
    {
      p_BitMapHeader = &BitMapHeader;
      Bitmaps = PagingFile->Bitmaps;
      RefreshAllocationCluster = 0;
      BitMapHeader.SizeOfBitMap = PagingFile->Size;
      BitMapHeader.Buffer = Bitmaps->AllocationBitmap.Buffer;
      p_AllocationBitmapHint = StartingIndex;
    }
    else
    {
      RefreshAllocationCluster = PagingFile->RefreshAllocationCluster;
      LargestAllocationCluster = PagingFile->LargestAllocationCluster;
      v20 = (unsigned int)MaxIoSize;
      if( LargestAllocationCluster < (unsigned int)MaxIoSize
        && PagingFile->LastRefreshAllocationCluster != RefreshAllocationCluster )
      {
        if( LargestAllocationCluster >= (unsigned int)MaxIoSize >> 3 )
        {
          v21 = LargestAllocationCluster + (LargestAllocationCluster >> 1);
          LargestAllocationCluster = MaxIoSize;
          if( v21 < (unsigned int)MaxIoSize )
            LargestAllocationCluster = v21;
        }
        else
        {
          LargestAllocationCluster *= 2;
        }
        PagingFile->LargestAllocationCluster = LargestAllocationCluster;
      }
      Bitmaps = &ReferencedBitmaps;
      if( v5 >= LargestAllocationCluster )
        v5 = LargestAllocationCluster;
      if( (PagingFile->_bf_cc & 0x800) != 0 )
        v5 = 1;
      MiRefPageFileSpaceBitmaps(
        PagingFile,
        &ReferencedBitmaps,
        v20,
        v14,
        *(INT64 *)&BitMapHeader.SizeOfBitMap,
        (INT64)BitMapHeader.Buffer,
        (INT64)ReferencedBitmaps.Anchor,
        *(INT64 *)&ReferencedBitmaps.AllocationBitmap.SizeOfBitMap,
        (INT64)ReferencedBitmaps.AllocationBitmap.Buffer,
        *(INT64 *)&ReferencedBitmaps.ReservationBitmap.SizeOfBitMap,
        (INT64)ReferencedBitmaps.ReservationBitmap.Buffer,
        v30,
        *(INT64 *)savedregs,
        *(INT64 *)&savedregs[8],
        *(INT64 *)&savedregs[16],
        *(INT64 *)&savedregs[24],
        *(INT64 *)&savedregs[32],
        *(INT64 *)&savedregs[40]);
      p_BitMapHeader = &ReferencedBitmaps.AllocationBitmap;
      p_AllocationBitmapHint = &PagingFile->AllocationBitmapHint;
    }
    LODWORD(StartingIndexa) = *p_AllocationBitmapHint;
    LongestRunClearCapped = RtlFindLongestRunClearCapped(p_BitMapHeader, v5, &StartingIndexa);
    v26 = v7 == 0;
    FreePageFileSpace = LongestRunClearCapped;
    LODWORD(v16) = StartingIndexa;
    if( v26 )
    {
      if( LongestRunClearCapped )
      {
        if( LongestRunClearCapped < v5 )
        {
          PagingFile->LargestAllocationCluster = LongestRunClearCapped;
          PagingFile->LastRefreshAllocationCluster = RefreshAllocationCluster;
        }
        PagingFile->AllocationBitmapHint = v16 + LongestRunClearCapped;
      }
      v27 = MiDerefPageFileSpaceBitmaps(PagingFile, Bitmaps, 0i64);
      if( v27 )
        ExFreePoolWithTag(v27, 0);
    }
    else
    {
      MiSetPageFileAllocationBits(PagingFile, (unsigned int)StartingIndexa, LongestRunClearCapped);
    }
  }
  v17 = v33;
  *(_DWORD *)StartingIndex = v16;
  *v17 = FreePageFileSpace;
}

Referenced by:

MiGatherPagefilePages