Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby32/include/ruby
File: memory_view.h
#ifndef RUBY_MEMORY_VIEW_H /*-*-C++-*-vi:se ft=cpp:*/
[0] Fix | Delete
#define RUBY_MEMORY_VIEW_H 1
[1] Fix | Delete
/**
[2] Fix | Delete
* @file
[3] Fix | Delete
* @author Ruby developers <ruby-core@ruby-lang.org>
[4] Fix | Delete
* @copyright This file is a part of the programming language Ruby.
[5] Fix | Delete
* Permission is hereby granted, to either redistribute and/or
[6] Fix | Delete
* modify this file, provided that the conditions mentioned in the
[7] Fix | Delete
* file COPYING are met. Consult the file for details.
[8] Fix | Delete
* @brief Memory View.
[9] Fix | Delete
*/
[10] Fix | Delete
[11] Fix | Delete
#include "ruby/internal/config.h"
[12] Fix | Delete
[13] Fix | Delete
#ifdef STDC_HEADERS
[14] Fix | Delete
# include <stddef.h> /* size_t */
[15] Fix | Delete
#endif
[16] Fix | Delete
[17] Fix | Delete
#ifdef HAVE_SYS_TYPES_H
[18] Fix | Delete
# include <sys/types.h> /* ssize_t */
[19] Fix | Delete
#endif
[20] Fix | Delete
[21] Fix | Delete
#include "ruby/internal/attr/pure.h" /* RBIMPL_ATTR_PURE */
[22] Fix | Delete
#include "ruby/internal/core/rtypeddata.h" /* rb_data_type_t */
[23] Fix | Delete
#include "ruby/internal/dllexport.h" /* RUBY_EXTERN */
[24] Fix | Delete
#include "ruby/internal/stdbool.h" /* bool */
[25] Fix | Delete
#include "ruby/internal/value.h" /* VALUE */
[26] Fix | Delete
[27] Fix | Delete
/**
[28] Fix | Delete
* Flags passed to rb_memory_view_get(), then to ::rb_memory_view_get_func_t.
[29] Fix | Delete
*/
[30] Fix | Delete
enum ruby_memory_view_flags {
[31] Fix | Delete
RUBY_MEMORY_VIEW_SIMPLE = 0,
[32] Fix | Delete
RUBY_MEMORY_VIEW_WRITABLE = (1<<0),
[33] Fix | Delete
RUBY_MEMORY_VIEW_FORMAT = (1<<1),
[34] Fix | Delete
RUBY_MEMORY_VIEW_MULTI_DIMENSIONAL = (1<<2),
[35] Fix | Delete
RUBY_MEMORY_VIEW_STRIDES = (1<<3) | RUBY_MEMORY_VIEW_MULTI_DIMENSIONAL,
[36] Fix | Delete
RUBY_MEMORY_VIEW_ROW_MAJOR = (1<<4) | RUBY_MEMORY_VIEW_STRIDES,
[37] Fix | Delete
RUBY_MEMORY_VIEW_COLUMN_MAJOR = (1<<5) | RUBY_MEMORY_VIEW_STRIDES,
[38] Fix | Delete
RUBY_MEMORY_VIEW_ANY_CONTIGUOUS = RUBY_MEMORY_VIEW_ROW_MAJOR | RUBY_MEMORY_VIEW_COLUMN_MAJOR,
[39] Fix | Delete
RUBY_MEMORY_VIEW_INDIRECT = (1<<6) | RUBY_MEMORY_VIEW_STRIDES,
[40] Fix | Delete
};
[41] Fix | Delete
[42] Fix | Delete
/** Memory view component metadata. */
[43] Fix | Delete
typedef struct {
[44] Fix | Delete
/** @see ::rb_memory_view_t::format */
[45] Fix | Delete
char format;
[46] Fix | Delete
[47] Fix | Delete
/** :FIXME: what is a "native" size is unclear. */
[48] Fix | Delete
unsigned native_size_p: 1;
[49] Fix | Delete
[50] Fix | Delete
/** Endian of the component */
[51] Fix | Delete
unsigned little_endian_p: 1;
[52] Fix | Delete
[53] Fix | Delete
/** The component's offset. */
[54] Fix | Delete
size_t offset;
[55] Fix | Delete
[56] Fix | Delete
/** The component's size. */
[57] Fix | Delete
size_t size;
[58] Fix | Delete
[59] Fix | Delete
/**
[60] Fix | Delete
* How many numbers of components are there. For instance "CCC"'s repeat is
[61] Fix | Delete
* 3.
[62] Fix | Delete
*/
[63] Fix | Delete
size_t repeat;
[64] Fix | Delete
} rb_memory_view_item_component_t;
[65] Fix | Delete
[66] Fix | Delete
/**
[67] Fix | Delete
* A MemoryView structure, `rb_memory_view_t`, is used for exporting objects'
[68] Fix | Delete
* MemoryView.
[69] Fix | Delete
*
[70] Fix | Delete
* This structure contains the reference of the object, which is the owner of
[71] Fix | Delete
* the MemoryView, the pointer to the head of exported memory, and the metadata
[72] Fix | Delete
* that describes the structure of the memory. The metadata can describe
[73] Fix | Delete
* multidimensional arrays with strides.
[74] Fix | Delete
*/
[75] Fix | Delete
typedef struct {
[76] Fix | Delete
/**
[77] Fix | Delete
* The original object that has the memory exported via this memory view.
[78] Fix | Delete
*/
[79] Fix | Delete
VALUE obj;
[80] Fix | Delete
[81] Fix | Delete
/** The pointer to the exported memory. */
[82] Fix | Delete
void *data;
[83] Fix | Delete
[84] Fix | Delete
/** The number of bytes in data. */
[85] Fix | Delete
ssize_t byte_size;
[86] Fix | Delete
[87] Fix | Delete
/** true for readonly memory, false for writable memory. */
[88] Fix | Delete
bool readonly;
[89] Fix | Delete
[90] Fix | Delete
/**
[91] Fix | Delete
* A string to describe the format of an element, or NULL for unsigned bytes.
[92] Fix | Delete
* The format string is a sequence of the following pack-template specifiers:
[93] Fix | Delete
*
[94] Fix | Delete
* c, C, s, s!, S, S!, n, v, i, i!, I, I!, l, l!, L, L!,
[95] Fix | Delete
* N, V, f, e, g, q, q!, Q, Q!, d, E, G, j, J, x
[96] Fix | Delete
*
[97] Fix | Delete
* For example, "dd" for an element that consists of two double values,
[98] Fix | Delete
* and "CCC" for an element that consists of three bytes, such as
[99] Fix | Delete
* an RGB color triplet.
[100] Fix | Delete
*
[101] Fix | Delete
* Also, the value endianness can be explicitly specified by '<' or '>'
[102] Fix | Delete
* following a value type specifier.
[103] Fix | Delete
*
[104] Fix | Delete
* The items are packed contiguously. When you emulate the alignment of
[105] Fix | Delete
* structure members, put '|' at the beginning of the format string,
[106] Fix | Delete
* like "|iqc". On x86_64 Linux ABI, the size of the item by this format
[107] Fix | Delete
* is 24 bytes instead of 13 bytes.
[108] Fix | Delete
*/
[109] Fix | Delete
const char *format;
[110] Fix | Delete
[111] Fix | Delete
/**
[112] Fix | Delete
* The number of bytes in each element.
[113] Fix | Delete
* item_size should equal to rb_memory_view_item_size_from_format(format). */
[114] Fix | Delete
ssize_t item_size;
[115] Fix | Delete
[116] Fix | Delete
/** Description of each components. */
[117] Fix | Delete
struct {
[118] Fix | Delete
/**
[119] Fix | Delete
* The array of rb_memory_view_item_component_t that describes the
[120] Fix | Delete
* item structure. rb_memory_view_prepare_item_desc and
[121] Fix | Delete
* rb_memory_view_get_item allocate this memory if needed,
[122] Fix | Delete
* and rb_memory_view_release frees it. */
[123] Fix | Delete
const rb_memory_view_item_component_t *components;
[124] Fix | Delete
[125] Fix | Delete
/** The number of components in an item. */
[126] Fix | Delete
size_t length;
[127] Fix | Delete
} item_desc;
[128] Fix | Delete
[129] Fix | Delete
/** The number of dimension. */
[130] Fix | Delete
ssize_t ndim;
[131] Fix | Delete
[132] Fix | Delete
/**
[133] Fix | Delete
* ndim size array indicating the number of elements in each dimension.
[134] Fix | Delete
* This can be NULL when ndim == 1. */
[135] Fix | Delete
const ssize_t *shape;
[136] Fix | Delete
[137] Fix | Delete
/**
[138] Fix | Delete
* ndim size array indicating the number of bytes to skip to go to the
[139] Fix | Delete
* next element in each dimension. */
[140] Fix | Delete
const ssize_t *strides;
[141] Fix | Delete
[142] Fix | Delete
/**
[143] Fix | Delete
* The offset in each dimension when this memory view exposes a nested array.
[144] Fix | Delete
* Or, NULL when this memory view exposes a flat array. */
[145] Fix | Delete
const ssize_t *sub_offsets;
[146] Fix | Delete
[147] Fix | Delete
/** The private data for managing this exported memory */
[148] Fix | Delete
void *private_data;
[149] Fix | Delete
[150] Fix | Delete
/** DO NOT TOUCH THIS: The memory view entry for the internal use */
[151] Fix | Delete
const struct rb_memory_view_entry *_memory_view_entry;
[152] Fix | Delete
} rb_memory_view_t;
[153] Fix | Delete
[154] Fix | Delete
/** Type of function of ::rb_memory_view_entry_t::get_func. */
[155] Fix | Delete
typedef bool (* rb_memory_view_get_func_t)(VALUE obj, rb_memory_view_t *view, int flags);
[156] Fix | Delete
[157] Fix | Delete
/** Type of function of ::rb_memory_view_entry_t::release_func. */
[158] Fix | Delete
typedef bool (* rb_memory_view_release_func_t)(VALUE obj, rb_memory_view_t *view);
[159] Fix | Delete
[160] Fix | Delete
/** Type of function of ::rb_memory_view_entry_t::available_p_func. */
[161] Fix | Delete
typedef bool (* rb_memory_view_available_p_func_t)(VALUE obj);
[162] Fix | Delete
[163] Fix | Delete
/** Operations applied to a specific kind of a memory view. */
[164] Fix | Delete
typedef struct rb_memory_view_entry {
[165] Fix | Delete
/**
[166] Fix | Delete
* Exports a memory view from a Ruby object.
[167] Fix | Delete
*/
[168] Fix | Delete
rb_memory_view_get_func_t get_func;
[169] Fix | Delete
[170] Fix | Delete
/**
[171] Fix | Delete
* Releases a memory view that was previously generated using
[172] Fix | Delete
* ::rb_memory_view_entry_t::get_func.
[173] Fix | Delete
*/
[174] Fix | Delete
rb_memory_view_release_func_t release_func;
[175] Fix | Delete
[176] Fix | Delete
/**
[177] Fix | Delete
* Queries if an object understands memory view protocol.
[178] Fix | Delete
*/
[179] Fix | Delete
rb_memory_view_available_p_func_t available_p_func;
[180] Fix | Delete
} rb_memory_view_entry_t;
[181] Fix | Delete
[182] Fix | Delete
RBIMPL_SYMBOL_EXPORT_BEGIN()
[183] Fix | Delete
[184] Fix | Delete
/* memory_view.c */
[185] Fix | Delete
[186] Fix | Delete
/**
[187] Fix | Delete
* Associates the passed class with the passed memory view entry. This has to
[188] Fix | Delete
* be called before actually creating a memory view from an instance.
[189] Fix | Delete
*/
[190] Fix | Delete
bool rb_memory_view_register(VALUE klass, const rb_memory_view_entry_t *entry);
[191] Fix | Delete
[192] Fix | Delete
RBIMPL_ATTR_PURE()
[193] Fix | Delete
/**
[194] Fix | Delete
* Return `true` if the data in the MemoryView `view` is row-major contiguous.
[195] Fix | Delete
*
[196] Fix | Delete
* Return `false` otherwise.
[197] Fix | Delete
*/
[198] Fix | Delete
bool rb_memory_view_is_row_major_contiguous(const rb_memory_view_t *view);
[199] Fix | Delete
[200] Fix | Delete
RBIMPL_ATTR_PURE()
[201] Fix | Delete
/**
[202] Fix | Delete
* Return `true` if the data in the MemoryView `view` is column-major
[203] Fix | Delete
* contiguous.
[204] Fix | Delete
*
[205] Fix | Delete
* Return `false` otherwise.
[206] Fix | Delete
*/
[207] Fix | Delete
bool rb_memory_view_is_column_major_contiguous(const rb_memory_view_t *view);
[208] Fix | Delete
[209] Fix | Delete
RBIMPL_ATTR_NOALIAS()
[210] Fix | Delete
/**
[211] Fix | Delete
* Fill the `strides` array with byte-Strides of a contiguous array of the
[212] Fix | Delete
* given shape with the given element size.
[213] Fix | Delete
*/
[214] Fix | Delete
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);
[215] Fix | Delete
[216] Fix | Delete
RBIMPL_ATTR_NOALIAS()
[217] Fix | Delete
/**
[218] Fix | Delete
* Fill the members of `view` as an 1-dimensional byte array.
[219] Fix | Delete
*/
[220] Fix | Delete
bool rb_memory_view_init_as_byte_array(rb_memory_view_t *view, VALUE obj, void *data, const ssize_t len, const bool readonly);
[221] Fix | Delete
[222] Fix | Delete
/**
[223] Fix | Delete
* Deconstructs the passed format string, as describe in
[224] Fix | Delete
* ::rb_memory_view_t::format.
[225] Fix | Delete
*/
[226] Fix | Delete
ssize_t rb_memory_view_parse_item_format(const char *format,
[227] Fix | Delete
rb_memory_view_item_component_t **members,
[228] Fix | Delete
size_t *n_members, const char **err);
[229] Fix | Delete
[230] Fix | Delete
/**
[231] Fix | Delete
* Calculate the number of bytes occupied by an element.
[232] Fix | Delete
*
[233] Fix | Delete
* When the calculation fails, the failed location in `format` is stored into
[234] Fix | Delete
* `err`, and returns `-1`.
[235] Fix | Delete
*/
[236] Fix | Delete
ssize_t rb_memory_view_item_size_from_format(const char *format, const char **err);
[237] Fix | Delete
[238] Fix | Delete
/**
[239] Fix | Delete
* Calculate the location of the item indicated by the given `indices`.
[240] Fix | Delete
*
[241] Fix | Delete
* The length of `indices` must equal to `view->ndim`.
[242] Fix | Delete
*
[243] Fix | Delete
* This function initializes `view->item_desc` if needed.
[244] Fix | Delete
*/
[245] Fix | Delete
void *rb_memory_view_get_item_pointer(rb_memory_view_t *view, const ssize_t *indices);
[246] Fix | Delete
[247] Fix | Delete
/**
[248] Fix | Delete
* Return a value that consists of item members.
[249] Fix | Delete
*
[250] Fix | Delete
* When an item is a single member, the return value is a single value.
[251] Fix | Delete
*
[252] Fix | Delete
* When an item consists of multiple members, an array will be returned.
[253] Fix | Delete
*/
[254] Fix | Delete
VALUE rb_memory_view_extract_item_members(const void *ptr, const rb_memory_view_item_component_t *members, const size_t n_members);
[255] Fix | Delete
[256] Fix | Delete
/** Fill the `item_desc` member of `view`. */
[257] Fix | Delete
void rb_memory_view_prepare_item_desc(rb_memory_view_t *view);
[258] Fix | Delete
[259] Fix | Delete
/** * Return a value that consists of item members in the given memory view. */
[260] Fix | Delete
VALUE rb_memory_view_get_item(rb_memory_view_t *view, const ssize_t *indices);
[261] Fix | Delete
[262] Fix | Delete
/**
[263] Fix | Delete
* Return `true` if `obj` supports to export a MemoryView. Return `false`
[264] Fix | Delete
* otherwise.
[265] Fix | Delete
*
[266] Fix | Delete
* If this function returns `true`, it doesn't mean the function
[267] Fix | Delete
* `rb_memory_view_get` will succeed.
[268] Fix | Delete
*/
[269] Fix | Delete
bool rb_memory_view_available_p(VALUE obj);
[270] Fix | Delete
[271] Fix | Delete
/**
[272] Fix | Delete
* If the given `obj` supports to export a MemoryView that conforms the given
[273] Fix | Delete
* `flags`, this function fills `view` by the information of the MemoryView and
[274] Fix | Delete
* returns `true`. In this case, the reference count of `obj` is increased.
[275] Fix | Delete
*
[276] Fix | Delete
* If the given combination of `obj` and `flags` cannot export a MemoryView,
[277] Fix | Delete
* this function returns `false`. The content of `view` is not touched in this
[278] Fix | Delete
* case.
[279] Fix | Delete
*
[280] Fix | Delete
* The exported MemoryView must be released by `rb_memory_view_release` when
[281] Fix | Delete
* the MemoryView is no longer needed.
[282] Fix | Delete
*/
[283] Fix | Delete
bool rb_memory_view_get(VALUE obj, rb_memory_view_t* memory_view, int flags);
[284] Fix | Delete
[285] Fix | Delete
/**
[286] Fix | Delete
* Release the given MemoryView `view` and decrement the reference count of
[287] Fix | Delete
* `memory_view->obj`.
[288] Fix | Delete
*
[289] Fix | Delete
* Consumers must call this function when the MemoryView is no longer needed.
[290] Fix | Delete
* Missing to call this function leads memory leak.
[291] Fix | Delete
*/
[292] Fix | Delete
bool rb_memory_view_release(rb_memory_view_t* memory_view);
[293] Fix | Delete
[294] Fix | Delete
/* for testing */
[295] Fix | Delete
/** @cond INTERNAL_MACRO */
[296] Fix | Delete
RUBY_EXTERN VALUE rb_memory_view_exported_object_registry;
[297] Fix | Delete
RUBY_EXTERN const rb_data_type_t rb_memory_view_exported_object_registry_data_type;
[298] Fix | Delete
/** @endcond */
[299] Fix | Delete
[300] Fix | Delete
RBIMPL_SYMBOL_EXPORT_END()
[301] Fix | Delete
[302] Fix | Delete
RBIMPL_ATTR_PURE()
[303] Fix | Delete
/**
[304] Fix | Delete
* Return `true` if the data in the MemoryView `view` is row-major or
[305] Fix | Delete
* column-major contiguous.
[306] Fix | Delete
*
[307] Fix | Delete
* Return `false` otherwise.
[308] Fix | Delete
*/
[309] Fix | Delete
static inline bool
[310] Fix | Delete
rb_memory_view_is_contiguous(const rb_memory_view_t *view)
[311] Fix | Delete
{
[312] Fix | Delete
if (rb_memory_view_is_row_major_contiguous(view)) {
[313] Fix | Delete
return true;
[314] Fix | Delete
}
[315] Fix | Delete
else if (rb_memory_view_is_column_major_contiguous(view)) {
[316] Fix | Delete
return true;
[317] Fix | Delete
}
[318] Fix | Delete
else {
[319] Fix | Delete
return false;
[320] Fix | Delete
}
[321] Fix | Delete
}
[322] Fix | Delete
[323] Fix | Delete
#endif /* RUBY_BUFFER_H */
[324] Fix | Delete
[325] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function