MiDeleteUltraMapContext

VOID __fastcall MiDeleteUltraMapContext(INT64 a1, UINT64 a2){
  __int64 v3; 
  __int64 v4; 
  _QWORD *v5; 
  UINT64 v6; 
  struct _KPRCB *CurrentPrcb; 
  __int64 v8; 
  bool v9; 
  signed __int32 v10; 
  if( *(_BYTE *)(a1 + 24) )
  {
    v3 = 2i64;
    v4 = (unsigned int)a2 > 2 ? 1i64 : MiLargePageSizes[(unsigned int)a2];
    MiGetUltraMapping((unsigned __int64 *)a1, a2, v4, 2);
    _InterlockedDecrement(&dword_140C4E96C);
    v5 = (_QWORD *)(a1 + 8);
    v6 = 0i64;
    do
    {
      if( *v5 != -1i64 )
      {
        MiReleaseFreshPage((_MMPFN *)((char *)MmGetPfnDb() + 48 * *v5));
        ++v6;
        *v5 = -1i64;
      }
      ++v5;
      --v3;
    }
    while( v3 );
    *(_BYTE *)(a1 + 24) = 0;
    if( v6 )
    {
      MiReturnCommit((_MI_PARTITION *)&MiSystemPartition, v6);
      CurrentPrcb = KeGetCurrentPrcb();
      v8 = *((int *)CurrentPrcb + 8391);
      if( (_DWORD)v8 != -1 )
      {
        if( v6 + v8 <= 0x100 )
        {
          do
          {
            if( v6 >= 0x80000 )
              break;
            v10 = _InterlockedCompareExchange((volatile signed __int32 *)CurrentPrcb + 8391, v8 + v6, v8);
            v9 = (_DWORD)v8 == v10;
            LODWORD(v8) = v10;
            if( v9 )
              return;
          }
          while( v10 != -1 && v6 + v10 <= 0x100 );
        }
        if( (int)v8 > 192
          && (_DWORD)v8 != -1
          && (_DWORD)v8 == _InterlockedCompareExchange((volatile signed __int32 *)CurrentPrcb + 8391, 192, v8) )
        {
          v6 += (int)v8 - 192;
        }
      }
      if( v6 )
        _InterlockedExchangeAdd64(&qword_140C526C0, v6);
    }
  }
}

Referenced by:

MiDeleteUltraThreadContext
MiGetUltraMdlContext
MiReclaimUnusedUltraMdlMaps