Ruby  3.1.4p223 (2023-03-30 revision HEAD)
buffer.h
Go to the documentation of this file.
1 #ifndef RUBY_IO_BUFFER_T
2 #define RUBY_IO_BUFFER_T 1
14 #pragma once
15 
16 #include "ruby/ruby.h"
17 #include "ruby/internal/config.h"
18 
20 
21 // WARNING: This entire interface is experimental and may change in the future!
22 #define RB_IO_BUFFER_EXPERIMENTAL 1
23 
24 RUBY_EXTERN VALUE rb_cIOBuffer;
25 RUBY_EXTERN size_t RUBY_IO_BUFFER_PAGE_SIZE;
26 RUBY_EXTERN size_t RUBY_IO_BUFFER_DEFAULT_SIZE;
27 
28 enum rb_io_buffer_flags {
29  // The memory in the buffer is owned by someone else.
30  // More specifically, it means that someone else owns the buffer and we shouldn't try to resize it.
31  RB_IO_BUFFER_EXTERNAL = 1,
32  // The memory in the buffer is allocated internally.
33  RB_IO_BUFFER_INTERNAL = 2,
34  // The memory in the buffer is mapped.
35  // A non-private mapping is marked as external.
36  RB_IO_BUFFER_MAPPED = 4,
37 
38  // The buffer is locked and cannot be resized.
39  // More specifically, it means we can't change the base address or size.
40  // A buffer is typically locked before a system call that uses the data.
41  RB_IO_BUFFER_LOCKED = 32,
42 
43  // The buffer mapping is private and will not impact other processes or the underlying file.
44  RB_IO_BUFFER_PRIVATE = 64,
45 
46  // The buffer is read-only and cannot be modified.
47  RB_IO_BUFFER_READONLY = 128
48 };
49 
50 enum rb_io_buffer_endian {
51  RB_IO_BUFFER_LITTLE_ENDIAN = 4,
52  RB_IO_BUFFER_BIG_ENDIAN = 8,
53 
54 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
55  RB_IO_BUFFER_HOST_ENDIAN = RB_IO_BUFFER_LITTLE_ENDIAN,
56 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
57  RB_IO_BUFFER_HOST_ENDIAN = RB_IO_BUFFER_BIG_ENDIAN,
58 #elif REG_DWORD == REG_DWORD_LITTLE_ENDIAN
59  RB_IO_BUFFER_HOST_ENDIAN = RB_IO_BUFFER_LITTLE_ENDIAN,
60 #elif REG_DWORD == REG_DWORD_BIG_ENDIAN
61  RB_IO_BUFFER_HOST_ENDIAN = RB_IO_BUFFER_BIG_ENDIAN,
62 #endif
63 
64  RB_IO_BUFFER_NETWORK_ENDIAN = RB_IO_BUFFER_BIG_ENDIAN
65 };
66 
67 VALUE rb_io_buffer_new(void *base, size_t size, enum rb_io_buffer_flags flags);
68 VALUE rb_io_buffer_map(VALUE io, size_t size, off_t offset, enum rb_io_buffer_flags flags);
69 
70 VALUE rb_io_buffer_lock(VALUE self);
71 VALUE rb_io_buffer_unlock(VALUE self);
72 int rb_io_buffer_try_unlock(VALUE self);
73 VALUE rb_io_buffer_free(VALUE self);
74 
75 int rb_io_buffer_get_bytes(VALUE self, void **base, size_t *size);
76 void rb_io_buffer_get_bytes_for_reading(VALUE self, const void **base, size_t *size);
77 void rb_io_buffer_get_bytes_for_writing(VALUE self, void **base, size_t *size);
78 
79 VALUE rb_io_buffer_transfer(VALUE self);
80 void rb_io_buffer_resize(VALUE self, size_t size);
81 void rb_io_buffer_clear(VALUE self, uint8_t value, size_t offset, size_t length);
82 
83 // The length is the minimum required length.
84 VALUE rb_io_buffer_read(VALUE self, VALUE io, size_t length);
85 VALUE rb_io_buffer_pread(VALUE self, VALUE io, size_t length, off_t offset);
86 VALUE rb_io_buffer_write(VALUE self, VALUE io, size_t length);
87 VALUE rb_io_buffer_pwrite(VALUE self, VALUE io, size_t length, off_t offset);
88 
90 
91 #endif /* RUBY_IO_BUFFER_T */
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition: dllexport.h:47
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition: dllexport.h:106
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition: dllexport.h:97
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40