From 77f859aab61fa6176bdf164925dbc2a84fac6597 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Tue, 8 Sep 2020 13:35:52 +1200 Subject: [PATCH] Add space between control statements and brackets. --- ext/nio4r/.clang-format | 2 +- ext/nio4r/bytebuffer.c | 46 +++++++++++++------------- ext/nio4r/monitor.c | 44 ++++++++++++------------- ext/nio4r/selector.c | 72 ++++++++++++++++++++--------------------- 4 files changed, 82 insertions(+), 82 deletions(-) diff --git a/ext/nio4r/.clang-format b/ext/nio4r/.clang-format index c2901e2..6ad3f63 100644 --- a/ext/nio4r/.clang-format +++ b/ext/nio4r/.clang-format @@ -11,6 +11,6 @@ BraceWrapping: AfterControlStatement: Never IndentCaseLabels: true PointerAlignment: Right -SpaceBeforeParens: Never +SpaceBeforeParens: ControlStatements IndentWidth: 4 ... diff --git a/ext/nio4r/bytebuffer.c b/ext/nio4r/bytebuffer.c index 03470a1..2bd7635 100644 --- a/ext/nio4r/bytebuffer.c +++ b/ext/nio4r/bytebuffer.c @@ -85,7 +85,7 @@ static void NIO_ByteBuffer_gc_mark(struct NIO_ByteBuffer *buffer) static void NIO_ByteBuffer_free(struct NIO_ByteBuffer *buffer) { - if(buffer->buffer) + if (buffer->buffer) xfree(buffer->buffer); xfree(buffer); } @@ -133,17 +133,17 @@ static VALUE NIO_ByteBuffer_set_position(VALUE self, VALUE new_position) pos = NUM2INT(new_position); - if(pos < 0) { + if (pos < 0) { rb_raise(rb_eArgError, "negative position given"); } - if(pos > buffer->limit) { + if (pos > buffer->limit) { rb_raise(rb_eArgError, "specified position exceeds limit"); } buffer->position = pos; - if(buffer->mark > buffer->position) { + if (buffer->mark > buffer->position) { buffer->mark = MARK_UNSET; } @@ -166,21 +166,21 @@ static VALUE NIO_ByteBuffer_set_limit(VALUE self, VALUE new_limit) lim = NUM2INT(new_limit); - if(lim < 0) { + if (lim < 0) { rb_raise(rb_eArgError, "negative limit given"); } - if(lim > buffer->capacity) { + if (lim > buffer->capacity) { rb_raise(rb_eArgError, "specified limit exceeds capacity"); } buffer->limit = lim; - if(buffer->position > lim) { + if (buffer->position > lim) { buffer->position = lim; } - if(buffer->mark > lim) { + if (buffer->mark > lim) { buffer->mark = MARK_UNSET; } @@ -220,17 +220,17 @@ static VALUE NIO_ByteBuffer_get(int argc, VALUE *argv, VALUE self) rb_scan_args(argc, argv, "01", &length); - if(length == Qnil) { + if (length == Qnil) { len = buffer->limit - buffer->position; } else { len = NUM2INT(length); } - if(len < 0) { + if (len < 0) { rb_raise(rb_eArgError, "negative length given"); } - if(len > buffer->limit - buffer->position) { + if (len > buffer->limit - buffer->position) { rb_raise(cNIO_ByteBuffer_UnderflowError, "not enough data in buffer"); } @@ -248,11 +248,11 @@ static VALUE NIO_ByteBuffer_fetch(VALUE self, VALUE index) i = NUM2INT(index); - if(i < 0) { + if (i < 0) { rb_raise(rb_eArgError, "negative index given"); } - if(i >= buffer->limit) { + if (i >= buffer->limit) { rb_raise(rb_eArgError, "specified index exceeds limit"); } @@ -268,7 +268,7 @@ static VALUE NIO_ByteBuffer_put(VALUE self, VALUE string) StringValue(string); length = RSTRING_LEN(string); - if(length > buffer->limit - buffer->position) { + if (length > buffer->limit - buffer->position) { rb_raise(cNIO_ByteBuffer_OverflowError, "buffer is full"); } @@ -289,14 +289,14 @@ static VALUE NIO_ByteBuffer_read_from(VALUE self, VALUE io) rb_io_set_nonblock(fptr); nbytes = buffer->limit - buffer->position; - if(nbytes == 0) { + if (nbytes == 0) { rb_raise(cNIO_ByteBuffer_OverflowError, "buffer is full"); } bytes_read = read(FPTR_TO_FD(fptr), buffer->buffer + buffer->position, nbytes); - if(bytes_read < 0) { - if(errno == EAGAIN) { + if (bytes_read < 0) { + if (errno == EAGAIN) { return INT2NUM(0); } else { rb_sys_fail("write"); @@ -319,14 +319,14 @@ static VALUE NIO_ByteBuffer_write_to(VALUE self, VALUE io) rb_io_set_nonblock(fptr); nbytes = buffer->limit - buffer->position; - if(nbytes == 0) { + if (nbytes == 0) { rb_raise(cNIO_ByteBuffer_UnderflowError, "no data remaining in buffer"); } bytes_written = write(FPTR_TO_FD(fptr), buffer->buffer + buffer->position, nbytes); - if(bytes_written < 0) { - if(errno == EAGAIN) { + if (bytes_written < 0) { + if (errno == EAGAIN) { return INT2NUM(0); } else { rb_sys_fail("write"); @@ -375,7 +375,7 @@ static VALUE NIO_ByteBuffer_reset(VALUE self) struct NIO_ByteBuffer *buffer; Data_Get_Struct(self, struct NIO_ByteBuffer, buffer); - if(buffer->mark < 0) { + if (buffer->mark < 0) { rb_raise(cNIO_ByteBuffer_MarkUnsetError, "mark has not been set"); } else { buffer->position = buffer->mark; @@ -402,8 +402,8 @@ static VALUE NIO_ByteBuffer_each(VALUE self) struct NIO_ByteBuffer *buffer; Data_Get_Struct(self, struct NIO_ByteBuffer, buffer); - if(rb_block_given_p()) { - for(i = 0; i < buffer->limit; i++) { + if (rb_block_given_p()) { + for (i = 0; i < buffer->limit; i++) { rb_yield(INT2NUM(buffer->buffer[i])); } } else { diff --git a/ext/nio4r/monitor.c b/ext/nio4r/monitor.c index a5e9858..0c46ea3 100644 --- a/ext/nio4r/monitor.c +++ b/ext/nio4r/monitor.c @@ -85,11 +85,11 @@ static VALUE NIO_Monitor_initialize(VALUE self, VALUE io, VALUE interests, VALUE Data_Get_Struct(self, struct NIO_Monitor, monitor); - if(interests_id == rb_intern("r")) { + if (interests_id == rb_intern("r")) { monitor->interests = EV_READ; - } else if(interests_id == rb_intern("w")) { + } else if (interests_id == rb_intern("w")) { monitor->interests = EV_WRITE; - } else if(interests_id == rb_intern("rw")) { + } else if (interests_id == rb_intern("rw")) { monitor->interests = EV_READ | EV_WRITE; } else { rb_raise(rb_eArgError, "invalid event type %s (must be :r, :w, or :rw)", RSTRING_PTR(rb_funcall(interests, rb_intern("inspect"), 0))); @@ -111,7 +111,7 @@ static VALUE NIO_Monitor_initialize(VALUE self, VALUE io, VALUE interests, VALUE object where it originally came from */ monitor->selector = selector; - if(monitor->interests) { + if (monitor->interests) { ev_io_start(selector->ev_loop, &monitor->ev_io); } @@ -127,9 +127,9 @@ static VALUE NIO_Monitor_close(int argc, VALUE *argv, VALUE self) rb_scan_args(argc, argv, "01", &deregister); selector = rb_ivar_get(self, rb_intern("selector")); - if(selector != Qnil) { + if (selector != Qnil) { /* if ev_loop is 0, it means that the loop has been stopped already (see NIO_Selector_shutdown) */ - if(monitor->interests && monitor->selector->ev_loop) { + if (monitor->interests && monitor->selector->ev_loop) { ev_io_stop(monitor->selector->ev_loop, &monitor->ev_io); } @@ -137,7 +137,7 @@ static VALUE NIO_Monitor_close(int argc, VALUE *argv, VALUE self) rb_ivar_set(self, rb_intern("selector"), Qnil); /* Default value is true */ - if(deregister == Qtrue || deregister == Qnil) { + if (deregister == Qtrue || deregister == Qnil) { rb_funcall(selector, rb_intern("deregister"), 1, rb_ivar_get(self, rb_intern("io"))); } } @@ -165,7 +165,7 @@ static VALUE NIO_Monitor_interests(VALUE self) static VALUE NIO_Monitor_set_interests(VALUE self, VALUE interests) { - if(NIL_P(interests)) { + if (NIL_P(interests)) { NIO_Monitor_update_interests(self, 0); } else { NIO_Monitor_update_interests(self, NIO_Monitor_symbol2interest(interests)); @@ -216,11 +216,11 @@ static VALUE NIO_Monitor_readiness(VALUE self) struct NIO_Monitor *monitor; Data_Get_Struct(self, struct NIO_Monitor, monitor); - if((monitor->revents & (EV_READ | EV_WRITE)) == (EV_READ | EV_WRITE)) { + if ((monitor->revents & (EV_READ | EV_WRITE)) == (EV_READ | EV_WRITE)) { return ID2SYM(rb_intern("rw")); - } else if(monitor->revents & EV_READ) { + } else if (monitor->revents & EV_READ) { return ID2SYM(rb_intern("r")); - } else if(monitor->revents & EV_WRITE) { + } else if (monitor->revents & EV_WRITE) { return ID2SYM(rb_intern("w")); } else { return Qnil; @@ -232,7 +232,7 @@ static VALUE NIO_Monitor_is_readable(VALUE self) struct NIO_Monitor *monitor; Data_Get_Struct(self, struct NIO_Monitor, monitor); - if(monitor->revents & EV_READ) { + if (monitor->revents & EV_READ) { return Qtrue; } else { return Qfalse; @@ -244,7 +244,7 @@ static VALUE NIO_Monitor_is_writable(VALUE self) struct NIO_Monitor *monitor; Data_Get_Struct(self, struct NIO_Monitor, monitor); - if(monitor->revents & EV_WRITE) { + if (monitor->revents & EV_WRITE) { return Qtrue; } else { return Qfalse; @@ -258,11 +258,11 @@ static int NIO_Monitor_symbol2interest(VALUE interests) ID interests_id; interests_id = SYM2ID(interests); - if(interests_id == rb_intern("r")) { + if (interests_id == rb_intern("r")) { return EV_READ; - } else if(interests_id == rb_intern("w")) { + } else if (interests_id == rb_intern("w")) { return EV_WRITE; - } else if(interests_id == rb_intern("rw")) { + } else if (interests_id == rb_intern("rw")) { return EV_READ | EV_WRITE; } else { rb_raise(rb_eArgError, "invalid interest type %s (must be :r, :w, or :rw)", RSTRING_PTR(rb_funcall(interests, rb_intern("inspect"), 0))); @@ -275,12 +275,12 @@ static void NIO_Monitor_update_interests(VALUE self, int interests) struct NIO_Monitor *monitor; Data_Get_Struct(self, struct NIO_Monitor, monitor); - if(NIO_Monitor_is_closed(self) == Qtrue) { + if (NIO_Monitor_is_closed(self) == Qtrue) { rb_raise(rb_eEOFError, "monitor is closed"); } - if(interests) { - switch(interests) { + if (interests) { + switch (interests) { case EV_READ: interests_id = rb_intern("r"); break; @@ -299,9 +299,9 @@ static void NIO_Monitor_update_interests(VALUE self, int interests) rb_ivar_set(self, rb_intern("interests"), Qnil); } - if(monitor->interests != interests) { + if (monitor->interests != interests) { // If the monitor currently has interests, we should stop it. - if(monitor->interests) { + if (monitor->interests) { ev_io_stop(monitor->selector->ev_loop, &monitor->ev_io); } @@ -310,7 +310,7 @@ static void NIO_Monitor_update_interests(VALUE self, int interests) ev_io_set(&monitor->ev_io, monitor->ev_io.fd, monitor->interests); // If we are interested in events, schedule the monitor back into the event loop: - if(monitor->interests) { + if (monitor->interests) { ev_io_start(monitor->selector->ev_loop, &monitor->ev_io); } } diff --git a/ext/nio4r/selector.c b/ext/nio4r/selector.c index 0be8508..1f30ec7 100644 --- a/ext/nio4r/selector.c +++ b/ext/nio4r/selector.c @@ -95,12 +95,12 @@ static VALUE NIO_Selector_allocate(VALUE klass) safety. Pipes are nice and safe to use between threads. Note that Java NIO uses this same mechanism */ - if(pipe(fds) < 0) { + if (pipe(fds) < 0) { rb_sys_fail("pipe"); } /* Use non-blocking reads/writes during wakeup, in case the buffer is full */ - if(fcntl(fds[0], F_SETFL, O_NONBLOCK) < 0 || fcntl(fds[1], F_SETFL, O_NONBLOCK) < 0) { + if (fcntl(fds[0], F_SETFL, O_NONBLOCK) < 0 || fcntl(fds[1], F_SETFL, O_NONBLOCK) < 0) { rb_sys_fail("fcntl"); } @@ -126,7 +126,7 @@ static VALUE NIO_Selector_allocate(VALUE klass) /* NIO selectors store all Ruby objects in instance variables so mark is a stub */ static void NIO_Selector_mark(struct NIO_Selector *selector) { - if(selector->ready_array != Qnil) { + if (selector->ready_array != Qnil) { rb_gc_mark(selector->ready_array); } } @@ -135,14 +135,14 @@ static void NIO_Selector_mark(struct NIO_Selector *selector) Called by both NIO::Selector#close and the finalizer below */ static void NIO_Selector_shutdown(struct NIO_Selector *selector) { - if(selector->closed) { + if (selector->closed) { return; } close(selector->wakeup_reader); close(selector->wakeup_writer); - if(selector->ev_loop) { + if (selector->ev_loop) { ev_loop_destroy(selector->ev_loop); selector->ev_loop = 0; } @@ -163,23 +163,23 @@ static VALUE NIO_Selector_supported_backends(VALUE klass) unsigned int backends = ev_supported_backends(); VALUE result = rb_ary_new(); - if(backends & EVBACKEND_EPOLL) { + if (backends & EVBACKEND_EPOLL) { rb_ary_push(result, ID2SYM(rb_intern("epoll"))); } - if(backends & EVBACKEND_POLL) { + if (backends & EVBACKEND_POLL) { rb_ary_push(result, ID2SYM(rb_intern("poll"))); } - if(backends & EVBACKEND_KQUEUE) { + if (backends & EVBACKEND_KQUEUE) { rb_ary_push(result, ID2SYM(rb_intern("kqueue"))); } - if(backends & EVBACKEND_SELECT) { + if (backends & EVBACKEND_SELECT) { rb_ary_push(result, ID2SYM(rb_intern("select"))); } - if(backends & EVBACKEND_PORT) { + if (backends & EVBACKEND_PORT) { rb_ary_push(result, ID2SYM(rb_intern("port"))); } @@ -201,22 +201,22 @@ static VALUE NIO_Selector_initialize(int argc, VALUE *argv, VALUE self) rb_scan_args(argc, argv, "01", &backend); - if(backend != Qnil) { - if(!rb_ary_includes(NIO_Selector_supported_backends(CLASS_OF(self)), backend)) { + if (backend != Qnil) { + if (!rb_ary_includes(NIO_Selector_supported_backends(CLASS_OF(self)), backend)) { rb_raise(rb_eArgError, "unsupported backend: %s", RSTRING_PTR(rb_funcall(backend, rb_intern("inspect"), 0))); } backend_id = SYM2ID(backend); - if(backend_id == rb_intern("epoll")) { + if (backend_id == rb_intern("epoll")) { flags = EVBACKEND_EPOLL; - } else if(backend_id == rb_intern("poll")) { + } else if (backend_id == rb_intern("poll")) { flags = EVBACKEND_POLL; - } else if(backend_id == rb_intern("kqueue")) { + } else if (backend_id == rb_intern("kqueue")) { flags = EVBACKEND_KQUEUE; - } else if(backend_id == rb_intern("select")) { + } else if (backend_id == rb_intern("select")) { flags = EVBACKEND_SELECT; - } else if(backend_id == rb_intern("port")) { + } else if (backend_id == rb_intern("port")) { flags = EVBACKEND_PORT; } else { rb_raise(rb_eArgError, "unsupported backend: %s", RSTRING_PTR(rb_funcall(backend, rb_intern("inspect"), 0))); @@ -227,7 +227,7 @@ static VALUE NIO_Selector_initialize(int argc, VALUE *argv, VALUE self) assert(!selector->ev_loop); selector->ev_loop = ev_loop_new(flags); - if(!selector->ev_loop) { + if (!selector->ev_loop) { rb_raise(rb_eIOError, "error initializing event loop"); } @@ -248,11 +248,11 @@ static VALUE NIO_Selector_backend(VALUE self) struct NIO_Selector *selector; Data_Get_Struct(self, struct NIO_Selector, selector); - if(selector->closed) { + if (selector->closed) { rb_raise(rb_eIOError, "selector is closed"); } - switch(ev_backend(selector->ev_loop)) { + switch (ev_backend(selector->ev_loop)) { case EVBACKEND_EPOLL: return ID2SYM(rb_intern("epoll")); case EVBACKEND_POLL: @@ -276,7 +276,7 @@ static VALUE NIO_Selector_synchronize(VALUE self, VALUE (*func)(VALUE *args), VA current_thread = rb_thread_current(); lock_holder = rb_ivar_get(self, rb_intern("lock_holder")); - if(lock_holder != current_thread) { + if (lock_holder != current_thread) { lock = rb_ivar_get(self, rb_intern("lock")); rb_funcall(lock, rb_intern("lock"), 0); rb_ivar_set(self, rb_intern("lock_holder"), current_thread); @@ -321,14 +321,14 @@ static VALUE NIO_Selector_register_synchronized(VALUE *args) interests = args[2]; Data_Get_Struct(self, struct NIO_Selector, selector); - if(selector->closed) { + if (selector->closed) { rb_raise(rb_eIOError, "selector is closed"); } selectables = rb_ivar_get(self, rb_intern("selectables")); monitor = rb_hash_lookup(selectables, io); - if(monitor != Qnil) + if (monitor != Qnil) rb_raise(rb_eArgError, "this IO is already registered with selector"); /* Create a new NIO::Monitor */ @@ -360,7 +360,7 @@ static VALUE NIO_Selector_deregister_synchronized(VALUE *args) selectables = rb_ivar_get(self, rb_intern("selectables")); monitor = rb_hash_delete(selectables, io); - if(monitor != Qnil) { + if (monitor != Qnil) { rb_funcall(monitor, rb_intern("close"), 1, Qfalse); } @@ -384,7 +384,7 @@ static VALUE NIO_Selector_select(int argc, VALUE *argv, VALUE self) rb_scan_args(argc, argv, "01", &timeout); - if(timeout != Qnil && NUM2DBL(timeout) < 0) { + if (timeout != Qnil && NUM2DBL(timeout) < 0) { rb_raise(rb_eArgError, "time interval must be positive"); } @@ -403,26 +403,26 @@ static VALUE NIO_Selector_select_synchronized(VALUE *args) Data_Get_Struct(args[0], struct NIO_Selector, selector); - if(selector->closed) { + if (selector->closed) { rb_raise(rb_eIOError, "selector is closed"); } - if(!rb_block_given_p()) { + if (!rb_block_given_p()) { selector->ready_array = rb_ary_new(); } ready = NIO_Selector_run(selector, args[1]); /* Timeout */ - if(ready < 0) { - if(!rb_block_given_p()) { + if (ready < 0) { + if (!rb_block_given_p()) { selector->ready_array = Qnil; } return Qnil; } - if(rb_block_given_p()) { + if (rb_block_given_p()) { return INT2NUM(ready); } else { ready_array = selector->ready_array; @@ -440,12 +440,12 @@ static int NIO_Selector_run(struct NIO_Selector *selector, VALUE timeout) selector->selecting = 1; selector->wakeup_fired = 0; - if(timeout == Qnil) { + if (timeout == Qnil) { /* Don't fire a wakeup timeout if we weren't passed one */ ev_timer_stop(selector->ev_loop, &selector->timer); } else { timeout_val = NUM2DBL(timeout); - if(timeout_val == 0) { + if (timeout_val == 0) { /* If we've been given an explicit timeout of 0, perform a non-blocking select operation */ ev_run_flags = EVRUN_NOWAIT; @@ -461,7 +461,7 @@ static int NIO_Selector_run(struct NIO_Selector *selector, VALUE timeout) result = selector->ready_count; selector->selecting = selector->ready_count = 0; - if(result > 0 || selector->wakeup_fired) { + if (result > 0 || selector->wakeup_fired) { selector->wakeup_fired = 0; return result; } else { @@ -475,7 +475,7 @@ static VALUE NIO_Selector_wakeup(VALUE self) struct NIO_Selector *selector; Data_Get_Struct(self, struct NIO_Selector, selector); - if(selector->closed) { + if (selector->closed) { rb_raise(rb_eIOError, "selector is closed"); } @@ -540,7 +540,7 @@ static void NIO_Selector_wakeup_callback(struct ev_loop *ev_loop, struct ev_io * selector->selecting = 0; /* Drain the wakeup pipe, giving us level-triggered behavior */ - while(read(selector->wakeup_reader, buffer, 128) > 0) + while (read(selector->wakeup_reader, buffer, 128) > 0) ; } @@ -557,7 +557,7 @@ void NIO_Selector_monitor_callback(struct ev_loop *ev_loop, struct ev_io *io, in selector->ready_count++; monitor_data->revents = revents; - if(rb_block_given_p()) { + if (rb_block_given_p()) { rb_yield(monitor); } else { assert(selector->ready_array != Qnil);