Revert "Revert "Upgrade to 2.1.8-stable (2017-01-22)." and "Probably Mac build fix?""

This reverts commit 83a0c9c65a60a92d3ea5542596b3ba56db492c37.

Bug: 64543673
Test: make checkbuild
Test: Manual tombstoned test
Change-Id: I84bb128d1dec433195f2cbdbf70236ba17fa9955
diff --git a/include/event2/bufferevent.h b/include/event2/bufferevent.h
index 0cc6cb1..825918e 100644
--- a/include/event2/bufferevent.h
+++ b/include/event2/bufferevent.h
@@ -24,8 +24,8 @@
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
-#ifndef _EVENT2_BUFFEREVENT_H_
-#define _EVENT2_BUFFEREVENT_H_
+#ifndef EVENT2_BUFFEREVENT_H_INCLUDED_
+#define EVENT2_BUFFEREVENT_H_INCLUDED_
 
 /**
    @file event2/bufferevent.h
@@ -74,15 +74,17 @@
   </dl>
  */
 
+#include <event2/visibility.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
 
 #include <event2/event-config.h>
-#ifdef _EVENT_HAVE_SYS_TYPES_H
+#ifdef EVENT__HAVE_SYS_TYPES_H
 #include <sys/types.h>
 #endif
-#ifdef _EVENT_HAVE_SYS_TIME_H
+#ifdef EVENT__HAVE_SYS_TIME_H
 #include <sys/time.h>
 #endif
 
@@ -109,7 +111,7 @@
    @see event2/bufferevent.h
  */
 struct bufferevent
-#ifdef _EVENT_IN_DOXYGEN
+#ifdef EVENT_IN_DOXYGEN_
 {}
 #endif
 ;
@@ -138,6 +140,9 @@
    The event callback is triggered if either an EOF condition or another
    unrecoverable error was encountered.
 
+   For bufferevents with deferred callbacks, this is a bitwise OR of all errors
+   that have happened on the bufferevent since the last callback invocation.
+
    @param bev the bufferevent for which the error condition was reached
    @param what a conjunction of flags: BEV_EVENT_READING or BEV_EVENT_WRITING
 	  to indicate if the error was encountered on the read or write path,
@@ -181,6 +186,7 @@
 	  error occurred
   @see bufferevent_free()
   */
+EVENT2_EXPORT_SYMBOL
 struct bufferevent *bufferevent_socket_new(struct event_base *base, evutil_socket_t fd, int options);
 
 /**
@@ -202,7 +208,8 @@
    @param socklen The length of the address
    @return 0 on success, -1 on failure.
  */
-int bufferevent_socket_connect(struct bufferevent *, struct sockaddr *, int);
+EVENT2_EXPORT_SYMBOL
+int bufferevent_socket_connect(struct bufferevent *, const struct sockaddr *, int);
 
 struct evdns_base;
 /**
@@ -231,6 +238,7 @@
    may block while it waits for a DNS response.	 This is probably not
    what you want.
  */
+EVENT2_EXPORT_SYMBOL
 int bufferevent_socket_connect_hostname(struct bufferevent *,
     struct evdns_base *, int, const char *, int);
 
@@ -242,6 +250,7 @@
    @return DNS error code.
    @see evutil_gai_strerror()
 */
+EVENT2_EXPORT_SYMBOL
 int bufferevent_socket_get_dns_error(struct bufferevent *bev);
 
 /**
@@ -255,11 +264,13 @@
   @return 0 if successful, or -1 if an error occurred
   @see bufferevent_new()
  */
+EVENT2_EXPORT_SYMBOL
 int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev);
 
 /**
    Return the event_base used by a bufferevent
 */
+EVENT2_EXPORT_SYMBOL
 struct event_base *bufferevent_get_base(struct bufferevent *bev);
 
 /**
@@ -271,14 +282,26 @@
   @param pri the priority to be assigned
   @return 0 if successful, or -1 if an error occurred
   */
+EVENT2_EXPORT_SYMBOL
 int bufferevent_priority_set(struct bufferevent *bufev, int pri);
 
+/**
+   Return the priority of a bufferevent.
+
+   Only supported for socket bufferevents
+ */
+EVENT2_EXPORT_SYMBOL
+int bufferevent_get_priority(const struct bufferevent *bufev);
 
 /**
   Deallocate the storage associated with a bufferevent structure.
 
+  If there is pending data to write on the bufferevent, it probably won't be
+  flushed before the bufferevent is freed.
+
   @param bufev the bufferevent structure to be freed.
   */
+EVENT2_EXPORT_SYMBOL
 void bufferevent_free(struct bufferevent *bufev);
 
 
@@ -296,29 +319,54 @@
 	 (readcb, writecb, and errorcb)
   @see bufferevent_new()
   */
+EVENT2_EXPORT_SYMBOL
 void bufferevent_setcb(struct bufferevent *bufev,
     bufferevent_data_cb readcb, bufferevent_data_cb writecb,
     bufferevent_event_cb eventcb, void *cbarg);
 
 /**
+ Retrieves the callbacks for a bufferevent.
+
+ @param bufev the bufferevent to examine.
+ @param readcb_ptr if readcb_ptr is nonnull, *readcb_ptr is set to the current
+    read callback for the bufferevent.
+ @param writecb_ptr if writecb_ptr is nonnull, *writecb_ptr is set to the
+    current write callback for the bufferevent.
+ @param eventcb_ptr if eventcb_ptr is nonnull, *eventcb_ptr is set to the
+    current event callback for the bufferevent.
+ @param cbarg_ptr if cbarg_ptr is nonnull, *cbarg_ptr is set to the current
+    callback argument for the bufferevent.
+ @see buffervent_setcb()
+*/
+EVENT2_EXPORT_SYMBOL
+void bufferevent_getcb(struct bufferevent *bufev,
+    bufferevent_data_cb *readcb_ptr,
+    bufferevent_data_cb *writecb_ptr,
+    bufferevent_event_cb *eventcb_ptr,
+    void **cbarg_ptr);
+
+/**
   Changes the file descriptor on which the bufferevent operates.
   Not supported for all bufferevent types.
 
   @param bufev the bufferevent object for which to change the file descriptor
   @param fd the file descriptor to operate on
 */
+EVENT2_EXPORT_SYMBOL
 int bufferevent_setfd(struct bufferevent *bufev, evutil_socket_t fd);
 
 /**
    Returns the file descriptor associated with a bufferevent, or -1 if
    no file descriptor is associated with the bufferevent.
  */
+EVENT2_EXPORT_SYMBOL
 evutil_socket_t bufferevent_getfd(struct bufferevent *bufev);
 
 /**
    Returns the underlying bufferevent associated with a bufferevent (if
    the bufferevent is a wrapper), or NULL if there is no underlying bufferevent.
  */
+EVENT2_EXPORT_SYMBOL
 struct bufferevent *bufferevent_get_underlying(struct bufferevent *bufev);
 
 /**
@@ -334,6 +382,7 @@
   @return 0 if successful, or -1 if an error occurred
   @see bufferevent_write_buffer()
   */
+EVENT2_EXPORT_SYMBOL
 int bufferevent_write(struct bufferevent *bufev,
     const void *data, size_t size);
 
@@ -347,6 +396,7 @@
   @return 0 if successful, or -1 if an error occurred
   @see bufferevent_write()
  */
+EVENT2_EXPORT_SYMBOL
 int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf);
 
 
@@ -360,6 +410,7 @@
   @param size the size of the data buffer, in bytes
   @return the amount of data read, in bytes.
  */
+EVENT2_EXPORT_SYMBOL
 size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size);
 
 /**
@@ -370,6 +421,7 @@
   @param buf the evbuffer to which to add data
   @return 0 if successful, or -1 if an error occurred.
  */
+EVENT2_EXPORT_SYMBOL
 int bufferevent_read_buffer(struct bufferevent *bufev, struct evbuffer *buf);
 
 /**
@@ -381,6 +433,7 @@
    @return the evbuffer object for the input buffer
  */
 
+EVENT2_EXPORT_SYMBOL
 struct evbuffer *bufferevent_get_input(struct bufferevent *bufev);
 
 /**
@@ -395,6 +448,7 @@
    @return the evbuffer object for the output buffer
  */
 
+EVENT2_EXPORT_SYMBOL
 struct evbuffer *bufferevent_get_output(struct bufferevent *bufev);
 
 /**
@@ -405,6 +459,7 @@
   @return 0 if successful, or -1 if an error occurred
   @see bufferevent_disable()
  */
+EVENT2_EXPORT_SYMBOL
 int bufferevent_enable(struct bufferevent *bufev, short event);
 
 /**
@@ -415,6 +470,7 @@
   @return 0 if successful, or -1 if an error occurred
   @see bufferevent_enable()
  */
+EVENT2_EXPORT_SYMBOL
 int bufferevent_disable(struct bufferevent *bufev, short event);
 
 /**
@@ -423,6 +479,7 @@
    @param bufev the bufferevent to inspect
    @return A combination of EV_READ | EV_WRITE
  */
+EVENT2_EXPORT_SYMBOL
 short bufferevent_get_enabled(struct bufferevent *bufev);
 
 /**
@@ -451,6 +508,7 @@
   @param timeout_read the read timeout, or NULL
   @param timeout_write the write timeout, or NULL
  */
+EVENT2_EXPORT_SYMBOL
 int bufferevent_set_timeouts(struct bufferevent *bufev,
     const struct timeval *timeout_read, const struct timeval *timeout_write);
 
@@ -472,21 +530,64 @@
   @param highmark the high watermark to set
 */
 
+EVENT2_EXPORT_SYMBOL
 void bufferevent_setwatermark(struct bufferevent *bufev, short events,
     size_t lowmark, size_t highmark);
 
 /**
+  Retrieves the watermarks for read or write events.
+  Returns non-zero if events contains not only EV_READ or EV_WRITE.
+  Returns zero if events equal EV_READ or EV_WRITE
+
+  @param bufev the bufferevent to be examined
+  @param events EV_READ or EV_WRITE
+  @param lowmark receives the lower watermark if not NULL
+  @param highmark receives the high watermark if not NULL
+*/
+EVENT2_EXPORT_SYMBOL
+int bufferevent_getwatermark(struct bufferevent *bufev, short events,
+    size_t *lowmark, size_t *highmark);
+
+/**
    Acquire the lock on a bufferevent.  Has no effect if locking was not
    enabled with BEV_OPT_THREADSAFE.
  */
+EVENT2_EXPORT_SYMBOL
 void bufferevent_lock(struct bufferevent *bufev);
 
 /**
    Release the lock on a bufferevent.  Has no effect if locking was not
    enabled with BEV_OPT_THREADSAFE.
  */
+EVENT2_EXPORT_SYMBOL
 void bufferevent_unlock(struct bufferevent *bufev);
 
+
+/**
+ * Public interface to manually increase the reference count of a bufferevent
+ * this is useful in situations where a user may reference the bufferevent
+ * somewhere eles (unknown to libevent)
+ *
+ * @param bufev the bufferevent to increase the refcount on
+ *
+ */
+EVENT2_EXPORT_SYMBOL
+void bufferevent_incref(struct bufferevent *bufev);
+
+/**
+ * Public interface to manually decrement the reference count of a bufferevent
+ *
+ * Warning: make sure you know what you're doing. This is mainly used in
+ * conjunction with bufferevent_incref(). This will free up all data associated
+ * with a bufferevent if the reference count hits 0.
+ *
+ * @param bufev the bufferevent to decrement the refcount on
+ *
+ * @return 1 if the bufferevent was freed, otherwise 0 (still referenced)
+ */
+EVENT2_EXPORT_SYMBOL
+int bufferevent_decref(struct bufferevent *bufev);
+
 /**
    Flags that can be passed into filters to let them know how to
    deal with the incoming data.
@@ -510,11 +611,55 @@
    @param mode either BEV_NORMAL or BEV_FLUSH or BEV_FINISHED
    @return -1 on failure, 0 if no data was produces, 1 if data was produced
  */
+EVENT2_EXPORT_SYMBOL
 int bufferevent_flush(struct bufferevent *bufev,
     short iotype,
     enum bufferevent_flush_mode mode);
 
 /**
+   Flags for bufferevent_trigger(_event) that modify when and how to trigger
+   the callback.
+*/
+enum bufferevent_trigger_options {
+	/** trigger the callback regardless of the watermarks */
+	BEV_TRIG_IGNORE_WATERMARKS = (1<<16),
+
+	/** defer even if the callbacks are not */
+	BEV_TRIG_DEFER_CALLBACKS = BEV_OPT_DEFER_CALLBACKS
+
+	/* (Note: for internal reasons, these need to be disjoint from
+	 * bufferevent_options, except when they mean the same thing. */
+};
+
+/**
+   Triggers bufferevent data callbacks.
+
+   The function will honor watermarks unless options contain
+   BEV_TRIG_IGNORE_WATERMARKS. If the options contain BEV_OPT_DEFER_CALLBACKS,
+   the callbacks are deferred.
+
+   @param bufev the bufferevent object
+   @param iotype either EV_READ or EV_WRITE or both.
+   @param options
+ */
+EVENT2_EXPORT_SYMBOL
+void bufferevent_trigger(struct bufferevent *bufev, short iotype,
+    int options);
+
+/**
+   Triggers the bufferevent event callback.
+
+   If the options contain BEV_OPT_DEFER_CALLBACKS, the callbacks are deferred.
+
+   @param bufev the bufferevent object
+   @param what the flags to pass onto the event callback
+   @param options
+ */
+EVENT2_EXPORT_SYMBOL
+void bufferevent_trigger_event(struct bufferevent *bufev, short what,
+    int options);
+
+/**
    @name Filtering support
 
    @{
@@ -569,6 +714,7 @@
      this bufferevent is freed.
    @param ctx A context pointer to pass to the filter functions.
  */
+EVENT2_EXPORT_SYMBOL
 struct bufferevent *
 bufferevent_filter_new(struct bufferevent *underlying,
 		       bufferevent_filter_cb input_filter,
@@ -588,6 +734,7 @@
    @param pair A pointer to an array to hold the two new bufferevent objects.
    @return 0 on success, -1 on failure.
  */
+EVENT2_EXPORT_SYMBOL
 int bufferevent_pair_new(struct event_base *base, int options,
     struct bufferevent *pair[2]);
 
@@ -595,6 +742,7 @@
    Given one bufferevent returned by bufferevent_pair_new(), returns the
    other one if it still exists.  Otherwise returns NULL.
  */
+EVENT2_EXPORT_SYMBOL
 struct bufferevent *bufferevent_pair_get_partner(struct bufferevent *bev);
 
 /**
@@ -628,6 +776,7 @@
    Note that all rate-limits hare are currently best-effort: future versions
    of Libevent may implement them more tightly.
  */
+EVENT2_EXPORT_SYMBOL
 struct ev_token_bucket_cfg *ev_token_bucket_cfg_new(
 	size_t read_rate, size_t read_burst,
 	size_t write_rate, size_t write_burst,
@@ -638,6 +787,7 @@
     Note: 'cfg' is not currently reference-counted; it is not safe to free it
     until no bufferevent is using it.
  */
+EVENT2_EXPORT_SYMBOL
 void ev_token_bucket_cfg_free(struct ev_token_bucket_cfg *cfg);
 
 /**
@@ -651,6 +801,7 @@
 
    Return 0 on sucess, -1 on failure.
  */
+EVENT2_EXPORT_SYMBOL
 int bufferevent_set_rate_limit(struct bufferevent *bev,
     struct ev_token_bucket_cfg *cfg);
 
@@ -671,6 +822,7 @@
    They are: socket-based bufferevents (normal and IOCP-based), and SSL-based
    bufferevents.
  */
+EVENT2_EXPORT_SYMBOL
 struct bufferevent_rate_limit_group *bufferevent_rate_limit_group_new(
 	struct event_base *base,
 	const struct ev_token_bucket_cfg *cfg);
@@ -679,6 +831,7 @@
 
    Return 0 on success, -1 on failure.
 */
+EVENT2_EXPORT_SYMBOL
 int bufferevent_rate_limit_group_set_cfg(
 	struct bufferevent_rate_limit_group *,
 	const struct ev_token_bucket_cfg *);
@@ -699,6 +852,7 @@
 
    Returns 0 on success, -1 on faulre.
  */
+EVENT2_EXPORT_SYMBOL
 int bufferevent_rate_limit_group_set_min_share(
 	struct bufferevent_rate_limit_group *, size_t);
 
@@ -706,6 +860,7 @@
    Free a rate-limiting group.  The group must have no members when
    this function is called.
 */
+EVENT2_EXPORT_SYMBOL
 void bufferevent_rate_limit_group_free(struct bufferevent_rate_limit_group *);
 
 /**
@@ -718,13 +873,43 @@
 
    Return 0 on success and -1 on failure.
  */
+EVENT2_EXPORT_SYMBOL
 int bufferevent_add_to_rate_limit_group(struct bufferevent *bev,
     struct bufferevent_rate_limit_group *g);
 
 /** Remove 'bev' from its current rate-limit group (if any). */
+EVENT2_EXPORT_SYMBOL
 int bufferevent_remove_from_rate_limit_group(struct bufferevent *bev);
 
 /**
+   Set the size limit for single read operation.
+
+   Set to 0 for a reasonable default.
+
+   Return 0 on success and -1 on failure.
+ */
+EVENT2_EXPORT_SYMBOL
+int bufferevent_set_max_single_read(struct bufferevent *bev, size_t size);
+
+/**
+   Set the size limit for single write operation.
+
+   Set to 0 for a reasonable default.
+
+   Return 0 on success and -1 on failure.
+ */
+EVENT2_EXPORT_SYMBOL
+int bufferevent_set_max_single_write(struct bufferevent *bev, size_t size);
+
+/** Get the current size limit for single read operation. */
+EVENT2_EXPORT_SYMBOL
+ev_ssize_t bufferevent_get_max_single_read(struct bufferevent *bev);
+
+/** Get the current size limit for single write operation. */
+EVENT2_EXPORT_SYMBOL
+ev_ssize_t bufferevent_get_max_single_write(struct bufferevent *bev);
+
+/**
    @name Rate limit inspection
 
    Return the current read or write bucket size for a bufferevent.
@@ -735,13 +920,20 @@
 
    @{
  */
+EVENT2_EXPORT_SYMBOL
 ev_ssize_t bufferevent_get_read_limit(struct bufferevent *bev);
+EVENT2_EXPORT_SYMBOL
 ev_ssize_t bufferevent_get_write_limit(struct bufferevent *bev);
 /*@}*/
 
+EVENT2_EXPORT_SYMBOL
 ev_ssize_t bufferevent_get_max_to_read(struct bufferevent *bev);
+EVENT2_EXPORT_SYMBOL
 ev_ssize_t bufferevent_get_max_to_write(struct bufferevent *bev);
 
+EVENT2_EXPORT_SYMBOL
+const struct ev_token_bucket_cfg *bufferevent_get_token_bucket_cfg(const struct bufferevent * bev);
+
 /**
    @name Group Rate limit inspection
 
@@ -751,8 +943,10 @@
 
    @{
  */
+EVENT2_EXPORT_SYMBOL
 ev_ssize_t bufferevent_rate_limit_group_get_read_limit(
 	struct bufferevent_rate_limit_group *);
+EVENT2_EXPORT_SYMBOL
 ev_ssize_t bufferevent_rate_limit_group_get_write_limit(
 	struct bufferevent_rate_limit_group *);
 /*@}*/
@@ -771,7 +965,9 @@
 
    @{
  */
+EVENT2_EXPORT_SYMBOL
 int bufferevent_decrement_read_limit(struct bufferevent *bev, ev_ssize_t decr);
+EVENT2_EXPORT_SYMBOL
 int bufferevent_decrement_write_limit(struct bufferevent *bev, ev_ssize_t decr);
 /*@}*/
 
@@ -788,8 +984,10 @@
 
    @{
  */
+EVENT2_EXPORT_SYMBOL
 int bufferevent_rate_limit_group_decrement_read(
 	struct bufferevent_rate_limit_group *, ev_ssize_t);
+EVENT2_EXPORT_SYMBOL
 int bufferevent_rate_limit_group_decrement_write(
 	struct bufferevent_rate_limit_group *, ev_ssize_t);
 /*@}*/
@@ -801,6 +999,7 @@
  * Set the variable pointed to by total_read_out to the total number of bytes
  * ever read on grp, and the variable pointed to by total_written_out to the
  * total number of bytes ever written on grp. */
+EVENT2_EXPORT_SYMBOL
 void bufferevent_rate_limit_group_get_totals(
     struct bufferevent_rate_limit_group *grp,
     ev_uint64_t *total_read_out, ev_uint64_t *total_written_out);
@@ -810,6 +1009,7 @@
  *
  * Reset the number of bytes read or written on grp as given by
  * bufferevent_rate_limit_group_reset_totals(). */
+EVENT2_EXPORT_SYMBOL
 void
 bufferevent_rate_limit_group_reset_totals(
 	struct bufferevent_rate_limit_group *grp);
@@ -818,4 +1018,4 @@
 }
 #endif
 
-#endif /* _EVENT2_BUFFEREVENT_H_ */
+#endif /* EVENT2_BUFFEREVENT_H_INCLUDED_ */