space -= MCLBYTES;
space -= len;
sosend(so, addr, uio, top, control, flags)	register struct socket *so;	struct mbuf *addr;	struct uio *uio;	struct mbuf *top;	struct mbuf *control;	int flags;{	struct proc *p = curproc;		/* XXX */	struct mbuf **mp;	register struct mbuf *m;	register long space, len, resid;	int clen = 0, error, s, dontroute, mlen;	int atomic = sosendallatonce(so) || top;	if (uio)		resid = uio->uio_resid;	else		resid = top->m_pkthdr.len;	/*	 * In theory resid should be unsigned.	 * However, space must be signed, as it might be less than 0	 * if we over-committed, and we must use a signed comparison	 * of space and resid.  On the other hand, a negative resid	 * causes us to loop sending 0-length segments to the protocol.	 */	if (resid < 0)		return (EINVAL);	dontroute =	    (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&	    (so->so_proto->pr_flags & PR_ATOMIC);	p->p_stats->p_ru.ru_msgsnd++;	if (control)		clen = control->m_len;#define	snderr(errno)	{ error = errno; splx(s); goto release; }restart:	if ((error = sblock(&so->so_snd, SBLOCKWAIT(flags))) != 0)		goto out;	do {		s = splsoftnet();		if (so->so_state & SS_CANTSENDMORE)			snderr(EPIPE);		if (so->so_error)			snderr(so->so_error);		if ((so->so_state & SS_ISCONNECTED) == 0) {			if (so->so_proto->pr_flags & PR_CONNREQUIRED) {				if ((so->so_state & SS_ISCONFIRMING) == 0 &&				    !(resid == 0 && clen != 0))					snderr(ENOTCONN);			} else if (addr == 0)				snderr(EDESTADDRREQ);		}		space = sbspace(&so->so_snd);		if (flags & MSG_OOB)			space += 1024;		if ((atomic && resid > so->so_snd.sb_hiwat) ||		    clen > so->so_snd.sb_hiwat)			snderr(EMSGSIZE);		if (space < resid + clen && uio &&		    (atomic || space < so->so_snd.sb_lowat || space < clen)) {			if (so->so_state & SS_NBIO)				snderr(EWOULDBLOCK);			sbunlock(&so->so_snd);			error = sbwait(&so->so_snd);			splx(s);			if (error)				goto out;			goto restart;		}		splx(s);		mp = &top;		space -= clen;		do {		    if (uio == NULL) {			/*			 * Data is prepackaged in "top".			 */			resid = 0;			if (flags & MSG_EOR)				top->m_flags |= M_EOR;		    } else do {			if (top == 0) {				MGETHDR(m, M_WAIT, MT_DATA);				mlen = MHLEN;				m->m_pkthdr.len = 0;				m->m_pkthdr.rcvif = (struct ifnet *)0;			} else {				MGET(m, M_WAIT, MT_DATA);				mlen = MLEN;			}			if (resid >= MINCLSIZE && space >= MCLBYTES) {				MCLGET(m, M_WAIT);				if ((m->m_flags & M_EXT) == 0)					goto nopages;				mlen = MCLBYTES;#ifdef	MAPPED_MBUFS				len = min(MCLBYTES, resid);#else				if (atomic && top == 0) {					len = min(MCLBYTES - max_hdr, resid);					m->m_data += max_hdr;				} else					len = min(MCLBYTES, resid);#endif				space -= MCLBYTES;			} else {nopages:				len = min(min(mlen, resid), space);				space -= len;				/*				 * For datagram protocols, leave room				 * for protocol headers in first mbuf.				 */				if (atomic && top == 0 && len < mlen)					MH_ALIGN(m, len);			}			error = uiomove(mtod(m, caddr_t), (int)len, uio);			resid = uio->uio_resid;			m->m_len = len;			*mp = m;			top->m_pkthdr.len += len;			if (error)				goto release;			mp = &m->m_next;			if (resid <= 0) {				if (flags & MSG_EOR)					top->m_flags |= M_EOR;				break;			}		    } while (space > 0 && atomic);		    if (dontroute)			    so->so_options |= SO_DONTROUTE;		    s = splsoftnet();				/* XXX */		    error = (*so->so_proto->pr_usrreq)(so, (flags & MSG_OOB) ?							PRU_SENDOOB : PRU_SEND,						       top, addr, control);		    splx(s);		    if (dontroute)			    so->so_options &= ~SO_DONTROUTE;		    clen = 0;		    control = 0;		    top = 0;		    mp = &top;		    if (error)			goto release;		} while (resid && space > 0);	} while (resid);release:	sbunlock(&so->so_snd);out:	if (top)		m_freem(top);	if (control)		m_freem(control);	return (error);}