Ruby 4.1.0dev (2026-10-04 revision 76aa225e9af575a320135123891fe5b5919411a1)
buffer.h
Go to the documentation of this file.
1#ifndef RUBY_IO_BUFFER_H
2#define RUBY_IO_BUFFER_H
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// Version 4: Buffer is the common view interface for Storage and Slice;
25// allocation lifecycle operations require Storage. Version 3 introduced
26// single-transfer IO semantics.
27#define RUBY_IO_BUFFER_VERSION 4
28
29// The `IO::Buffer` class.
30RUBY_EXTERN VALUE rb_cIOBuffer;
31RUBY_EXTERN VALUE rb_cIOBufferStorage;
32RUBY_EXTERN VALUE rb_cIOBufferSlice;
33
34// Returns non-zero for a native Storage or Slice representation. This verifies
35// the native payload, rather than only Ruby inheritance from IO::Buffer.
36int rb_io_buffer_p(VALUE self);
37
38// Returns whether the view is read-only, including restrictions inherited
39// from its current source.
40int rb_io_buffer_readonly_p(VALUE self);
41
42// The operating system page size.
43RUBY_EXTERN size_t RUBY_IO_BUFFER_PAGE_SIZE;
44
45// The alignment required for file mapping offsets.
46RUBY_EXTERN size_t RUBY_IO_BUFFER_MAP_ALIGNMENT;
47
48// The default buffer size, usually a (small) multiple of the page size.
49// Can be overridden by the RUBY_IO_BUFFER_DEFAULT_SIZE environment variable.
50RUBY_EXTERN size_t RUBY_IO_BUFFER_DEFAULT_SIZE;
51
52// Represents the internal state of the buffer.
53// More than one flag can be set at a time.
54enum rb_io_buffer_flags {
55 // The memory in the buffer is owned by someone else.
56 // More specifically, it means that someone else owns the buffer and we shouldn't try to resize it.
57 RB_IO_BUFFER_EXTERNAL = 1,
58 // The memory in the buffer is allocated internally.
59 RB_IO_BUFFER_INTERNAL = 2,
60 // The memory in the buffer is mapped.
61 // A non-private mapping is marked as external.
62 RB_IO_BUFFER_MAPPED = 4,
63
64 // A mapped buffer that is also shared.
65 RB_IO_BUFFER_SHARED = 8,
66
67 // The buffer mapping is private and will not impact other processes or the underlying file.
68 RB_IO_BUFFER_PRIVATE = 64,
69
70 // The buffer is read-only and cannot be modified.
71 RB_IO_BUFFER_READONLY = 128,
72
73 // The buffer is backed by a file.
74 RB_IO_BUFFER_FILE = 256,
75};
76
77// Represents the endian of the data types.
78enum rb_io_buffer_endian {
79 // The least significant units are put first.
80 RB_IO_BUFFER_LITTLE_ENDIAN = 4,
81 RB_IO_BUFFER_BIG_ENDIAN = 8,
82
83#if defined(WORDS_BIGENDIAN)
84 RB_IO_BUFFER_HOST_ENDIAN = RB_IO_BUFFER_BIG_ENDIAN,
85#else
86 RB_IO_BUFFER_HOST_ENDIAN = RB_IO_BUFFER_LITTLE_ENDIAN,
87#endif
88
89 RB_IO_BUFFER_NETWORK_ENDIAN = RB_IO_BUFFER_BIG_ENDIAN
90};
91
92VALUE rb_io_buffer_new(void *base, size_t size, enum rb_io_buffer_flags flags);
93// Create a buffer with an initial lock count of one. This is typically used
94// for temporary wrappers around borrowed memory and paired with
95// rb_io_buffer_free_locked.
96VALUE rb_io_buffer_new_locked(void *base, size_t size, enum rb_io_buffer_flags flags);
97VALUE rb_io_buffer_map(VALUE io, size_t size, rb_off_t offset, enum rb_io_buffer_flags flags);
98
99// Acquire and release a reference-counted lock on the backing allocation.
100// Every successful lock call must be paired with exactly one unlock call.
101VALUE rb_io_buffer_lock(VALUE self);
102VALUE rb_io_buffer_unlock(VALUE self);
103int rb_io_buffer_try_unlock(VALUE self);
104
105// Allocation lifecycle operations require Storage and reject Slice receivers.
106VALUE rb_io_buffer_free(VALUE self);
107// Release the buffer's only lock and immediately invalidate it. This is for
108// temporary wrappers around borrowed memory. Calls rb_bug if the lock count is
109// not exactly one.
110VALUE rb_io_buffer_free_locked(VALUE self);
111
112// Access the buffer and flags. Validates the pointers. READONLY reflects both
113// the view's own restriction and its source's current permissions. If the
114// returned base is NULL, the returned size is always zero.
115// The pointers may not remain valid if the source buffer is manipulated.
116// Consider using rb_io_buffer_lock if needed.
117enum rb_io_buffer_flags rb_io_buffer_get_bytes(VALUE self, void **base, size_t *size);
118void rb_io_buffer_get_bytes_for_reading(VALUE self, const void **base, size_t *size);
119void rb_io_buffer_get_bytes_for_writing(VALUE self, void **base, size_t *size);
120
121// Lock the backing allocation, invoke the callback with its readable bytes,
122// and automatically unlock it when the callback returns or raises. The bytes
123// are only valid for the duration of the callback. This protects the lifetime
124// of the allocation; it does not provide synchronization for its contents.
125VALUE rb_io_buffer_locked_for_reading(VALUE self, VALUE (*callback)(const void *base, size_t size, VALUE argument), VALUE argument);
126
127// Lock the backing allocation, invoke the callback with its writable bytes,
128// and automatically unlock it when the callback returns or raises. The bytes
129// are only valid for the duration of the callback. This protects the lifetime
130// of the allocation; it does not provide synchronization for its contents.
131VALUE rb_io_buffer_locked_for_writing(VALUE self, VALUE (*callback)(void *base, size_t size, VALUE argument), VALUE argument);
132
133VALUE rb_io_buffer_transfer(VALUE self);
134void rb_io_buffer_resize(VALUE self, size_t size);
135// Advance the start of a non-owning buffer or slice by the given amount.
136void rb_io_buffer_advance(VALUE self, size_t amount);
137void rb_io_buffer_clear(VALUE self, uint8_t value, size_t offset, size_t length);
138
139// The length is the maximum transfer length. Each function performs one
140// logical IO operation and may return a short result.
141VALUE rb_io_buffer_read(VALUE self, VALUE io, size_t offset, size_t length);
142VALUE rb_io_buffer_pread(VALUE self, VALUE io, rb_off_t from, size_t offset, size_t length);
143VALUE rb_io_buffer_write(VALUE self, VALUE io, size_t offset, size_t length);
144VALUE rb_io_buffer_pwrite(VALUE self, VALUE io, rb_off_t from, size_t offset, size_t length);
145
147
148#endif /* RUBY_IO_BUFFER_H */
#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
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40