Ruby 3.5.0dev (2025-02-22 revision 412997300569c1853c09813e4924b6df3d7e8669)
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
45typedef struct {
47 char format;
48
51
54
56 size_t offset;
57
59 size_t size;
60
65 size_t repeat;
67
77typedef struct {
82
84 void *data;
85
87 ssize_t byte_size;
88
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
157typedef bool (* rb_memory_view_get_func_t)(VALUE obj, rb_memory_view_t *view, int flags);
158
161
164
183
185
186/* memory_view.c */
187
188
193
201
210
216void 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
222bool rb_memory_view_init_as_byte_array(rb_memory_view_t *view, VALUE obj, void *data, const ssize_t len, const bool readonly);
223
228ssize_t rb_memory_view_parse_item_format(const char *format,
230 size_t *n_members, const char **err);
231
238ssize_t rb_memory_view_item_size_from_format(const char *format, const char **err);
239
247void *rb_memory_view_get_item_pointer(rb_memory_view_t *view, const ssize_t *indices);
248
256VALUE rb_memory_view_extract_item_members(const void *ptr, const rb_memory_view_item_component_t *members, const size_t n_members);
257
260
262VALUE rb_memory_view_get_item(rb_memory_view_t *view, const ssize_t *indices);
263
272
285bool rb_memory_view_get(VALUE obj, rb_memory_view_t* memory_view, int flags);
286
295
296/* for testing */
298RUBY_EXTERN VALUE rb_memory_view_exported_object_registry;
299RUBY_EXTERN const rb_data_type_t rb_memory_view_exported_object_registry_data_type;
303
311static 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
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.
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.
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...
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.
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.
ssize_t rb_memory_view_item_size_from_format(const char *format, const char **err)
Calculate the number of bytes occupied by an element.
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.
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.
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.
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...
bool rb_memory_view_available_p(VALUE obj)
Return true if obj supports to export a MemoryView.
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.
bool rb_memory_view_register(VALUE klass, const rb_memory_view_entry_t *entry)
Associates the passed class with the passed memory view entry.
bool(* rb_memory_view_available_p_func_t)(VALUE obj)
Type of function of rb_memory_view_entry_t::available_p_func.
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.
VALUE rb_memory_view_get_item(rb_memory_view_t *view, const ssize_t *indices)
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.
void rb_memory_view_prepare_item_desc(rb_memory_view_t *view)
Fill the item_desc member of view.
#define RBIMPL_ATTR_NOALIAS()
Wraps (or simulates) __declspec((noalias))
Definition noalias.h:66
#define inline
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition defines.h:91
Defines RBIMPL_ATTR_PURE.
#define RBIMPL_ATTR_PURE()
Wraps (or simulates) __attribute__((pure))
Definition pure.h:33
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.
rb_memory_view_get_func_t get_func
Exports a memory view from a Ruby object.
rb_memory_view_available_p_func_t available_p_func
Queries if an object understands memory view protocol.
rb_memory_view_release_func_t release_func
Releases a memory view that was previously generated using rb_memory_view_entry_t::get_func.
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.
ssize_t item_size
The number of bytes in each element.
const ssize_t * strides
ndim size array indicating the number of bytes to skip to go to the next element in each dimension.
ssize_t ndim
The number of dimension.
const struct rb_memory_view_entry * _memory_view_entry
DO NOT TOUCH THIS: The memory view entry for the internal use.
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.
const ssize_t * shape
ndim size array indicating the number of elements in each dimension.
void * private_data
The private data for managing this exported memory.
size_t length
The number of components in an item.
const char * format
A string to describe the format of an element, or NULL for unsigned bytes.
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