shl: ring: provide offset parameter for shl_ring_peek()
authorDavid Herrmann <dh.herrmann@googlemail.com>
Thu, 25 Oct 2012 12:47:32 +0000 (14:47 +0200)
committerDavid Herrmann <dh.herrmann@googlemail.com>
Thu, 25 Oct 2012 12:47:32 +0000 (14:47 +0200)
If we want to fill a whole buffer, we actually might have to read more
data than just the data from the beginning. Therefore, provide an offset
so we can read from multiple pages.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
src/pty.c
src/shl_ring.h

index 61d6b6c..bcd7e16 100644 (file)
--- a/src/pty.c
+++ b/src/pty.c
@@ -367,7 +367,7 @@ static int send_buf(struct kmscon_pty *pty)
        size_t len;
        int ret;
 
-       while ((buf = shl_ring_peek(pty->msgbuf, &len))) {
+       while ((buf = shl_ring_peek(pty->msgbuf, &len, 0))) {
                ret = write(pty->fd, buf, len);
                if (ret > 0) {
                        shl_ring_drop(pty->msgbuf, ret);
index d668ab6..5e6622f 100644 (file)
@@ -131,13 +131,30 @@ next:
        return 0;
 }
 
-static inline const char *shl_ring_peek(struct shl_ring *ring, size_t *len)
+static inline const char *shl_ring_peek(struct shl_ring *ring, size_t *len,
+                                       size_t offset)
 {
-       if (!ring || !ring->first)
+       struct shl_ring_entry *iter;
+
+       if (!ring || !ring->first || !len) {
+               if (len)
+                       *len = 0;
                return NULL;
+       }
+
+       iter = ring->first;
+       while (iter->len <= offset) {
+               if (!iter->next) {
+                       *len = 0;
+                       return NULL;
+               }
+
+               offset -= iter->len;
+               iter = iter->next;
+       }
 
-       *len = ring->first->len;
-       return ring->first->buf;
+       *len = ring->first->len - offset;
+       return &ring->first->buf[offset];
 }
 
 static inline void shl_ring_drop(struct shl_ring *ring, size_t len)