about summary refs log tree commit homepage
path: root/alloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'alloc.c')
-rw-r--r--alloc.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/alloc.c b/alloc.c
index bddad4d..7a14173 100644
--- a/alloc.c
+++ b/alloc.c
@@ -193,3 +193,30 @@ void *mog_fsbuf_get(size_t *size)
 
         return ptr;
 }
+
+/*
+ * attempts to reattach an rbuf belonging to a previously-idle client
+ * if it makes sense to reattach.
+ *
+ * We want to favor rbufs attached to clients if they are bigger than
+ * the thread-local one.
+ */
+void mog_rbuf_reattach_and_null(struct mog_rbuf **ptrptr)
+{
+        struct mog_rbuf *rbuf = *ptrptr;
+
+        if (!rbuf)
+                return;
+        *ptrptr = NULL;
+
+        assert(rbuf != tls_rbuf && "cannot reattach, already attached");
+        if (tls_rbuf) {
+                /* we never want to swap a small buffer for a big buffer */
+                if (rbuf->rcapa < tls_rbuf->rcapa) {
+                        mog_rbuf_free(rbuf);
+                        return;
+                }
+                free(tls_rbuf);
+        }
+        tls_rbuf = rbuf;
+}