Skip to content

Commit

Permalink
Use clang-format to improve code consistency.
Browse files Browse the repository at this point in the history
  • Loading branch information
boazsegev authored and ioquatix committed Sep 8, 2020
1 parent 60b3d74 commit aadfd52
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 48 deletions.
16 changes: 16 additions & 0 deletions ext/nio4r/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
Language: Cpp
BasedOnStyle: WebKit
AllowAllParametersOfDeclarationOnNextLine: false
BinPackArguments: false
BinPackParameters: false
AlignConsecutiveMacros: false
AlignConsecutiveAssignments: false
BreakBeforeBraces: Linux
BraceWrapping:
AfterControlStatement: Never
IndentCaseLabels: true
PointerAlignment: Right
SpaceBeforeParens: Never
IndentWidth: 4
...
9 changes: 4 additions & 5 deletions ext/nio4r/bytebuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void Init_NIO_ByteBuffer()
cNIO_ByteBuffer = rb_define_class_under(mNIO, "ByteBuffer", rb_cObject);
rb_define_alloc_func(cNIO_ByteBuffer, NIO_ByteBuffer_allocate);

cNIO_ByteBuffer_OverflowError = rb_define_class_under(cNIO_ByteBuffer, "OverflowError", rb_eIOError);
cNIO_ByteBuffer_OverflowError = rb_define_class_under(cNIO_ByteBuffer, "OverflowError", rb_eIOError);
cNIO_ByteBuffer_UnderflowError = rb_define_class_under(cNIO_ByteBuffer, "UnderflowError", rb_eIOError);
cNIO_ByteBuffer_MarkUnsetError = rb_define_class_under(cNIO_ByteBuffer, "MarkUnsetError", rb_eIOError);

Expand Down Expand Up @@ -86,7 +86,7 @@ static void NIO_ByteBuffer_gc_mark(struct NIO_ByteBuffer *buffer)
static void NIO_ByteBuffer_free(struct NIO_ByteBuffer *buffer)
{
if(buffer->buffer)
xfree(buffer->buffer);
xfree(buffer->buffer);
xfree(buffer);
}

Expand Down Expand Up @@ -421,9 +421,8 @@ static VALUE NIO_ByteBuffer_inspect(VALUE self)
return rb_sprintf(
"#<%s:%p @position=%d @limit=%d @capacity=%d>",
rb_class2name(CLASS_OF(self)),
(void*)self,
(void *)self,
buffer->position,
buffer->limit,
buffer->capacity
);
buffer->capacity);
}
2 changes: 1 addition & 1 deletion ext/nio4r/libev.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
#define EV_USE_REALTIME 0
#endif

#include "../libev/ev.h"
#include "../libev/ev.h"
20 changes: 10 additions & 10 deletions ext/nio4r/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ static VALUE NIO_Monitor_initialize(VALUE self, VALUE io, VALUE interests, VALUE
} 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)));
rb_raise(rb_eArgError, "invalid event type %s (must be :r, :w, or :rw)", RSTRING_PTR(rb_funcall(interests, rb_intern("inspect"), 0)));
}

GetOpenFile(rb_convert_type(io, T_FILE, "IO", "to_io"), fptr);
Expand All @@ -112,8 +111,8 @@ static VALUE NIO_Monitor_initialize(VALUE self, VALUE io, VALUE interests, VALUE
object where it originally came from */
monitor->selector = selector;

if (monitor->interests) {
ev_io_start(selector->ev_loop, &monitor->ev_io);
if(monitor->interests) {
ev_io_start(selector->ev_loop, &monitor->ev_io);
}

return Qnil;
Expand All @@ -131,12 +130,12 @@ static VALUE NIO_Monitor_close(int argc, VALUE *argv, VALUE self)
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) {
ev_io_stop(monitor->selector->ev_loop, &monitor->ev_io);
ev_io_stop(monitor->selector->ev_loop, &monitor->ev_io);
}

monitor->selector = 0;
rb_ivar_set(self, rb_intern("selector"), Qnil);

/* Default value is true */
if(deregister == Qtrue || deregister == Qnil) {
rb_funcall(selector, rb_intern("deregister"), 1, rb_ivar_get(self, rb_intern("io")));
Expand Down Expand Up @@ -175,7 +174,8 @@ static VALUE NIO_Monitor_set_interests(VALUE self, VALUE interests)
return rb_ivar_get(self, rb_intern("interests"));
}

static VALUE NIO_Monitor_add_interest(VALUE self, VALUE interest) {
static VALUE NIO_Monitor_add_interest(VALUE self, VALUE interest)
{
struct NIO_Monitor *monitor;
Data_Get_Struct(self, struct NIO_Monitor, monitor);

Expand All @@ -185,7 +185,8 @@ static VALUE NIO_Monitor_add_interest(VALUE self, VALUE interest) {
return rb_ivar_get(self, rb_intern("interests"));
}

static VALUE NIO_Monitor_remove_interest(VALUE self, VALUE interest) {
static VALUE NIO_Monitor_remove_interest(VALUE self, VALUE interest)
{
struct NIO_Monitor *monitor;
Data_Get_Struct(self, struct NIO_Monitor, monitor);

Expand Down Expand Up @@ -264,8 +265,7 @@ static int NIO_Monitor_symbol2interest(VALUE interests)
} 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)));
rb_raise(rb_eArgError, "invalid interest type %s (must be :r, :w, or :rw)", RSTRING_PTR(rb_funcall(interests, rb_intern("inspect"), 0)));
}
}

Expand Down
19 changes: 7 additions & 12 deletions ext/nio4r/nio4r.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
#ifndef NIO4R_H
#define NIO4R_H

#include "libev.h"
#include "ruby.h"
#include "ruby/io.h"
#include "libev.h"

struct NIO_Selector
{
struct NIO_Selector {
struct ev_loop *ev_loop;
struct ev_timer timer; /* for timeouts */
struct ev_io wakeup;
Expand All @@ -24,31 +23,27 @@ struct NIO_Selector
VALUE ready_array;
};

struct NIO_callback_data
{
struct NIO_callback_data {
VALUE *monitor;
struct NIO_Selector *selector;
};

struct NIO_Monitor
{
struct NIO_Monitor {
VALUE self;
int interests, revents;
struct ev_io ev_io;
struct NIO_Selector *selector;
};

struct NIO_ByteBuffer
{
struct NIO_ByteBuffer {
char *buffer;
int position, limit, capacity, mark;
};


#ifdef GetReadFile
# define FPTR_TO_FD(fptr) (fileno(GetReadFile(fptr)))
#define FPTR_TO_FD(fptr) (fileno(GetReadFile(fptr)))
#else
# define FPTR_TO_FD(fptr) fptr->fd
#define FPTR_TO_FD(fptr) fptr->fd
#endif /* GetReadFile */

/* Thunk between libev callbacks in NIO::Monitors and NIO::Selectors */
Expand Down
2 changes: 1 addition & 1 deletion ext/nio4r/nio4r_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* See LICENSE.txt for further details.
*/

#include "nio4r.h"
#include "../libev/ev.c"
#include "nio4r.h"

void Init_NIO_Selector();
void Init_NIO_Monitor();
Expand Down
37 changes: 18 additions & 19 deletions ext/nio4r/selector.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "nio4r.h"
#ifdef HAVE_RUBYSIG_H
# include "rubysig.h"
#include "rubysig.h"
#endif

#ifdef HAVE_UNISTD_H
Expand All @@ -14,11 +14,11 @@
#include <io.h>
#endif

#include <fcntl.h>
#include <assert.h>
#include <fcntl.h>

static VALUE mNIO = Qnil;
static VALUE cNIO_Monitor = Qnil;
static VALUE cNIO_Monitor = Qnil;
static VALUE cNIO_Selector = Qnil;

/* Allocator/deallocator */
Expand Down Expand Up @@ -80,7 +80,7 @@ void Init_NIO_Selector()
rb_define_method(cNIO_Selector, "closed?", NIO_Selector_closed, 0);
rb_define_method(cNIO_Selector, "empty?", NIO_Selector_is_empty, 0);

cNIO_Monitor = rb_define_class_under(mNIO, "Monitor", rb_cObject);
cNIO_Monitor = rb_define_class_under(mNIO, "Monitor", rb_cObject);
}

/* Create the libev event loop and incoming event buffer */
Expand All @@ -100,8 +100,7 @@ static VALUE NIO_Selector_allocate(VALUE klass)
}

/* 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");
}

Expand Down Expand Up @@ -159,7 +158,8 @@ static void NIO_Selector_free(struct NIO_Selector *selector)
}

/* Return an array of symbols for supported backends */
static VALUE NIO_Selector_supported_backends(VALUE klass) {
static VALUE NIO_Selector_supported_backends(VALUE klass)
{
unsigned int backends = ev_supported_backends();
VALUE result = rb_ary_new();

Expand Down Expand Up @@ -203,8 +203,7 @@ static VALUE NIO_Selector_initialize(int argc, VALUE *argv, VALUE self)

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)));
rb_raise(rb_eArgError, "unsupported backend: %s", RSTRING_PTR(rb_funcall(backend, rb_intern("inspect"), 0)));
}

backend_id = SYM2ID(backend);
Expand All @@ -220,8 +219,7 @@ static VALUE NIO_Selector_initialize(int argc, VALUE *argv, VALUE self)
} 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)));
rb_raise(rb_eArgError, "unsupported backend: %s", RSTRING_PTR(rb_funcall(backend, rb_intern("inspect"), 0)));
}
}

Expand All @@ -245,15 +243,16 @@ static VALUE NIO_Selector_initialize(int argc, VALUE *argv, VALUE self)
return Qnil;
}

static VALUE NIO_Selector_backend(VALUE self) {
static VALUE NIO_Selector_backend(VALUE self)
{
struct NIO_Selector *selector;

Data_Get_Struct(self, struct NIO_Selector, selector);
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:
Expand Down Expand Up @@ -306,7 +305,7 @@ static VALUE NIO_Selector_unlock(VALUE self)
/* Register an IO object with the selector for the given interests */
static VALUE NIO_Selector_register(VALUE self, VALUE io, VALUE interests)
{
VALUE args[3] = {self, io, interests};
VALUE args[3] = { self, io, interests };
return NIO_Selector_synchronize(self, NIO_Selector_register_synchronized, args);
}

Expand Down Expand Up @@ -346,7 +345,7 @@ static VALUE NIO_Selector_register_synchronized(VALUE *args)
/* Deregister an IO object from the selector */
static VALUE NIO_Selector_deregister(VALUE self, VALUE io)
{
VALUE args[2] = {self, io};
VALUE args[2] = { self, io };
return NIO_Selector_synchronize(self, NIO_Selector_deregister_synchronized, args);
}

Expand Down Expand Up @@ -489,7 +488,7 @@ static VALUE NIO_Selector_wakeup(VALUE self)
/* Close the selector and free system resources */
static VALUE NIO_Selector_close(VALUE self)
{
VALUE args[1] = {self};
VALUE args[1] = { self };
return NIO_Selector_synchronize(self, NIO_Selector_close_synchronized, args);
}

Expand All @@ -507,7 +506,7 @@ static VALUE NIO_Selector_close_synchronized(VALUE *args)
/* Is the selector closed? */
static VALUE NIO_Selector_closed(VALUE self)
{
VALUE args[1] = {self};
VALUE args[1] = { self };
return NIO_Selector_synchronize(self, NIO_Selector_closed_synchronized, args);
}

Expand All @@ -528,7 +527,6 @@ static VALUE NIO_Selector_is_empty(VALUE self)
return rb_funcall(selectables, rb_intern("empty?"), 0) == Qtrue ? Qtrue : Qfalse;
}


/* Called whenever a timeout fires on the event loop */
static void NIO_Selector_timeout_callback(struct ev_loop *ev_loop, struct ev_timer *timer, int revents)
{
Expand All @@ -542,7 +540,8 @@ 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)
;
}

/* libev callback fired whenever a monitor gets an event */
Expand Down

0 comments on commit aadfd52

Please sign in to comment.