Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
provided buffered events
svn:r95
  • Loading branch information
provos committed Mar 23, 2004
1 parent cd699ab commit 5908bd7
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
Expand Up @@ -21,7 +21,7 @@ EXTRA_DIST = acconfig.h err.c event.h evsignal.h event.3 kqueue.c \

lib_LIBRARIES = libevent.a

libevent_a_SOURCES = event.c buffer.c
libevent_a_SOURCES = event.c buffer.c evbuffer.c
libevent_a_LIBADD = @LIBOBJS@

include_HEADERS = event.h
Expand Down
49 changes: 31 additions & 18 deletions buffer.c
Expand Up @@ -25,22 +25,14 @@
#endif
#include <unistd.h>

#undef timeout_pending
#undef timeout_initialized

#include <event.h>

#include "event.h"

extern int debug;

struct evbuffer *
evbuffer_new(void)
{
struct evbuffer *buffer;

if ((buffer = calloc(1, sizeof(struct evbuffer))) == NULL)
err(1, "%s: calloc", __func__);
buffer = calloc(1, sizeof(struct evbuffer));

return (buffer);
}
Expand All @@ -53,11 +45,20 @@ evbuffer_free(struct evbuffer *buffer)
free(buffer);
}

void
/*
* This is a destructive add. The data from one buffer moves into
* the other buffer.
*/

int
evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf)
{
evbuffer_add(outbuf, inbuf->buffer, inbuf->off);
evbuffer_drain(inbuf, inbuf->off);
int res;
res = evbuffer_add(outbuf, inbuf->buffer, inbuf->off);
if (res == 0)
evbuffer_drain(inbuf, inbuf->off);

return (res);
}

int
Expand All @@ -73,29 +74,41 @@ evbuffer_add_printf(struct evbuffer *buf, char *fmt, ...)
goto end;

res = strlen(msg);
evbuffer_add(buf, msg, res);
if (evbuffer_add(buf, msg, res) == -1)
res = -1;
free(msg);


end:
va_end(ap);

return (res);
}

void
int
evbuffer_add(struct evbuffer *buf, u_char *data, size_t datlen)
{
size_t need = buf->off + datlen;

if (buf->totallen < need) {
if ((buf->buffer = realloc(buf->buffer, need)) == NULL)
err(1, "%s: realloc", __func__);
buf->totallen = need;
void *newbuf;
int length = buf->totallen;

if (length < 256)
length = 256;
while (length < need)
length <<= 1;

if ((newbuf = realloc(buf->buffer, length)) == NULL)
return (-1);

buf->buffer = newbuf;
buf->totallen = length;
}

memcpy(buf->buffer + buf->off, data, datlen);
buf->off += datlen;

return (0);
}

void
Expand Down
8 changes: 4 additions & 4 deletions compat/sys/tree.h
Expand Up @@ -379,7 +379,7 @@ void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
struct type *name##_RB_REMOVE(struct name *, struct type *); \
struct type *name##_RB_INSERT(struct name *, struct type *); \
struct type *name##_RB_FIND(struct name *, struct type *); \
struct type *name##_RB_NEXT(struct name *, struct type *); \
struct type *name##_RB_NEXT(struct type *); \
struct type *name##_RB_MINMAX(struct name *, int); \
\

Expand Down Expand Up @@ -624,7 +624,7 @@ name##_RB_FIND(struct name *head, struct type *elm) \
} \
\
struct type * \
name##_RB_NEXT(struct name *head, struct type *elm) \
name##_RB_NEXT(struct type *elm) \
{ \
if (RB_RIGHT(elm, field)) { \
elm = RB_RIGHT(elm, field); \
Expand Down Expand Up @@ -665,13 +665,13 @@ name##_RB_MINMAX(struct name *head, int val) \
#define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
#define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
#define RB_FIND(name, x, y) name##_RB_FIND(x, y)
#define RB_NEXT(name, x, y) name##_RB_NEXT(x, y)
#define RB_NEXT(name, x, y) name##_RB_NEXT(y)
#define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
#define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)

#define RB_FOREACH(x, name, head) \
for ((x) = RB_MIN(name, head); \
(x) != NULL; \
(x) = name##_RB_NEXT(head, x))
(x) = name##_RB_NEXT(x))

#endif /* _SYS_TREE_H_ */
2 changes: 1 addition & 1 deletion configure.in
Expand Up @@ -61,7 +61,7 @@ dnl Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_TIME

dnl Checks for library functions.
AC_CHECK_FUNCS(gettimeofday)
AC_CHECK_FUNCS(gettimeofday vasprintf)

needsignal=no
haveselect=no
Expand Down

0 comments on commit 5908bd7

Please sign in to comment.