]> granicus.if.org Git - libevent/commitdiff
Add a "flags" field to evbuffer callbacks.
authorNick Mathewson <nickm@torproject.org>
Fri, 23 Jan 2009 18:04:34 +0000 (18:04 +0000)
committerNick Mathewson <nickm@torproject.org>
Fri, 23 Jan 2009 18:04:34 +0000 (18:04 +0000)
For now, there is just one: enabled.  This lets us avoid lots of
mallocs/frees/tailq-manipulations just to turn a callback on and off.
The revised bufferevent code wants this.

svn:r1047

buffer.c
evbuffer-internal.h
include/event2/buffer.h

index ee930bf0bdf1724fb291c8d0897b1f1d2f3374e4..029ea988ffe1f589c57319cb0f79aae3d2d550cb 100644 (file)
--- a/buffer.c
+++ b/buffer.c
@@ -115,15 +115,20 @@ evbuffer_new(void)
 static inline void
 evbuffer_invoke_callbacks(struct evbuffer *buffer, size_t old_size)
 {
+       struct evbuffer_cb_entry *cbent, *next;
        size_t new_size = buffer->total_len;
-       if (!TAILQ_EMPTY(&buffer->callbacks) && old_size != new_size) {
-               struct evbuffer_cb_entry *cbent, *next;
-               for (cbent = TAILQ_FIRST(&buffer->callbacks);
-                        cbent != TAILQ_END(&buffer->callbacks);
-                        cbent = next) {
-                       next = TAILQ_NEXT(cbent, next);
+       if (TAILQ_EMPTY(&buffer->callbacks) || old_size == new_size)
+               return;
+
+
+       for (cbent = TAILQ_FIRST(&buffer->callbacks);
+            cbent != TAILQ_END(&buffer->callbacks);
+            cbent = next) {
+               /* Get the 'next' pointer now in case this callback decides
+                * to remove itself or something. */
+               next = TAILQ_NEXT(cbent, next);
+               if ((cbent->flags & EVBUFFER_CB_ENABLED))
                        cbent->cb(buffer, old_size, new_size, cbent->cbarg);
-               }
        }
 }
 
@@ -1231,6 +1236,7 @@ evbuffer_add_cb(struct evbuffer *buffer, evbuffer_cb cb, void *cbarg)
                return NULL;
        e->cb = cb;
        e->cbarg = cbarg;
+       e->flags = EVBUFFER_CB_ENABLED;
        TAILQ_INSERT_HEAD(&buffer->callbacks, e, next);
        return e;
 }
@@ -1255,3 +1261,13 @@ evbuffer_remove_cb(struct evbuffer *buffer, evbuffer_cb cb, void *cbarg)
        }
        return -1;
 }
+
+int
+evbuffer_cb_set_flags(struct evbuffer *buffer,
+                     struct evbuffer_cb_entry *cb, unsigned flags)
+{
+       (void)buffer; /* unused */
+       cb->flags = flags;
+       return 0;
+}
+
index bd4130e6fbde458aa78575e9b409c856d5895437..0659b409de39f58dd15c2337dec0b087030d1655 100644 (file)
@@ -42,6 +42,7 @@ struct evbuffer_cb_entry {
        TAILQ_ENTRY(evbuffer_cb_entry) next;
        evbuffer_cb cb;
        void *cbarg;
+       unsigned flags;
 };
 
 struct evbuffer_chain;
index 00691fea6d32e1c472539d49927f0775eec4f5a1..343da75adf0d7b732f34ff6cb29587942290b982 100644 (file)
@@ -374,6 +374,19 @@ int evbuffer_remove_cb_entry(struct evbuffer *buffer,
  */
 int evbuffer_remove_cb(struct evbuffer *buffer, evbuffer_cb cb, void *cbarg);
 
+#define EVBUFFER_CB_ENABLED 1
+/** Change whether a given callback is enabled on a buffer or not.  A
+    disabled callback is not invoked even when the buffer size changes.
+
+    @param buffer the evbuffer that the callback is watching.
+    @param cb the callback whose status we want to change.
+    @param flags EVBUFFER_CB_ENABLED to enable the callback, or 0 to
+        disable it.
+    @return 0 on success, -1 on failure.
+ */
+int evbuffer_cb_set_flags(struct evbuffer *buffer,
+                         struct evbuffer_cb_entry *cb, unsigned flags);
+
 /**
   Makes the data at the begging of an evbuffer contiguous.