Ruby  3.4.0dev (2024-12-06 revision 892c46283a5ea4179500d951c9d4866c0051f27b)
memory_view.h
Go to the documentation of this file.
1 #ifndef RUBY_MEMORY_VIEW_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RUBY_MEMORY_VIEW_H 1
13 #include "ruby/internal/config.h"
14 
15 #ifdef STDC_HEADERS
16 # include <stddef.h> /* size_t */
17 #endif
18 
19 #ifdef HAVE_SYS_TYPES_H
20 # include <sys/types.h> /* ssize_t */
21 #endif
22 
23 #include "ruby/internal/attr/pure.h" /* RBIMPL_ATTR_PURE */
24 #include "ruby/internal/core/rtypeddata.h" /* rb_data_type_t */
25 #include "ruby/internal/dllexport.h" /* RUBY_EXTERN */
26 #include "ruby/internal/stdbool.h" /* bool */
27 #include "ruby/internal/value.h" /* VALUE */
28 
33  RUBY_MEMORY_VIEW_SIMPLE = 0,
34  RUBY_MEMORY_VIEW_WRITABLE = (1<<0),
35  RUBY_MEMORY_VIEW_FORMAT = (1<<1),
36  RUBY_MEMORY_VIEW_MULTI_DIMENSIONAL = (1<<2),
37  RUBY_MEMORY_VIEW_STRIDES = (1<<3) | RUBY_MEMORY_VIEW_MULTI_DIMENSIONAL,
38  RUBY_MEMORY_VIEW_ROW_MAJOR = (1<<4) | RUBY_MEMORY_VIEW_STRIDES,
39  RUBY_MEMORY_VIEW_COLUMN_MAJOR = (1<<5) | RUBY_MEMORY_VIEW_STRIDES,
40  RUBY_MEMORY_VIEW_ANY_CONTIGUOUS = RUBY_MEMORY_VIEW_ROW_MAJOR | RUBY_MEMORY_VIEW_COLUMN_MAJOR,
41  RUBY_MEMORY_VIEW_INDIRECT = (1<<6) | RUBY_MEMORY_VIEW_STRIDES,
42 };
43 
45 typedef struct {
47  char format;
48 
51 
54 
56  size_t offset;
57 
59  size_t size;
60 
65  size_t repeat;
67 
77 typedef struct {
82 
84  void *data;
85 
87  ssize_t byte_size;
88 
90  bool readonly;
91 
111  const char *format;
112 
116  ssize_t item_size;
117 
119  struct {
126 
128  size_t length;
129  } item_desc;
130 
132  ssize_t ndim;
133 
137  const ssize_t *shape;
138 
142  const ssize_t *strides;
143 
147  const ssize_t *sub_offsets;
148 
151 
155 
157 typedef bool (* rb_memory_view_get_func_t)(VALUE obj, rb_memory_view_t *view, int flags);
158 
161 
164 
166 typedef struct rb_memory_view_entry {
171 
177 
183 
185 
186 /* memory_view.c */
187 
188 
192 bool rb_memory_view_register(VALUE klass, const rb_memory_view_entry_t *entry);
193 
201 
210 
216 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);
217 
222 bool rb_memory_view_init_as_byte_array(rb_memory_view_t *view, VALUE obj, void *data, const ssize_t len, const bool readonly);
223 
228 ssize_t rb_memory_view_parse_item_format(const char *format,
230  size_t *n_members, const char **err);
231 
238 ssize_t rb_memory_view_item_size_from_format(const char *format, const char **err);
239 
247 void *rb_memory_view_get_item_pointer(rb_memory_view_t *view, const ssize_t *indices);
248 
256 VALUE rb_memory_view_extract_item_members(const void *ptr, const rb_memory_view_item_component_t *members, const size_t n_members);
257 
260 
262 VALUE rb_memory_view_get_item(rb_memory_view_t *view, const ssize_t *indices);
263 
272 
285 bool rb_memory_view_get(VALUE obj, rb_memory_view_t* memory_view, int flags);
286 
294 bool rb_memory_view_release(rb_memory_view_t* memory_view);
295 
296 /* for testing */
298 RUBY_EXTERN VALUE rb_memory_view_exported_object_registry;
299 RUBY_EXTERN const rb_data_type_t rb_memory_view_exported_object_registry_data_type;
303 
311 static inline bool
313 {
315  return true;
316  }
318  return true;
319  }
320  else {
321  return false;
322  }
323 }
324 
325 #endif /* RUBY_BUFFER_H */
Tweaking visibility of C variables/functions.
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition: dllexport.h:45
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition: dllexport.h:74
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition: dllexport.h:65
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
Definition: io.h:2
RBIMPL_ATTR_PURE() int rb_io_read_pending(rb_io_t *fptr)
Queries if the passed IO has any pending reads.
int len
Length of the buffer.
Definition: io.h:8
ruby_memory_view_flags
Flags passed to rb_memory_view_get(), then to rb_memory_view_get_func_t.
Definition: memory_view.h:32
void * rb_memory_view_get_item_pointer(rb_memory_view_t *view, const ssize_t *indices)
Calculate the location of the item indicated by the given indices.
Definition: memory_view.c:513
bool rb_memory_view_is_row_major_contiguous(const rb_memory_view_t *view)
Return true if the data in the MemoryView view is row-major contiguous.
Definition: memory_view.c:149
bool rb_memory_view_get(VALUE obj, rb_memory_view_t *memory_view, int flags)
If the given obj supports to export a MemoryView that conforms the given flags, this function fills v...
Definition: memory_view.c:815
VALUE rb_memory_view_extract_item_members(const void *ptr, const rb_memory_view_item_component_t *members, const size_t n_members)
Return a value that consists of item members.
Definition: memory_view.c:727
bool(* rb_memory_view_get_func_t)(VALUE obj, rb_memory_view_t *view, int flags)
Type of function of rb_memory_view_entry_t::get_func.
Definition: memory_view.h:157
ssize_t rb_memory_view_item_size_from_format(const char *format, const char **err)
Calculate the number of bytes occupied by an element.
Definition: memory_view.c:506
bool(* rb_memory_view_release_func_t)(VALUE obj, rb_memory_view_t *view)
Type of function of rb_memory_view_entry_t::release_func.
Definition: memory_view.h:160
static bool rb_memory_view_is_contiguous(const rb_memory_view_t *view)
Return true if the data in the MemoryView view is row-major or column-major contiguous.
Definition: memory_view.h:312
struct rb_memory_view_entry rb_memory_view_entry_t
Operations applied to a specific kind of a memory view.
bool rb_memory_view_release(rb_memory_view_t *memory_view)
Release the given MemoryView view and decrement the reference count of memory_view->obj.
Definition: memory_view.c:837
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)
Fill the strides array with byte-Strides of a contiguous array of the given shape with the given elem...
Definition: memory_view.c:181
bool rb_memory_view_available_p(VALUE obj)
Return true if obj supports to export a MemoryView.
Definition: memory_view.c:803
bool rb_memory_view_is_column_major_contiguous(const rb_memory_view_t *view)
Return true if the data in the MemoryView view is column-major contiguous.
Definition: memory_view.c:165
bool rb_memory_view_register(VALUE klass, const rb_memory_view_entry_t *entry)
Associates the passed class with the passed memory view entry.
Definition: memory_view.c:132
bool(* rb_memory_view_available_p_func_t)(VALUE obj)
Type of function of rb_memory_view_entry_t::available_p_func.
Definition: memory_view.h:163
bool rb_memory_view_init_as_byte_array(rb_memory_view_t *view, VALUE obj, void *data, const ssize_t len, const bool readonly)
Fill the members of view as an 1-dimensional byte array.
Definition: memory_view.c:200
VALUE rb_memory_view_get_item(rb_memory_view_t *view, const ssize_t *indices)
Definition: memory_view.c:767
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)
Deconstructs the passed format string, as describe in rb_memory_view_t::format.
Definition: memory_view.c:389
void rb_memory_view_prepare_item_desc(rb_memory_view_t *view)
Fill the item_desc member of view.
Definition: memory_view.c:750
#define RBIMPL_ATTR_NOALIAS()
Wraps (or simulates) __declspec((noalias))
Definition: noalias.h:62
#define inline
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition: defines.h:88
Defines RBIMPL_ATTR_PURE.
Defines struct RTypedData.
C99 shim for <stdbool.h>
This is the struct that holds necessary info for a struct.
Definition: rtypeddata.h:200
Operations applied to a specific kind of a memory view.
Definition: memory_view.h:166
rb_memory_view_get_func_t get_func
Exports a memory view from a Ruby object.
Definition: memory_view.h:170
rb_memory_view_available_p_func_t available_p_func
Queries if an object understands memory view protocol.
Definition: memory_view.h:181
rb_memory_view_release_func_t release_func
Releases a memory view that was previously generated using rb_memory_view_entry_t::get_func.
Definition: memory_view.h:176
Memory view component metadata.
Definition: memory_view.h:45
bool native_size_p
:FIXME: what is a "native" size is unclear.
Definition: memory_view.h:50
size_t size
The component's size.
Definition: memory_view.h:59
size_t repeat
How many numbers of components are there.
Definition: memory_view.h:65
size_t offset
The component's offset.
Definition: memory_view.h:56
bool little_endian_p
Endian of the component.
Definition: memory_view.h:53
A MemoryView structure, rb_memory_view_t, is used for exporting objects' MemoryView.
Definition: memory_view.h:77
const rb_memory_view_item_component_t * components
The array of rb_memory_view_item_component_t that describes the item structure.
Definition: memory_view.h:125
ssize_t item_size
The number of bytes in each element.
Definition: memory_view.h:116
const ssize_t * strides
ndim size array indicating the number of bytes to skip to go to the next element in each dimension.
Definition: memory_view.h:142
ssize_t ndim
The number of dimension.
Definition: memory_view.h:132
const struct rb_memory_view_entry * _memory_view_entry
DO NOT TOUCH THIS: The memory view entry for the internal use.
Definition: memory_view.h:153
void * data
The pointer to the exported memory.
Definition: memory_view.h:84
VALUE obj
The original object that has the memory exported via this memory view.
Definition: memory_view.h:81
const ssize_t * sub_offsets
The offset in each dimension when this memory view exposes a nested array.
Definition: memory_view.h:147
const ssize_t * shape
ndim size array indicating the number of elements in each dimension.
Definition: memory_view.h:137
void * private_data
The private data for managing this exported memory.
Definition: memory_view.h:150
size_t length
The number of components in an item.
Definition: memory_view.h:128
const char * format
A string to describe the format of an element, or NULL for unsigned bytes.
Definition: memory_view.h:111
ssize_t byte_size
The number of bytes in data.
Definition: memory_view.h:87
bool readonly
true for readonly memory, false for writable memory.
Definition: memory_view.h:90
Defines VALUE and ID.
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40