From 150fba5ecdb3fedd8aed71ee65fb8cc6b0354070 Mon Sep 17 00:00:00 2001 From: Tiago Cunha Date: Tue, 15 Sep 2009 23:59:40 +0000 Subject: [PATCH] Sync OpenBSD patchset 329: Enclose repeated buffer draining code in a new msgbuf_drain() function, which is additionally exported for use by others. From nicm@, who reminded me that tmux is now using buffer.c, too. --- compat/imsg-buffer.c | 50 +++++++++++++++++++++----------------------- compat/imsg.h | 5 +++-- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/compat/imsg-buffer.c b/compat/imsg-buffer.c index 41cf0346..ebfd3ab7 100644 --- a/compat/imsg-buffer.c +++ b/compat/imsg-buffer.c @@ -1,5 +1,5 @@ -/* $Id: imsg-buffer.c,v 1.3 2009-08-20 12:54:08 nicm Exp $ */ -/* $OpenBSD: imsg-buffer.c,v 1.1 2009/08/11 17:18:35 nicm Exp $ */ +/* $Id: imsg-buffer.c,v 1.4 2009-09-15 23:59:40 tcunha Exp $ */ +/* $OpenBSD: imsg-buffer.c,v 1.2 2009/09/15 18:12:51 jacekm Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -144,7 +144,7 @@ int buf_write(struct msgbuf *msgbuf) { struct iovec iov[IOV_MAX]; - struct buf *buf, *next; + struct buf *buf; unsigned int i = 0; ssize_t n; @@ -170,17 +170,7 @@ buf_write(struct msgbuf *msgbuf) return (-2); } - for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0; - buf = next) { - next = TAILQ_NEXT(buf, entry); - if (buf->rpos + n >= buf->wpos) { - n -= buf->wpos - buf->rpos; - buf_dequeue(msgbuf, buf); - } else { - buf->rpos += n; - n = 0; - } - } + msgbuf_drain(msgbuf, n); return (0); } @@ -200,6 +190,24 @@ msgbuf_init(struct msgbuf *msgbuf) TAILQ_INIT(&msgbuf->bufs); } +void +msgbuf_drain(struct msgbuf *msgbuf, size_t n) +{ + struct buf *buf, *next; + + for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0; + buf = next) { + next = TAILQ_NEXT(buf, entry); + if (buf->rpos + n >= buf->wpos) { + n -= buf->wpos - buf->rpos; + buf_dequeue(msgbuf, buf); + } else { + buf->rpos += n; + n = 0; + } + } +} + void msgbuf_clear(struct msgbuf *msgbuf) { @@ -213,7 +221,7 @@ int msgbuf_write(struct msgbuf *msgbuf) { struct iovec iov[IOV_MAX]; - struct buf *buf, *next; + struct buf *buf; unsigned int i = 0; ssize_t n; struct msghdr msg; @@ -270,17 +278,7 @@ msgbuf_write(struct msgbuf *msgbuf) buf->fd = -1; } - for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0; - buf = next) { - next = TAILQ_NEXT(buf, entry); - if (buf->rpos + n >= buf->wpos) { - n -= buf->wpos - buf->rpos; - buf_dequeue(msgbuf, buf); - } else { - buf->rpos += n; - n = 0; - } - } + msgbuf_drain(msgbuf, n); return (0); } diff --git a/compat/imsg.h b/compat/imsg.h index 2ec9937a..6ff4901c 100644 --- a/compat/imsg.h +++ b/compat/imsg.h @@ -1,5 +1,5 @@ -/* $Id: imsg.h,v 1.3 2009-08-20 12:54:08 nicm Exp $ */ -/* $OpenBSD: imsg.h,v 1.1 2009/08/11 17:18:35 nicm Exp $ */ +/* $Id: imsg.h,v 1.4 2009-09-15 23:59:40 tcunha Exp $ */ +/* $OpenBSD: imsg.h,v 1.2 2009/09/15 18:12:51 jacekm Exp $ */ /* * Copyright (c) 2006, 2007 Pierre-Yves Ritschard @@ -91,6 +91,7 @@ void buf_free(struct buf *); void msgbuf_init(struct msgbuf *); void msgbuf_clear(struct msgbuf *); int msgbuf_write(struct msgbuf *); +void msgbuf_drain(struct msgbuf *, size_t); /* imsg.c */ void imsg_init(struct imsgbuf *, int);