KdSetDbgPrintBufferSize

NTSTATUS __stdcall KdSetDbgPrintBufferSize(UINT64 Size){
  unsigned int v1; 
  VOID **PoolWithTag; 
  unsigned __int8 CurrentIrql; 
  unsigned __int64 v5; 
  char *v6; 
  signed __int64 v7; 
  unsigned __int64 v8; 
  unsigned __int64 v9; 

  v1 = Size;
  if( KdPitchDebugger )
    return -1073740972;
  if( (unsigned int)Size > 0x1000000 )
    return -1073741585;
  if( (unsigned int)Size <= 0x1000 )
  {
    v1 = 4096;
    PoolWithTag = (VOID **)((char *)&KiSwapEvent + 512);
    while( 1 )
    {
LABEL_9:
      if( !KdpPrintSpinLock )
      {
        CurrentIrql = KeGetCurrentIrql();
        __writecr8(0xFui64);
        if( !_InterlockedCompareExchange64(&KdpPrintSpinLock, 1i64, 0i64) )
        {
          v5 = 0i64;
          v6 = (char *)KdPrintCircularBuffer;
          qword_140CF9C88 = (__int64)KeGetCurrentPrcb();
          if( v1 > (unsigned __int64)(unsigned int)KdPrintBufferSize )
          {
            if( (_BYTE *)KdPrintWritePointer - (_BYTE *)KdPrintCircularBuffer >= (unsigned __int64)(unsigned int)KdPrintBufferSize )
              *(&KiSwapEvent + 1152) = 0;
            v7 = ((_BYTE *)KdPrintWritePointer - (_BYTE *)KdPrintCircularBuffer) & -(__int64)((_BYTE *)KdPrintWritePointer
                                                                                            - (_BYTE *)KdPrintCircularBuffer < (unsigned __int64)(unsigned int)KdPrintBufferSize);
            if( *(&KiSwapEvent + 1152) )
            {
              v5 = (unsigned int)KdPrintBufferSize - v7;
              memmove(PoolWithTag, (char *)KdPrintCircularBuffer + v7, KdPrintBufferSize - v7);
            }
            memmove((char *)PoolWithTag + v5, v6, v7);
            v5 += v7;
            if( *(&KiSwapEvent + 1152) )
            {
              v8 = 0i64;
              if( *(_BYTE *)PoolWithTag )
              {
                do
                {
                  if( v8 >= v5 )
                    break;
                  ++v8;
                }
                while( *((_BYTE *)PoolWithTag + v8) );
              }
              v9 = v8 + 1;
              if( v9 < v5 )
              {
                v5 -= v9;
                memmove(PoolWithTag, (char *)PoolWithTag + v9, v5);
              }
            }
          }
          memset((char *)PoolWithTag + v5, 0i64, v1 - v5);
          ++*(&KiSwapEvent + 1153);
          qword_140CF9C88 = 0i64;
          KdPrintCircularBuffer = PoolWithTag;
          KdPrintBufferSize = v1;
          KdPrintWritePointer = (char *)PoolWithTag + v5;
          *(&KiSwapEvent + 1152) = 0;
          _InterlockedExchange((volatile __int32 *)&KdpPrintSpinLock, 0);
          __writecr8(CurrentIrql);
          if( v6 )
          {
            if( v6 != (char *)&KiSwapEvent + 512 )
              ExFreePoolWithTag(v6, 0);
          }
          return 0;
        }
        __writecr8(CurrentIrql);
      }
      _mm_pause();
    }
  }
  PoolWithTag = ExAllocatePoolWithTag(0x200ui64, (unsigned int)Size, 1649435723i64);
  if( PoolWithTag )
    goto LABEL_9;
  return -1073741801;
}

Referenced by:

KdAllocateDynamicMemory
NtSystemDebugControl