MiChargeCommit

NTSTATUS __stdcall MiChargeCommit(_MI_PARTITION *Partition, UINT64 QuotaCharge, UINT64 CommitFlags){
  CHAR v3; 
  _MI_PARTITION *v5; 
  struct _KPRCB *CurrentPrcb; 
  UINT64 CachedCommit; 
  signed __int32 v8; 
  CHAR v10; 
  INT64 SystemCommitReserve; 
  unsigned __int64 TotalCommittedPages; 
  unsigned __int64 v13; 
  unsigned __int64 TotalCommitLimit; 
  signed __int64 v15; 
  unsigned __int64 v16; 
  unsigned __int64 v17; 
  unsigned __int64 LowCommitThreshold; 
  unsigned __int64 v19; 
  unsigned __int64 v20; 
  unsigned __int64 v21; 
  UINT64 v22; 
  UINT64 v23; 
  unsigned __int64 HighCommitThreshold; 
  int v25; 
  int v26; 
  bool v27; 
  char *v28; 
  unsigned __int64 v29; 
  __int64 v30; 
  int v31; 

  v3 = CommitFlags;
  v5 = Partition;
  while( 2 )
  {
    CurrentPrcb = KeGetCurrentPrcb();
    if( v5 == &Irp )
    {
      _m_prefetchw((const void *)&CurrentPrcb->CachedCommit);
      CachedCommit = CurrentPrcb->CachedCommit;
      if( QuotaCharge <= CachedCommit )
      {
        do
        {
          v8 = _InterlockedCompareExchange(
                 (volatile signed __int32 *)&CurrentPrcb->CachedCommit,
                 CachedCommit - QuotaCharge,
                 CachedCommit);
          Partition = (_MI_PARTITION *)v8;
          if( v8 == CachedCommit )
            return 1;
          CachedCommit = v8;
        }
        while( QuotaCharge <= v8 );
      }
    }
    v31 = 0;
    v10 = v3 | 2;
    if( (v3 & 4) == 0 )
      v10 = v3;
    if( (v10 & 8) == 0 )
    {
      if( (v10 & 7) != 0 )
        SystemCommitReserve = 0i64;
      else
        SystemCommitReserve = v5->Commit.SystemCommitReserve;
      goto LABEL_12;
    }
    v10 |= 2u;
    Partition = (_MI_PARTITION *)*(unsigned int *)&KeGetCurrentThread()[1].gapD8[12];
    if( ((unsigned __int8)Partition & 2) != 0 && (KeGetPcr()->Prcb._bf_0 & 0x10001) == 0 )
    {
      v10 |= 4u;
      SystemCommitReserve = 0i64;
LABEL_12:
      v30 = SystemCommitReserve;
      goto LABEL_13;
    }
    SystemCommitReserve = 64i64;
    v30 = 64i64;
    if( v5 != &Irp && v5->Vp.NumberOfPhysicalPages >> 6 < 0x40 )
    {
      SystemCommitReserve = v5->Vp.NumberOfPhysicalPages >> 6;
      v30 = SystemCommitReserve;
    }
    while( 1 )
    {
LABEL_13:
      while( 1 )
      {
        TotalCommittedPages = v5->Vp.TotalCommittedPages;
        v13 = TotalCommittedPages + QuotaCharge;
        if( TotalCommittedPages + QuotaCharge <= TotalCommittedPages || SystemCommitReserve + v13 < v13 )
          break;
        TotalCommitLimit = v5->Vp.TotalCommitLimit;
        if( SystemCommitReserve + v13 <= TotalCommitLimit )
        {
          if( !v31 && v13 >= 95 * (TotalCommitLimit / 0x64) )
          {
            MiApplyCommitDelay((INT64)v5, v10, v5->Vp.TotalCommitLimit);
            SystemCommitReserve = v30;
            v31 = 1;
          }
          goto LABEL_19;
        }
        HighCommitThreshold = v5->Commit.HighCommitThreshold;
        if( v13 >= HighCommitThreshold && TotalCommittedPages < HighCommitThreshold )
        {
          MiPulseCommitSignal(v5);
          SystemCommitReserve = v30;
        }
        if( (v10 & 4) != 0 )
        {
          if( !SystemCommitReserve )
            ++*(&stru_140C4DB30 + 627);
          goto LABEL_61;
        }
        if( (v10 & 2) != 0 )
          return 0;
        if( TotalCommitLimit == v5->Commit.TotalCommitLimitMaximum )
        {
          ++v5->Vp.ChargeCommitmentFailures[1];
          if( v5 != &Irp )
            return 0;
          MiIssuePageExtendRequest(v5, QuotaCharge, 2 * (v10 & 1u) + 8, 0i64);
          if( (v10 & 1) != 0 )
            return 0;
          if( !v26 )
          {
            MiCauseOverCommitPopup(v5);
            return 0;
          }
          SystemCommitReserve = v30;
          goto LABEL_19;
        }
        if( (v10 & 1) != 0 )
        {
          ++v5->Vp.ChargeCommitmentFailures[2];
          MiIssuePageExtendRequest(v5, 0x1000ui64, 2ui64, 0i64);
          return 0;
        }
        MiIssuePageExtendRequest(v5, QuotaCharge, 0i64, 0xFFui64);
        if( !v25 )
        {
          ++v5->Vp.ChargeCommitmentFailures[0];
          MiCauseOverCommitPopup(v5);
          return 0;
        }
        SystemCommitReserve = v30;
      }
      if( (v10 & 4) == 0 )
      {
        if( (v10 & 2) == 0 )
        {
          ++v5->Vp.ChargeCommitmentFailures[3];
          if( (v10 & 1) == 0 )
            MiCauseOverCommitPopup(v5);
          MiPulseCommitSignal(v5);
        }
        return 0;
      }
      if( !SystemCommitReserve )
        ++*(&stru_140C4DB30 + 628);
LABEL_61:
      if( QuotaCharge )
        break;
LABEL_19:
      v15 = _InterlockedCompareExchange64(
              (volatile signed __int64 *)&v5->Vp.TotalCommittedPages,
              v13,
              TotalCommittedPages);
      if( TotalCommittedPages == v15 )
      {
        v16 = v5->Commit.HighCommitThreshold;
        v17 = QuotaCharge + v15;
        if( v17 >= v16 && TotalCommittedPages < v16
          || (LowCommitThreshold = v5->Commit.LowCommitThreshold, v17 >= LowCommitThreshold)
          && TotalCommittedPages < LowCommitThreshold )
        {
          MiSyncCommitSignals(v5, 0i64);
          SystemCommitReserve = v30;
        }
        if( v17 > v5->Commit.PeakCommitment )
          v5->Commit.PeakCommitment = v17;
        if( (v10 & 2) != 0 )
          return 1;
        v19 = v5->Vp.TotalCommitLimit;
        v20 = v19 / 0xA;
        if( v17 < 9 * (v19 / 0xA) )
        {
LABEL_26:
          if( v5 == &Irp )
          {
            if( (v10 & 1) == 0 && v17 >= v5->Commit.HighCommitThreshold && v19 == v5->Commit.TotalCommitLimitMaximum )
            {
              MiFreeExcessSegments((UINT64)Partition);
              SystemCommitReserve = v30;
            }
            v21 = CurrentPrcb->CachedCommit;
            if( v21 < 0x80 )
            {
              v22 = v17;
              v23 = 256 - v21 + v17;
              if( v23 > v22 && SystemCommitReserve + v23 > v22 && SystemCommitReserve + v23 <= v19 )
                MiReplenishLocalCommit(v5, CurrentPrcb, v22, 256 - v21);
            }
          }
          return 1;
        }
        v27 = v17 <= v19;
        if( v17 < v19 )
        {
          if( v19 - v17 >= 0x400000 )
            goto LABEL_26;
          v27 = v17 <= v19;
        }
        if( v27 )
        {
          Partition = (_MI_PARTITION *)v5->Commit.TotalCommitLimitMaximum;
          if( v19 < (unsigned __int64)Partition && v17 < (unsigned __int64)Partition )
          {
            if( v20 > 0x40000 )
              v20 = 0x40000i64;
            v28 = (char *)Partition - v19;
            v29 = v20 - v17;
            if( v29 + v19 <= (unsigned __int64)v28 )
              v28 = (char *)(v29 + v19);
            MiIssuePageExtendRequest(v5, (UINT64)v28, 2ui64, 0i64);
            SystemCommitReserve = v30;
          }
        }
        goto LABEL_26;
      }
    }
    if( !MiConsumeOverCommit((UINT64 *)&v5->Core.PartitionId, QuotaCharge, SystemCommitReserve) )
      continue;
    return 1;
  }
}

Referenced by:

MiAcquireNonPagedResources
MiAllocatePerSessionProtos
MiAttemptPageFileReductionApc
MiBuildForkPte
MiChargeForLockedPage
MiChargeForWriteInProgressPage
MiChargeFullProcessCommitment
MiChargeSegmentCommit
MiCopyOnWrite
MiCreateImageFileMap
MiCreateLargePfnList
MiCreatePagingFile
MiCreatePagingFileMap
MiDeletePteRun
MiFinishHardFault
MiFlushSectionInternal
MiGetSubsectionCharges
MiHandleForkTransitionPte
MiInitializeCommitment
MiInitializePoolCommitPacket
MiInsertInSystemSpace
MiMakePageAvoidRead
MiMakePartitionActive
MiObtainFaultCharges
MiObtainSystemCharges
MiProbeLockFrame
MiPurgeImageSection
MiReAcquireOutSwappedProcessCommit
MiReferenceCloneProto
MiRelocateImage
MiResolveProtoPteFault
MiSessionCreate
MiSessionCreateInternal
MiSessionInsertImage
MiSetPagesModified
MiSetProtectionOnSection
MmChargeResources
MmCheckCachedPageStates
MmCreateKernelStack
MmCreateProcessAddressSpace
MmRotatePhysicalView
NtLockVirtualMemory