evbuffer-internal.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
  3. * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef EVBUFFER_INTERNAL_H_INCLUDED_
  28. #define EVBUFFER_INTERNAL_H_INCLUDED_
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. #include "event2/event-config.h"
  33. #include "evconfig-private.h"
  34. #include "event2/util.h"
  35. #include "event2/event_struct.h"
  36. #include "util-internal.h"
  37. #include "defer-internal.h"
  38. /* Experimental cb flag: "never deferred." Implementation note:
  39. * these callbacks may get an inaccurate view of n_del/n_added in their
  40. * arguments. */
  41. #define EVBUFFER_CB_NODEFER 2
  42. #ifdef _WIN32
  43. #include <winsock2.h>
  44. #endif
  45. #include <sys/queue.h>
  46. /* Minimum allocation for a chain. We define this so that we're burning no
  47. * more than 5% of each allocation on overhead. It would be nice to lose even
  48. * less space, though. */
  49. #if EVENT__SIZEOF_VOID_P < 8
  50. #define MIN_BUFFER_SIZE 512
  51. #else
  52. #define MIN_BUFFER_SIZE 1024
  53. #endif
  54. /** A single evbuffer callback for an evbuffer. This function will be invoked
  55. * when bytes are added to or removed from the evbuffer. */
  56. struct evbuffer_cb_entry {
  57. /** Structures to implement a doubly-linked queue of callbacks */
  58. LIST_ENTRY(evbuffer_cb_entry) next;
  59. /** The callback function to invoke when this callback is called.
  60. If EVBUFFER_CB_OBSOLETE is set in flags, the cb_obsolete field is
  61. valid; otherwise, cb_func is valid. */
  62. union {
  63. evbuffer_cb_func cb_func;
  64. evbuffer_cb cb_obsolete;
  65. } cb;
  66. /** Argument to pass to cb. */
  67. void *cbarg;
  68. /** Currently set flags on this callback. */
  69. ev_uint32_t flags;
  70. };
  71. struct bufferevent;
  72. struct evbuffer_chain;
  73. struct evbuffer {
  74. /** The first chain in this buffer's linked list of chains. */
  75. struct evbuffer_chain *first;
  76. /** The last chain in this buffer's linked list of chains. */
  77. struct evbuffer_chain *last;
  78. /** Pointer to the next pointer pointing at the 'last_with_data' chain.
  79. *
  80. * To unpack:
  81. *
  82. * The last_with_data chain is the last chain that has any data in it.
  83. * If all chains in the buffer are empty, it is the first chain.
  84. * If the buffer has no chains, it is NULL.
  85. *
  86. * The last_with_datap pointer points at _whatever 'next' pointer_
  87. * pointing at the last_with_data chain. If the last_with_data chain
  88. * is the first chain, or it is NULL, then the last_with_datap pointer
  89. * is &buf->first.
  90. */
  91. struct evbuffer_chain **last_with_datap;
  92. /** Total amount of bytes stored in all chains.*/
  93. size_t total_len;
  94. /** Number of bytes we have added to the buffer since we last tried to
  95. * invoke callbacks. */
  96. size_t n_add_for_cb;
  97. /** Number of bytes we have removed from the buffer since we last
  98. * tried to invoke callbacks. */
  99. size_t n_del_for_cb;
  100. #ifndef EVENT__DISABLE_THREAD_SUPPORT
  101. /** A lock used to mediate access to this buffer. */
  102. void *lock;
  103. #endif
  104. /** True iff we should free the lock field when we free this
  105. * evbuffer. */
  106. unsigned own_lock : 1;
  107. /** True iff we should not allow changes to the front of the buffer
  108. * (drains or prepends). */
  109. unsigned freeze_start : 1;
  110. /** True iff we should not allow changes to the end of the buffer
  111. * (appends) */
  112. unsigned freeze_end : 1;
  113. /** True iff this evbuffer's callbacks are not invoked immediately
  114. * upon a change in the buffer, but instead are deferred to be invoked
  115. * from the event_base's loop. Useful for preventing enormous stack
  116. * overflows when we have mutually recursive callbacks, and for
  117. * serializing callbacks in a single thread. */
  118. unsigned deferred_cbs : 1;
  119. #ifdef _WIN32
  120. /** True iff this buffer is set up for overlapped IO. */
  121. unsigned is_overlapped : 1;
  122. #endif
  123. /** Zero or more EVBUFFER_FLAG_* bits */
  124. ev_uint32_t flags;
  125. /** Used to implement deferred callbacks. */
  126. struct event_base *cb_queue;
  127. /** A reference count on this evbuffer. When the reference count
  128. * reaches 0, the buffer is destroyed. Manipulated with
  129. * evbuffer_incref and evbuffer_decref_and_unlock and
  130. * evbuffer_free. */
  131. int refcnt;
  132. /** A struct event_callback handle to make all of this buffer's callbacks
  133. * invoked from the event loop. */
  134. struct event_callback deferred;
  135. /** A doubly-linked-list of callback functions */
  136. LIST_HEAD(evbuffer_cb_queue, evbuffer_cb_entry) callbacks;
  137. /** The parent bufferevent object this evbuffer belongs to.
  138. * NULL if the evbuffer stands alone. */
  139. struct bufferevent *parent;
  140. };
  141. #if EVENT__SIZEOF_OFF_T < EVENT__SIZEOF_SIZE_T
  142. typedef ev_ssize_t ev_misalign_t;
  143. #define EVBUFFER_CHAIN_MAX ((size_t)EV_SSIZE_MAX)
  144. #else
  145. typedef ev_off_t ev_misalign_t;
  146. #if EVENT__SIZEOF_OFF_T > EVENT__SIZEOF_SIZE_T
  147. #define EVBUFFER_CHAIN_MAX EV_SIZE_MAX
  148. #else
  149. #define EVBUFFER_CHAIN_MAX ((size_t)EV_SSIZE_MAX)
  150. #endif
  151. #endif
  152. /** A single item in an evbuffer. */
  153. struct evbuffer_chain {
  154. /** points to next buffer in the chain */
  155. struct evbuffer_chain *next;
  156. /** total allocation available in the buffer field. */
  157. size_t buffer_len;
  158. /** unused space at the beginning of buffer or an offset into a
  159. * file for sendfile buffers. */
  160. ev_misalign_t misalign;
  161. /** Offset into buffer + misalign at which to start writing.
  162. * In other words, the total number of bytes actually stored
  163. * in buffer. */
  164. size_t off;
  165. /** Set if special handling is required for this chain */
  166. unsigned flags;
  167. #define EVBUFFER_FILESEGMENT 0x0001 /**< A chain used for a file segment */
  168. #define EVBUFFER_SENDFILE 0x0002 /**< a chain used with sendfile */
  169. #define EVBUFFER_REFERENCE 0x0004 /**< a chain with a mem reference */
  170. #define EVBUFFER_IMMUTABLE 0x0008 /**< read-only chain */
  171. /** a chain that mustn't be reallocated or freed, or have its contents
  172. * memmoved, until the chain is un-pinned. */
  173. #define EVBUFFER_MEM_PINNED_R 0x0010
  174. #define EVBUFFER_MEM_PINNED_W 0x0020
  175. #define EVBUFFER_MEM_PINNED_ANY (EVBUFFER_MEM_PINNED_R|EVBUFFER_MEM_PINNED_W)
  176. /** a chain that should be freed, but can't be freed until it is
  177. * un-pinned. */
  178. #define EVBUFFER_DANGLING 0x0040
  179. /** a chain that is a referenced copy of another chain */
  180. #define EVBUFFER_MULTICAST 0x0080
  181. /** number of references to this chain */
  182. int refcnt;
  183. /** Usually points to the read-write memory belonging to this
  184. * buffer allocated as part of the evbuffer_chain allocation.
  185. * For mmap, this can be a read-only buffer and
  186. * EVBUFFER_IMMUTABLE will be set in flags. For sendfile, it
  187. * may point to NULL.
  188. */
  189. unsigned char *buffer;
  190. };
  191. /** callback for a reference chain; lets us know what to do with it when
  192. * we're done with it. Lives at the end of an evbuffer_chain with the
  193. * EVBUFFER_REFERENCE flag set */
  194. struct evbuffer_chain_reference {
  195. evbuffer_ref_cleanup_cb cleanupfn;
  196. void *extra;
  197. };
  198. /** File segment for a file-segment chain. Lives at the end of an
  199. * evbuffer_chain with the EVBUFFER_FILESEGMENT flag set. */
  200. struct evbuffer_chain_file_segment {
  201. struct evbuffer_file_segment *segment;
  202. #ifdef _WIN32
  203. /** If we're using CreateFileMapping, this is the handle to the view. */
  204. HANDLE view_handle;
  205. #endif
  206. };
  207. /* Declared in event2/buffer.h; defined here. */
  208. struct evbuffer_file_segment {
  209. void *lock; /**< lock prevent concurrent access to refcnt */
  210. int refcnt; /**< Reference count for this file segment */
  211. unsigned flags; /**< combination of EVBUF_FS_* flags */
  212. /** What kind of file segment is this? */
  213. unsigned can_sendfile : 1;
  214. unsigned is_mapping : 1;
  215. /** The fd that we read the data from. */
  216. int fd;
  217. /** If we're using mmap, this is the raw mapped memory. */
  218. void *mapping;
  219. #ifdef _WIN32
  220. /** If we're using CreateFileMapping, this is the mapping */
  221. HANDLE mapping_handle;
  222. #endif
  223. /** If we're using mmap or IO, this is the content of the file
  224. * segment. */
  225. char *contents;
  226. /** Position of this segment within the file. */
  227. ev_off_t file_offset;
  228. /** If we're using mmap, this is the offset within 'mapping' where
  229. * this data segment begins. */
  230. ev_off_t mmap_offset;
  231. /** The length of this segment. */
  232. ev_off_t length;
  233. /** Cleanup callback function */
  234. evbuffer_file_segment_cleanup_cb cleanup_cb;
  235. /** Argument to be pass to cleanup callback function */
  236. void *cleanup_cb_arg;
  237. };
  238. /** Information about the multicast parent of a chain. Lives at the
  239. * end of an evbuffer_chain with the EVBUFFER_MULTICAST flag set. */
  240. struct evbuffer_multicast_parent {
  241. /** source buffer the multicast parent belongs to */
  242. struct evbuffer *source;
  243. /** multicast parent for this chain */
  244. struct evbuffer_chain *parent;
  245. };
  246. #define EVBUFFER_CHAIN_SIZE sizeof(struct evbuffer_chain)
  247. /** Return a pointer to extra data allocated along with an evbuffer. */
  248. #define EVBUFFER_CHAIN_EXTRA(t, c) (t *)((struct evbuffer_chain *)(c) + 1)
  249. /** Assert that we are holding the lock on an evbuffer */
  250. #define ASSERT_EVBUFFER_LOCKED(buffer) \
  251. EVLOCK_ASSERT_LOCKED((buffer)->lock)
  252. #define EVBUFFER_LOCK(buffer) \
  253. do { \
  254. EVLOCK_LOCK((buffer)->lock, 0); \
  255. } while (0)
  256. #define EVBUFFER_UNLOCK(buffer) \
  257. do { \
  258. EVLOCK_UNLOCK((buffer)->lock, 0); \
  259. } while (0)
  260. #define EVBUFFER_LOCK2(buffer1, buffer2) \
  261. do { \
  262. EVLOCK_LOCK2((buffer1)->lock, (buffer2)->lock, 0, 0); \
  263. } while (0)
  264. #define EVBUFFER_UNLOCK2(buffer1, buffer2) \
  265. do { \
  266. EVLOCK_UNLOCK2((buffer1)->lock, (buffer2)->lock, 0, 0); \
  267. } while (0)
  268. /** Increase the reference count of buf by one. */
  269. void evbuffer_incref_(struct evbuffer *buf);
  270. /** Increase the reference count of buf by one and acquire the lock. */
  271. void evbuffer_incref_and_lock_(struct evbuffer *buf);
  272. /** Pin a single buffer chain using a given flag. A pinned chunk may not be
  273. * moved or freed until it is unpinned. */
  274. void evbuffer_chain_pin_(struct evbuffer_chain *chain, unsigned flag);
  275. /** Unpin a single buffer chain using a given flag. */
  276. void evbuffer_chain_unpin_(struct evbuffer_chain *chain, unsigned flag);
  277. /** As evbuffer_free, but requires that we hold a lock on the buffer, and
  278. * releases the lock before freeing it and the buffer. */
  279. void evbuffer_decref_and_unlock_(struct evbuffer *buffer);
  280. /** As evbuffer_expand, but does not guarantee that the newly allocated memory
  281. * is contiguous. Instead, it may be split across two or more chunks. */
  282. int evbuffer_expand_fast_(struct evbuffer *, size_t, int);
  283. /** Helper: prepares for a readv/WSARecv call by expanding the buffer to
  284. * hold enough memory to read 'howmuch' bytes in possibly noncontiguous memory.
  285. * Sets up the one or two iovecs in 'vecs' to point to the free memory and its
  286. * extent, and *chainp to point to the first chain that we'll try to read into.
  287. * Returns the number of vecs used.
  288. */
  289. int evbuffer_read_setup_vecs_(struct evbuffer *buf, ev_ssize_t howmuch,
  290. struct evbuffer_iovec *vecs, int n_vecs, struct evbuffer_chain ***chainp,
  291. int exact);
  292. /* Helper macro: copies an evbuffer_iovec in ei to a win32 WSABUF in i. */
  293. #define WSABUF_FROM_EVBUFFER_IOV(i,ei) do { \
  294. (i)->buf = (ei)->iov_base; \
  295. (i)->len = (unsigned long)(ei)->iov_len; \
  296. } while (0)
  297. /* XXXX the cast above is safe for now, but not if we allow mmaps on win64.
  298. * See note in buffer_iocp's launch_write function */
  299. /** Set the parent bufferevent object for buf to bev */
  300. void evbuffer_set_parent_(struct evbuffer *buf, struct bufferevent *bev);
  301. void evbuffer_invoke_callbacks_(struct evbuffer *buf);
  302. int evbuffer_get_callbacks_(struct evbuffer *buffer,
  303. struct event_callback **cbs,
  304. int max_cbs);
  305. #ifdef __cplusplus
  306. }
  307. #endif
  308. #endif /* EVBUFFER_INTERNAL_H_INCLUDED_ */