From d1e7e16224c881ee3093658fe4cbf14db244cec7 Mon Sep 17 00:00:00 2001 From: Andrew Shadura Date: Tue, 17 Jun 2014 11:57:32 +0200 Subject: Update to the latest upstream snapshot --- src/examples/Makefile | 3 +- src/examples/async_resolver/async_resolver.c | 27 +++++--- src/examples/echoserver/echoserver.c | 27 +++++--- src/examples/formattertest/formattertest.c | 3 +- src/examples/futuretest/Makefile | 7 -- src/examples/futuretest/futuretest.c | 89 -------------------------- src/examples/helpertest/helpertest.c | 32 +++++---- src/examples/jsontest/Makefile | 7 ++ src/examples/jsontest/jsontest.c | 47 ++++++++++++++ src/examples/libevent-bench/bench.c | 82 +++++++++++++++--------- src/examples/linetest/linetest.c | 27 +++++--- src/examples/listsort/listsort.c | 24 ++++--- src/examples/memslice-bench/memslice-bench.c | 56 +++++++++------- src/examples/patriciatest/patriciatest.c | 32 ++++++--- src/examples/patriciatest2/patriciatest2.c | 40 ++++++++---- src/examples/randomtest/randomtest.c | 8 ++- src/examples/timertest/timertest.c | 9 ++- src/examples/vio-udplistener/vio-udplistener.c | 15 +++-- 18 files changed, 300 insertions(+), 235 deletions(-) delete mode 100644 src/examples/futuretest/Makefile delete mode 100644 src/examples/futuretest/futuretest.c create mode 100644 src/examples/jsontest/Makefile create mode 100644 src/examples/jsontest/jsontest.c (limited to 'src/examples') diff --git a/src/examples/Makefile b/src/examples/Makefile index 8512305..4fb1edd 100644 --- a/src/examples/Makefile +++ b/src/examples/Makefile @@ -1,3 +1,2 @@ -SUBDIRS = echoserver vio-udplistener async_resolver formattertest helpertest libevent-bench linetest listsort memslice-bench patriciatest patriciatest2 randomtest timertest futuretest - +SUBDIRS = echoserver vio-udplistener async_resolver formattertest helpertest jsontest libevent-bench linetest listsort memslice-bench patriciatest patriciatest2 randomtest timertest include ../../buildsys.mk diff --git a/src/examples/async_resolver/async_resolver.c b/src/examples/async_resolver/async_resolver.c index 1a05236..8bd0b12 100644 --- a/src/examples/async_resolver/async_resolver.c +++ b/src/examples/async_resolver/async_resolver.c @@ -8,7 +8,8 @@ typedef struct mowgli_dns_query_t query; } dns_query; -static void resolve_cb(mowgli_dns_reply_t *reply, int reason, void *vptr) +static void +resolve_cb(mowgli_dns_reply_t *reply, int reason, void *vptr) { char buf[2048]; dns_query *dnsquery = vptr; @@ -17,6 +18,7 @@ static void resolve_cb(mowgli_dns_reply_t *reply, int reason, void *vptr) if (reply == NULL) { printf("Got null reply for %s\n", dnsquery->domain); + switch (reason) { case MOWGLI_DNS_RES_NXDOMAIN: @@ -29,6 +31,7 @@ static void resolve_cb(mowgli_dns_reply_t *reply, int reason, void *vptr) printf("Timed out\n"); break; } + goto end; } @@ -37,12 +40,12 @@ static void resolve_cb(mowgli_dns_reply_t *reply, int reason, void *vptr) if (reply->addr.addr.ss_family == AF_INET) { - const struct sockaddr_in *saddr = (const struct sockaddr_in *)&reply->addr.addr; + const struct sockaddr_in *saddr = (const struct sockaddr_in *) &reply->addr.addr; sockptr = &saddr->sin_addr; } else if (reply->addr.addr.ss_family == AF_INET6) { - const struct sockaddr_in6 *saddr = (const struct sockaddr_in6 *)&reply->addr.addr; + const struct sockaddr_in6 *saddr = (const struct sockaddr_in6 *) &reply->addr.addr; sockptr = &saddr->sin6_addr; } else @@ -59,7 +62,8 @@ end: mowgli_free(vptr); } -static void read_data(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata) +static void +read_data(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata) { mowgli_eventloop_pollable_t *pollable = mowgli_eventloop_io_pollable(io); mowgli_dns_t *dns = userdata; @@ -76,11 +80,14 @@ static void read_data(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, return; } else if (ret == 0) + { return; + } buf[--ret] = '\0'; ch = strtok(buf, " "); + while (ch != NULL) { dns_query *dnsquery = mowgli_alloc(sizeof(dns_query)); @@ -99,15 +106,15 @@ static void read_data(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, void *addrptr; struct sockaddr_storage addr; - if(strchr(++ch, ':') != NULL) + if (strchr(++ch, ':') != NULL) { - struct sockaddr_in6 *saddr = (struct sockaddr_in6 *)&addr; + struct sockaddr_in6 *saddr = (struct sockaddr_in6 *) &addr; type = AF_INET6; addrptr = &saddr->sin6_addr; } else { - struct sockaddr_in *saddr = (struct sockaddr_in *)&addr; + struct sockaddr_in *saddr = (struct sockaddr_in *) &addr; type = AF_INET; addrptr = &saddr->sin_addr; } @@ -127,18 +134,22 @@ static void read_data(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_dns_gethost_byaddr(dns, &addr, query); } else + { mowgli_dns_gethost_byname(dns, ch, query, MOWGLI_DNS_T_A); + } dnsquery->domain = mowgli_strdup(ch); ch = strtok(NULL, " "); } } -int main (void) +int +main(void) { mowgli_eventloop_t *evloop = mowgli_eventloop_create(); mowgli_dns_t *dns = mowgli_dns_create(evloop, MOWGLI_DNS_TYPE_ASYNC); mowgli_eventloop_pollable_t *stdin_pollable = mowgli_pollable_create(evloop, STDIN_FILENO, dns); + mowgli_pollable_set_nonblocking(stdin_pollable, true); mowgli_pollable_setselect(evloop, stdin_pollable, MOWGLI_EVENTLOOP_IO_READ, read_data); diff --git a/src/examples/echoserver/echoserver.c b/src/examples/echoserver/echoserver.c index dd1fc05..053b33b 100644 --- a/src/examples/echoserver/echoserver.c +++ b/src/examples/echoserver/echoserver.c @@ -26,23 +26,27 @@ mowgli_eventloop_t *base_eventloop; mowgli_eventloop_pollable_t *listener; -typedef struct { +typedef struct +{ mowgli_eventloop_io_t *io; char buf[1024]; } client_t; #ifdef DEBUG -static void timer_tick(void *unused) +static void +timer_tick(void *unused) { static int ticks = 0; printf("tick: %d\n", ++ticks); } + #endif -static int setup_listener(void) +static int +setup_listener(void) { - struct sockaddr_in in = {}; + struct sockaddr_in in = { }; int fd = socket(AF_INET, SOCK_STREAM, 0); in.sin_family = AF_INET; @@ -59,7 +63,8 @@ static int setup_listener(void) return fd; } -static void write_data(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata) +static void +write_data(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata) { mowgli_eventloop_pollable_t *pollable = mowgli_eventloop_io_pollable(io); client_t *client = userdata; @@ -72,7 +77,8 @@ static void write_data(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_pollable_setselect(base_eventloop, client->io, MOWGLI_EVENTLOOP_IO_WRITE, NULL); } -static void read_data(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata) +static void +read_data(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata) { mowgli_eventloop_pollable_t *pollable = mowgli_eventloop_io_pollable(io); int ret; @@ -90,13 +96,15 @@ static void read_data(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_pollable_setselect(base_eventloop, client->io, MOWGLI_EVENTLOOP_IO_WRITE, write_data); } -static void client_error(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata) +static void +client_error(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata) { mowgli_free(userdata); mowgli_pollable_destroy(eventloop, io); } -static void accept_client(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata) +static void +accept_client(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata) { mowgli_eventloop_pollable_t *pollable = mowgli_eventloop_io_pollable(io); client_t *client; @@ -115,7 +123,8 @@ static void accept_client(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t * mowgli_pollable_setselect(base_eventloop, client->io, MOWGLI_EVENTLOOP_IO_ERROR, client_error); } -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { int fd; diff --git a/src/examples/formattertest/formattertest.c b/src/examples/formattertest/formattertest.c index 9ec014c..f56b671 100644 --- a/src/examples/formattertest/formattertest.c +++ b/src/examples/formattertest/formattertest.c @@ -33,7 +33,8 @@ #include -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { char buf[65535]; diff --git a/src/examples/futuretest/Makefile b/src/examples/futuretest/Makefile deleted file mode 100644 index 3be7e23..0000000 --- a/src/examples/futuretest/Makefile +++ /dev/null @@ -1,7 +0,0 @@ -PROG_NOINST = futuretest${PROG_SUFFIX} -SRCS = futuretest.c - -include ../../../buildsys.mk - -CPPFLAGS += -I../../libmowgli -LIBS += -L../../libmowgli -lmowgli-2 diff --git a/src/examples/futuretest/futuretest.c b/src/examples/futuretest/futuretest.c deleted file mode 100644 index e426733..0000000 --- a/src/examples/futuretest/futuretest.c +++ /dev/null @@ -1,89 +0,0 @@ -/* - * libmowgli: A collection of useful routines for programming. - * futuretest: Combustable lemons - * - * Copyright (c) 2012 Patrick McFarland - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include - -#include - -int main(int argc, char *argv[]) { - char *text = "hello world"; - - printf("create future: "); - mowgli_future_t *future = mowgli_future_create(); - if(future != NULL) - printf("correctly created future\n"); - else - printf("error: abandon all hope\n"); - - printf("get state manually of waiting future: "); - if(mowgli_future_state(future) == MOWGLI_FUTURE_STATE_WAITING) - printf("correctly waiting\n"); - else - printf("error: %i\n", mowgli_future_state(future)); - - printf("finish future: "); - if(mowgli_future_finish(future, text) == MOWGLI_FUTURE_STATE_FINISHED) - printf("correctly finished\n"); - else - printf("error: %i\n", mowgli_future_state(future)); - - printf("get result of finished future: "); - if(mowgli_future_result(future) == text) - printf("correct: %s\n", text); - else - printf("error: %s\n", (char *)mowgli_future_result(future)); - - printf("get state of finished future: "); - if(mowgli_future_state(future) == MOWGLI_FUTURE_STATE_FINISHED) - printf("correctly finished\n"); - else - printf("error: %i\n", mowgli_future_state(future)); - - printf("reinit then cancel: "); - if(mowgli_future_init(future) == 0) { - if(mowgli_future_cancel(future) == MOWGLI_FUTURE_STATE_CANCELED) - printf("correctly canceled\n"); - else - printf("error: failed to cancel: %i\n", mowgli_future_state(future)); - - printf("try to finish on canceled future: "); - if(mowgli_future_finish(future, text) == MOWGLI_FUTURE_STATE_CANCELED) - printf("correctly caught cancel\n"); - else - printf("error: failed to cancel: %s\n", text); - } else { - printf("error: failed to reinit\n"); - } - - printf("reinit then finish twice: "); - if(mowgli_future_init(future) == 0) { - mowgli_future_finish(future, text); - - if(mowgli_future_finish(future, text) == MOWGLI_FUTURE_STATE_CONSISTENCY_FAILURE) - printf("correctly raised consistency failure\n"); - else - printf("error: finished twice: %s\n", text); - } else { - printf("error: failed to reinit\n"); - } -} diff --git a/src/examples/helpertest/helpertest.c b/src/examples/helpertest/helpertest.c index 9106000..fde9b15 100644 --- a/src/examples/helpertest/helpertest.c +++ b/src/examples/helpertest/helpertest.c @@ -25,40 +25,44 @@ int helper_count = 0; -void timer_oneshot(mowgli_eventloop_helper_proc_t *helper) +void +timer_oneshot(mowgli_eventloop_helper_proc_t *helper) { - mowgli_writef(helper->out_fd, "oneshot timer hit\n"); + mowgli_writef(helper->fd, "oneshot timer hit\n"); } -void timer_tick(mowgli_eventloop_helper_proc_t *helper) +void +timer_tick(mowgli_eventloop_helper_proc_t *helper) { static int ticks = 0; - mowgli_writef(helper->out_fd, "tick: %d\n", ++ticks); + mowgli_writef(helper->fd, "tick: %d\n", ++ticks); if (ticks > 10) mowgli_eventloop_break(helper->eventloop); } -void helper_start(mowgli_eventloop_helper_proc_t *helper, void *userdata) +void +helper_start(mowgli_eventloop_helper_proc_t *helper, void *userdata) { mowgli_eventloop_t *eventloop = helper->eventloop; - mowgli_writef(helper->out_fd, "hi from pid %d\n", getpid()); + mowgli_writef(helper->fd, "hi from pid %d\n", getpid()); mowgli_timer_add(eventloop, "timer_tick", (mowgli_event_dispatch_func_t *) timer_tick, helper, 1); mowgli_timer_add_once(eventloop, "timer_oneshot", (mowgli_event_dispatch_func_t *) timer_oneshot, helper, 5); mowgli_eventloop_run(eventloop); - mowgli_writef(helper->out_fd, "eventloop halted\n"); + mowgli_writef(helper->fd, "eventloop halted\n"); mowgli_eventloop_destroy(eventloop); } void helper_read(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata); -void helper_spawn(mowgli_eventloop_t *eventloop) +void +helper_spawn(mowgli_eventloop_t *eventloop) { mowgli_eventloop_helper_proc_t *helper; @@ -71,17 +75,20 @@ void helper_spawn(mowgli_eventloop_t *eventloop) helper_count++; } -void helper_read(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata) +void +helper_read(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata) { size_t r; char buf[16384]; mowgli_eventloop_helper_proc_t *helper = mowgli_eventloop_io_helper(io); bzero(buf, sizeof buf); - r = read(helper->in_fd, buf, sizeof buf); + r = read(helper->fd, buf, sizeof buf); if (r > 0) - printf("helper %p [%d/%d]: %s", helper, helper->child->pid, helper->in_fd, buf); + { + printf("helper %p [%d/%d]: %s", helper, helper->child->pid, helper->fd, buf); + } else if (r <= 0) { helper_count--; @@ -92,7 +99,8 @@ void helper_read(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgl helper_spawn(eventloop); } -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { mowgli_eventloop_t *base_eventloop; diff --git a/src/examples/jsontest/Makefile b/src/examples/jsontest/Makefile new file mode 100644 index 0000000..eb5d82d --- /dev/null +++ b/src/examples/jsontest/Makefile @@ -0,0 +1,7 @@ +PROG_NOINST = jsontest${PROG_SUFFIX} +SRCS = jsontest.c + +include ../../../buildsys.mk + +CPPFLAGS += -I../../libmowgli +LIBS += -L../../libmowgli -lmowgli-2 diff --git a/src/examples/jsontest/jsontest.c b/src/examples/jsontest/jsontest.c new file mode 100644 index 0000000..a63dbc5 --- /dev/null +++ b/src/examples/jsontest/jsontest.c @@ -0,0 +1,47 @@ +#include + +void +out_string(mowgli_json_output_t *out, const char *str, size_t len) +{ + fwrite(str, 1, len, stdout); +} + +void +out_char(mowgli_json_output_t *out, const char c) +{ + fputc(c, stdout); +} + +mowgli_json_output_t out = +{ + .append = out_string, + .append_char = out_char, +}; + +int +main(int argc, char *argv[]) +{ + int i; + mowgli_json_t *n; + + if (argc < 2) + { + printf("Usage: %s file [file ...]\n", argv[0]); + return 1; + } + + for (i = 1; i < argc; i++) + { + n = mowgli_json_parse_file(argv[i]); + + if (n != NULL) + { + mowgli_json_serialize(n, &out, 1); + putchar('\n'); + } + + mowgli_json_decref(n); + } + + return 0; +} diff --git a/src/examples/libevent-bench/bench.c b/src/examples/libevent-bench/bench.c index 0e2cb08..9940b45 100644 --- a/src/examples/libevent-bench/bench.c +++ b/src/examples/libevent-bench/bench.c @@ -28,6 +28,7 @@ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ + /* * Copyright 2003 Niels Provos * All rights reserved. @@ -63,19 +64,20 @@ * */ -#define timersub(tvp, uvp, vvp) \ - do { \ - (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ - (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ - if ((vvp)->tv_usec < 0) { \ - (vvp)->tv_sec--; \ - (vvp)->tv_usec += 1000000; \ - } \ +#define timersub(tvp, uvp, vvp) \ + do \ + { \ + (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ + (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ + if ((vvp)->tv_usec < 0) \ + { \ + (vvp)->tv_sec--; \ + (vvp)->tv_usec += 1000000; \ + } \ } while (0) #include - static int count, writes, fired; static mowgli_eventloop_t *base_eventloop; static mowgli_descriptor_t *pipes; @@ -93,13 +95,17 @@ void read_cb(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *arg) { mowgli_eventloop_pollable_t *pollable = mowgli_eventloop_io_pollable(io); + int idx = (int) (long) arg, widx = idx + 1; u_char ch; count += read(pollable->fd, &ch, sizeof(ch)); - if (writes) { + + if (writes) + { if (widx >= num_pipes) widx -= num_pipes; + write(pipes[2 * widx + 1], "e", 1); writes--; fired++; @@ -110,24 +116,28 @@ read_cb(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventlo void read_thunk(struct ev_io *w, int revents) { - read_cb (w->fd, revents, w->data); + read_cb(w->fd, revents, w->data); } void -timer_cb (struct ev_timer *w, int revents) +timer_cb(struct ev_timer *w, int revents) { - /* nop */ + /* nop */ } + #endif struct timeval * run_once(void) { int *cp, i, space; + static struct timeval ta, ts, te; gettimeofday(&ta, NULL); - for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) { + + for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) + { if (events[i] != NULL) mowgli_pollable_destroy(base_eventloop, events[i]); @@ -138,6 +148,7 @@ run_once(void) fired = 0; space = num_pipes / num_active; space = space * 2; + for (i = 0; i < num_active; i++, fired++) write(pipes[i * space + 1], "e", 1); @@ -146,7 +157,9 @@ run_once(void) int xcount = 0; gettimeofday(&ts, NULL); - do { + + do + { mowgli_eventloop_run_once(base_eventloop); xcount++; } while (count != fired); @@ -158,15 +171,16 @@ run_once(void) fprintf(stdout, "%ld\t%ld\n", ta.tv_sec * 1000000L + ta.tv_usec, ts.tv_sec * 1000000L + ts.tv_usec - ); + ); - return (&te); + return &te; } int -main (int argc, char **argv) +main(int argc, char **argv) { struct rlimit rl; + int i, c; int *cp; extern char *optarg; @@ -174,8 +188,11 @@ main (int argc, char **argv) num_pipes = 100; num_active = 1; num_writes = num_pipes; - while ((c = getopt(argc, argv, "n:a:w:te")) != -1) { - switch (c) { + + while ((c = getopt(argc, argv, "n:a:w:te")) != -1) + { + switch (c) + { case 'n': num_pipes = atoi(optarg); break; @@ -186,7 +203,7 @@ main (int argc, char **argv) num_writes = atoi(optarg); break; case 't': - timers = 1; + timers = 1; break; default: fprintf(stderr, "Illegal argument \"%c\"\n", c); @@ -196,14 +213,17 @@ main (int argc, char **argv) #if 1 rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50; - if (setrlimit(RLIMIT_NOFILE, &rl) == -1) { + + if (setrlimit(RLIMIT_NOFILE, &rl) == -1) perror("setrlimit"); - } + #endif events = calloc(num_pipes * 2, sizeof(mowgli_eventloop_pollable_t *)); pipes = calloc(num_pipes * 2, sizeof(mowgli_descriptor_t)); - if (events == NULL || pipes == NULL) { + + if ((events == NULL) || (pipes == NULL)) + { perror("malloc"); exit(1); } @@ -211,20 +231,24 @@ main (int argc, char **argv) mowgli_thread_set_policy(MOWGLI_THREAD_POLICY_DISABLED); base_eventloop = mowgli_eventloop_create(); - for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) { + for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) + { #ifdef USE_PIPES - if (pipe(cp) == -1) { + + if (pipe(cp) == -1) + { #else - if (socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) { + + if (socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) + { #endif perror("pipe"); exit(1); } } - for (i = 0; i < 2; i++) { + for (i = 0; i < 2; i++) run_once(); - } exit(0); } diff --git a/src/examples/linetest/linetest.c b/src/examples/linetest/linetest.c index 98b8064..3175b84 100644 --- a/src/examples/linetest/linetest.c +++ b/src/examples/linetest/linetest.c @@ -27,22 +27,26 @@ mowgli_eventloop_t *base_eventloop; char buf[512]; -typedef struct { +typedef struct +{ mowgli_linebuf_t *linebuf; } client_t; void eat_line(mowgli_linebuf_t *linebuf, char *line, size_t len, void *userdata); -void write_line(mowgli_linebuf_t *linebuf, char *buf, size_t len) +void +write_line(mowgli_linebuf_t *linebuf, char *buf, size_t len) { - printf("> %s\n", buf); + printf("-> %s\n", buf); mowgli_linebuf_write(linebuf, buf, len); } -client_t * create_client(const char *server, const char *port, const char *nick, const char *user, const char *realname) +client_t * +create_client(const char *server, const char *port, const char *nick, const char *user, const char *realname) { client_t *client; struct addrinfo hints, *res; + bool use_ssl = false; mowgli_vio_sockaddr_t addr; int ret; @@ -77,12 +81,11 @@ client_t * create_client(const char *server, const char *port, const char *nick, /* Wrap the VIO object */ if (use_ssl) - { - if (mowgli_vio_openssl_setssl(linebuf->vio, NULL) != 0) + if (mowgli_vio_openssl_setssl(linebuf->vio, NULL, NULL) != 0) return NULL; - } /* We have to have a socket before starting the linebuf */ + if (mowgli_vio_socket(linebuf->vio, res->ai_family, res->ai_socktype, res->ai_protocol) != 0) return NULL; @@ -103,7 +106,8 @@ client_t * create_client(const char *server, const char *port, const char *nick, return client; } -void eat_line(mowgli_linebuf_t *linebuf, char *line, size_t len, void *userdata) +void +eat_line(mowgli_linebuf_t *linebuf, char *line, size_t len, void *userdata) { char str[512]; @@ -114,12 +118,13 @@ void eat_line(mowgli_linebuf_t *linebuf, char *line, size_t len, void *userdata) strncpy(str, line, sizeof(str)); str[len + 1] = '\0'; - printf("-> %s\n", str); + printf("<- %s\n", str); /* Since this is just a basic example, we don't have a real dispatcher :p */ if (strstr(str, "PING")) { char *pos = strpbrk(str, ":"); + if (pos) { char buf[512]; @@ -131,7 +136,8 @@ void eat_line(mowgli_linebuf_t *linebuf, char *line, size_t len, void *userdata) return; } -int main(int argc, const char *argv[]) +int +main(int argc, const char *argv[]) { client_t *client; const char *serv, *port; @@ -150,6 +156,7 @@ int main(int argc, const char *argv[]) port = argv[2]; client = create_client(serv, port, "Mowglibot", "Mowglibot", "The libmowgli example bot that does nothing useful"); + if (client == NULL) return EXIT_FAILURE; diff --git a/src/examples/listsort/listsort.c b/src/examples/listsort/listsort.c index 7aa44c2..17768b5 100644 --- a/src/examples/listsort/listsort.c +++ b/src/examples/listsort/listsort.c @@ -34,18 +34,21 @@ #include #ifdef _WIN32 -#define strcasecmp _stricmp +# define strcasecmp _stricmp #endif -int str_comparator(mowgli_node_t *n, mowgli_node_t *n2, void *opaque) +int +str_comparator(mowgli_node_t *n, mowgli_node_t *n2, void *opaque) { - int ret; + int ret; + ret = strcasecmp(n->data, n2->data); return ret; } -void test_strings(void) +void +test_strings(void) { mowgli_list_t l = { NULL, NULL, 0 }; mowgli_node_t *n, *tn; @@ -73,12 +76,13 @@ void test_strings(void) MOWGLI_LIST_FOREACH_SAFE(n, tn, l.head) { - printf(" %s\n", (char*) n->data); + printf(" %s\n", (char *) n->data); mowgli_node_delete(n, &l); } } -int int_comparator(mowgli_node_t *n, mowgli_node_t *n2, void *opaque) +int +int_comparator(mowgli_node_t *n, mowgli_node_t *n2, void *opaque) { long a = (long) n->data; long b = (long) n2->data; @@ -86,11 +90,12 @@ int int_comparator(mowgli_node_t *n, mowgli_node_t *n2, void *opaque) return a - b; } -void test_integers(void) +void +test_integers(void) { mowgli_list_t l = { NULL, NULL, 0 }; mowgli_node_t *n, *tn; - + mowgli_node_add((void *) 3, mowgli_node_create(), &l); mowgli_node_add((void *) 2, mowgli_node_create(), &l); mowgli_node_add((void *) 4, mowgli_node_create(), &l); @@ -107,7 +112,8 @@ void test_integers(void) } } -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { test_strings(); test_integers(); diff --git a/src/examples/memslice-bench/memslice-bench.c b/src/examples/memslice-bench/memslice-bench.c index 3fb05d5..3ed95fc 100644 --- a/src/examples/memslice-bench/memslice-bench.c +++ b/src/examples/memslice-bench/memslice-bench.c @@ -18,15 +18,17 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#define timersub(tvp, uvp, vvp) \ - do { \ - (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ - (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ - if ((vvp)->tv_usec < 0) { \ - (vvp)->tv_sec--; \ - (vvp)->tv_usec += 1000000; \ - } \ - } while (0) +#define timersub(tvp, uvp, vvp) \ + do \ + { \ + (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ + (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ + if ((vvp)->tv_usec < 0) \ + { \ + (vvp)->tv_sec--; \ + (vvp)->tv_usec += 1000000; \ + } \ + } while (0) #include @@ -37,10 +39,11 @@ int main(int argc, char *argv[]) { size_t i; + size_t objects; size_t *obj_sizes; void **ptrs; - struct timeval ts, te; + struct timeval ts, te; mowgli_thread_set_policy(MOWGLI_THREAD_POLICY_DISABLED); @@ -66,52 +69,57 @@ main(int argc, char *argv[]) printf("Going to allocate %zu objects of random sizes < 256\n", objects); printf("Assigning sizes...\n"); - for (i = 0; i < objects; i++) { + + for (i = 0; i < objects; i++) obj_sizes[i] = rand() % 256; - } + printf("Done! Lets benchmark.\n"); /* allocate using sysmalloc */ gettimeofday(&ts, NULL); - for (i = 0; i < objects; i++) { + + for (i = 0; i < objects; i++) ptrs[i] = mowgli_alloc_using_policy(sysmalloc, obj_sizes[i]); - } + gettimeofday(&te, NULL); timersub(&te, &ts, &ts); printf("sysmalloc alloc time: %ld usec\n", - ts.tv_sec * 1000000L + ts.tv_usec); + ts.tv_sec * 1000000L + ts.tv_usec); gettimeofday(&ts, NULL); - for (i = 0; i < objects; i++) { + + for (i = 0; i < objects; i++) mowgli_free(ptrs[i]); - } + gettimeofday(&te, NULL); timersub(&te, &ts, &ts); printf("sysmalloc free time: %ld usec\n", - ts.tv_sec * 1000000L + ts.tv_usec); + ts.tv_sec * 1000000L + ts.tv_usec); /* allocate using memslice */ gettimeofday(&ts, NULL); - for (i = 0; i < objects; i++) { + + for (i = 0; i < objects; i++) ptrs[i] = mowgli_alloc_using_policy(memslice, obj_sizes[i]); - } + gettimeofday(&te, NULL); timersub(&te, &ts, &ts); printf("memslice alloc time: %ld usec\n", - ts.tv_sec * 1000000L + ts.tv_usec); + ts.tv_sec * 1000000L + ts.tv_usec); gettimeofday(&ts, NULL); - for (i = 0; i < objects; i++) { + + for (i = 0; i < objects; i++) mowgli_free(ptrs[i]); - } + gettimeofday(&te, NULL); timersub(&te, &ts, &ts); printf("memslice free time: %ld usec\n", - ts.tv_sec * 1000000L + ts.tv_usec); + ts.tv_sec * 1000000L + ts.tv_usec); return EXIT_SUCCESS; } diff --git a/src/examples/patriciatest/patriciatest.c b/src/examples/patriciatest/patriciatest.c index 98c5f65..4dbb212 100644 --- a/src/examples/patriciatest/patriciatest.c +++ b/src/examples/patriciatest/patriciatest.c @@ -35,18 +35,21 @@ int errors = 0; -void str_canon(char *key) +void +str_canon(char *key) { return; } -void statscb(const char *line, void *data) +void +statscb(const char *line, void *data) { printf("%s\n", line); } /* assumes data is key */ -static void check_all_retrievable(mowgli_patricia_t *dtree) +static void +check_all_retrievable(mowgli_patricia_t *dtree) { mowgli_patricia_iteration_state_t state; void *elem, *elem2; @@ -58,37 +61,45 @@ static void check_all_retrievable(mowgli_patricia_t *dtree) n2 = mowgli_patricia_size(dtree); MOWGLI_PATRICIA_FOREACH(elem, &state, dtree) { - elem2 = mowgli_patricia_retrieve(dtree, (const char *)elem); + elem2 = mowgli_patricia_retrieve(dtree, (const char *) elem); + if (elem2 == NULL) { errors++; printf("failed to find element %s\n", - (const char *)elem); + (const char *) elem); } else if (strcmp(elem2, elem)) { printf("element %s != %s\n", - (const char *)elem, - (const char *)elem2); + (const char *) elem, + (const char *) elem2); errors++; } else + { printf("."); + } + fflush(stdout); n1++; + if (n1 > n2 * 2) break; } + if (n1 != n2) { errors++; printf("number of iterated elements %u != size %u\n", n1, n2); } + printf("\n"); fflush(stdout); } -void test_patricia(void) +void +test_patricia(void) { mowgli_patricia_t *dtree; mowgli_patricia_iteration_state_t state; @@ -119,7 +130,7 @@ void test_patricia(void) MOWGLI_PATRICIA_FOREACH(elem, &state, dtree) { - printf("element -> %s\n", (const char *)elem); + printf("element -> %s\n", (const char *) elem); } printf("End of elements\n"); mowgli_patricia_stats(dtree, statscb, NULL); @@ -147,7 +158,8 @@ void test_patricia(void) mowgli_patricia_destroy(dtree, NULL, NULL); } -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { test_patricia(); diff --git a/src/examples/patriciatest2/patriciatest2.c b/src/examples/patriciatest2/patriciatest2.c index d73b1ee..800c59e 100644 --- a/src/examples/patriciatest2/patriciatest2.c +++ b/src/examples/patriciatest2/patriciatest2.c @@ -37,17 +37,19 @@ int errors = 0; -void str_canon(char *key) +void +str_canon(char *key) { return; } -void statscb(const char *line, void *data) -{ -} +void +statscb(const char *line, void *data) +{ } /* assumes data is key */ -static void check_all_retrievable(mowgli_patricia_t *dtree) +static void +check_all_retrievable(mowgli_patricia_t *dtree) { mowgli_patricia_iteration_state_t state; void *elem, *elem2; @@ -57,24 +59,28 @@ static void check_all_retrievable(mowgli_patricia_t *dtree) n2 = mowgli_patricia_size(dtree); MOWGLI_PATRICIA_FOREACH(elem, &state, dtree) { - elem2 = mowgli_patricia_retrieve(dtree, (const char *)elem); + elem2 = mowgli_patricia_retrieve(dtree, (const char *) elem); + if (elem2 == NULL) { errors++; printf("failed to find element %s\n", - (const char *)elem); + (const char *) elem); } else if (strcmp(elem2, elem)) { printf("element %s != %s\n", - (const char *)elem, - (const char *)elem2); + (const char *) elem, + (const char *) elem2); errors++; } + n1++; + if (n1 > n2 * 2) break; } + if (n1 != n2) { errors++; @@ -82,17 +88,20 @@ static void check_all_retrievable(mowgli_patricia_t *dtree) } } -void test_patricia(void) +void +test_patricia(void) { mowgli_patricia_t *dtree; int i, j; char buf[100], *strings[TESTSIZE]; srandom(12346); + for (i = 0; i < TESTSIZE; i++) { for (j = 0; j < 40; j++) buf[j] = 'a' + random() % 26; + buf[20 + random() % 20] = '\0'; strings[i] = strdup(buf); } @@ -110,12 +119,14 @@ void test_patricia(void) for (i = 0; i < TESTSIZE / 2; i++) { mowgli_patricia_delete(dtree, strings[i]); + if (mowgli_patricia_retrieve(dtree, strings[i])) { printf("still retrievable after delete: %s\n", - strings[i]); + strings[i]); errors++; } + check_all_retrievable(dtree); } @@ -128,12 +139,14 @@ void test_patricia(void) for (i = 0; i < TESTSIZE; i++) { mowgli_patricia_delete(dtree, strings[i]); + if (mowgli_patricia_retrieve(dtree, strings[i])) { printf("still retrievable after delete: %s\n", - strings[i]); + strings[i]); errors++; } + check_all_retrievable(dtree); } @@ -143,7 +156,8 @@ void test_patricia(void) free(strings[i]); } -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { test_patricia(); diff --git a/src/examples/randomtest/randomtest.c b/src/examples/randomtest/randomtest.c index 2c4b2d7..eb7ef05 100644 --- a/src/examples/randomtest/randomtest.c +++ b/src/examples/randomtest/randomtest.c @@ -33,16 +33,20 @@ */ #include -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { mowgli_random_t *r = mowgli_random_create(); int i; printf("1000 iterations:\n"); + for (i = 0; i < 1000; i++) { printf("%10u ", mowgli_random_int(r)); - if (i % 5 == 4) printf("\n"); + + if (i % 5 == 4) + printf("\n"); } mowgli_object_unref(r); diff --git a/src/examples/timertest/timertest.c b/src/examples/timertest/timertest.c index 242b8a9..f99410b 100644 --- a/src/examples/timertest/timertest.c +++ b/src/examples/timertest/timertest.c @@ -25,12 +25,14 @@ mowgli_eventloop_t *eventloop; -void timer_oneshot(void *unused) +void +timer_oneshot(void *unused) { printf("oneshot timer hit\n"); } -void timer_tick(void *unused) +void +timer_tick(void *unused) { static int ticks = 0; @@ -40,7 +42,8 @@ void timer_tick(void *unused) mowgli_eventloop_break(eventloop); } -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { eventloop = mowgli_eventloop_create(); diff --git a/src/examples/vio-udplistener/vio-udplistener.c b/src/examples/vio-udplistener/vio-udplistener.c index 9de4e69..1e1dda1 100644 --- a/src/examples/vio-udplistener/vio-udplistener.c +++ b/src/examples/vio-udplistener/vio-udplistener.c @@ -7,18 +7,19 @@ #define BUFSIZE 2048 -#define PROTO AF_INET6 -#define LISTEN "::ffff:127.0.0.1" /* 6to4 mapping */ -#define PORT 31337 +#define PROTO AF_INET6 +#define LISTEN "::ffff:127.0.0.1" /* 6to4 mapping */ +#define PORT 31337 #define ECHOBACK "Echo: " -int main (void) +int +main(void) { mowgli_vio_t *vio = mowgli_vio_create(NULL); mowgli_vio_sockaddr_t addr; - - mowgli_vio_sockaddr_create(&addr, PROTO, LISTEN, 31337); + + mowgli_vio_sockaddr_create(&addr, PROTO, LISTEN, PORT); if (mowgli_vio_socket(vio, PROTO, SOCK_DGRAM, 0)) return EXIT_FAILURE; @@ -41,5 +42,5 @@ int main (void) mowgli_vio_sendto(vio, buf, strlen(buf), &addr); } - return EXIT_SUCCESS; /* Not reached */ + return EXIT_SUCCESS; /* Not reached */ } -- cgit v1.2.3