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