Ruby 4.1.0dev (2026-11-09 revision 05e951b61434561a845b0888e5c61ac1735498f7)
memory.h
Go to the documentation of this file.
1#ifndef RBIMPL_MEMORY_H /*-*-C++-*-vi:se ft=cpp:*/
2#define RBIMPL_MEMORY_H
23#include "ruby/internal/config.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# if defined(_M_AMD64)
44# pragma intrinsic(_umul128)
45# if !defined(__clang__) /* clang-cl defines it as an inline function */
46# pragma intrinsic(_addcarry_u64)
47# endif
48# endif
49# if defined(_M_ARM64)
50# pragma intrinsic(__umulh)
51# endif
52#endif
53
62#include "ruby/internal/cast.h"
67#include "ruby/internal/stdckdint.h"
69#include "ruby/backward/2/limits.h"
72#include "ruby/defines.h"
73
76/* Make alloca work the best possible way. */
77#if defined(alloca)
78# /* Take that. */
79#elif RBIMPL_HAS_BUILTIN(__builtin_alloca)
80# define alloca __builtin_alloca
81#elif defined(_AIX)
82# pragma alloca
83#elif defined(__cplusplus)
84extern "C" void *alloca(size_t);
85#else
86extern void *alloca();
87#endif
88
91#if defined(__DOXYGEN__)
99typedef uint128_t DSIZE_T;
100#elif defined(HAVE_INT128_T) && SIZEOF_SIZE_T <= 8
101# define DSIZE_T uint128_t
102#elif SIZEOF_SIZE_T * 2 <= SIZEOF_LONG_LONG
103# define DSIZE_T unsigned LONG_LONG
104#endif
105
113#ifdef C_ALLOCA
114# define RUBY_ALLOCV_LIMIT 0
115#else
116# define RUBY_ALLOCV_LIMIT 1024
117#endif
118
169#ifdef __GNUC__
170#define RB_GC_GUARD(v) \
171 (*__extension__ ({ \
172 volatile VALUE *rb_gc_guarded_ptr = &(v); \
173 __asm__("" : : "m"(rb_gc_guarded_ptr)); \
174 rb_gc_guarded_ptr; \
175 }))
176#elif defined _MSC_VER
177#define RB_GC_GUARD(v) (*rb_gc_guarded_ptr(&(v)))
178#else
179#define HAVE_RB_GC_GUARDED_PTR_VAL 1
180#define RB_GC_GUARD(v) (*rb_gc_guarded_ptr_val(&(v),(v)))
181#endif
182
183/* Casts needed because void* is NOT compatible with others in C++. */
184
202#define RB_ALLOC_N(type,n) RBIMPL_CAST((type *)ruby_xmalloc2((n), sizeof(type)))
203
216#define RB_ALLOC(type) RBIMPL_CAST((type *)ruby_xmalloc(sizeof(type)))
217
237#define RB_ZALLOC_N(type,n) RBIMPL_CAST((type *)ruby_xcalloc((n), sizeof(type)))
238
252#define RB_ZALLOC(type) (RB_ZALLOC_N(type, 1))
253
285#define RB_REALLOC_N(var,type,n) \
286 ((var) = RBIMPL_CAST((type *)ruby_xrealloc2((void *)(var), (n), sizeof(type))))
287
295#define ALLOCA_N(type,n) \
296 RBIMPL_CAST((type *)alloca(rbimpl_size_mul_or_raise(sizeof(type), (n))))
297
307#define RB_ALLOCV(v, n) \
308 ((n) < RUBY_ALLOCV_LIMIT ? \
309 ((v) = 0, alloca(n)) : \
310 rb_alloc_tmp_buffer(&(v), (n), true))
311
339#define RB_ALLOCV_N(type, v, n) \
340 RBIMPL_CAST((type *) \
341 (((size_t)(n) < RUBY_ALLOCV_LIMIT / sizeof(type)) ? \
342 ((v) = 0, alloca((n) * sizeof(type))) : \
343 rb_alloc_tmp_buffer2(&(v), (n), sizeof(type))))
344
352#define RB_ALLOCV_END(v) rb_free_tmp_buffer(&(v))
353
363#define MEMZERO(p,type,n) memset((p), 0, rbimpl_size_mul_or_raise(sizeof(type), (n)))
364
375#define MEMCPY(p1,p2,type,n) ruby_nonempty_memcpy((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
376
387#define MEMMOVE(p1,p2,type,n) memmove((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
388
400#define MEMCMP(p1,p2,type,n) memcmp((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
401
402#define ALLOC_N RB_ALLOC_N
403#define ALLOC RB_ALLOC
404#define ZALLOC_N RB_ZALLOC_N
405#define ZALLOC RB_ZALLOC
406#define REALLOC_N RB_REALLOC_N
407#define ALLOCV RB_ALLOCV
408#define ALLOCV_N RB_ALLOCV_N
409#define ALLOCV_END RB_ALLOCV_END
423struct rbimpl_size_overflow_tag {
424 bool overflowed;
425 size_t result;
426};
427
444void *rb_alloc_tmp_buffer(volatile VALUE *store, rb_len_t len, bool mark);
445
457void rb_free_tmp_buffer(volatile VALUE *store);
458
470void ruby_malloc_size_overflow(size_t x, size_t y);
471
482void ruby_malloc_add_size_overflow(size_t x, size_t y);
483
484#ifdef HAVE_RB_GC_GUARDED_PTR_VAL
485volatile VALUE *rb_gc_guarded_ptr_val(volatile VALUE *ptr, VALUE val);
486#endif
488
489#ifdef _MSC_VER
490# pragma optimize("", off)
491
501static inline volatile VALUE *
502rb_gc_guarded_ptr(volatile VALUE *ptr)
503{
504 return ptr;
505}
506
507# pragma optimize("", on)
508#endif
509
510#if defined(__DOXYGEN__)
512#elif RBIMPL_COMPILER_SINCE(GCC, 7, 0, 0)
513RBIMPL_ATTR_CONSTEXPR(CXX14) /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70507 */
514#elif RBIMPL_COMPILER_SINCE(Clang, 7, 0, 0)
515RBIMPL_ATTR_CONSTEXPR(CXX14) /* https://bugs.llvm.org/show_bug.cgi?id=37633 */
516#endif
534static inline struct rbimpl_size_overflow_tag
535rbimpl_size_mul_overflow(size_t x, size_t y)
536{
537 struct rbimpl_size_overflow_tag ret = { false, 0, };
538
539#if defined(ckd_mul)
540 ret.overflowed = ckd_mul(&ret.result, x, y);
541
542#elif RBIMPL_HAS_BUILTIN(__builtin_mul_overflow)
543 ret.overflowed = __builtin_mul_overflow(x, y, &ret.result);
544
545#elif defined(DSIZE_T)
546 RB_GNUC_EXTENSION DSIZE_T dx = x;
547 RB_GNUC_EXTENSION DSIZE_T dy = y;
548 RB_GNUC_EXTENSION DSIZE_T dz = dx * dy;
549 ret.overflowed = dz > SIZE_MAX;
550 ret.result = RBIMPL_CAST((size_t)dz);
551
552#elif defined(_MSC_VER) && defined(_M_AMD64)
553 unsigned __int64 dp = 0;
554 unsigned __int64 dz = _umul128(x, y, &dp);
555 ret.overflowed = RBIMPL_CAST((bool)dp);
556 ret.result = RBIMPL_CAST((size_t)dz);
557
558#elif defined(_MSC_VER) && defined(_M_ARM64)
559 ret.overflowed = __umulh(x, y) != 0;
560 ret.result = x * y;
561
562#else
563 /* https://wiki.sei.cmu.edu/confluence/display/c/INT30-C.+Ensure+that+unsigned+integer+operations+do+not+wrap */
564 ret.overflowed = (y != 0) && (x > SIZE_MAX / y);
565 ret.result = x * y;
566#endif
567
568 return ret;
569}
570
586static inline size_t
587rbimpl_size_mul_or_raise(size_t x, size_t y)
588{
589 struct rbimpl_size_overflow_tag size =
590 rbimpl_size_mul_overflow(x, y);
591
592 if (RB_LIKELY(! size.overflowed)) {
593 return size.result;
594 }
595 else {
596 ruby_malloc_size_overflow(x, y);
598 }
599}
600
601#if defined(__DOXYGEN__)
603#elif RBIMPL_COMPILER_SINCE(GCC, 7, 0, 0)
604RBIMPL_ATTR_CONSTEXPR(CXX14) /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70507 */
605#elif RBIMPL_COMPILER_SINCE(Clang, 7, 0, 0)
606RBIMPL_ATTR_CONSTEXPR(CXX14) /* https://bugs.llvm.org/show_bug.cgi?id=37633 */
607#endif
622static inline struct rbimpl_size_overflow_tag
623rbimpl_size_add_overflow(size_t x, size_t y)
624{
625 struct rbimpl_size_overflow_tag ret = { false, 0, };
626
627#if defined(ckd_add)
628 ret.overflowed = ckd_add(&ret.result, x, y);
629
630#elif RBIMPL_HAS_BUILTIN(__builtin_add_overflow)
631 ret.overflowed = __builtin_add_overflow(x, y, &ret.result);
632
633#elif defined(DSIZE_T)
634 RB_GNUC_EXTENSION DSIZE_T dx = x;
635 RB_GNUC_EXTENSION DSIZE_T dy = y;
636 RB_GNUC_EXTENSION DSIZE_T dz = dx + dy;
637 ret.overflowed = dz > SIZE_MAX;
638 ret.result = (size_t)dz;
639
640#elif defined(_MSC_VER) && defined(_M_AMD64)
641 unsigned __int64 dz = 0;
642 unsigned char carry = _addcarry_u64(0, x, y, &dz);
643 ret.overflowed = RBIMPL_CAST((bool)carry);
644 ret.result = RBIMPL_CAST((size_t)dz);
645
646#else
647 ret.result = x + y;
648 ret.overflowed = ret.result < y;
649
650#endif
651
652 return ret;
653}
654
667static inline size_t
668rbimpl_size_add_or_raise(size_t x, size_t y)
669{
670 struct rbimpl_size_overflow_tag size =
671 rbimpl_size_add_overflow(x, y);
672
673 if (RB_LIKELY(!size.overflowed)) {
674 return size.result;
675 }
676 else {
677 ruby_malloc_add_size_overflow(x, y);
679 }
680}
681
696static inline void *
697rb_alloc_tmp_buffer2(volatile VALUE *store, rb_len_t count, size_t elsize)
698{
699 const size_t total_size = rbimpl_size_mul_or_raise(RBIMPL_CAST((size_t)count), elsize);
700 return rb_alloc_tmp_buffer(store, (rb_len_t)total_size, elsize >= sizeof(VALUE));
701}
702
707/* At least since 2004, glibc's <string.h> annotates memcpy to be
708 * __attribute__((__nonnull__(1, 2))). However it is safe to pass NULL to the
709 * source pointer, if n is 0. Let's wrap memcpy. */
710static inline void *
711ruby_nonempty_memcpy(void *dest, const void *src, size_t n)
712{
713 if (n) {
714 return memcpy(dest, src, n);
715 }
716 else {
717 return dest;
718 }
719}
721
722#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 old LONG_LONG.
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:74
#define RB_GNUC_EXTENSION
This is expanded to nothing for non-GCC compilers.
Definition defines.h:89
Tweaking visibility of C variables/functions.
#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
Defines ASSUME / RB_LIKELY / UNREACHABLE.
#define RBIMPL_UNREACHABLE_RETURN(_)
Wraps (or simulates) __builtin_unreachable.
Definition assume.h:48
Defines RBIMPL_ATTR_NONNULL.
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Definition nonnull.h:30
Defines RBIMPL_HAS_BUILTIN.
int len
Length of the buffer.
Definition io.h:8
Defines RBIMPL_ALIGNAS / RBIMPL_ALIGNOF.
static void * rb_alloc_tmp_buffer2(volatile VALUE *store, rb_len_t count, size_t elsize)
This is an implementation detail of RB_ALLOCV_N().
Definition memory.h:697
Defines RBIMPL_ATTR_NOALIAS.
#define RBIMPL_ATTR_NOALIAS()
Wraps (or simulates) __declspec((noalias))
Definition noalias.h:66
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:41
Defines RBIMPL_ATTR_RETURNS_NONNULL.
#define RBIMPL_ATTR_RETURNS_NONNULL()
Wraps (or simulates) __attribute__((returns_nonnull))
C99 shim for <stdbool.h>
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
Definition value.h:142
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
Declares ruby_xmalloc().