aboutsummaryrefslogtreecommitdiff
path: root/imsg-buffer.c
diff options
context:
space:
mode:
authorJacek Masiulaniec <jacekm@openbsd.org>2009-09-15 18:12:51 +0000
committerJacek Masiulaniec <jacekm@openbsd.org>2009-09-15 18:12:51 +0000
commita6dd9e8e7eb0d7734b0905a97f6e8b0087a2e09c (patch)
treef61cb760bd6842b73988ebf8d92e3f535f318b8d /imsg-buffer.c
parent427819910184a7174391c2e688da6b89634045ec (diff)
downloadrtmux-a6dd9e8e7eb0d7734b0905a97f6e8b0087a2e09c.tar.gz
rtmux-a6dd9e8e7eb0d7734b0905a97f6e8b0087a2e09c.tar.bz2
rtmux-a6dd9e8e7eb0d7734b0905a97f6e8b0087a2e09c.zip
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.
Diffstat (limited to 'imsg-buffer.c')
-rw-r--r--imsg-buffer.c46
1 files changed, 22 insertions, 24 deletions
diff --git a/imsg-buffer.c b/imsg-buffer.c
index 06fe0b1a..613220d4 100644
--- a/imsg-buffer.c
+++ b/imsg-buffer.c
@@ -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);
}
@@ -201,6 +191,24 @@ msgbuf_init(struct msgbuf *msgbuf)
}
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)
{
struct buf *buf;
@@ -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);
}