]> arthur.barton.de Git - netatalk.git/blob - libevent/include/event2/buffer.h
Add libevent
[netatalk.git] / libevent / include / event2 / buffer.h
1 /*
2  * Copyright (c) 2007-2010 Niels Provos and Nick Mathewson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #ifndef _EVENT2_BUFFER_H_
27 #define _EVENT2_BUFFER_H_
28
29 /** @file buffer.h
30
31   Functions for buffering data for network sending or receiving.
32
33   An evbuffer can be used for preparing data before sending it to
34   the network or conversely for reading data from the network.
35   Evbuffers try to avoid memory copies as much as possible.  As a
36   result evbuffers can be used to pass data around without actually
37   incurring the overhead of copying the data.
38
39   A new evbuffer can be allocated with evbuffer_new(), and can be
40   freed with evbuffer_free().
41
42   There are several guide lines for using evbuffers.
43
44   - if you already know how much data you are going to add as a result
45     of calling evbuffer_add() multiple times, it makes sense to use
46     evbuffer_expand() first to make sure that enough memory is allocated
47     before hand.
48
49   - evbuffer_add_buffer() adds the contents of one buffer to the other
50     without incurring any unnecessary memory copies.
51
52   - evbuffer_add() and evbuffer_add_buffer() do not mix very well:
53     if you use them, you will wind up with fragmented memory in your
54         buffer.
55
56   As the contents of an evbuffer can be stored into multiple different
57   memory blocks, it cannot be accessed directly.  Instead, evbuffer_pullup()
58   can be used to force a specified number of bytes to be continuous. This
59   will cause memory reallocation and memory copies if the data is split
60   across multiple blocks.
61
62  */
63
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67
68 #include <event2/event-config.h>
69 #include <stdarg.h>
70 #ifdef _EVENT_HAVE_SYS_TYPES_H
71 #include <sys/types.h>
72 #endif
73 #ifdef _EVENT_HAVE_SYS_UIO_H
74 #include <sys/uio.h>
75 #endif
76 #include <event2/util.h>
77
78 struct evbuffer;
79
80 /** Points to a position within an evbuffer. Used when repeatedly searching
81     through a buffer.  Calls to any function that modifies or re-packs the
82     buffer contents may invalidate all evbuffer_ptrs for that buffer.  Do not
83     modify these values except with evbuffer_ptr_set.
84  */
85 struct evbuffer_ptr {
86         ev_ssize_t pos;
87
88         /* Do not alter the values of fields. */
89         struct {
90                 void *chain;
91                 size_t pos_in_chain;
92         } _internal;
93 };
94
95 /** Describes a single extent of memory inside an evbuffer.  Used for
96     direct-access functions.
97
98     @see evbuffer_reserve_space, evbuffer_commit_space, evbuffer_peek
99  */
100 #ifdef _EVENT_HAVE_SYS_UIO_H
101 #define evbuffer_iovec iovec
102 /* Internal use -- defined only if we are using the native struct iovec */
103 #define _EVBUFFER_IOVEC_IS_NATIVE
104 #else
105 struct evbuffer_iovec {
106         /** The start of the extent of memory. */
107         void *iov_base;
108         /** The length of the extent of memory. */
109         size_t iov_len;
110 };
111 #endif
112
113 /**
114   Allocate storage for a new evbuffer.
115
116   @return a pointer to a newly allocated evbuffer struct, or NULL if an error
117         occurred
118  */
119 struct evbuffer *evbuffer_new(void);
120
121
122 /**
123   Deallocate storage for an evbuffer.
124
125   @param buf pointer to the evbuffer to be freed
126  */
127 void evbuffer_free(struct evbuffer *buf);
128
129 /**
130    Enable locking on an evbuffer so that it can safely be used by multiple
131    threads at the same time.
132
133    NOTE: when locking is enabled, the lock will be held when callbacks are
134    invoked.  This could result in deadlock if you aren't careful.  Plan
135    accordingly!
136
137    @param buf An evbuffer to make lockable.
138    @param lock A lock object, or NULL if we should allocate our own.
139    @return 0 on success, -1 on failure.
140  */
141 int evbuffer_enable_locking(struct evbuffer *buf, void *lock);
142
143 /**
144    Acquire the lock on an evbuffer.  Has no effect if locking was not enabled
145    with evbuffer_enable_locking.
146 */
147 void evbuffer_lock(struct evbuffer *buf);
148
149 /**
150    Release the lock on an evbuffer.  Has no effect if locking was not enabled
151    with evbuffer_enable_locking.
152 */
153 void evbuffer_unlock(struct evbuffer *buf);
154
155 /**
156   Returns the total number of bytes stored in the event buffer
157
158   @param buf pointer to the evbuffer
159   @return the number of bytes stored in the event buffer
160 */
161 size_t evbuffer_get_length(const struct evbuffer *buf);
162
163 /**
164    Returns the number of contiguous available bytes in the first buffer chain.
165
166    This is useful when processing data that might be split into multiple
167    chains, or that might all be in the first chain.  Calls to
168    evbuffer_pullup() that cause reallocation and copying of data can thus be
169    avoided.
170
171    @param buf pointer to the evbuffer
172    @return 0 if no data is available, otherwise the number of available bytes
173      in the first buffer chain.
174 */
175 size_t evbuffer_get_contiguous_space(const struct evbuffer *buf);
176
177 /**
178   Expands the available space in an event buffer.
179
180   Expands the available space in the event buffer to at least datlen, so that
181   appending datlen additional bytes will not require any new allocations.
182
183   @param buf the event buffer to be expanded
184   @param datlen the new minimum length requirement
185   @return 0 if successful, or -1 if an error occurred
186 */
187 int evbuffer_expand(struct evbuffer *buf, size_t datlen);
188
189 /**
190    Reserves space in the last chain of an event buffer.
191
192    Makes space available in the last chain of an event buffer that can
193    be arbitrarily written to by a user.  The space does not become
194    available for reading until it has been committed with
195    evbuffer_commit_space().
196
197    The space is made available as one or more extents, represented by
198    an initial pointer and a length.  You can force the memory to be
199    available as only one extent.  Allowing more, however, makes the
200    function more efficient.
201
202    Multiple subsequent calls to this function will make the same space
203    available until evbuffer_commit_space() has been called.
204
205    It is an error to do anything that moves around the buffer's internal
206    memory structures before committing the space.
207
208    NOTE: The code currently does not ever use more than two extents.
209    This may change in future versions.
210
211    @param buf the event buffer in which to reserve space.
212    @param size how much space to make available, at minimum.  The
213       total length of the extents may be greater than the requested
214       length.
215    @param vec an array of one or more evbuffer_iovec structures to
216       hold pointers to the reserved extents of memory.
217    @param n_vec The length of the vec array.  Must be at least 1.
218    @return the number of provided extents, or -1 on error.
219    @see evbuffer_commit_space
220 */
221 int
222 evbuffer_reserve_space(struct evbuffer *buf, ev_ssize_t size,
223     struct evbuffer_iovec *vec, int n_vecs);
224
225 /**
226    Commits previously reserved space.
227
228    Commits some of the space previously reserved with
229    evbuffer_reserve_space().  It then becomes available for reading.
230
231    This function may return an error if the pointer in the extents do
232    not match those returned from evbuffer_reserve_space, or if data
233    has been added to the buffer since the space was reserved.
234
235    If you want to commit less data than you got reserved space for,
236    modify the iov_len pointer of the buffer to a smaller value.  Note
237    that you may have received more space than you requested if it was
238    available!
239
240    @param buf the event buffer in which to reserve space.
241    @param vec one or two extents returned by evbuffer_reserve_space.
242    @param n_vecs the number of extents.
243    @return 0 on success, -1 on error
244    @see evbuffer_reserve_space
245 */
246 int evbuffer_commit_space(struct evbuffer *buf,
247     struct evbuffer_iovec *vec, int n_vecs);
248
249 /**
250   Append data to the end of an evbuffer.
251
252   @param buf the event buffer to be appended to
253   @param data pointer to the beginning of the data buffer
254   @param datlen the number of bytes to be copied from the data buffer
255   @return 0 on success, -1 on failure.
256  */
257 int evbuffer_add(struct evbuffer *buf, const void *data, size_t datlen);
258
259
260 /**
261   Read data from an event buffer and drain the bytes read.
262
263   @param buf the event buffer to be read from
264   @param data the destination buffer to store the result
265   @param datlen the maximum size of the destination buffer
266   @return the number of bytes read, or -1 if we can't drain the buffer.
267  */
268 int evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen);
269
270 /**
271   Read data from an event buffer, and leave the buffer unchanged.
272
273   @param buf the event buffer to be read from
274   @param data the destination buffer to store the result
275   @param datlen the maximum size of the destination buffer
276   @return the number of bytes read, or -1 if we can't drain the buffer.
277  */
278 ev_ssize_t evbuffer_copyout(struct evbuffer *buf, void *data_out, size_t datlen);
279
280 /**
281   Read data from an event buffer into another event buffer draining
282   the bytes from the src buffer read.  This function avoids memcpy
283   as possible.
284
285   @param src the event buffer to be read from
286   @param dst the destination event buffer to store the result into
287   @param datlen the maximum numbers of bytes to transfer
288   @return the number of bytes read
289  */
290 int evbuffer_remove_buffer(struct evbuffer *src, struct evbuffer *dst,
291     size_t datlen);
292
293 /** Used to tell evbuffer_readln what kind of line-ending to look for.
294  */
295 enum evbuffer_eol_style {
296         /** Any sequence of CR and LF characters is acceptable as an EOL. */
297         EVBUFFER_EOL_ANY,
298         /** An EOL is an LF, optionally preceded by a CR.  This style is
299          * most useful for implementing text-based internet protocols. */
300         EVBUFFER_EOL_CRLF,
301         /** An EOL is a CR followed by an LF. */
302         EVBUFFER_EOL_CRLF_STRICT,
303         /** An EOL is a LF. */
304         EVBUFFER_EOL_LF
305 };
306
307 /**
308  * Read a single line from an event buffer.
309  *
310  * Reads a line terminated by an EOL as determined by the evbuffer_eol_style
311  * argument.  Returns a newly allocated nul-terminated string; the caller must
312  * free the returned value.  The EOL is not included in the returned string.
313  *
314  * @param buffer the evbuffer to read from
315  * @param n_read_out if non-NULL, points to a size_t that is set to the
316  *       number of characters in the returned string.  This is useful for
317  *       strings that can contain NUL characters.
318  * @param eol_style the style of line-ending to use.
319  * @return pointer to a single line, or NULL if an error occurred
320  */
321 char *evbuffer_readln(struct evbuffer *buffer, size_t *n_read_out,
322     enum evbuffer_eol_style eol_style);
323
324 /**
325   Move data from one evbuffer into another evbuffer.
326
327   This is a destructive add.  The data from one buffer moves into
328   the other buffer.  However, no unnecessary memory copies occur.
329
330   @param outbuf the output buffer
331   @param inbuf the input buffer
332   @return 0 if successful, or -1 if an error occurred
333  */
334 int evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf);
335
336
337 typedef void (*evbuffer_ref_cleanup_cb)(const void *data,
338     size_t datalen, void *extra);
339
340 /**
341   Reference memory into an evbuffer without copying.
342
343   The memory needs to remain valid until all the added data has been
344   read.  This function keeps just a reference to the memory without
345   actually incurring the overhead of a copy.
346
347   @param outbuf the output buffer
348   @param data the memory to reference
349   @param datlen how memory to reference
350   @param cleanupfn callback to be invoked when the memory is no longer
351         referenced
352   @param extra optional argument to the cleanup callback
353   @return 0 if successful, or -1 if an error occurred
354  */
355 int evbuffer_add_reference(struct evbuffer *outbuf,
356     const void *data, size_t datlen,
357     evbuffer_ref_cleanup_cb cleanupfn, void *extra);
358
359 /**
360   Move data from a file into the evbuffer for writing to a socket.
361
362   This function avoids unnecessary data copies between userland and
363   kernel.  Where available, it uses sendfile or splice.
364
365   The function owns the resulting file descriptor and will close it
366   when finished transferring data.
367
368   The results of using evbuffer_remove() or evbuffer_pullup() are
369   undefined.
370
371   @param outbuf the output buffer
372   @param fd the file descriptor
373   @param off the offset from which to read data
374   @param length how much data to read
375   @return 0 if successful, or -1 if an error occurred
376 */
377
378 int evbuffer_add_file(struct evbuffer *output, int fd, ev_off_t offset,
379     ev_off_t length);
380
381 /**
382   Append a formatted string to the end of an evbuffer.
383
384   @param buf the evbuffer that will be appended to
385   @param fmt a format string
386   @param ... arguments that will be passed to printf(3)
387   @return The number of bytes added if successful, or -1 if an error occurred.
388
389  */
390 int evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...)
391 #ifdef __GNUC__
392   __attribute__((format(printf, 2, 3)))
393 #endif
394 ;
395
396
397 /**
398   Append a va_list formatted string to the end of an evbuffer.
399
400   @param buf the evbuffer that will be appended to
401   @param fmt a format string
402   @param ap a varargs va_list argument array that will be passed to vprintf(3)
403   @return The number of bytes added if successful, or -1 if an error occurred.
404  */
405 int evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap);
406
407
408 /**
409   Remove a specified number of bytes data from the beginning of an evbuffer.
410
411   @param buf the evbuffer to be drained
412   @param len the number of bytes to drain from the beginning of the buffer
413   @return 0 on success, -1 on failure.
414  */
415 int evbuffer_drain(struct evbuffer *buf, size_t len);
416
417
418 /**
419   Write the contents of an evbuffer to a file descriptor.
420
421   The evbuffer will be drained after the bytes have been successfully written.
422
423   @param buffer the evbuffer to be written and drained
424   @param fd the file descriptor to be written to
425   @return the number of bytes written, or -1 if an error occurred
426   @see evbuffer_read()
427  */
428 int evbuffer_write(struct evbuffer *buffer, evutil_socket_t fd);
429
430 /**
431   Write some of the contents of an evbuffer to a file descriptor.
432
433   The evbuffer will be drained after the bytes have been successfully written.
434
435   @param buffer the evbuffer to be written and drained
436   @param fd the file descriptor to be written to
437   @param howmuch the largest allowable number of bytes to write, or -1
438         to write as many bytes as we can.
439   @return the number of bytes written, or -1 if an error occurred
440   @see evbuffer_read()
441  */
442 int evbuffer_write_atmost(struct evbuffer *buffer, evutil_socket_t fd,
443                                                   ev_ssize_t howmuch);
444
445 /**
446   Read from a file descriptor and store the result in an evbuffer.
447
448   @param buf the evbuffer to store the result
449   @param fd the file descriptor to read from
450   @param howmuch the number of bytes to be read
451   @return the number of bytes read, or -1 if an error occurred
452   @see evbuffer_write()
453  */
454 int evbuffer_read(struct evbuffer *buffer, evutil_socket_t fd, int howmuch);
455
456 /**
457    Search for a string within an evbuffer.
458
459    @param buffer the evbuffer to be searched
460    @param what the string to be searched for
461    @param len the length of the search string
462    @param start NULL or a pointer to a valid struct evbuffer_ptr.
463    @return a struct evbuffer_ptr whose 'pos' field has the offset of the
464      first occurrence of the string in the buffer after 'start'.  The 'pos'
465      field of the result is -1 if the string was not found.
466  */
467 struct evbuffer_ptr evbuffer_search(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start);
468
469 /**
470    Search for a string within part of an evbuffer.
471
472    @param buffer the evbuffer to be searched
473    @param what the string to be searched for
474    @param len the length of the search string
475    @param start NULL or a pointer to a valid struct evbuffer_ptr that
476      indicates where we should start searching.
477    @param end NULL or a pointer to a valid struct evbuffer_ptr that
478      indicates where we should stop searching.
479    @return a struct evbuffer_ptr whose 'pos' field has the offset of the
480      first occurrence of the string in the buffer after 'start'.  The 'pos'
481      field of the result is -1 if the string was not found.
482  */
483 struct evbuffer_ptr evbuffer_search_range(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start, const struct evbuffer_ptr *end);
484
485 enum evbuffer_ptr_how {
486         /** Sets the pointer to the position; can be called on with an
487             uninitialized evbuffer_ptr. */
488         EVBUFFER_PTR_SET,
489         /** Advances the pointer by adding to the current position. */
490         EVBUFFER_PTR_ADD
491 };
492
493 /**
494    Sets the search pointer in the buffer to position.
495
496    If evbuffer_ptr is not initialized.  This function can only be called
497    with EVBUFFER_PTR_SET.
498
499    @param buffer the evbuffer to be search
500    @param ptr a pointer to a struct evbuffer_ptr
501    @param position the position at which to start the next search
502    @param how determines how the pointer should be manipulated.
503    @returns 0 on success or -1 otherwise
504 */
505 int
506 evbuffer_ptr_set(struct evbuffer *buffer, struct evbuffer_ptr *pos,
507     size_t position, enum evbuffer_ptr_how how);
508
509 /**
510    Search for an end-of-line string within an evbuffer.
511
512    @param buffer the evbuffer to be searched
513    @param start NULL or a pointer to a valid struct evbuffer_ptr to start
514       searching at.
515    @param eol_len_out If non-NULL, the pointed-to value will be set to
516       the length of the end-of-line string.
517    @param eol_style The kind of EOL to look for; see evbuffer_readln() for
518       more information
519    @return a struct evbuffer_ptr whose 'pos' field has the offset of the
520      first occurrence EOL in the buffer after 'start'.  The 'pos'
521      field of the result is -1 if the string was not found.
522  */
523 struct evbuffer_ptr evbuffer_search_eol(struct evbuffer *buffer,
524     struct evbuffer_ptr *start, size_t *eol_len_out,
525     enum evbuffer_eol_style eol_style);
526
527 /** Structure passed to an evbuffer callback */
528 struct evbuffer_cb_info {
529         /** The size of */
530         size_t orig_size;
531         size_t n_added;
532         size_t n_deleted;
533 };
534
535 /** Function to peek at data inside an evbuffer without removing it or
536     copying it out.
537
538     Pointers to the data are returned by filling the 'vec_out' array
539     with pointers to one or more extents of data inside the buffer.
540
541     The total data in the extents that you get back may be more than
542     you requested (if there is more data last extent than you asked
543     for), or less (if you do not provide enough evbuffer_iovecs, or if
544     the buffer does not have as much data as you asked to see).
545
546     @param buffer the evbuffer to peek into,
547     @param len the number of bytes to try to peek.  If negative, we
548        will try to fill as much of vec_out as we can.
549     @param start_at an evbuffer_ptr indicating the point at which we
550        should start looking for data.  NULL means, "At the start of the
551        buffer."
552     @param vec_out an array of evbuffer_iovec
553     @param n_vec the length of vec_out.  If 0, we only count how many
554        extents would be necessary to point to the requested amount of
555        data.
556     @return The number of extents needed.  This may be less than n_vec
557        if we didn't need all the evbuffer_iovecs we were given, or more
558        than n_vec if we would need more to return all the data that was
559        requested.
560  */
561 int evbuffer_peek(struct evbuffer *buffer, ev_ssize_t len,
562     struct evbuffer_ptr *start_at,
563     struct evbuffer_iovec *vec_out, int n_vec);
564
565 /** Type definition for a callback that is invoked whenever data is added or
566     removed from an evbuffer.
567
568     An evbuffer may have one or more callbacks set at a time.  The order
569     in which they are executed is undefined.
570
571     A callback function may add more callbacks, or remove itself from the
572     list of callbacks, or add or remove data from the buffer.  It may not
573     remove another callback from the list.
574
575     If a callback adds or removes data from the buffer or from another
576     buffer, this can cause a recursive invocation of your callback or
577     other callbacks.  If you ask for an infinite loop, you might just get
578     one: watch out!
579
580     @param buffer the buffer whose size has changed
581     @param info a structure describing how the buffer changed.
582     @param arg a pointer to user data
583 */
584 typedef void (*evbuffer_cb_func)(struct evbuffer *buffer, const struct evbuffer_cb_info *info, void *arg);
585
586 struct evbuffer_cb_entry;
587 /** Add a new callback to an evbuffer.
588
589   Subsequent calls to evbuffer_add_cb() add new callbacks.  To remove this
590   callback, call evbuffer_remove_cb or evbuffer_remove_cb_entry.
591
592   @param buffer the evbuffer to be monitored
593   @param cb the callback function to invoke when the evbuffer is modified,
594         or NULL to remove all callbacks.
595   @param cbarg an argument to be provided to the callback function
596   @return a handle to the callback on success, or NULL on failure.
597  */
598 struct evbuffer_cb_entry *evbuffer_add_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg);
599
600 /** Remove a callback from an evbuffer, given a handle returned from
601     evbuffer_add_cb.
602
603     Calling this function invalidates the handle.
604
605     @return 0 if a callback was removed, or -1 if no matching callback was
606     found.
607  */
608 int evbuffer_remove_cb_entry(struct evbuffer *buffer,
609                              struct evbuffer_cb_entry *ent);
610
611 /** Remove a callback from an evbuffer, given the function and argument
612     used to add it.
613
614     @return 0 if a callback was removed, or -1 if no matching callback was
615     found.
616  */
617 int evbuffer_remove_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg);
618
619 /** If this flag is not set, then a callback is temporarily disabled, and
620  * should not be invoked. */
621 #define EVBUFFER_CB_ENABLED 1
622
623 /** Change the flags that are set for a callback on a buffer by adding more.
624
625     @param buffer the evbuffer that the callback is watching.
626     @param cb the callback whose status we want to change.
627     @param flags EVBUFFER_CB_ENABLED to re-enable the callback.
628     @return 0 on success, -1 on failure.
629  */
630 int evbuffer_cb_set_flags(struct evbuffer *buffer,
631                           struct evbuffer_cb_entry *cb, ev_uint32_t flags);
632
633 /** Change the flags that are set for a callback on a buffer by removing some
634
635     @param buffer the evbuffer that the callback is watching.
636     @param cb the callback whose status we want to change.
637     @param flags EVBUFFER_CB_ENABLED to disable the callback.
638     @return 0 on success, -1 on failure.
639  */
640 int evbuffer_cb_clear_flags(struct evbuffer *buffer,
641                           struct evbuffer_cb_entry *cb, ev_uint32_t flags);
642
643 #if 0
644 /** Postpone calling a given callback until unsuspend is called later.
645
646     This is different from disabling the callback, since the callback will get
647         invoked later if the buffer size changes between now and when we unsuspend
648         it.
649
650         @param the buffer that the callback is watching.
651         @param cb the callback we want to suspend.
652  */
653 void evbuffer_cb_suspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb);
654 /** Stop postponing a callback that we postponed with evbuffer_cb_suspend.
655
656         If data was added to or removed from the buffer while the callback was
657         suspended, the callback will get called once now.
658
659         @param the buffer that the callback is watching.
660         @param cb the callback we want to stop suspending.
661  */
662 void evbuffer_cb_unsuspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb);
663 #endif
664
665 /**
666   Makes the data at the begging of an evbuffer contiguous.
667
668   @param buf the evbuffer to make contiguous
669   @param size the number of bytes to make contiguous, or -1 to make the
670         entire buffer contiguous.
671   @return a pointer to the contiguous memory array
672 */
673
674 unsigned char *evbuffer_pullup(struct evbuffer *buf, ev_ssize_t size);
675
676 /**
677   Prepends data to the beginning of the evbuffer
678
679   @param buf the evbuffer to which to prepend data
680   @param data a pointer to the memory to prepend
681   @param size the number of bytes to prepend
682   @return 0 if successful, or -1 otherwise
683 */
684
685 int evbuffer_prepend(struct evbuffer *buf, const void *data, size_t size);
686
687 /**
688   Prepends all data from the src evbuffer to the beginning of the dst
689   evbuffer.
690
691   @param dst the evbuffer to which to prepend data
692   @param src the evbuffer to prepend; it will be emptied as a result
693   @return 0 if successful, or -1 otherwise
694 */
695 int evbuffer_prepend_buffer(struct evbuffer *dst, struct evbuffer* src);
696
697 /**
698    Prevent calls that modify an evbuffer from succeeding. A buffer may
699    frozen at the front, at the back, or at both the front and the back.
700
701    If the front of a buffer is frozen, operations that drain data from
702    the front of the buffer, or that prepend data to the buffer, will
703    fail until it is unfrozen.   If the back a buffer is frozen, operations
704    that append data from the buffer will fail until it is unfrozen.
705
706    @param buf The buffer to freeze
707    @param at_front If true, we freeze the front of the buffer.  If false,
708       we freeze the back.
709    @return 0 on success, -1 on failure.
710 */
711 int evbuffer_freeze(struct evbuffer *buf, int at_front);
712 /**
713    Re-enable calls that modify an evbuffer.
714
715    @param buf The buffer to un-freeze
716    @param at_front If true, we unfreeze the front of the buffer.  If false,
717       we unfreeze the back.
718    @return 0 on success, -1 on failure.
719  */
720 int evbuffer_unfreeze(struct evbuffer *buf, int at_front);
721
722 struct event_base;
723 /**
724    Force all the callbacks on an evbuffer to be run, not immediately after
725    the evbuffer is altered, but instead from inside the event loop.
726
727    This can be used to serialize all the callbacks to a single thread
728    of execution.
729  */
730 int evbuffer_defer_callbacks(struct evbuffer *buffer, struct event_base *base);
731
732 #ifdef __cplusplus
733 }
734 #endif
735
736 #endif /* _EVENT2_BUFFER_H_ */