diff options
author | Fedor Indutny <fedor@indutny.com> | 2016-05-27 23:50:46 -0400 |
---|---|---|
committer | Fedor Indutny <fedor@indutny.com> | 2016-05-27 23:50:46 -0400 |
commit | d4069cc59b0c9d4ce00977a9268f90e306f1cdf8 (patch) | |
tree | b4d35fb43555503db6566de9d0cf1cd71d6497f1 | |
parent | 6a1f2335d96dfaf95a4423b94c36df389e3d4ec2 (diff) | |
download | uv_link_t-d4069cc59b0c9d4ce00977a9268f90e306f1cdf8.tar.gz |
api: inherit structs from uv_link_t
-rw-r--r-- | README.md | 7 | ||||
-rw-r--r-- | docs/api.md | 44 | ||||
-rw-r--r-- | example/src/main.c | 10 | ||||
-rw-r--r-- | include/uv_link_t.h | 55 | ||||
-rw-r--r-- | src/uv_link_observer_t.c | 40 | ||||
-rw-r--r-- | src/uv_link_source_t.c | 22 | ||||
-rw-r--r-- | test/src/test-uv-link-observer-t.c | 8 | ||||
-rw-r--r-- | test/src/test-uv-link-source-t.c | 26 |
8 files changed, 122 insertions, 90 deletions
diff --git a/README.md b/README.md index 981bbe0..3fcad81 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ uv_link_source_t source; uv_link_source_init(uv_default_loop(), &source, stream); ``` -A chain can be formed with `&source.link` and any other `uv_link_t` instance: +A chain can be formed with `&source` and any other `uv_link_t` instance: ```c uv_link_t link; @@ -74,7 +74,9 @@ static int shutdown_impl(uv_link_t* link, } ``` -Please see [uv_link_t.h][1] for additional information. +## Technical details + +[API Docs][2] ## LICENSE @@ -103,3 +105,4 @@ USE OR OTHER DEALINGS IN THE SOFTWARE. [0]: https://github.com/libuv/libuv [1]: https://github.com/indutny/uv_link_t/blob/master/include/uv_link_t.h +[2]: https://github.com/indutny/uv_link_t/blob/master/docs/api.md diff --git a/docs/api.md b/docs/api.md new file mode 100644 index 0000000..18e73c1 --- /dev/null +++ b/docs/api.md @@ -0,0 +1,44 @@ +# uv_link_t + +## Types + +### uv_link_t + +The base unit of all APIs. Pointer to this structure is what needs to be passed +to the most of the methods. + +### uv_link_methods_t + +TODO(indutny) + +### uv_link_source_t + +TODO(indutny) + +### uv_link_observer_t + +TODO(indutny) + +## Methods + +### uv_link_t + +#### int uv_link_init(...) +#### void uv_link_close(...) +#### int uv_link_chain(...) +#### int uv_link_unchain(...) +#### void uv_link_propagate_alloc_cb(...) +#### void uv_link_propagate_read_cb(...) +#### int uv_link_propagate_write(...) +#### int uv_link_propagate_shutdown(...) +#### void uv_link_propagate_close(...) +#### int uv_link_read_start(...) +#### int uv_link_read_stop(...) +#### int uv_link_write(...) +#### int uv_link_try_write(...) +#### int uv_link_shutdown(...) + +### uv_link_source_t + +#### int uv_link_source_init(...) +#### int uv_link_observer_init(...) diff --git a/example/src/main.c b/example/src/main.c index 5c847ec..215a35a 100644 --- a/example/src/main.c +++ b/example/src/main.c @@ -34,7 +34,7 @@ static void read_cb(uv_link_observer_t* observer, const uv_buf_t* buf) { client_t* client; - client = observer->link.data; + client = observer->data; if (nread < 0) { fprintf(stderr, "error or close\n"); @@ -56,14 +56,14 @@ static void connection_cb(uv_stream_t* s, int status) { CHECK(uv_link_source_init(&client->source, (uv_stream_t*) &client->tcp)); CHECK(uv_link_init(&client->middle, &middle_methods)); - CHECK(uv_link_chain(&client->source.link, &client->middle)); + CHECK(uv_link_chain((uv_link_t*) &client->source, &client->middle)); CHECK(uv_link_observer_init(&client->observer)); - CHECK(uv_link_chain(&client->middle, &client->observer.link)); + CHECK(uv_link_chain(&client->middle, (uv_link_t*) &client->observer)); client->observer.read_cb = read_cb; - client->observer.link.data = client; + client->observer.data = client; - CHECK(uv_link_read_start(&client->observer.link)); + CHECK(uv_link_read_start((uv_link_t*) &client->observer)); } diff --git a/include/uv_link_t.h b/include/uv_link_t.h index f1a1dc6..03d34df 100644 --- a/include/uv_link_t.h +++ b/include/uv_link_t.h @@ -45,27 +45,30 @@ struct uv_link_methods_s { uv_link_read_cb read_cb_override; }; -struct uv_link_s { - uv_link_t* parent; - uv_link_t* child; - - uv_link_alloc_cb alloc_cb; - uv_link_read_cb read_cb; - - void* data; - - /* Read-only after assigning initial values */ +#define UV_LINK_FIELDS \ + uv_link_t* parent; \ + uv_link_t* child; \ + \ + uv_link_alloc_cb alloc_cb; \ + uv_link_read_cb read_cb; \ + \ + void* data; \ + \ + /* Read-only after assigning initial values */ \ + \ + /* Sort of virtual table */ \ + uv_link_methods_t const* methods; \ + \ + /* Private, used for chain/unchain */ \ + uv_link_alloc_cb saved_alloc_cb; \ + uv_link_read_cb saved_read_cb; \ + \ + /* Private, used for close */ \ + int close_waiting; \ + uv_link_close_cb saved_close_cb; - /* Sort of virtual table */ - uv_link_methods_t const* methods; - - /* Private, used for chain/unchain */ - uv_link_alloc_cb saved_alloc_cb; - uv_link_read_cb saved_read_cb; - - /* Private, used for close */ - int close_waiting; - uv_link_close_cb saved_close_cb; +struct uv_link_s { + UV_LINK_FIELDS }; UV_EXTERN int uv_link_init(uv_link_t* link, uv_link_methods_t const* methods); @@ -129,7 +132,8 @@ static int uv_link_shutdown(uv_link_t* link, uv_link_shutdown_cb cb, /* Link Source */ struct uv_link_source_s { - uv_link_t link; + /* inherits(uv_link_source_t, uv_link_t) */ + UV_LINK_FIELDS uv_stream_t* stream; @@ -144,12 +148,13 @@ UV_EXTERN int uv_link_source_init(uv_link_source_t* source, /* Link Observer */ struct uv_link_observer_s { - uv_link_t link; + /* inherits(uv_link_observer_t, uv_link_t) */ + UV_LINK_FIELDS /* This will be called, even if the ones in `link` will be overwritten */ - void (*read_cb)(uv_link_observer_t* observer, - ssize_t nread, - const uv_buf_t* buf); + void (*observer_read_cb)(uv_link_observer_t* observer, + ssize_t nread, + const uv_buf_t* buf); }; UV_EXTERN int uv_link_observer_init(uv_link_observer_t* observer); diff --git a/src/uv_link_observer_t.c b/src/uv_link_observer_t.c index dbd42ba..395a97c 100644 --- a/src/uv_link_observer_t.c +++ b/src/uv_link_observer_t.c @@ -5,20 +5,12 @@ static int uv_link_observer_read_start(uv_link_t* link) { - uv_link_observer_t* observer; - - observer = container_of(link, uv_link_observer_t, link); - - return uv_link_read_start(observer->link.parent); + return uv_link_read_start(link->parent); } static int uv_link_observer_read_stop(uv_link_t* link) { - uv_link_observer_t* observer; - - observer = container_of(link, uv_link_observer_t, link); - - return uv_link_read_stop(observer->link.parent); + return uv_link_read_stop(link->parent); } @@ -29,11 +21,7 @@ static int uv_link_observer_write(uv_link_t* link, uv_stream_t* send_handle, uv_link_write_cb cb, void* arg) { - uv_link_observer_t* observer; - - observer = container_of(link, uv_link_observer_t, link); - - return uv_link_propagate_write(observer->link.parent, source, bufs, nbufs, + return uv_link_propagate_write(link->parent, source, bufs, nbufs, send_handle, cb, arg); } @@ -41,11 +29,7 @@ static int uv_link_observer_write(uv_link_t* link, static int uv_link_observer_try_write(uv_link_t* link, const uv_buf_t bufs[], unsigned int nbufs) { - uv_link_observer_t* observer; - - observer = container_of(link, uv_link_observer_t, link); - - return uv_link_try_write(observer->link.parent, bufs, nbufs); + return uv_link_try_write(link->parent, bufs, nbufs); } @@ -53,11 +37,7 @@ static int uv_link_observer_shutdown(uv_link_t* link, uv_link_t* source, uv_link_shutdown_cb cb, void* arg) { - uv_link_observer_t* observer; - - observer = container_of(link, uv_link_observer_t, link); - - return uv_link_propagate_shutdown(observer->link.parent, source, cb, arg); + return uv_link_propagate_shutdown(link->parent, source, cb, arg); } @@ -73,10 +53,10 @@ static void uv_link_observer_read_cb(uv_link_t* link, const uv_buf_t* buf) { uv_link_observer_t* observer; - observer = container_of(link, uv_link_observer_t, link); + observer = (uv_link_observer_t*) link; - if (observer->read_cb != NULL) - observer->read_cb(observer, nread, buf); + if (observer->observer_read_cb != NULL) + observer->observer_read_cb(observer, nread, buf); return uv_link_propagate_read_cb(link, nread, buf); } @@ -104,9 +84,9 @@ static uv_link_methods_t uv_link_observer_methods = { int uv_link_observer_init(uv_link_observer_t* observer) { int err; - memset(observer, 0, sizeof(*observer)); + observer->read_cb = NULL; - err = uv_link_init(&observer->link, &uv_link_observer_methods); + err = uv_link_init((uv_link_t*) observer, &uv_link_observer_methods); if (err != 0) return err; diff --git a/src/uv_link_source_t.c b/src/uv_link_source_t.c index 3d94ab9..96fc937 100644 --- a/src/uv_link_source_t.c +++ b/src/uv_link_source_t.c @@ -28,7 +28,7 @@ static void uv_link_source_wrap_alloc_cb(uv_handle_t* handle, source = handle->data; - uv_link_propagate_alloc_cb(&source->link, suggested_size, buf); + uv_link_propagate_alloc_cb((uv_link_t*) source, suggested_size, buf); } @@ -39,14 +39,14 @@ static void uv_link_source_wrap_read_cb(uv_stream_t* stream, source = stream->data; - uv_link_propagate_read_cb(&source->link, nread, buf); + uv_link_propagate_read_cb((uv_link_t*) source, nread, buf); } static int uv_link_source_read_start(uv_link_t* link) { uv_link_source_t* source; - source = container_of(link, uv_link_source_t, link); + source = (uv_link_source_t*) link; return uv_read_start(source->stream, uv_link_source_wrap_alloc_cb, @@ -57,7 +57,7 @@ static int uv_link_source_read_start(uv_link_t* link) { static int uv_link_source_read_stop(uv_link_t* link) { uv_link_source_t* source; - source = container_of(link, uv_link_source_t, link); + source = (uv_link_source_t*) link; return uv_read_stop(source->stream); } @@ -82,7 +82,7 @@ static int uv_link_source_write(uv_link_t* link, uv_link_source_t* s; uv_link_source_write_t* req; - s= container_of(link, uv_link_source_t, link); + s = (uv_link_source_t*) link; req = malloc(sizeof(*req)); if (req == NULL) return UV_ENOMEM; @@ -101,7 +101,7 @@ static int uv_link_source_try_write(uv_link_t* link, unsigned int nbufs) { uv_link_source_t* source; - source = container_of(link, uv_link_source_t, link); + source = (uv_link_source_t*) link; return uv_try_write(source->stream, bufs, nbufs); } @@ -123,7 +123,7 @@ static int uv_link_source_shutdown(uv_link_t* link, uv_link_source_t* s; uv_link_source_shutdown_t* req; - s = container_of(link, uv_link_source_t, link); + s = (uv_link_source_t*) link; req = malloc(sizeof(*req)); if (req == NULL) @@ -151,7 +151,7 @@ static void uv_link_source_close(uv_link_t* link, uv_link_t* source, uv_link_close_cb cb) { uv_link_source_t* s; - s = container_of(link, uv_link_source_t, link); + s = (uv_link_source_t*) link; s->close_cb = cb; s->close_source = source; @@ -174,14 +174,14 @@ int uv_link_source_init(uv_link_source_t* source, uv_stream_t* stream) { int err; - memset(source, 0, sizeof(*source)); - - err = uv_link_init(&source->link, &uv_link_source_methods); + err = uv_link_init((uv_link_t*) source, &uv_link_source_methods); if (err != 0) return err; source->stream = stream; source->stream->data = source; + source->close_cb = NULL; + source->close_source = NULL; return 0; } diff --git a/test/src/test-uv-link-observer-t.c b/test/src/test-uv-link-observer-t.c index 0ce7b80..d81b679 100644 --- a/test/src/test-uv-link-observer-t.c +++ b/test/src/test-uv-link-observer-t.c @@ -40,7 +40,7 @@ static void observer_read_cb(uv_link_observer_t* o, static void close_cb(uv_link_t* link) { - CHECK_EQ(link, &observer.link, "close_cb link"); + CHECK_EQ(link, (uv_link_t*) &observer, "close_cb link"); close_cb_called++; } @@ -52,10 +52,10 @@ TEST_IMPL(uv_link_observer_t) { CHECK_EQ(uv_link_init(&source, &methods), 0, "uv_link_init(source)"); CHECK_EQ(uv_link_observer_init(&observer), 0, "uv_link_observer_init()"); - CHECK_EQ(uv_link_chain(&source, &observer.link), 0, + CHECK_EQ(uv_link_chain(&source, (uv_link_t*) &observer), 0, "uv_link_chain"); - observer.read_cb = observer_read_cb; + observer.observer_read_cb = observer_read_cb; uv_link_propagate_alloc_cb(&source, 1024, &buf); @@ -63,7 +63,7 @@ TEST_IMPL(uv_link_observer_t) { uv_link_propagate_read_cb(&source, 1, &buf); CHECK_EQ(observer_read_cb_called, 1, "observer.read_cb must be called"); - uv_link_close(&observer.link, close_cb); + uv_link_close((uv_link_t*) &observer, close_cb); CHECK_EQ(fake_close_called, 1, "fake close count"); CHECK_EQ(close_cb_called, 1, "close_cb count"); } diff --git a/test/src/test-uv-link-source-t.c b/test/src/test-uv-link-source-t.c index 1d12ac0..4024f5f 100644 --- a/test/src/test-uv-link-source-t.c +++ b/test/src/test-uv-link-source-t.c @@ -27,7 +27,7 @@ static void read_one() { } static void source_write_cb(uv_link_t* link, int status, void* arg) { - CHECK_EQ(link, &source.link, "link == &source.link"); + CHECK_EQ(link, (uv_link_t*) &source, "link == &source"); CHECK_EQ(arg, &test_arg, "arg == &test_arg"); write_cb_called++; @@ -41,18 +41,18 @@ static void test_writes() { /* .write() should work */ buf = uv_buf_init("x", 1); - CHECK_EQ(uv_link_write(&source.link, &buf, 1, NULL, source_write_cb, + CHECK_EQ(uv_link_write((uv_link_t*) &source, &buf, 1, NULL, source_write_cb, &test_arg), 0, - "source.link.write() should return 0"); + "source.write() should return 0"); CHECK_EQ(uv_run(loop, UV_RUN_DEFAULT), 0, "uv_run()"); CHECK_EQ(write_cb_called, 1, "source_write_cb() must be called"); /* .try_write() should work too */ - CHECK_EQ(uv_link_try_write(&source.link, &buf, 1), 1, - "source.link.try_write() should return 1"); + CHECK_EQ(uv_link_try_write((uv_link_t*) &source, &buf, 1), 1, + "source.try_write() should return 1"); read_one(); } @@ -62,7 +62,7 @@ static void source_alloc_cb(uv_link_t* link, uv_buf_t* buf) { static char storage[1024]; - CHECK_EQ(link, &source.link, "link == &source.link"); + CHECK_EQ(link, (uv_link_t*) &source, "link == &source"); alloc_cb_called++; @@ -73,14 +73,14 @@ static void source_alloc_cb(uv_link_t* link, static void source_read_cb(uv_link_t* link, ssize_t nread, const uv_buf_t* buf) { - CHECK_EQ(link, &source.link, "link == &source.link"); + CHECK_EQ(link, (uv_link_t*) &source, "link == &source"); read_cb_called++; CHECK_EQ(nread, 1, "source_read_cb must read one byte"); CHECK_EQ(buf->base[0], 'x', "source_read_cb must read correct one byte"); - CHECK_EQ(uv_link_read_stop(&source.link), 0, "source.link.read_stop()"); + CHECK_EQ(uv_link_read_stop((uv_link_t*) &source), 0, "source.read_stop()"); } @@ -88,15 +88,15 @@ static void test_reads() { int err; /* alloc_cb/read_cb */ - source.link.alloc_cb = source_alloc_cb; - source.link.read_cb = source_read_cb; + source.alloc_cb = source_alloc_cb; + source.read_cb = source_read_cb; do err = write(fds[0], "x", 1); while (err == -1 && errno == EINTR); CHECK_EQ(err, 1, "write() == 1"); - CHECK_EQ(uv_link_read_start(&source.link), 0, "source.link.read_start()"); + CHECK_EQ(uv_link_read_start((uv_link_t*) &source), 0, "source.read_start()"); CHECK_EQ(uv_run(loop, UV_RUN_DEFAULT), 0, "uv_run()"); CHECK_EQ(alloc_cb_called, 1, "alloc_cb must be called once"); @@ -105,7 +105,7 @@ static void test_reads() { static void close_cb(uv_link_t* link) { - CHECK_EQ(link, &source.link, "close_cb link"); + CHECK_EQ(link, (uv_link_t*) &source, "close_cb link"); close_cb_called++; } @@ -124,7 +124,7 @@ TEST_IMPL(uv_link_source_t) { test_writes(); test_reads(); - uv_link_close(&source.link, close_cb); + uv_link_close((uv_link_t*) &source, close_cb); CHECK_EQ(uv_run(loop, UV_RUN_DEFAULT), 0, "uv_run()"); CHECK_EQ(close_cb_called, 1, "close_cb count"); |