package quic

import 

// The write buffer is a go-iroh addition. Upstream returns from Write only
// once the data has been packetized, or once it fits the single-packet
// nextFrame; a stream written in small pieces therefore pays a wakeup per
// write. The buffer lets Write copy into stream-owned storage and return, so
// a burst of small writes costs one wakeup instead of one per write.
//
// Three places can hold unsent stream data, and they are drained in this
// order, which is also age order:
//
//	nextFrame      staged by a blocking write, oldest
//	writeBuffer    appended by the buffered fast path
//	dataForWriting the write currently blocked, newest
//
// Two rules keep that order true. The fast path appends only while
// dataForWriting is nil, so its bytes cannot land between an older write's
// staged bytes and the rest of that write. And both sites that extend
// nextFrame, canBufferStreamFrame and tryWriteAll, refuse while the buffer
// holds data, so neither can put bytes in nextFrame that are newer than
// buffered ones.

const (
	// sendStreamWriteBufferSize is the initial write buffer limit.
	sendStreamWriteBufferSize = 4096
	// sendStreamWriteBufferMaxSize caps the demand-grown write buffer limit.
	// Only streams under sustained full-buffer pressure reach it; it bounds
	// the worst-case buffered-unsent bytes on cancel.
	sendStreamWriteBufferMaxSize = 65536
	// maxBufferedWriteSize is the largest single write that buffers (and
	// grows the buffer). Larger writes keep the blocking path, whose frames
	// copy straight from the caller's slice — routing them through the
	// buffer would add a full extra copy per write.
	maxBufferedWriteSize = sendStreamWriteBufferMaxSize / 4
)

func ( *SendStream) () int {
	return len(.writeBuffer) - .writeBufferHead
}

// writeBufferLimitLocked returns the current demand-grown buffer limit
// without growing it. The caller holds s.mutex.
func ( *SendStream) () int {
	if .writeBufferLimit == 0 {
		return sendStreamWriteBufferSize
	}
	return .writeBufferLimit
}

// growWriteBufferFor reports whether n more bytes fit the write buffer,
// doubling the demand-grown limit toward the cap when they do not. Growth
// happens only here, on the write path, never on the drain side. The caller
// holds s.mutex.
func ( *SendStream) ( int) bool {
	 := .writeBufferLimitLocked()
	 := .bufferedWriteLen() + 
	for  >  &&  < sendStreamWriteBufferMaxSize {
		 *= 2
	}
	.writeBufferLimit = 
	return  <= 
}

func ( *SendStream) ( []byte) {
	if .writeBuffer == nil {
		.writeBuffer = make([]byte, 0, sendStreamWriteBufferSize)
	}
	if cap(.writeBuffer)-len(.writeBuffer) < len() {
		copy(.writeBuffer, .writeBuffer[.writeBufferHead:])
		.writeBuffer = .writeBuffer[:.bufferedWriteLen()]
		.writeBufferHead = 0
	}
	.writeBuffer = append(.writeBuffer, ...)
}

// dropWriteBuffer discards buffered data. The caller holds s.mutex.
func ( *SendStream) () {
	.writeBuffer = nil
	.writeBufferHead = 0
}

// takeWriteBuffer copies up to maxBytes buffered bytes into f, reporting how
// many it took. The caller holds s.mutex.
func ( *SendStream) ( []byte) int {
	 := min(.bufferedWriteLen(), len())
	if  == 0 {
		return 0
	}
	copy(, .writeBuffer[.writeBufferHead:.writeBufferHead+])
	.writeBufferHead += 
	if .writeBufferHead == len(.writeBuffer) {
		.writeBuffer = .writeBuffer[:0]
		.writeBufferHead = 0
	}
	return 
}

// bufferStartOffset is the stream offset of the first buffered byte. The
// caller holds s.mutex.
func ( *SendStream) () protocol.ByteCount {
	 := .writeOffset
	if .nextFrame != nil {
		 += .nextFrame.DataLen()
	}
	return 
}