MiForcedTrim

NTSTATUS __stdcall MiForcedTrim(_MMSUPPORT_INSTANCE *Vm){
  UINT8 v1; 
  NTSTATUS v2; 
  _MI_PARTITION *v4; 
  _QWORD *WorkingSetControl; 
  unsigned __int64 v6; 
  unsigned __int64 WorkingSetSize; 
  unsigned __int64 v8; 
  unsigned __int64 v9; 
  unsigned __int64 LockedEntries; 
  unsigned __int64 v11; 
  unsigned int v12; 
  unsigned __int64 v13; 
  __int64 v14; 
  unsigned __int64 *v15; 
  unsigned __int64 v16; 
  unsigned int v17; 
  UINT8 v18; 
  unsigned __int64 v20; 
  unsigned __int64 v21; 
  unsigned __int64 v22; 
  UINT64 v23; 
  __int64 v24; 
  UINT64 TrimFlags; 
  unsigned __int64 NumberOfPhysicalPages; 
  UINT8 WsIrql; 

  WsIrql = v1;
  v2 = 0;
  v4 = *(_MI_PARTITION **)(*(&stru_140C4DB30 + 267) + 8i64 * Vm->PartitionId);
  WorkingSetControl = v4->WorkingSetControl;
  LODWORD(v6) = MiGetAvailablePagesBelowPriority(v4, 6ui64);
  WorkingSetSize = Vm->WorkingSetSize;
  v8 = v6;
  v9 = WorkingSetControl[300];
  LockedEntries = Vm->VmWorkingSetList->LockedEntries;
  if( WorkingSetSize <= LockedEntries )
    return 0;
  v11 = WorkingSetSize - LockedEntries;
  NumberOfPhysicalPages = v4->Vp.NumberOfPhysicalPages;
  v12 = 1;
  v13 = 0i64;
  v14 = (15 * v11 * (unsigned __int128)0x47AE147AE147AE15ui64) >> 64;
  v15 = &Vm->AgeDistribution[1];
  v16 = (v14 + ((15 * v11 - v14) >> 1)) >> 6;
  do
  {
    v13 += *v15;
    if( v13 >= v16 )
      break;
    ++v12;
    ++v15;
  }
  while( v12 < 8 );
  v17 = 7;
  if( (Vm->Flags._bf_0 & 7) != 0 )
  {
    v20 = v4->Vp.NumberOfPhysicalPages;
    v21 = NumberOfPhysicalPages / 0xA;
    if( v11 < NumberOfPhysicalPages )
      v20 = v11;
    if( 16 * v9 <= v21 )
      v21 = 16 * v9;
    v9 = v21 * (100 * v20 / NumberOfPhysicalPages) / 0x64;
    if( v9 > v11 / 0xA )
      v9 = v11 / 0xA;
  }
  if( v8 >= v9 )
    goto LABEL_7;
  v22 = v9 - v8;
  v23 = 0i64;
  if( v22 < 0x10 )
    v22 = 16i64;
  while( 1 )
  {
    v23 += Vm->AgeDistribution[v17];
    if( v23 >= v22 )
      break;
    if( --v17 == 1 )
      goto LABEL_26;
  }
  v23 = v22;
LABEL_26:
  if( v23 )
  {
    if( (Vm->Flags._bf_0 & 7) == 0 )
      v23 &= -(__int64)(v8 < v4->PageLists.LowMemoryThreshold >> 1);
    v18 = WsIrql;
    if( v23 )
    {
      LODWORD(TrimFlags) = 121;
      LODWORD(v24) = MiTrimWorkingSet(Vm, v23, WsIrql, v17, TrimFlags);
      WorkingSetControl[310] += v24;
      v2 = v24;
    }
  }
  else
  {
LABEL_7:
    v18 = WsIrql;
  }
  if( v13 < v16 || (Vm->PageFaultCount & 0x3FFF) == 0 )
    MiForceAgeWorkingSet(Vm, v18);
  return v2;
}

Referenced by:

MiPreUnlockWorkingSetShared