Exemple #1
0
static VALUE
rb_queue_initialize(VALUE self)
{
    RSTRUCT_SET(self, QUEUE_QUE, ary_buf_new());
    RSTRUCT_SET(self, QUEUE_WAITERS, ary_buf_new());
    return self;
}
VALUE
rb_struct_aset(VALUE s, VALUE idx, VALUE val)
{
    long i;

    if (RB_TYPE_P(idx, T_SYMBOL)) {
        return rb_struct_aset_id(s, SYM2ID(idx), val);
    }
    if (RB_TYPE_P(idx, T_STRING)) {
        ID id = rb_check_id(&idx);
        if (!id) {
            rb_name_error_str(idx, "no member '%"PRIsVALUE"' in struct",
                              QUOTE(idx));
        }
        return rb_struct_aset_id(s, id, val);
    }

    i = NUM2LONG(idx);
    if (i < 0) i = RSTRUCT_LEN(s) + i;
    if (i < 0) {
        rb_raise(rb_eIndexError, "offset %ld too small for struct(size:%ld)",
                 i, RSTRUCT_LEN(s));
    }
    if (RSTRUCT_LEN(s) <= i) {
        rb_raise(rb_eIndexError, "offset %ld too large for struct(size:%ld)",
                 i, RSTRUCT_LEN(s));
    }
    rb_struct_modify(s);
    RSTRUCT_SET(s, i, val);
    return val;
}
Exemple #3
0
static VALUE
rb_szqueue_initialize(VALUE self, VALUE vmax)
{
    long max;

    max = NUM2LONG(vmax);
    if (max <= 0) {
	rb_raise(rb_eArgError, "queue size must be positive");
    }

    RSTRUCT_SET(self, QUEUE_QUE, ary_buf_new());
    RSTRUCT_SET(self, QUEUE_WAITERS, ary_buf_new());
    RSTRUCT_SET(self, SZQUEUE_WAITERS, ary_buf_new());
    RSTRUCT_SET(self, SZQUEUE_MAX, vmax);

    return self;
}
/* :nodoc: */
VALUE
rb_struct_init_copy(VALUE copy, VALUE s)
{
    long i, len;

    if (!OBJ_INIT_COPY(copy, s)) return copy;
    if (RSTRUCT_LEN(copy) != RSTRUCT_LEN(s)) {
        rb_raise(rb_eTypeError, "struct size mismatch");
    }

    for (i=0, len=RSTRUCT_LEN(copy); i<len; i++) {
        RSTRUCT_SET(copy, i, RSTRUCT_GET(s, i));
    }

    return copy;
}
Exemple #5
0
static VALUE
rb_szqueue_max_set(VALUE self, VALUE vmax)
{
    long max = NUM2LONG(vmax), diff = 0;
    VALUE t;

    if (max <= 0) {
	rb_raise(rb_eArgError, "queue size must be positive");
    }
    if ((unsigned long)max > GET_SZQUEUE_ULONGMAX(self)) {
	diff = max - GET_SZQUEUE_ULONGMAX(self);
    }
    RSTRUCT_SET(self, SZQUEUE_MAX, vmax);
    while (diff-- > 0 && !NIL_P(t = rb_ary_shift(GET_SZQUEUE_WAITERS(self)))) {
	rb_thread_wakeup_alive(t);
    }
    return vmax;
}
static VALUE
rb_struct_initialize_m(int argc, const VALUE *argv, VALUE self)
{
    VALUE klass = rb_obj_class(self);
    long i, n;

    rb_struct_modify(self);
    n = num_members(klass);
    if (n < argc) {
        rb_raise(rb_eArgError, "struct size differs");
    }
    for (i=0; i<argc; i++) {
        RSTRUCT_SET(self, i, argv[i]);
    }
    if (n > argc) {
        rb_mem_clear((VALUE *)RSTRUCT_CONST_PTR(self)+argc, n-argc);
    }
    return Qnil;
}
static VALUE
rb_struct_set(VALUE obj, VALUE val)
{
    VALUE members, slot;
    long i, len;

    members = rb_struct_members(obj);
    len = RARRAY_LEN(members);
    rb_struct_modify(obj);
    for (i=0; i<len; i++) {
        slot = RARRAY_AREF(members, i);
        if (rb_id_attrset(SYM2ID(slot)) == rb_frame_this_func()) {
            RSTRUCT_SET(obj, i, val);
            return val;
        }
    }
    rb_name_error(rb_frame_this_func(), "`%s' is not a struct member",
                  rb_id2name(rb_frame_this_func()));

    UNREACHABLE;
}
static VALUE
rb_struct_aset_id(VALUE s, ID id, VALUE val)
{
    VALUE members = rb_struct_members(s);
    long i, len = RARRAY_LEN(members);

    if (RSTRUCT_LEN(s) != len) {
        rb_raise(rb_eTypeError, "struct size differs (%ld required %ld given)",
                 len, RSTRUCT_LEN(s));
    }

    for (i=0; i<len; i++) {
        if (SYM2ID(RARRAY_AREF(members, i)) == id) {
            rb_struct_modify(s);
            RSTRUCT_SET(s, i, val);
            return val;
        }
    }
    rb_name_error(id, "no member '%s' in struct", rb_id2name(id));

    UNREACHABLE;
}
Exemple #9
0
static VALUE
rb_condvar_initialize(VALUE self)
{
    RSTRUCT_SET(self, CONDVAR_WAITERS, ary_buf_new());
    return self;
}