KdSetDbgPrintBufferSize

INT64 __stdcall KdSetDbgPrintBufferSize(UINT64 Size){
  unsigned int v1; 
  UINT8 *PoolWithTag; 
  unsigned __int8 CurrentIrql; 
  UINT64 v5; 
  UINT8 *v6; 
  signed __int64 v7; 
  UINT64 v8; 
  UINT64 v9; 
  v1 = Size;
  if( KdPitchDebugger )
    return 3221226324i64;
  if( (unsigned int)Size > 0x1000000 )
    return 3221225711i64;
  if( (unsigned int)Size <= 0x1000 )
  {
    v1 = 4096;
    PoolWithTag = (UINT8 *)KdPrintDefaultCircularBuffer;
    while( 1 )
    {
LABEL_9:
      if( !KdpPrintSpinLock )
      {
        CurrentIrql = KeGetCurrentIrql();
        __writecr8(0xFui64);
        if( !_InterlockedCompareExchange64(&KdpPrintSpinLock, 1i64, 0i64) )
        {
          v5 = 0i64;
          v6 = (UINT8 *)KdPrintCircularBuffer;
          qword_140CF9C88 = (__int64)KeGetCurrentPrcb();
          if( v1 > (unsigned __int64)(unsigned int)KdPrintBufferSize )
          {
            if( (char *)KdPrintWritePointer - (_BYTE *)KdPrintCircularBuffer >= (unsigned __int64)(unsigned int)KdPrintBufferSize )
              KdPrintRolloverCount = 0;
            v7 = ((char *)KdPrintWritePointer - (_BYTE *)KdPrintCircularBuffer) & -(__int64)((char *)KdPrintWritePointer
                                                                                           - (_BYTE *)KdPrintCircularBuffer < (unsigned __int64)(unsigned int)KdPrintBufferSize);
            if( KdPrintRolloverCount )
            {
              v5 = (unsigned int)KdPrintBufferSize - v7;
              memmove(PoolWithTag, (UINT8 *)KdPrintCircularBuffer + v7, v5);
            }
            memmove(&PoolWithTag[v5], v6, v7);
            v5 += v7;
            if( KdPrintRolloverCount )
            {
              v8 = 0i64;
              if( *PoolWithTag )
              {
                do
                {
                  if( v8 >= v5 )
                    break;
                  ++v8;
                }
                while( PoolWithTag[v8] );
              }
              v9 = v8 + 1;
              if( v9 < v5 )
              {
                v5 -= v9;
                memmove(PoolWithTag, &PoolWithTag[v9], v5);
              }
            }
          }
          memset((INT64)&PoolWithTag[v5], 0i64);
          ++KdPrintBufferChanges;
          qword_140CF9C88 = 0i64;
          KdPrintCircularBuffer = PoolWithTag;
          KdPrintBufferSize = v1;
          KdPrintWritePointer = (__int64(*)[512])&PoolWithTag[v5];
          KdPrintRolloverCount = 0;
          _InterlockedExchange((volatile __int32 *)&KdpPrintSpinLock, 0);
          __writecr8(CurrentIrql);
          if( v6 )
          {
            if( v6 != (UINT8 *)KdPrintDefaultCircularBuffer )
              ExFreePoolWithTag(v6, 0);
          }
          return 0i64;
        }
        __writecr8(CurrentIrql);
      }
      _mm_pause();
    }
  }
  PoolWithTag = (UINT8 *)ExAllocatePoolWithTag(NonPagedPoolNx, (unsigned int)Size, 0x6250644Bui64);
  if( PoolWithTag )
    goto LABEL_9;
  return 3221225495i64;
}

Referenced by:

KdAllocateDynamicMemory
NtSystemDebugControl