Ruby 4.0.0dev (2025-12-17 revision c99670d6683fec770271d35c2ae082514b1abce3)
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# endif
46# if defined(_M_ARM64)
47# pragma intrinsic(__umulh)
48# endif
49#endif
50
59#include "ruby/internal/cast.h"
64#include "ruby/internal/stdckdint.h"
66#include "ruby/backward/2/limits.h"
69#include "ruby/defines.h"
70
73/* Make alloca work the best possible way. */
74#if defined(alloca)
75# /* Take that. */
76#elif RBIMPL_HAS_BUILTIN(__builtin_alloca)
77# define alloca __builtin_alloca
78#elif defined(_AIX)
79# pragma alloca
80#elif defined(__cplusplus)
81extern "C" void *alloca(size_t);
82#else
83extern void *alloca();
84#endif
85
88#if defined(__DOXYGEN__)
96typedef uint128_t DSIZE_T;
97#elif defined(HAVE_INT128_T) && SIZEOF_SIZE_T <= 8
98# define DSIZE_T uint128_t
99#elif SIZEOF_SIZE_T * 2 <= SIZEOF_LONG_LONG
100# define DSIZE_T unsigned LONG_LONG
101#endif
102
110#ifdef C_ALLOCA
111# define RUBY_ALLOCV_LIMIT 0
112#else
113# define RUBY_ALLOCV_LIMIT 1024
114#endif
115
166#ifdef __GNUC__
167#define RB_GC_GUARD(v) \
168 (*__extension__ ({ \
169 volatile VALUE *rb_gc_guarded_ptr = &(v); \
170 __asm__("" : : "m"(rb_gc_guarded_ptr)); \
171 rb_gc_guarded_ptr; \
172 }))
173#elif defined _MSC_VER
174#define RB_GC_GUARD(v) (*rb_gc_guarded_ptr(&(v)))
175#else
176#define HAVE_RB_GC_GUARDED_PTR_VAL 1
177#define RB_GC_GUARD(v) (*rb_gc_guarded_ptr_val(&(v),(v)))
178#endif
179
180/* Casts needed because void* is NOT compatible with others in C++. */
181
199#define RB_ALLOC_N(type,n) RBIMPL_CAST((type *)ruby_xmalloc2((n), sizeof(type)))
200
213#define RB_ALLOC(type) RBIMPL_CAST((type *)ruby_xmalloc(sizeof(type)))
214
234#define RB_ZALLOC_N(type,n) RBIMPL_CAST((type *)ruby_xcalloc((n), sizeof(type)))
235
249#define RB_ZALLOC(type) (RB_ZALLOC_N(type, 1))
250
282#define RB_REALLOC_N(var,type,n) \
283 ((var) = RBIMPL_CAST((type *)ruby_xrealloc2((void *)(var), (n), sizeof(type))))
284
292#define ALLOCA_N(type,n) \
293 RBIMPL_CAST((type *)alloca(rbimpl_size_mul_or_raise(sizeof(type), (n))))
294
304#define RB_ALLOCV(v, n) \
305 ((n) < RUBY_ALLOCV_LIMIT ? \
306 ((v) = 0, alloca(n)) : \
307 rb_alloc_tmp_buffer(&(v), (n)))
308
336#define RB_ALLOCV_N(type, v, n) \
337 RBIMPL_CAST((type *) \
338 (((size_t)(n) < RUBY_ALLOCV_LIMIT / sizeof(type)) ? \
339 ((v) = 0, alloca((n) * sizeof(type))) : \
340 rb_alloc_tmp_buffer2(&(v), (n), sizeof(type))))
341
349#define RB_ALLOCV_END(v) rb_free_tmp_buffer(&(v))
350
360#define MEMZERO(p,type,n) memset((p), 0, rbimpl_size_mul_or_raise(sizeof(type), (n)))
361
372#define MEMCPY(p1,p2,type,n) ruby_nonempty_memcpy((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
373
384#define MEMMOVE(p1,p2,type,n) memmove((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
385
397#define MEMCMP(p1,p2,type,n) memcmp((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
398
399#define ALLOC_N RB_ALLOC_N
400#define ALLOC RB_ALLOC
401#define ZALLOC_N RB_ZALLOC_N
402#define ZALLOC RB_ZALLOC
403#define REALLOC_N RB_REALLOC_N
404#define ALLOCV RB_ALLOCV
405#define ALLOCV_N RB_ALLOCV_N
406#define ALLOCV_END RB_ALLOCV_END
420struct rbimpl_size_overflow_tag {
421 bool overflowed;
422 size_t result;
423};
424
441void *rb_alloc_tmp_buffer(volatile VALUE *store, long len);
442
464void *rb_alloc_tmp_buffer_with_count(volatile VALUE *store, size_t len,size_t count);
465
477void rb_free_tmp_buffer(volatile VALUE *store);
478
490void ruby_malloc_size_overflow(size_t x, size_t y);
491
502void ruby_malloc_add_size_overflow(size_t x, size_t y);
503
504#ifdef HAVE_RB_GC_GUARDED_PTR_VAL
505volatile VALUE *rb_gc_guarded_ptr_val(volatile VALUE *ptr, VALUE val);
506#endif
508
509#ifdef _MSC_VER
510# pragma optimize("", off)
511
521static inline volatile VALUE *
522rb_gc_guarded_ptr(volatile VALUE *ptr)
523{
524 return ptr;
525}
526
527# pragma optimize("", on)
528#endif
529
543static inline int
544rb_mul_size_overflow(size_t a, size_t b, size_t max, size_t *c)
545{
546#ifdef DSIZE_T
547 RB_GNUC_EXTENSION DSIZE_T da, db, c2;
548 da = a;
549 db = b;
550 c2 = da * db;
551 if (c2 > max) return 1;
552 *c = RBIMPL_CAST((size_t)c2);
553#else
554 if (b != 0 && a > max / b) return 1;
555 *c = a * b;
556#endif
557 return 0;
558}
559
560#if defined(__DOXYGEN__)
562#elif RBIMPL_COMPILER_SINCE(GCC, 7, 0, 0)
563RBIMPL_ATTR_CONSTEXPR(CXX14) /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70507 */
564#elif RBIMPL_COMPILER_SINCE(Clang, 7, 0, 0)
565RBIMPL_ATTR_CONSTEXPR(CXX14) /* https://bugs.llvm.org/show_bug.cgi?id=37633 */
566#endif
584static inline struct rbimpl_size_overflow_tag
585rbimpl_size_mul_overflow(size_t x, size_t y)
586{
587 struct rbimpl_size_overflow_tag ret = { false, 0, };
588
589#if defined(ckd_mul)
590 ret.overflowed = ckd_mul(&ret.result, x, y);
591
592#elif RBIMPL_HAS_BUILTIN(__builtin_mul_overflow)
593 ret.overflowed = __builtin_mul_overflow(x, y, &ret.result);
594
595#elif defined(DSIZE_T)
596 RB_GNUC_EXTENSION DSIZE_T dx = x;
597 RB_GNUC_EXTENSION DSIZE_T dy = y;
598 RB_GNUC_EXTENSION DSIZE_T dz = dx * dy;
599 ret.overflowed = dz > SIZE_MAX;
600 ret.result = RBIMPL_CAST((size_t)dz);
601
602#elif defined(_MSC_VER) && defined(_M_AMD64)
603 unsigned __int64 dp = 0;
604 unsigned __int64 dz = _umul128(x, y, &dp);
605 ret.overflowed = RBIMPL_CAST((bool)dp);
606 ret.result = RBIMPL_CAST((size_t)dz);
607
608#elif defined(_MSC_VER) && defined(_M_ARM64)
609 ret.overflowed = __umulh(x, y) != 0;
610 ret.result = x * y;
611
612#else
613 /* https://wiki.sei.cmu.edu/confluence/display/c/INT30-C.+Ensure+that+unsigned+integer+operations+do+not+wrap */
614 ret.overflowed = (y != 0) && (x > SIZE_MAX / y);
615 ret.result = x * y;
616#endif
617
618 return ret;
619}
620
636static inline size_t
637rbimpl_size_mul_or_raise(size_t x, size_t y)
638{
639 struct rbimpl_size_overflow_tag size =
640 rbimpl_size_mul_overflow(x, y);
641
642 if (RB_LIKELY(! size.overflowed)) {
643 return size.result;
644 }
645 else {
646 ruby_malloc_size_overflow(x, y);
648 }
649}
650
651#if defined(__DOXYGEN__)
653#elif RBIMPL_COMPILER_SINCE(GCC, 7, 0, 0)
654RBIMPL_ATTR_CONSTEXPR(CXX14) /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70507 */
655#elif RBIMPL_COMPILER_SINCE(Clang, 7, 0, 0)
656RBIMPL_ATTR_CONSTEXPR(CXX14) /* https://bugs.llvm.org/show_bug.cgi?id=37633 */
657#endif
672static inline struct rbimpl_size_overflow_tag
673rbimpl_size_add_overflow(size_t x, size_t y)
674{
675 struct rbimpl_size_overflow_tag ret = { false, 0, };
676
677#if defined(ckd_add)
678 ret.overflowed = ckd_add(&ret.result, x, y);
679
680#elif RBIMPL_HAS_BUILTIN(__builtin_add_overflow)
681 ret.overflowed = __builtin_add_overflow(x, y, &ret.result);
682
683#elif defined(DSIZE_T)
684 RB_GNUC_EXTENSION DSIZE_T dx = x;
685 RB_GNUC_EXTENSION DSIZE_T dy = y;
686 RB_GNUC_EXTENSION DSIZE_T dz = dx + dy;
687 ret.overflowed = dz > SIZE_MAX;
688 ret.result = (size_t)dz;
689
690#else
691 ret.result = x + y;
692 ret.overflowed = ret.result < y;
693
694#endif
695
696 return ret;
697}
698
711static inline size_t
712rbimpl_size_add_or_raise(size_t x, size_t y)
713{
714 struct rbimpl_size_overflow_tag size =
715 rbimpl_size_add_overflow(x, y);
716
717 if (RB_LIKELY(!size.overflowed)) {
718 return size.result;
719 }
720 else {
721 ruby_malloc_add_size_overflow(x, y);
723 }
724}
725
740static inline void *
741rb_alloc_tmp_buffer2(volatile VALUE *store, long count, size_t elsize)
742{
743 const size_t total_size = rbimpl_size_mul_or_raise(RBIMPL_CAST((size_t)count), elsize);
744 const size_t cnt = (total_size + sizeof(VALUE) - 1) / sizeof(VALUE);
745 return rb_alloc_tmp_buffer_with_count(store, total_size, cnt);
746}
747
752/* At least since 2004, glibc's <string.h> annotates memcpy to be
753 * __attribute__((__nonnull__(1, 2))). However it is safe to pass NULL to the
754 * source pointer, if n is 0. Let's wrap memcpy. */
755static inline void *
756ruby_nonempty_memcpy(void *dest, const void *src, size_t n)
757{
758 if (n) {
759 return memcpy(dest, src, n);
760 }
761 else {
762 return dest;
763 }
764}
766
767#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.
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
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
#define RB_GNUC_EXTENSION
This is expanded to nothing for non-GCC compilers.
Definition defines.h:89
Defines RBIMPL_HAS_BUILTIN.
int len
Length of the buffer.
Definition io.h:8
#define RBIMPL_UNREACHABLE_RETURN(_)
Wraps (or simulates) __builtin_unreachable.
Definition assume.h:48
Defines RBIMPL_ALIGNAS / RBIMPL_ALIGNOF.
static void * rb_alloc_tmp_buffer2(volatile VALUE *store, long count, size_t elsize)
This is an implementation detail of RB_ALLOCV_N().
Definition memory.h:741
static int rb_mul_size_overflow(size_t a, size_t b, size_t max, size_t *c)
Definition memory.h:544
Defines RBIMPL_ATTR_NOALIAS.
#define RBIMPL_ATTR_NOALIAS()
Wraps (or simulates) __declspec((noalias))
Definition noalias.h:66
Defines RBIMPL_ATTR_NONNULL.
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Definition nonnull.h:30
Defines RBIMPL_ATTR_NORETURN.
#define RBIMPL_ATTR_NORETURN()
Wraps (or simulates) [[noreturn]]
Definition noreturn.h:38
#define inline
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition defines.h:91
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>
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
Declares ruby_xmalloc().