Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)
memory.h
Go to the documentation of this file.
1#ifndef RBIMPL_MEMORY_H /*-*-C++-*-vi:se ft=cpp:*/
2#define RBIMPL_MEMORY_H
24
25#ifdef STDC_HEADERS
26# include <stddef.h>
27#endif
28
29#ifdef HAVE_STRING_H
30# include <string.h>
31#endif
32
33#ifdef HAVE_STDINT_H
34# include <stdint.h>
35#endif
36
37#ifdef HAVE_ALLOCA_H
38# include <alloca.h>
39#endif
40
41#if defined(_MSC_VER) && defined(_WIN64)
42# include <intrin.h>
43# pragma intrinsic(_umul128)
44#endif
45
54#include "ruby/internal/cast.h"
63#include "ruby/defines.h"
64
65/* Make alloca work the best possible way. */
66#if defined(alloca)
67# /* Take that. */
68#elif RBIMPL_HAS_BUILTIN(__builtin_alloca)
69# define alloca __builtin_alloca
70#elif defined(_AIX)
71# pragma alloca
72#elif defined(__cplusplus)
73extern "C" void *alloca(size_t);
74#else
75extern void *alloca();
76#endif
77
78#if defined(HAVE_INT128_T) && SIZEOF_SIZE_T <= 8
79# define DSIZE_T uint128_t
80#elif SIZEOF_SIZE_T * 2 <= SIZEOF_LONG_LONG
81# define DSIZE_T unsigned LONG_LONG
82#endif
83
84#ifdef C_ALLOCA
85# define RUBY_ALLOCV_LIMIT 0
86#else
87# define RUBY_ALLOCV_LIMIT 1024
88#endif
89
90#ifdef __GNUC__
91#define RB_GC_GUARD(v) \
92 (*__extension__ ({ \
93 volatile VALUE *rb_gc_guarded_ptr = &(v); \
94 __asm__("" : : "m"(rb_gc_guarded_ptr)); \
95 rb_gc_guarded_ptr; \
96 }))
97#elif defined _MSC_VER
98#define RB_GC_GUARD(v) (*rb_gc_guarded_ptr(&(v)))
99#else
100#define HAVE_RB_GC_GUARDED_PTR_VAL 1
101#define RB_GC_GUARD(v) (*rb_gc_guarded_ptr_val(&(v),(v)))
102#endif
103
104/* Casts needed because void* is NOT compaible with others in C++. */
105#define RB_ALLOC_N(type,n) RBIMPL_CAST((type *)ruby_xmalloc2((n), sizeof(type)))
106#define RB_ALLOC(type) RBIMPL_CAST((type *)ruby_xmalloc(sizeof(type)))
107#define RB_ZALLOC_N(type,n) RBIMPL_CAST((type *)ruby_xcalloc((n), sizeof(type)))
108#define RB_ZALLOC(type) (RB_ZALLOC_N(type, 1))
109#define RB_REALLOC_N(var,type,n) \
110 ((var) = RBIMPL_CAST((type *)ruby_xrealloc2((void *)(var), (n), sizeof(type))))
111
112#define ALLOCA_N(type,n) \
113 RBIMPL_CAST((type *)(!(n) ? NULL : alloca(rbimpl_size_mul_or_raise(sizeof(type), (n)))))
114
115/* allocates _n_ bytes temporary buffer and stores VALUE including it
116 * in _v_. _n_ may be evaluated twice. */
117#define RB_ALLOCV(v, n) \
118 ((n) < RUBY_ALLOCV_LIMIT ? \
119 ((v) = 0, !(n) ? NULL : alloca(n)) : \
120 rb_alloc_tmp_buffer(&(v), (n)))
121#define RB_ALLOCV_N(type, v, n) \
122 RBIMPL_CAST((type *) \
123 (((size_t)(n) < RUBY_ALLOCV_LIMIT / sizeof(type)) ? \
124 ((v) = 0, !(n) ? NULL : alloca((n) * sizeof(type))) : \
125 rb_alloc_tmp_buffer2(&(v), (n), sizeof(type))))
126#define RB_ALLOCV_END(v) rb_free_tmp_buffer(&(v))
127
128#define MEMZERO(p,type,n) memset((p), 0, rbimpl_size_mul_or_raise(sizeof(type), (n)))
129#define MEMCPY(p1,p2,type,n) memcpy((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
130#define MEMMOVE(p1,p2,type,n) memmove((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
131#define MEMCMP(p1,p2,type,n) memcmp((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
132
133#define ALLOC_N RB_ALLOC_N
134#define ALLOC RB_ALLOC
135#define ZALLOC_N RB_ZALLOC_N
136#define ZALLOC RB_ZALLOC
137#define REALLOC_N RB_REALLOC_N
138#define ALLOCV RB_ALLOCV
139#define ALLOCV_N RB_ALLOCV_N
140#define ALLOCV_END RB_ALLOCV_END
141
142/* Expecting this struct to be eliminated by function inlinings */
144 bool left;
145 size_t right;
146};
147
152void *rb_alloc_tmp_buffer(volatile VALUE *store, long len);
153
157void *rb_alloc_tmp_buffer_with_count(volatile VALUE *store, size_t len,size_t count);
158
159void rb_free_tmp_buffer(volatile VALUE *store);
160
162void ruby_malloc_size_overflow(size_t, size_t);
163
164#ifdef HAVE_RB_GC_GUARDED_PTR_VAL
165volatile VALUE *rb_gc_guarded_ptr_val(volatile VALUE *ptr, VALUE val);
166#endif
168
169#ifdef _MSC_VER
170# pragma optimize("", off)
171
172static inline volatile VALUE *
173rb_gc_guarded_ptr(volatile VALUE *ptr)
174{
175 return ptr;
176}
177
178# pragma optimize("", on)
179#endif
180
181/* Does anyone use it? Just here for backwards compatibility. */
182static inline int
183rb_mul_size_overflow(size_t a, size_t b, size_t max, size_t *c)
184{
185#ifdef DSIZE_T
186 RB_GNUC_EXTENSION DSIZE_T da, db, c2;
187 da = a;
188 db = b;
189 c2 = da * db;
190 if (c2 > max) return 1;
191 *c = RBIMPL_CAST((size_t)c2);
192#else
193 if (b != 0 && a > max / b) return 1;
194 *c = a * b;
195#endif
196 return 0;
197}
198
199#if RBIMPL_COMPILER_SINCE(GCC, 7, 0, 0)
200RBIMPL_ATTR_CONSTEXPR(CXX14) /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70507 */
201#elif RBIMPL_COMPILER_SINCE(Clang, 7, 0, 0)
202RBIMPL_ATTR_CONSTEXPR(CXX14) /* https://bugs.llvm.org/show_bug.cgi?id=37633 */
203#endif
205static inline struct rbimpl_size_mul_overflow_tag
206rbimpl_size_mul_overflow(size_t x, size_t y)
207{
208 struct rbimpl_size_mul_overflow_tag ret = { false, 0, };
209
210#if RBIMPL_HAS_BUILTIN(__builtin_mul_overflow)
211 ret.left = __builtin_mul_overflow(x, y, &ret.right);
212
213#elif defined(DSIZE_T)
216 RB_GNUC_EXTENSION DSIZE_T dz = dx * dy;
217 ret.left = dz > SIZE_MAX;
218 ret.right = RBIMPL_CAST((size_t)dz);
219
220#elif defined(_MSC_VER) && defined(_WIN64)
221 unsigned __int64 dp = 0;
222 unsigned __int64 dz = _umul128(x, y, &dp);
223 ret.left = RBIMPL_CAST((bool)dp);
224 ret.right = RBIMPL_CAST((size_t)dz);
225
226#else
227 /* https://wiki.sei.cmu.edu/confluence/display/c/INT30-C.+Ensure+that+unsigned+integer+operations+do+not+wrap */
228 ret.left = (y != 0) && (x > SIZE_MAX / y);
229 ret.right = x * y;
230#endif
231
232 return ret;
233}
234
235static inline size_t
236rbimpl_size_mul_or_raise(size_t x, size_t y)
237{
239 rbimpl_size_mul_overflow(x, y);
240
241 if (RB_LIKELY(! size.left)) {
242 return size.right;
243 }
244 else {
247 }
248}
249
250static inline void *
251rb_alloc_tmp_buffer2(volatile VALUE *store, long count, size_t elsize)
252{
253 const size_t total_size = rbimpl_size_mul_or_raise(count, elsize);
254 const size_t cnt = (total_size + sizeof(VALUE) - 1) / sizeof(VALUE);
255 return rb_alloc_tmp_buffer_with_count(store, total_size, cnt);
256}
257
258#ifndef __MINGW32__
263/* At least since 2004, glibc's <string.h> annotates memcpy to be
264 * __attribute__((__nonnull__(1, 2))). However it is safe to pass NULL to the
265 * source pointer, if n is 0. Let's wrap memcpy. */
266static inline void *
267ruby_nonempty_memcpy(void *dest, const void *src, size_t n)
268{
269 if (n) {
270 return memcpy(dest, src, n);
271 }
272 else {
273 return dest;
274 }
275}
277#undef memcpy
278#define memcpy ruby_nonempty_memcpy
279#endif
280
281#endif /* RBIMPL_MEMORY_H */
Defines RBIMPL_ATTR_ALLOC_SIZE.
#define RBIMPL_ATTR_ALLOC_SIZE(tuple)
Wraps (or simulates) __attribute__((alloc_size))
Definition: alloc_size.h:29
Defines ASSUME / RB_LIKELY / UNREACHABLE.
#define RB_LIKELY(x)
Definition: assume.h:39
Defines old #LONG_LONG.
Defines RBIMPL_CAST.
Defines RBIMPL_ATTR_CONST.
#define RBIMPL_ATTR_CONST()
Wraps (or simulates) __attribute__((const))
Definition: const.h:36
RBIMPL_ATTR_CONSTEXPR.
#define RBIMPL_ATTR_CONSTEXPR(_)
Wraps (or simulates) C++11 constexpr.
Definition: constexpr.h:75
struct RIMemo * ptr
Definition: debug.c:88
#define RB_GNUC_EXTENSION
Definition: defines.h:85
Tewaking visibility of C variables/functions.
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition: dllexport.h:86
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition: dllexport.h:77
int max
Definition: enough.c:225
uint8_t len
Definition: escape.c:17
#define alloca
Definition: ffi_common.h:27
Thin wrapper to ruby/config.h.
Defines RBIMPL_HAS_BUILTIN.
#define RBIMPL_UNREACHABLE_RETURN(_)
Wraps (or simulates) __builtin_unreachable.
Definition: assume.h:51
Defines RBIMPL_ALIGNAS / RBIMPL_ALIGNOF.
voidpf void uLong size
Definition: ioapi.h:138
Historical shim for <limits.h>.
#define SIZE_MAX
Definition: limits.h:71
#define memcpy
Definition: memory.h:278
void * rb_alloc_tmp_buffer(volatile VALUE *store, long len)
Definition: gc.c:11007
void ruby_malloc_size_overflow(size_t, size_t)
Definition: gc.c:10838
void * rb_alloc_tmp_buffer_with_count(volatile VALUE *store, size_t len, size_t count)
Definition: gc.c:10989
#define DSIZE_T
Definition: memory.h:81
void rb_free_tmp_buffer(volatile VALUE *store)
Definition: gc.c:11019
int count
Definition: nkf.c:5055
Defines RBIMPL_ATTR_NOALIAS.
#define RBIMPL_ATTR_NOALIAS()
Wraps (or simulates) __declspec((noalias))
Definition: noalias.h:55
Defines RBIMPL_ATTR_NONNULL.
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Definition: nonnull.h:29
Defines RBIMPL_ATTR_NORETURN.
#define RBIMPL_ATTR_NORETURN()
Wraps (or simulates) [[noreturn]]
Definition: noreturn.h:38
Defines RBIMPL_ATTR_RESTRICT.
#define RBIMPL_ATTR_RESTRICT()
Wraps (or simulates) __declspec(restrict)
Definition: restrict.h:42
Defines RBIMPL_ATTR_RETURNS_NONNULL.
#define RBIMPL_ATTR_RETURNS_NONNULL()
Wraps (or simulates) __attribute__((returns_nonnull))
rb_atomic_t cnt[RUBY_NSIG]
Definition: signal.c:508
C99 shim for <stdbool.h>
#define const
Definition: strftime.c:108
unsigned long VALUE
Definition: value.h:38
#define dp(v)
Definition: vm_debug.h:20
Declares ruby_xmalloc().