Ruby  3.4.0dev (2024-11-22 revision 37a72b0150ec36b4ea27175039afc28c62207b0c)
Data Structures | Macros | Enumerations | Functions
rarray.h File Reference

(37a72b0150ec36b4ea27175039afc28c62207b0c)

Defines struct RArray. More...

#include "ruby/internal/arithmetic/long.h"
#include "ruby/internal/attr/artificial.h"
#include "ruby/internal/attr/constexpr.h"
#include "ruby/internal/attr/maybe_unused.h"
#include "ruby/internal/attr/pure.h"
#include "ruby/internal/cast.h"
#include "ruby/internal/core/rbasic.h"
#include "ruby/internal/dllexport.h"
#include "ruby/internal/fl_type.h"
#include "ruby/internal/gc.h"
#include "ruby/internal/stdbool.h"
#include "ruby/internal/value.h"
#include "ruby/internal/value_type.h"
#include "ruby/assert.h"
Include dependency graph for rarray.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  RArray
 Ruby's array. More...
 

Macros

#define RARRAY(obj)   RBIMPL_CAST((struct RArray *)(obj))
 Convenient casting macro. More...
 
#define RARRAY_LEN   rb_array_len
 Just another name of rb_array_len. More...
 
#define RARRAY_CONST_PTR   rb_array_const_ptr
 Just another name of rb_array_const_ptr. More...
 
#define RBIMPL_RARRAY_STMT(ary, var, expr)
 This is an implementation detail of RARRAY_PTR_USE. More...
 
#define RARRAY_PTR_USE(ary, ptr_name, expr)    RBIMPL_RARRAY_STMT(ary, ptr_name, expr)
 Declares a section of code where raw pointers are used. More...
 
#define RARRAY_AREF(a, i)   RARRAY_CONST_PTR(a)[i]
 

Enumerations

enum  ruby_rarray_consts { RARRAY_EMBED_LEN_SHIFT = RUBY_FL_USHIFT + 3 }
 This is an enum because GDB wants it (rather than a macro). More...
 

Functions

static long RARRAY_EMBED_LEN (VALUE ary)
 Queries the length of the array. More...
 
static long rb_array_len (VALUE a)
 Queries the length of the array. More...
 
static int RARRAY_LENINT (VALUE ary)
 Identical to rb_array_len(), except it differs for the return type. More...
 
static VALUERARRAY_PTR (VALUE ary)
 Wild use of a C pointer. More...
 
static void RARRAY_ASET (VALUE ary, long i, VALUE v)
 Assigns an object in an array. More...
 

Detailed Description

Defines struct RArray.

Author
Ruby developers ruby-.nosp@m.core.nosp@m.@ruby.nosp@m.-lan.nosp@m.g.org
Warning
Symbols prefixed with either RBIMPL or rbimpl are implementation details. Don't take them as canon. They could rapidly appear then vanish. The name (path) of this header file is also an implementation detail. Do not expect it to persist at the place it is now. Developers are free to move it anywhere anytime at will.
Note
To ruby-core: remember that this header can be possibly recursively included from extension libraries written in C++. Do not expect for instance __VA_ARGS__ is always available. We assume C99 for ruby itself but we don't assume languages of extension libraries. They could be written in C++98.

Definition in file rarray.h.

Macro Definition Documentation

◆ RARRAY

#define RARRAY (   obj)    RBIMPL_CAST((struct RArray *)(obj))

Convenient casting macro.

Parameters
objAn object, which is in fact an RArray.
Returns
The passed object casted to RArray.

Definition at line 44 of file rarray.h.

◆ RARRAY_AREF

#define RARRAY_AREF (   a,
 
)    RARRAY_CONST_PTR(a)[i]
Deprecated:

:FIXME: we want to convert RARRAY_AREF into an inline function (to add rooms for more sanity checks). However there were situations where the address of this macro is taken i.e. &RARRAY_AREF(...). They cannot be possible if this is not a macro. Such usages are abuse, and we eliminated them internally. However we are afraid of similar things to remain in the wild. This macro remains as it is due to that. If we could warn such usages we can set a transition path, but currently no way is found to do so.

Definition at line 403 of file rarray.h.

◆ RARRAY_CONST_PTR

#define RARRAY_CONST_PTR   rb_array_const_ptr

Just another name of rb_array_const_ptr.

Definition at line 52 of file rarray.h.

◆ RARRAY_LEN

#define RARRAY_LEN   rb_array_len

Just another name of rb_array_len.

Definition at line 51 of file rarray.h.

◆ RARRAY_PTR_USE

#define RARRAY_PTR_USE (   ary,
  ptr_name,
  expr 
)     RBIMPL_RARRAY_STMT(ary, ptr_name, expr)

Declares a section of code where raw pointers are used.

In case you need to touch the raw C array instead of polite CAPIs, then that operation shall be wrapped using this macro.

const auto ary = rb_eval_string("[...]");
const auto len = RARRAY_LENINT(ary);
const auto symwrite = rb_intern("write");
rb_funcallv(rb_stdout, symwrite, len, ptr);
});
VALUE rb_stdout
STDOUT constant.
Definition: io.c:201
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
Definition: vm_eval.c:1058
VALUE rb_eval_string(const char *str)
Evaluates the given string.
Definition: vm_eval.c:1975
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
Definition: symbol.c:823
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
Definition: io.h:2
int len
Length of the buffer.
Definition: io.h:8
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
Definition: rarray.h:281
#define RARRAY_PTR_USE(ary, ptr_name, expr)
Declares a section of code where raw pointers are used.
Definition: rarray.h:348
Parameters
aryAn object of RArray.
ptr_nameA variable name which points the C array in expr.
exprThe expression that touches ptr_name.

Definition at line 348 of file rarray.h.

◆ RBIMPL_RARRAY_STMT

#define RBIMPL_RARRAY_STMT (   ary,
  var,
  expr 
)
Value:
do { \
RBIMPL_ASSERT_TYPE((ary), RUBY_T_ARRAY); \
const VALUE rbimpl_ary = (ary); \
VALUE *var = rb_ary_ptr_use_start(rbimpl_ary); \
expr; \
rb_ary_ptr_use_end(rbimpl_ary); \
} while (0)
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40
@ RUBY_T_ARRAY
Definition: value_type.h:122

This is an implementation detail of RARRAY_PTR_USE.

People do not use it directly.

Definition at line 315 of file rarray.h.

Enumeration Type Documentation

◆ ruby_rarray_consts

This is an enum because GDB wants it (rather than a macro).

People need not bother.

Enumerator
RARRAY_EMBED_LEN_SHIFT 

Where ::RARRAY_EMBED_LEN_MASK resides.

Definition at line 122 of file rarray.h.

Function Documentation

◆ RARRAY_ASET()

static void RARRAY_ASET ( VALUE  ary,
long  i,
VALUE  v 
)
inlinestatic

Assigns an object in an array.

Parameters
[out]aryDestination array object.
[in]iIndex of ary.
[in]vArbitrary ruby object.
Precondition
ary must be an instance of RArray.
ary's length must be longer than or equal to i.
i must be greater than or equal to zero.
Postcondition
ary's ith element is set to v.

Definition at line 386 of file rarray.h.

◆ RARRAY_EMBED_LEN()

static long RARRAY_EMBED_LEN ( VALUE  ary)
inlinestatic

Queries the length of the array.

Parameters
[in]aryArray in question.
Returns
Its number of elements.
Precondition
ary must be an instance of RArray, and must has its ::RARRAY_EMBED_FLAG flag set.

Definition at line 235 of file rarray.h.

◆ RARRAY_LENINT()

static int RARRAY_LENINT ( VALUE  ary)
inlinestatic

Identical to rb_array_len(), except it differs for the return type.

Parameters
[in]aryArray in question.
Exceptions
rb_eRangeErrorToo long.
Returns
Its number of elements.
Precondition
ary must be an instance of RArray.

Definition at line 281 of file rarray.h.

Referenced by rb_apply(), rb_eval_cmd_kw(), rb_struct_alloc(), rb_struct_initialize(), rb_yield_splat(), and rb_yield_splat_kw().

◆ RARRAY_PTR()

static VALUE* RARRAY_PTR ( VALUE  ary)
inlinestatic

Wild use of a C pointer.

This function accesses the backend storage directly. This is slower than RARRAY_PTR_USE. It exercises extra manoeuvres to protect our generational GC. Use of this function is considered archaic. Use a modern way instead.

Parameters
[in]aryAn object of RArray.
Returns
The backend C array.

Definition at line 366 of file rarray.h.

Referenced by rb_struct_new().

◆ rb_array_len()

static long rb_array_len ( VALUE  a)
inlinestatic

Queries the length of the array.

Parameters
[in]aArray in question.
Returns
Its number of elements.
Precondition
a must be an instance of RArray.

Definition at line 255 of file rarray.h.