[Tarantool-patches] [PATCH 1/4] xrow: rework coio_read_xrow to keep parsed input

Serge Petrenko sergepetrenko at tarantool.org
Mon Dec 6 06:05:59 MSK 2021


Here's the corresponding change in libsmall:

Subject: [PATCH] ibuf: introduce xpos field

ibuf->xpos is the pointer to the not-yet-parsed input, so that
rpos <= xpos <= wpos. This is useful when the user processes input in
multiple stages, and needs the input data for a longer period of time than
just "until the data is read".
---
https://github.com/tarantool/small/tree/sp/ibuf-xpos

include/small/ibuf.h | 10 +++++++++-
  small/ibuf.c         |  4 +++-
  2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/include/small/ibuf.h b/include/small/ibuf.h
index 00d7781..a6646b5 100644
--- a/include/small/ibuf.h
+++ b/include/small/ibuf.h
@@ -61,6 +61,8 @@ struct ibuf
      char *buf;
      /** Start of input. */
      char *rpos;
+    /** End of parsed input. */
+    char *xpos;
      /** End of useful input */
      char *wpos;
      /** End of buffer. */
@@ -100,6 +102,12 @@ ibuf_capacity(struct ibuf *ibuf)
      return ibuf->end - ibuf->buf;
  }

+static inline size_t
+ibuf_unparsed(struct ibuf *ibuf)
+{
+    return ibuf->wpos - ibuf->xpos;
+}
+
  /**
   * Integer value of the position in the buffer - stable
   * in case of realloc.
@@ -115,7 +123,7 @@ ibuf_pos(struct ibuf *ibuf)
  static inline void
  ibuf_reset(struct ibuf *ibuf)
  {
-    ibuf->rpos = ibuf->wpos = ibuf->buf;
+    ibuf->rpos = ibuf->xpos = ibuf->wpos = ibuf->buf;
  }

  void *
diff --git a/small/ibuf.c b/small/ibuf.c
index fe3f441..85345f4 100644
--- a/small/ibuf.c
+++ b/small/ibuf.c
@@ -37,7 +37,7 @@ void
  ibuf_create(struct ibuf *ibuf, struct slab_cache *slabc, size_t 
start_capacity)
  {
      ibuf->slabc = slabc;
-    ibuf->buf = ibuf->rpos = ibuf->wpos = ibuf->end = NULL;
+    ibuf->buf = ibuf->rpos = ibuf->xpos = ibuf->wpos = ibuf->end = NULL;
      ibuf->start_capacity = start_capacity;
      /* Don't allocate the buffer yet. */
  }
@@ -99,7 +99,9 @@ ibuf_reserve_slow(struct ibuf *ibuf, size_t size)
          ibuf->buf = ptr;
          ibuf->end = ibuf->buf + slab_capacity(slab);
      }
+    size_t xsize = ibuf->xpos - ibuf->rpos;
      ibuf->rpos = ibuf->buf;
+    ibuf->xpos = ibuf->rpos + xsize;
      ibuf->wpos = ibuf->rpos + used;
      return ibuf->wpos;
  }
-- 
2.30.1 (Apple Git-130)


-- 
Serge Petrenko



More information about the Tarantool-patches mailing list