154 Commits

Author SHA1 Message Date
Nick Mathewson
3467f2fa3b Fix logic in correcting high values from FIONREAD
The old logic made sense back when buffer.c was an enormous linear
buffer, but it doesn't make any sense for the chain-based
implementation.

This patch also refactors the ioctl{socket}? call into its own function.
2010-05-28 15:05:32 -04:00
Nick Mathewson
c44de06c76 Numerous opensolaris compilation fixes
For future note, opensolaris doesn't have sys/sysctl.h, doesn't like
comparing iov_buf to a chain_space_ptr without a cast, and is (predictably)
unforgiving of dumb syntax errors.

Also, we had accidentally broken the devpoll backend test in configure.in
2010-05-08 19:34:09 -04:00
Nick Mathewson
dcdae6b743 Make evbuffer_add_file() work on windows
Right now only the add_file() mode is supported, when it would be
nicer to have mmap support.  Perhaps for Libevent 2.1.x.
2010-05-08 16:34:18 -04:00
Nick Mathewson
d49b92a835 Remove one last bug in last_with_datap logic. Found with valgrind 2010-04-23 23:04:20 -04:00
Giuseppe Scrivano
d469c503c5 Fix compiler warnings under WIN32 2010-04-13 02:00:58 -04:00
Nick Mathewson
eb86c8c5ff Add evbuffer_copyout to copy data from an evbuffer without draining
The evbuffer_remove() function copies data from the front of an
evbuffer into an array of char, and removes the data from the buffer.
This function behaves the same, but does not remove the data.  This
behavior can be handy for lots of protocols, where you want the
evbuffer to accumulate data until a complete record has arrived.

Lots of people have asked for a function more or less like this, and
though it isn't too hard to code one from evbuffer_peek(), it is
apparently annoying to do it in every app you write.  The
evbuffer_peek() function is significantly faster, but it requires that
the user be able to handle data in separate extents.

This patch also reimplements evbufer_remove() as evbuffer_copyout()
followed by evbuffer_drain().  I am reasonably confident that this
won't be a performance hit: the memcpy() overhead should dominate the
cost of walking the list an extra time.
2010-04-13 01:42:01 -04:00
Nick Mathewson
8c83e99579 Add more unit tests for evbuffer_expand 2010-04-09 16:40:53 -04:00
Nick Mathewson
06a4443abe Unit-test every evbuffer_add_file() implementation.
Previously, we'd only test the default one, even if the others were still
compiled in.
2010-04-09 15:28:26 -04:00
Christopher Davis
28bfed47e2 Clean up a mistake in pointer manipulation in evbuffer_remove 2010-04-02 19:08:32 -04:00
Nick Mathewson
d5ebcf370d Rewrite evbuffer_expand and its users
The previous evbuffer_expand was not only incorrect; it was
inefficient too.  On all questions of time vs memory tradeoffs, it
chose to burn time in order to avoid wasting memory.  The new code
tries to be a little more balanced: it only resizes an existing chain
when doing so doesn't require too much copying, and when failing to do
so would waste a lot of the chain's space.

This patch also rewrites evbuffer_chain_insert to work properly with
last_with_datap, and adds a few convenience functions to buffer.c.
2010-03-31 12:53:20 -04:00
Nick Mathewson
45068a312c Fix a memory leak when appending/prepending to a buffer with unused space. 2010-03-31 12:53:20 -04:00
Nick Mathewson
8e227b04da Make the no_iovecs case of write_atmost compile
Apparently nobody had tested it before on a system that had sendfile.
Why would you have sendfile and not writev?  Perhaps you're trying to
test the no-iovecs code to make sure it still works.
2010-03-31 12:52:57 -04:00
Nick Mathewson
96865c4783 Turn the increasingly complex *_CHAIN() macros into functions 2010-03-31 12:50:32 -04:00
Nick Mathewson
b7442f8e83 Replace last_with_data with a slightly smarter version
To implement evbuffer_expand() properly, you need to be able to
replace the last chunk that has data, which means that we need to keep
track of the the next pointer pointing to the last_with_data chunk,
not the last_with_data chunk itself.
2010-03-31 12:50:32 -04:00
Nick Mathewson
cda56abf19 Fix critical bug in evbuffer_write when writev is not available
evbuffer_pullup() returns NULL if you try to pull up more bytes than
are there.  But evbuffer_write_atmost would sometimes ask for more
bytes to be pulled up than it had, get a NULL, and fail.
2010-03-31 12:50:32 -04:00
Nick Mathewson
c87272b7b9 Make evbuffer_prepend handle empty buffers better
If the first chunk of a buffer is empty, and we're told to prepend to
the buffer, we should be willing to use the entire first chunk.
Instead, we were dependent on the value of chunk->misalign.
2010-03-26 14:51:39 -04:00
Nick Mathewson
5c0ebb33f4 Do not use evbuffer_expand() to add the first chain to a buffer
(It's a big function, and using it this way is overkill.)
2010-03-26 14:50:45 -04:00
Nick Mathewson
f1bc125eb4 Improve robustness for refcounting
Document that we do intend to double-decref underlying bufferevents under
some circumstances.  Check to make sure that we don't decref past 0.
2010-03-13 00:28:50 -05:00
Nick Mathewson
1e7b986827 Fix last_with_data compilation on windows 2010-03-11 14:23:02 -05:00
Nick Mathewson
e470ad3c35 Allow evbuffer_read() to split across more than 2 iovecs
Previously it would only accept 2 iovecs at most, because our
previous_to_last nonsense didn't let it take any more.  This forced us
to do more reallocations in some cases when an extra small malloc
would have sufficed.
2010-03-10 23:39:30 -05:00
Nick Mathewson
6f47bd12ed Remove previous_to_last from evbuffer 2010-03-10 23:28:51 -05:00
Nick Mathewson
c8ac57f1f5 Use last_with_data in place of previous_to_last
This actually makes some of the code a lot simpler.  The only
ones that actually used previous_to_last for anything were reserving
and committing space.
2010-03-10 23:24:14 -05:00
Nick Mathewson
2a6d2a1e4b Revise evbuffer to add last_with_data
This is the first patch in a series to replace previous_to_last with
last_with_data.  Currently, we can only use two partially empty chains
at the end of an evbuffer, so if we have one with 511 bytes free, and
another with 512 bytes free, and we try to do a 1024 byte read, we
can't just stick another chain on the end: we need to reallocate the
last one.  That's stupid and inefficient.

Instead, this patch adds a last_with_data pointer to eventually
replace previous_to_last.  Instead of pointing to the penultimated
chain (if any) as previous_to_last does, last_with_data points to the
last chain that has any data in it, if any.  If all chains are empty,
last_with_data points to the first chain.  If there are no chains,
last_with_data is NULL.

The next step is to start using last_with_data everywhere that we
currently use previous_to_last.  When that's done, we can remove
previous_to_last and the code that maintains it.
2010-03-10 22:16:14 -05:00
Nick Mathewson
17efc1cdfa Update all our copyright notices to say "2010" 2010-03-04 01:38:48 -05:00
Nick Mathewson
cc1600afef Improve the speed of evbuffer_readln()
This makes some cases of bench_http about 5% faster.

Our internal evbuffer_strpbrk() function was overly general (it tried
to handle all character sets when we only used it for "\r\n"), and
not very efficient (it called memchr once for each character in the
buffer until it found a \r or a \n).  It actually showed up in some
profiles for HTTP testing, since evbuffer_readln() calls it when doing
loose CRLF detection.  This patch replaces it with a faster
implementation.
2010-03-03 16:45:32 -05:00
Nick Mathewson
b2fbeb3f07 Make bufferevent_free() clear all callbacks immediately.
This should end the family of bugs where we call bufferevent_free()
while a pending callback is holding a reference on the bufferevent,
and the callback tries to invoke the user callbacks before it releases
its own final reference.

This means that bufferevent_decref() is now a separate function from
bufferevent_free().
2010-02-22 15:38:23 -05:00
Nick Mathewson
e5bbd40ad7 Clean up formatting: use tabs, not 8-spaces, to indent. 2010-02-18 17:44:09 -05:00
Nick Mathewson
7116bf2314 Fix two unlocked reads in evbuffer.
Some initializers (in evbuffer_read and evbuffer_commit) were reading
the last and/or previous_to_last fields without grabbing the evbuffer
lock.

This may fix a hard-to-trigger race condition or two.
2010-02-15 21:11:41 -05:00
Nick Mathewson
3fe60fdf47 Use off_t for the length parameter of evbuffer_add_file 2010-02-12 23:40:13 -05:00
Nick Mathewson
29151e65b7 Fix byte counts when mixing deferred and non-deferred evbuffer callbacks.
This patch finishes 390e0561, which was somehow committed in a half-finished
state.  It solves a failing unit test on windows.
2010-01-06 18:42:59 -05:00
Nick Mathewson
a47d88d7c2 Replace some cases of uint32_t with ev_uint32_t.
Spotted by Roman Puls.
2009-12-23 07:53:19 -05:00
Nick Mathewson
390e056152 Fix up behavior of never-defered callbacks a little 2009-12-22 15:52:12 -05:00
Nick Mathewson
438f9ed26c Add the abilitity to mark some buffer callbacks as never-deferred. 2009-12-04 14:15:17 -05:00
Nick Mathewson
76cd2b70bb Stop passing EVTHREAD_READ and EVTHREAD_WRITE to non-rw locks.
Previously, our default lock model kind of assumed that every lock was
potentially a read-write lock.  This was a poor choice, since
read-write locks are far more expensive than regular locks, and so the
lock API should only use them when we can actually take advantage of
them.  Neither our pthreads or win32 lock implementation provided rw
locks.

Now that we have a way (not currently used!) to	indicate that we
really want a read-write lock, we shouldn't actually say "lock this
for reading" or "lock this for writing" unless we mean it.
2009-11-27 17:36:51 -05:00
Nick Mathewson
347952ffe0 Revise the locking API: deprecate the old locking callbacks and add trylock.
Previously, there was no good way to request different kinds of lock
(say, read/write vs writeonly or recursive vs nonrecursive), or for a
lock function to signal failure (which would be important for a
trylock mode).

This patch revises the lock API to be a bit more useful.  The older
lock calls are still supported for now.

We also add a debugging mode to catch common errors in using the
locking APIs.
2009-11-27 17:36:24 -05:00
Niels Provos
f169153956 Remove most calls to event_err() in http and deal with memory errors instead
svn:r1555
2009-11-19 23:08:50 +00:00
Nick Mathewson
d7d1f1da09 Move responsibility for IOCP callback into bufferevent_async.
This patch from Chris Davis saves some callback depth, and adds proper
ref-counting to bufferevents when there's a deferred evbuffer callback
inflight.  It could use a couple more comments to really nail down what
its invariants are.

svn:r1543
2009-11-17 20:31:09 +00:00
Nick Mathewson
784b8773a4 We do not work any more without an event-config.h; stop pretending that it is meaningful to check for HAVE_CONFIG_H
svn:r1516
2009-11-06 21:46:57 +00:00
Nick Mathewson
ac633aebdf Fix some build warnings on MSVC, mostly related to signed/unsigned comparisons.
svn:r1510
2009-11-05 21:22:23 +00:00
Nick Mathewson
7f0ad2f63c Fix an errant user of ssize_t to use ev_ssize_t.
My usual strategy of grep '[^_]ssize_t' had apparently failed me,
since this ssize_t was in the first column.

Resolves bug 2890434; spotted by Mihai Draghicioiu.

svn:r1484
2009-11-02 16:17:06 +00:00
Nick Mathewson
10cf631e84 Do not add a newline to the end of log statements.
svn:r1468
2009-10-27 04:04:07 +00:00
Nick Mathewson
2e36dbe1a6 Use EVUTIL_ASSERT() consistently instead of assert.
svn:r1464
2009-10-26 20:00:43 +00:00
Nick Mathewson
e3fd294a6d Spelling fixes in comments and strings.
svn:r1445
2009-10-16 13:19:57 +00:00
Niels Provos
8e8d94a3e0 Do not drop data from evbuffer when out of memory; reported by Jacek Masiulaniec
svn:r1436
2009-09-24 22:18:19 +00:00
Nick Mathewson
3b461a6d03 Treat a negative number of bytes to read as the kernel saying "I don't know."
svn:r1426
2009-09-11 18:21:37 +00:00
Nick Mathewson
22bd5b4287 Support sendfile on solaris: patch from Caitlin Mercer.
svn:r1419
2009-08-16 16:40:42 +00:00
Nick Mathewson
8a99083f01 Add an evbuffer_search_range() to search a bounded range of a buffer
This can be handy when you have	one search to find the end of a	header
section, and then you want to find a substring within the header
section without looking at the body.

svn:r1410
2009-08-07 17:16:52 +00:00
Nick Mathewson
d41347722a Refactor evbuffer_readln() into a search-for-eol function and an extract-line function.
svn:r1404
2009-07-31 17:35:42 +00:00
Nick Mathewson
a26d2d1b87 Refactor evbuffer_readln to use evbuffer_ptr; remove old evbuffer_iterator.
svn:r1403
2009-07-31 17:34:47 +00:00
Nick Mathewson
b06b2649b4 Make "deferred callback queue" independent of event_base.
This way, we can more easily have an IOCP bufferevent implementation
that does not need an event_base at all.  Woot.

svn:r1381
2009-07-26 01:29:39 +00:00