Ruby  3.4.0dev (2024-12-06 revision 892c46283a5ea4179500d951c9d4866c0051f27b)
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(_M_AMD64)
42 # include <intrin.h>
43 # pragma intrinsic(_umul128)
44 #endif
45 
54 #include "ruby/internal/cast.h"
57 #include "ruby/internal/stdalign.h"
58 #include "ruby/internal/stdbool.h"
59 #include "ruby/internal/stdckdint.h"
60 #include "ruby/internal/xmalloc.h"
61 #include "ruby/backward/2/limits.h"
63 #include "ruby/backward/2/assume.h"
64 #include "ruby/defines.h"
65 
68 /* Make alloca work the best possible way. */
69 #if defined(alloca)
70 # /* Take that. */
71 #elif RBIMPL_HAS_BUILTIN(__builtin_alloca)
72 # define alloca __builtin_alloca
73 #elif defined(_AIX)
74 # pragma alloca
75 #elif defined(__cplusplus)
76 extern "C" void *alloca(size_t);
77 #else
78 extern void *alloca();
79 #endif
80 
83 #if defined(__DOXYGEN__)
91 typedef uint128_t DSIZE_T;
92 #elif defined(HAVE_INT128_T) && SIZEOF_SIZE_T <= 8
93 # define DSIZE_T uint128_t
94 #elif SIZEOF_SIZE_T * 2 <= SIZEOF_LONG_LONG
95 # define DSIZE_T unsigned LONG_LONG
96 #endif
97 
105 #ifdef C_ALLOCA
106 # define RUBY_ALLOCV_LIMIT 0
107 #else
108 # define RUBY_ALLOCV_LIMIT 1024
109 #endif
110 
161 #ifdef __GNUC__
162 #define RB_GC_GUARD(v) \
163  (*__extension__ ({ \
164  volatile VALUE *rb_gc_guarded_ptr = &(v); \
165  __asm__("" : : "m"(rb_gc_guarded_ptr)); \
166  rb_gc_guarded_ptr; \
167  }))
168 #elif defined _MSC_VER
169 #define RB_GC_GUARD(v) (*rb_gc_guarded_ptr(&(v)))
170 #else
171 #define HAVE_RB_GC_GUARDED_PTR_VAL 1
172 #define RB_GC_GUARD(v) (*rb_gc_guarded_ptr_val(&(v),(v)))
173 #endif
174 
175 /* Casts needed because void* is NOT compatible with others in C++. */
176 
194 #define RB_ALLOC_N(type,n) RBIMPL_CAST((type *)ruby_xmalloc2((n), sizeof(type)))
195 
208 #define RB_ALLOC(type) RBIMPL_CAST((type *)ruby_xmalloc(sizeof(type)))
209 
229 #define RB_ZALLOC_N(type,n) RBIMPL_CAST((type *)ruby_xcalloc((n), sizeof(type)))
230 
244 #define RB_ZALLOC(type) (RB_ZALLOC_N(type, 1))
245 
277 #define RB_REALLOC_N(var,type,n) \
278  ((var) = RBIMPL_CAST((type *)ruby_xrealloc2((void *)(var), (n), sizeof(type))))
279 
287 #define ALLOCA_N(type,n) \
288  RBIMPL_CAST((type *)alloca(rbimpl_size_mul_or_raise(sizeof(type), (n))))
289 
299 #define RB_ALLOCV(v, n) \
300  ((n) < RUBY_ALLOCV_LIMIT ? \
301  ((v) = 0, alloca(n)) : \
302  rb_alloc_tmp_buffer(&(v), (n)))
303 
331 #define RB_ALLOCV_N(type, v, n) \
332  RBIMPL_CAST((type *) \
333  (((size_t)(n) < RUBY_ALLOCV_LIMIT / sizeof(type)) ? \
334  ((v) = 0, alloca((n) * sizeof(type))) : \
335  rb_alloc_tmp_buffer2(&(v), (n), sizeof(type))))
336 
344 #define RB_ALLOCV_END(v) rb_free_tmp_buffer(&(v))
345 
355 #define MEMZERO(p,type,n) memset((p), 0, rbimpl_size_mul_or_raise(sizeof(type), (n)))
356 
367 #define MEMCPY(p1,p2,type,n) ruby_nonempty_memcpy((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
368 
379 #define MEMMOVE(p1,p2,type,n) memmove((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
380 
392 #define MEMCMP(p1,p2,type,n) memcmp((p1), (p2), rbimpl_size_mul_or_raise(sizeof(type), (n)))
393 
394 #define ALLOC_N RB_ALLOC_N
395 #define ALLOC RB_ALLOC
396 #define ZALLOC_N RB_ZALLOC_N
397 #define ZALLOC RB_ZALLOC
398 #define REALLOC_N RB_REALLOC_N
399 #define ALLOCV RB_ALLOCV
400 #define ALLOCV_N RB_ALLOCV_N
401 #define ALLOCV_END RB_ALLOCV_END
414 struct rbimpl_size_mul_overflow_tag {
415  bool left;
416  size_t right;
417 };
418 
435 void *rb_alloc_tmp_buffer(volatile VALUE *store, long len);
436 
458 void *rb_alloc_tmp_buffer_with_count(volatile VALUE *store, size_t len,size_t count);
459 
471 void rb_free_tmp_buffer(volatile VALUE *store);
472 
484 void ruby_malloc_size_overflow(size_t x, size_t y);
485 
486 #ifdef HAVE_RB_GC_GUARDED_PTR_VAL
487 volatile VALUE *rb_gc_guarded_ptr_val(volatile VALUE *ptr, VALUE val);
488 #endif
490 
491 #ifdef _MSC_VER
492 # pragma optimize("", off)
493 
503 static inline volatile VALUE *
504 rb_gc_guarded_ptr(volatile VALUE *ptr)
505 {
506  return ptr;
507 }
508 
509 # pragma optimize("", on)
510 #endif
511 
525 static inline int
526 rb_mul_size_overflow(size_t a, size_t b, size_t max, size_t *c)
527 {
528 #ifdef DSIZE_T
529  RB_GNUC_EXTENSION DSIZE_T da, db, c2;
530  da = a;
531  db = b;
532  c2 = da * db;
533  if (c2 > max) return 1;
534  *c = RBIMPL_CAST((size_t)c2);
535 #else
536  if (b != 0 && a > max / b) return 1;
537  *c = a * b;
538 #endif
539  return 0;
540 }
541 
542 #if defined(__DOXYGEN__)
544 #elif RBIMPL_COMPILER_SINCE(GCC, 7, 0, 0)
545 RBIMPL_ATTR_CONSTEXPR(CXX14) /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70507 */
546 #elif RBIMPL_COMPILER_SINCE(Clang, 7, 0, 0)
547 RBIMPL_ATTR_CONSTEXPR(CXX14) /* https://bugs.llvm.org/show_bug.cgi?id=37633 */
548 #endif
566 static inline struct rbimpl_size_mul_overflow_tag
567 rbimpl_size_mul_overflow(size_t x, size_t y)
568 {
569  struct rbimpl_size_mul_overflow_tag ret = { false, 0, };
570 
571 #if defined(ckd_mul)
572  ret.left = ckd_mul(&ret.right, x, y);
573 
574 #elif RBIMPL_HAS_BUILTIN(__builtin_mul_overflow)
575  ret.left = __builtin_mul_overflow(x, y, &ret.right);
576 
577 #elif defined(DSIZE_T)
578  RB_GNUC_EXTENSION DSIZE_T dx = x;
579  RB_GNUC_EXTENSION DSIZE_T dy = y;
580  RB_GNUC_EXTENSION DSIZE_T dz = dx * dy;
581  ret.left = dz > SIZE_MAX;
582  ret.right = RBIMPL_CAST((size_t)dz);
583 
584 #elif defined(_MSC_VER) && defined(_WIN64)
585  unsigned __int64 dp = 0;
586  unsigned __int64 dz = _umul128(x, y, &dp);
587  ret.left = RBIMPL_CAST((bool)dp);
588  ret.right = RBIMPL_CAST((size_t)dz);
589 
590 #else
591  /* https://wiki.sei.cmu.edu/confluence/display/c/INT30-C.+Ensure+that+unsigned+integer+operations+do+not+wrap */
592  ret.left = (y != 0) && (x > SIZE_MAX / y);
593  ret.right = x * y;
594 #endif
595 
596  return ret;
597 }
598 
614 static inline size_t
615 rbimpl_size_mul_or_raise(size_t x, size_t y)
616 {
617  struct rbimpl_size_mul_overflow_tag size =
618  rbimpl_size_mul_overflow(x, y);
619 
620  if (RB_LIKELY(! size.left)) {
621  return size.right;
622  }
623  else {
624  ruby_malloc_size_overflow(x, y);
626  }
627 }
628 
643 static inline void *
644 rb_alloc_tmp_buffer2(volatile VALUE *store, long count, size_t elsize)
645 {
646  const size_t total_size = rbimpl_size_mul_or_raise(RBIMPL_CAST((size_t)count), elsize);
647  const size_t cnt = (total_size + sizeof(VALUE) - 1) / sizeof(VALUE);
648  return rb_alloc_tmp_buffer_with_count(store, total_size, cnt);
649 }
650 
655 /* At least since 2004, glibc's <string.h> annotates memcpy to be
656  * __attribute__((__nonnull__(1, 2))). However it is safe to pass NULL to the
657  * source pointer, if n is 0. Let's wrap memcpy. */
658 static inline void *
659 ruby_nonempty_memcpy(void *dest, const void *src, size_t n)
660 {
661  if (n) {
662  return memcpy(dest, src, n);
663  }
664  else {
665  return dest;
666  }
667 }
669 
670 #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:27
Defines ASSUME / RB_LIKELY / UNREACHABLE.
#define RB_LIKELY(x)
Asserts that the given Boolean expression likely holds.
Definition: assume.h:43
Defines old LONG_LONG.
Defines RBIMPL_ATTR_CONST.
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.
RBIMPL_ATTR_CONST() int rb_io_oflags_fmode(int oflags)
Converts an oflags (that rb_io_modestr_oflags() returns) to a fmode (that rb_io_mode_flags() returns)...
RBIMPL_ATTR_NORETURN() void rb_eof_error(void)
Utility function to raise rb_eEOFError.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
Definition: io.h:2
int len
Length of the buffer.
Definition: io.h:8
#define RBIMPL_UNREACHABLE_RETURN(_)
Wraps (or simulates) __builtin_unreachable.
Definition: assume.h:42
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:644
static int rb_mul_size_overflow(size_t a, size_t b, size_t max, size_t *c)
Definition: memory.h:526
Defines RBIMPL_ATTR_NOALIAS.
#define RBIMPL_ATTR_NOALIAS()
Wraps (or simulates) __declspec((noalias))
Definition: noalias.h:62
Defines RBIMPL_ATTR_NONNULL.
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Definition: nonnull.h:27
Defines RBIMPL_ATTR_NORETURN.
#define inline
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition: defines.h:88
Defines RBIMPL_ATTR_RESTRICT.
#define RBIMPL_ATTR_RESTRICT()
Wraps (or simulates) __declspec(restrict)
Definition: restrict.h:35
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().