Ruby: include/ruby/internal/memory.h Source File

Ruby 3.3.6p108 (2024-11-05 revision 75015d4c1f6965b5e85e96fb309f1f2129f933c0)

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# pragma intrinsic(_umul128)
44#endif
45
46#include "ruby/internal/attr/alloc_size.h"
47#include "ruby/internal/attr/const.h"
48#include "ruby/internal/attr/constexpr.h"
49#include "ruby/internal/attr/noalias.h"
50#include "ruby/internal/attr/nonnull.h"
51#include "ruby/internal/attr/noreturn.h"
52#include "ruby/internal/attr/restrict.h"
53#include "ruby/internal/attr/returns_nonnull.h"
54#include "ruby/internal/cast.h"
55#include "ruby/internal/dllexport.h"
56#include "ruby/internal/has/builtin.h"
57#include "ruby/internal/stdalign.h"
58#include "ruby/internal/stdbool.h"
59#include "ruby/internal/xmalloc.h"
60#include "ruby/backward/2/limits.h"
61#include "ruby/backward/2/long_long.h"
62#include "ruby/backward/2/assume.h"
63#include "ruby/defines.h"
64
67/* Make alloca work the best possible way. */
68#if defined(alloca)
69# /* Take that. */
70#elif RBIMPL_HAS_BUILTIN(__builtin_alloca)
71# define alloca __builtin_alloca
72#elif defined(_AIX)
73# pragma alloca
74#elif defined(__cplusplus)
75extern "C" void *alloca(size_t);
76#else
77extern void *alloca();
78#endif
79
82#if defined(__DOXYGEN__)
90typedef uint128_t DSIZE_T;
91#elif defined(HAVE_INT128_T) && SIZEOF_SIZE_T <= 8
92# define DSIZE_T uint128_t
93#elif SIZEOF_SIZE_T * 2 <= SIZEOF_LONG_LONG
94# define DSIZE_T unsigned LONG_LONG
95#endif
96
104#ifdef C_ALLOCA
105# define RUBY_ALLOCV_LIMIT 0
106#else
107# define RUBY_ALLOCV_LIMIT 1024
108#endif
109
160#ifdef __GNUC__
161#define RB_GC_GUARD(v) \
162 (*__extension__ ({ \
163 volatile VALUE *rb_gc_guarded_ptr = &(v); \
164 __asm__("" : : "m"(rb_gc_guarded_ptr)); \
165 rb_gc_guarded_ptr; \
166 }))
167#elif defined _MSC_VER
168#define RB_GC_GUARD(v) (*rb_gc_guarded_ptr(&(v)))
169#else
170#define HAVE_RB_GC_GUARDED_PTR_VAL 1
171#define RB_GC_GUARD(v) (*rb_gc_guarded_ptr_val(&(v),(v)))
172#endif
173
174/* Casts needed because void* is NOT compatible with others in C++. */
175
193#define RB_ALLOC_N(type,n) RBIMPL_CAST((type *)ruby_xmalloc2((n), sizeof(type)))
194
207#define RB_ALLOC(type) RBIMPL_CAST((type *)ruby_xmalloc(sizeof(type)))
208
228#define RB_ZALLOC_N(type,n) RBIMPL_CAST((type *)ruby_xcalloc((n), sizeof(type)))
229
243#define RB_ZALLOC(type) (RB_ZALLOC_N(type, 1))
244
276#define RB_REALLOC_N(var,type,n) \
277 ((var) = RBIMPL_CAST((type *)ruby_xrealloc2((void *)(var), (n), sizeof(type))))
278
286#define ALLOCA_N(type,n) \
287 RBIMPL_CAST((type *)alloca(rbimpl_size_mul_or_raise(sizeof(type), (n))))
288
298#define RB_ALLOCV(v, n) \
299 ((n) < RUBY_ALLOCV_LIMIT ? \
300 ((v) = 0, alloca(n)) : \
301 rb_alloc_tmp_buffer(&(v), (n)))
302
330#define RB_ALLOCV_N(type, v, n) \
331 RBIMPL_CAST((type *) \
332 (((size_t)(n) < RUBY_ALLOCV_LIMIT / sizeof(type)) ? \
333 ((v) = 0, alloca((n) * sizeof(type))) : \
334 rb_alloc_tmp_buffer2(&(v), (n), sizeof(type))))
335
343#define RB_ALLOCV_END(v) rb_free_tmp_buffer(&(v))
344
354#define MEMZERO(p,type,n) memset((p), 0, rbimpl_size_mul_or_raise(sizeof(type), (n)))
355
366#define MEMCPY(p1,p2,type,n) ruby_nonempty_memcpy((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
367
378#define MEMMOVE(p1,p2,type,n) memmove((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
379
391#define MEMCMP(p1,p2,type,n) memcmp((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
392
393#define ALLOC_N RB_ALLOC_N
394#define ALLOC RB_ALLOC
395#define ZALLOC_N RB_ZALLOC_N
396#define ZALLOC RB_ZALLOC
397#define REALLOC_N RB_REALLOC_N
398#define ALLOCV RB_ALLOCV
399#define ALLOCV_N RB_ALLOCV_N
400#define ALLOCV_END RB_ALLOCV_END
413struct rbimpl_size_mul_overflow_tag {
414 bool left;
415 size_t right;
416};
417
418 RBIMPL_SYMBOL_EXPORT_BEGIN()
419 RBIMPL_ATTR_RESTRICT()
420 RBIMPL_ATTR_RETURNS_NONNULL()
421 RBIMPL_ATTR_ALLOC_SIZE((2))
422 RBIMPL_ATTR_NONNULL(())
434void *rb_alloc_tmp_buffer(volatile VALUE *store, long len);
435
436 RBIMPL_ATTR_RESTRICT()
437 RBIMPL_ATTR_RETURNS_NONNULL()
438 RBIMPL_ATTR_ALLOC_SIZE((2,3))
439 RBIMPL_ATTR_NONNULL(())
457void *rb_alloc_tmp_buffer_with_count(volatile VALUE *store, size_t len,size_t count);
458
470void rb_free_tmp_buffer(volatile VALUE *store);
471
472 RBIMPL_ATTR_NORETURN()
483void ruby_malloc_size_overflow(size_t x, size_t y);
484
485#ifdef HAVE_RB_GC_GUARDED_PTR_VAL
486volatile VALUE *rb_gc_guarded_ptr_val(volatile VALUE *ptr, VALUE val);
487#endif
488 RBIMPL_SYMBOL_EXPORT_END()
489
490#ifdef _MSC_VER
491# pragma optimize("", off)
492
502static inline volatile VALUE *
503rb_gc_guarded_ptr(volatile VALUE *ptr)
504{
505 return ptr;
506}
507
508# pragma optimize("", on)
509#endif
510
524static inline int
525rb_mul_size_overflow(size_t a, size_t b, size_t max, size_t *c)
526{
527#ifdef DSIZE_T
528 RB_GNUC_EXTENSION DSIZE_T da, db, c2;
529 da = a;
530 db = b;
531 c2 = da * db;
532 if (c2 > max) return 1;
533 *c = RBIMPL_CAST((size_t)c2);
534#else
535 if (b != 0 && a > max / b) return 1;
536 *c = a * b;
537#endif
538 return 0;
539}
540
541#if defined(__DOXYGEN__)
542 RBIMPL_ATTR_CONSTEXPR(CXX14)
543#elif RBIMPL_COMPILER_SINCE(GCC, 7, 0, 0)
544 RBIMPL_ATTR_CONSTEXPR(CXX14) /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70507 */
545#elif RBIMPL_COMPILER_SINCE(Clang, 7, 0, 0)
546 RBIMPL_ATTR_CONSTEXPR(CXX14) /* https://bugs.llvm.org/show_bug.cgi?id=37633 */
547#endif
548 RBIMPL_ATTR_CONST()
565static inline struct rbimpl_size_mul_overflow_tag
566rbimpl_size_mul_overflow(size_t x, size_t y)
567{
568 struct rbimpl_size_mul_overflow_tag ret = { false, 0, };
569
570#if RBIMPL_HAS_BUILTIN(__builtin_mul_overflow)
571 ret.left = __builtin_mul_overflow(x, y, &ret.right);
572
573#elif defined(DSIZE_T)
574 RB_GNUC_EXTENSION DSIZE_T dx = x;
575 RB_GNUC_EXTENSION DSIZE_T dy = y;
576 RB_GNUC_EXTENSION DSIZE_T dz = dx * dy;
577 ret.left = dz > SIZE_MAX;
578 ret.right = RBIMPL_CAST((size_t)dz);
579
580#elif defined(_MSC_VER) && defined(_WIN64)
581 unsigned __int64 dp = 0;
582 unsigned __int64 dz = _umul128(x, y, &dp);
583 ret.left = RBIMPL_CAST((bool)dp);
584 ret.right = RBIMPL_CAST((size_t)dz);
585
586#else
587 /* https://wiki.sei.cmu.edu/confluence/display/c/INT30-C.+Ensure+that+unsigned+integer+operations+do+not+wrap */
588 ret.left = (y != 0) && (x > SIZE_MAX / y);
589 ret.right = x * y;
590#endif
591
592 return ret;
593}
594
610static inline size_t
611rbimpl_size_mul_or_raise(size_t x, size_t y)
612{
613 struct rbimpl_size_mul_overflow_tag size =
614 rbimpl_size_mul_overflow(x, y);
615
616 if (RB_LIKELY(! size.left)) {
617 return size.right;
618 }
619 else {
620 ruby_malloc_size_overflow(x, y);
621 RBIMPL_UNREACHABLE_RETURN(0);
622 }
623}
624
639static inline void *
640rb_alloc_tmp_buffer2(volatile VALUE *store, long count, size_t elsize)
641{
642 const size_t total_size = rbimpl_size_mul_or_raise(count, elsize);
643 const size_t cnt = (total_size + sizeof(VALUE) - 1) / sizeof(VALUE);
644 return rb_alloc_tmp_buffer_with_count(store, total_size, cnt);
645}
646
647 RBIMPL_SYMBOL_EXPORT_BEGIN()
648 RBIMPL_ATTR_NOALIAS()
649 RBIMPL_ATTR_NONNULL((1))
650 RBIMPL_ATTR_RETURNS_NONNULL()
651/* At least since 2004, glibc's <string.h> annotates memcpy to be
652 * __attribute__((__nonnull__(1, 2))). However it is safe to pass NULL to the
653 * source pointer, if n is 0. Let's wrap memcpy. */
654static inline void *
655ruby_nonempty_memcpy(void *dest, const void *src, size_t n)
656{
657 if (n) {
658 return memcpy(dest, src, n);
659 }
660 else {
661 return dest;
662 }
663}
664 RBIMPL_SYMBOL_EXPORT_END()
665
666#endif /* RBIMPL_MEMORY_H */
alloc_size.h
Defines RBIMPL_ATTR_ALLOC_SIZE.
RBIMPL_ATTR_ALLOC_SIZE
#define RBIMPL_ATTR_ALLOC_SIZE(tuple)
Wraps (or simulates) __attribute__((alloc_size))
Definition alloc_size.h:29
assume.h
Defines ASSUME / RB_LIKELY / UNREACHABLE.
long_long.h
Defines old LONG_LONG.
const.h
Defines RBIMPL_ATTR_CONST.
RBIMPL_ATTR_CONST
#define RBIMPL_ATTR_CONST()
Wraps (or simulates) __attribute__((const))
Definition const.h:36
constexpr.h
RBIMPL_ATTR_CONSTEXPR.
RBIMPL_ATTR_CONSTEXPR
#define RBIMPL_ATTR_CONSTEXPR(_)
Wraps (or simulates) C++11 constexpr.
Definition constexpr.h:74
dllexport.h
Tweaking visibility of C variables/functions.
RBIMPL_SYMBOL_EXPORT_END
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition dllexport.h:74
RBIMPL_SYMBOL_EXPORT_BEGIN
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition dllexport.h:65
defines.h
RB_GNUC_EXTENSION
#define RB_GNUC_EXTENSION
This is expanded to nothing for non-GCC compilers.
Definition defines.h:89
builtin.h
Defines RBIMPL_HAS_BUILTIN.
len
int len
Length of the buffer.
Definition io.h:8
RBIMPL_UNREACHABLE_RETURN
#define RBIMPL_UNREACHABLE_RETURN(_)
Wraps (or simulates) __builtin_unreachable.
Definition assume.h:48
stdalign.h
Defines RBIMPL_ALIGNAS / RBIMPL_ALIGNOF.
rb_alloc_tmp_buffer2
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:640
rb_mul_size_overflow
static int rb_mul_size_overflow(size_t a, size_t b, size_t max, size_t *c)
Definition memory.h:525
noalias.h
Defines RBIMPL_ATTR_NOALIAS.
RBIMPL_ATTR_NOALIAS
#define RBIMPL_ATTR_NOALIAS()
Wraps (or simulates) __declspec((noalias))
Definition noalias.h:66
nonnull.h
Defines RBIMPL_ATTR_NONNULL.
RBIMPL_ATTR_NONNULL
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Definition nonnull.h:30
noreturn.h
Defines RBIMPL_ATTR_NORETURN.
RBIMPL_ATTR_NORETURN
#define RBIMPL_ATTR_NORETURN()
Wraps (or simulates) [[noreturn]]
Definition noreturn.h:38
inline
#define inline
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition defines.h:66
restrict.h
Defines RBIMPL_ATTR_RESTRICT.
RBIMPL_ATTR_RESTRICT
#define RBIMPL_ATTR_RESTRICT()
Wraps (or simulates) __declspec(restrict)
Definition restrict.h:41
returns_nonnull.h
Defines RBIMPL_ATTR_RETURNS_NONNULL.
RBIMPL_ATTR_RETURNS_NONNULL
#define RBIMPL_ATTR_RETURNS_NONNULL()
Wraps (or simulates) __attribute__((returns_nonnull))
Definition returns_nonnull.h:34
stdbool.h
C99 shim for <stdbool.h>
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
xmalloc.h
Declares ruby_xmalloc().