Skip to content

Commit

Permalink
[memory_view][fiddle] Use bool for boolean return value
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkn committed Dec 23, 2020
1 parent 74a3569 commit 05014dc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
14 changes: 7 additions & 7 deletions ext/-test-/memory_view/memory_view.c
Expand Up @@ -24,15 +24,15 @@ static VALUE sym_endianness;
static VALUE sym_little_endian;
static VALUE sym_big_endian;

static int
static bool
exportable_string_get_memory_view(VALUE obj, rb_memory_view_t *view, int flags)
{
VALUE str = rb_ivar_get(obj, id_str);
rb_memory_view_init_as_byte_array(view, obj, RSTRING_PTR(str), RSTRING_LEN(str), true);
return 1;
return true;
}

static int
static bool
exportable_string_memory_view_available_p(VALUE obj)
{
VALUE str = rb_ivar_get(obj, id_str);
Expand Down Expand Up @@ -260,7 +260,7 @@ expstr_initialize(VALUE obj, VALUE s)
return Qnil;
}

static int
static bool
mdview_get_memory_view(VALUE obj, rb_memory_view_t *view, int flags)
{
VALUE buf_v = rb_ivar_get(obj, id_str);
Expand All @@ -272,7 +272,7 @@ mdview_get_memory_view(VALUE obj, rb_memory_view_t *view, int flags)
const char *err;
const ssize_t item_size = rb_memory_view_item_size_from_format(format, &err);
if (item_size < 0) {
return 0;
return false;
}

ssize_t ndim = RARRAY_LEN(shape_v);
Expand Down Expand Up @@ -308,10 +308,10 @@ mdview_get_memory_view(VALUE obj, rb_memory_view_t *view, int flags)
view->shape = shape;
view->strides = strides;

return 1;
return true;
}

static int
static bool
mdview_memory_view_available_p(VALUE obj)
{
return true;
Expand Down
6 changes: 3 additions & 3 deletions ext/fiddle/pointer.c
Expand Up @@ -102,19 +102,19 @@ fiddle_ptr_check_memory_view(VALUE obj)
return data;
}

static int
static bool
fiddle_ptr_memory_view_available_p(VALUE obj)
{
return fiddle_ptr_check_memory_view(obj) != NULL;
}

static int
static bool
fiddle_ptr_get_memory_view(VALUE obj, rb_memory_view_t *view, int flags)
{
struct ptr_data *data = fiddle_ptr_check_memory_view(obj);
rb_memory_view_init_as_byte_array(view, obj, data->ptr, data->size, true);

return 1;
return true;
}

static const rb_memory_view_entry_t fiddle_ptr_memory_view_entry = {
Expand Down
16 changes: 8 additions & 8 deletions include/ruby/memory_view.h
Expand Up @@ -48,7 +48,7 @@ typedef struct {
/* The number of bytes in data. */
ssize_t len;

/* 1 for readonly memory, 0 for writable memory. */
/* true for readonly memory, false for writable memory. */
bool readonly;

/* A string to describe the format of an element, or NULL for unsigned byte.
Expand Down Expand Up @@ -105,9 +105,9 @@ typedef struct {
void *const private;
} rb_memory_view_t;

typedef int (* rb_memory_view_get_func_t)(VALUE obj, rb_memory_view_t *view, int flags);
typedef int (* rb_memory_view_release_func_t)(VALUE obj, rb_memory_view_t *view);
typedef int (* rb_memory_view_available_p_func_t)(VALUE obj);
typedef bool (* rb_memory_view_get_func_t)(VALUE obj, rb_memory_view_t *view, int flags);
typedef bool (* rb_memory_view_release_func_t)(VALUE obj, rb_memory_view_t *view);
typedef bool (* rb_memory_view_available_p_func_t)(VALUE obj);

typedef struct {
rb_memory_view_get_func_t get_func;
Expand All @@ -127,7 +127,7 @@ bool rb_memory_view_is_column_major_contiguous(const rb_memory_view_t *view);
RBIMPL_ATTR_NOALIAS()
void rb_memory_view_fill_contiguous_strides(const ssize_t ndim, const ssize_t item_size, const ssize_t *const shape, const bool row_major_p, ssize_t *const strides);
RBIMPL_ATTR_NOALIAS()
int rb_memory_view_init_as_byte_array(rb_memory_view_t *view, VALUE obj, void *data, const ssize_t len, const bool readonly);
bool rb_memory_view_init_as_byte_array(rb_memory_view_t *view, VALUE obj, void *data, const ssize_t len, const bool readonly);
ssize_t rb_memory_view_parse_item_format(const char *format,
rb_memory_view_item_component_t **members,
size_t *n_members, const char **err);
Expand All @@ -137,9 +137,9 @@ VALUE rb_memory_view_extract_item_members(const void *ptr, const rb_memory_view_
void rb_memory_view_prepare_item_desc(rb_memory_view_t *view);
VALUE rb_memory_view_get_item(rb_memory_view_t *view, const ssize_t *indices);

int rb_memory_view_available_p(VALUE obj);
int rb_memory_view_get(VALUE obj, rb_memory_view_t* memory_view, int flags);
int rb_memory_view_release(rb_memory_view_t* memory_view);
bool rb_memory_view_available_p(VALUE obj);
bool rb_memory_view_get(VALUE obj, rb_memory_view_t* memory_view, int flags);
bool rb_memory_view_release(rb_memory_view_t* memory_view);

/* for testing */
RUBY_EXTERN VALUE rb_memory_view_exported_object_registry;
Expand Down
22 changes: 11 additions & 11 deletions memory_view.c
Expand Up @@ -194,7 +194,7 @@ rb_memory_view_fill_contiguous_strides(const ssize_t ndim, const ssize_t item_si
}

/* Initialize view to expose a simple byte array */
int
bool
rb_memory_view_init_as_byte_array(rb_memory_view_t *view, VALUE obj, void *data, const ssize_t len, const bool readonly)
{
view->obj = obj;
Expand All @@ -211,7 +211,7 @@ rb_memory_view_init_as_byte_array(rb_memory_view_t *view, VALUE obj, void *data,
view->sub_offsets = NULL;
*((void **)&view->private) = NULL;

return 1;
return true;
}

#ifdef HAVE_TRUE_LONG_LONG
Expand Down Expand Up @@ -794,46 +794,46 @@ lookup_memory_view_entry(VALUE klass)
}

/* Examine whether the given object supports memory view. */
int
bool
rb_memory_view_available_p(VALUE obj)
{
VALUE klass = CLASS_OF(obj);
const rb_memory_view_entry_t *entry = lookup_memory_view_entry(klass);
if (entry)
return (* entry->available_p_func)(obj);
else
return 0;
return false;
}

/* Obtain a memory view from obj, and substitute the information to view. */
int
bool
rb_memory_view_get(VALUE obj, rb_memory_view_t* view, int flags)
{
VALUE klass = CLASS_OF(obj);
const rb_memory_view_entry_t *entry = lookup_memory_view_entry(klass);
if (entry) {
if (!(*entry->available_p_func)(obj)) {
return 0;
return false;
}

int rv = (*entry->get_func)(obj, view, flags);
bool rv = (*entry->get_func)(obj, view, flags);
if (rv) {
register_exported_object(view->obj);
}
return rv;
}
else
return 0;
return false;
}

/* Release the memory view obtained from obj. */
int
bool
rb_memory_view_release(rb_memory_view_t* view)
{
VALUE klass = CLASS_OF(view->obj);
const rb_memory_view_entry_t *entry = lookup_memory_view_entry(klass);
if (entry) {
int rv = 1;
bool rv = true;
if (entry->release_func) {
rv = (*entry->release_func)(view->obj, view);
}
Expand All @@ -847,7 +847,7 @@ rb_memory_view_release(rb_memory_view_t* view)
return rv;
}
else
return 0;
return false;
}

void
Expand Down

0 comments on commit 05014dc

Please sign in to comment.