Skip to content

Commit 61bab18

Browse files
committed
Rename to struct rbimpl_size_overflow_tag
This struct is used for addition not only for multiplication, so remove the word `mul`, and make the member names more descriptive.
1 parent 79f36c5 commit 61bab18

File tree

3 files changed

+60
-59
lines changed

3 files changed

+60
-59
lines changed

gc.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -484,31 +484,31 @@ rb_malloc_grow_capa(size_t current, size_t type_size)
484484
return new_capacity;
485485
}
486486

487-
static inline struct rbimpl_size_mul_overflow_tag
487+
static inline struct rbimpl_size_overflow_tag
488488
size_mul_add_overflow(size_t x, size_t y, size_t z) /* x * y + z */
489489
{
490-
struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
491-
struct rbimpl_size_mul_overflow_tag u = rbimpl_size_add_overflow(t.right, z);
492-
return (struct rbimpl_size_mul_overflow_tag) { t.left || u.left, u.right };
490+
struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(x, y);
491+
struct rbimpl_size_overflow_tag u = rbimpl_size_add_overflow(t.result, z);
492+
return (struct rbimpl_size_overflow_tag) { t.overflowed || u.overflowed, u.result };
493493
}
494494

495-
static inline struct rbimpl_size_mul_overflow_tag
495+
static inline struct rbimpl_size_overflow_tag
496496
size_mul_add_mul_overflow(size_t x, size_t y, size_t z, size_t w) /* x * y + z * w */
497497
{
498-
struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
499-
struct rbimpl_size_mul_overflow_tag u = rbimpl_size_mul_overflow(z, w);
500-
struct rbimpl_size_mul_overflow_tag v = rbimpl_size_add_overflow(t.right, u.right);
501-
return (struct rbimpl_size_mul_overflow_tag) { t.left || u.left || v.left, v.right };
498+
struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(x, y);
499+
struct rbimpl_size_overflow_tag u = rbimpl_size_mul_overflow(z, w);
500+
struct rbimpl_size_overflow_tag v = rbimpl_size_add_overflow(t.result, u.result);
501+
return (struct rbimpl_size_overflow_tag) { t.overflowed || u.overflowed || v.overflowed, v.result };
502502
}
503503

504504
PRINTF_ARGS(NORETURN(static void gc_raise(VALUE, const char*, ...)), 2, 3);
505505

506506
static inline size_t
507507
size_mul_or_raise(size_t x, size_t y, VALUE exc)
508508
{
509-
struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(x, y);
510-
if (LIKELY(!t.left)) {
511-
return t.right;
509+
struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(x, y);
510+
if (LIKELY(!t.overflowed)) {
511+
return t.result;
512512
}
513513
else if (rb_during_gc()) {
514514
rb_memerror(); /* or...? */
@@ -532,9 +532,9 @@ rb_size_mul_or_raise(size_t x, size_t y, VALUE exc)
532532
static inline size_t
533533
size_mul_add_or_raise(size_t x, size_t y, size_t z, VALUE exc)
534534
{
535-
struct rbimpl_size_mul_overflow_tag t = size_mul_add_overflow(x, y, z);
536-
if (LIKELY(!t.left)) {
537-
return t.right;
535+
struct rbimpl_size_overflow_tag t = size_mul_add_overflow(x, y, z);
536+
if (LIKELY(!t.overflowed)) {
537+
return t.result;
538538
}
539539
else if (rb_during_gc()) {
540540
rb_memerror(); /* or...? */
@@ -559,9 +559,9 @@ rb_size_mul_add_or_raise(size_t x, size_t y, size_t z, VALUE exc)
559559
static inline size_t
560560
size_mul_add_mul_or_raise(size_t x, size_t y, size_t z, size_t w, VALUE exc)
561561
{
562-
struct rbimpl_size_mul_overflow_tag t = size_mul_add_mul_overflow(x, y, z, w);
563-
if (LIKELY(!t.left)) {
564-
return t.right;
562+
struct rbimpl_size_overflow_tag t = size_mul_add_mul_overflow(x, y, z, w);
563+
if (LIKELY(!t.overflowed)) {
564+
return t.result;
565565
}
566566
else if (rb_during_gc()) {
567567
rb_memerror(); /* or...? */
@@ -5372,11 +5372,11 @@ ruby_mimcalloc(size_t num, size_t size)
53725372
{
53735373
void *mem;
53745374
#if CALC_EXACT_MALLOC_SIZE
5375-
struct rbimpl_size_mul_overflow_tag t = rbimpl_size_mul_overflow(num, size);
5376-
if (UNLIKELY(t.left)) {
5375+
struct rbimpl_size_overflow_tag t = rbimpl_size_mul_overflow(num, size);
5376+
if (UNLIKELY(t.overflowed)) {
53775377
return NULL;
53785378
}
5379-
size = t.right + sizeof(struct malloc_obj_info);
5379+
size = t.result + sizeof(struct malloc_obj_info);
53805380
mem = calloc1(size);
53815381
if (!mem) {
53825382
return NULL;

include/ruby/internal/memory.h

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -408,17 +408,18 @@ typedef uint128_t DSIZE_T;
408408
/**
409409
* @private
410410
*
411-
* This is an implementation detail of rbimpl_size_mul_overflow().
411+
* This is an implementation detail of rbimpl_size_mul_overflow() and
412+
* rbimpl_size_add_overflow().
412413
*
413414
* @internal
414415
*
415416
* Expecting this struct to be eliminated by function inlinings. This is
416417
* nothing more than std::variant<std::size_t> if we could use recent C++, but
417418
* reality is we cannot.
418419
*/
419-
struct rbimpl_size_mul_overflow_tag {
420-
bool left; /**< Whether overflow happened or not. */
421-
size_t right; /**< Multiplication result. */
420+
struct rbimpl_size_overflow_tag {
421+
bool overflowed; /**< Whether overflow happened or not. */
422+
size_t result; /**< Calculation result. */
422423
};
423424

424425
RBIMPL_SYMBOL_EXPORT_BEGIN()
@@ -572,46 +573,46 @@ RBIMPL_ATTR_CONST()
572573
*
573574
* @param[in] x Arbitrary value.
574575
* @param[in] y Arbitrary value.
575-
* @return `{ left, right }`, where `left` is whether there is an integer
576-
* overflow or not, and `right` is a (possibly overflowed) result
577-
* of `x` * `y`.
576+
* @return `{ overflowed, result }`, where `overflowed` is whether there is
577+
* an integer overflow or not, and `result` is a (possibly
578+
* overflowed) result of `x` * `y`.
578579
*
579580
* @internal
580581
*
581582
* This is in fact also an implementation detail of ruby_xmalloc2() etc.
582583
*/
583-
static inline struct rbimpl_size_mul_overflow_tag
584+
static inline struct rbimpl_size_overflow_tag
584585
rbimpl_size_mul_overflow(size_t x, size_t y)
585586
{
586-
struct rbimpl_size_mul_overflow_tag ret = { false, 0, };
587+
struct rbimpl_size_overflow_tag ret = { false, 0, };
587588

588589
#if defined(ckd_mul)
589-
ret.left = ckd_mul(&ret.right, x, y);
590+
ret.overflowed = ckd_mul(&ret.result, x, y);
590591

591592
#elif RBIMPL_HAS_BUILTIN(__builtin_mul_overflow)
592-
ret.left = __builtin_mul_overflow(x, y, &ret.right);
593+
ret.overflowed = __builtin_mul_overflow(x, y, &ret.result);
593594

594595
#elif defined(DSIZE_T)
595596
RB_GNUC_EXTENSION DSIZE_T dx = x;
596597
RB_GNUC_EXTENSION DSIZE_T dy = y;
597598
RB_GNUC_EXTENSION DSIZE_T dz = dx * dy;
598-
ret.left = dz > SIZE_MAX;
599-
ret.right = RBIMPL_CAST((size_t)dz);
599+
ret.overflowed = dz > SIZE_MAX;
600+
ret.result = RBIMPL_CAST((size_t)dz);
600601

601602
#elif defined(_MSC_VER) && defined(_M_AMD64)
602603
unsigned __int64 dp = 0;
603604
unsigned __int64 dz = _umul128(x, y, &dp);
604-
ret.left = RBIMPL_CAST((bool)dp);
605-
ret.right = RBIMPL_CAST((size_t)dz);
605+
ret.overflowed = RBIMPL_CAST((bool)dp);
606+
ret.result = RBIMPL_CAST((size_t)dz);
606607

607608
#elif defined(_MSC_VER) && defined(_M_ARM64)
608-
ret.left = __umulh(x, y) != 0;
609-
ret.right = x * y;
609+
ret.overflowed = __umulh(x, y) != 0;
610+
ret.result = x * y;
610611

611612
#else
612613
/* https://wiki.sei.cmu.edu/confluence/display/c/INT30-C.+Ensure+that+unsigned+integer+operations+do+not+wrap */
613-
ret.left = (y != 0) && (x > SIZE_MAX / y);
614-
ret.right = x * y;
614+
ret.overflowed = (y != 0) && (x > SIZE_MAX / y);
615+
ret.result = x * y;
615616
#endif
616617

617618
return ret;
@@ -635,11 +636,11 @@ rbimpl_size_mul_overflow(size_t x, size_t y)
635636
static inline size_t
636637
rbimpl_size_mul_or_raise(size_t x, size_t y)
637638
{
638-
struct rbimpl_size_mul_overflow_tag size =
639+
struct rbimpl_size_overflow_tag size =
639640
rbimpl_size_mul_overflow(x, y);
640641

641-
if (RB_LIKELY(! size.left)) {
642-
return size.right;
642+
if (RB_LIKELY(! size.overflowed)) {
643+
return size.result;
643644
}
644645
else {
645646
ruby_malloc_size_overflow(x, y);
@@ -662,33 +663,33 @@ RBIMPL_ATTR_CONST()
662663
*
663664
* @param[in] x Arbitrary value.
664665
* @param[in] y Arbitrary value.
665-
* @return `{ left, right }`, where `left` is whether there is an integer
666-
* overflow or not, and `right` is a (possibly overflowed) result
667-
* of `x` + `y`.
666+
* @return `{ overflowed, result }`, where `overflowed` is whether there is
667+
* an integer overflow or not, and `result` is a (possibly
668+
* overflowed) result of `x` + `y`.
668669
*
669670
* @internal
670671
*/
671-
static inline struct rbimpl_size_mul_overflow_tag
672+
static inline struct rbimpl_size_overflow_tag
672673
rbimpl_size_add_overflow(size_t x, size_t y)
673674
{
674-
struct rbimpl_size_mul_overflow_tag ret = { false, 0, };
675+
struct rbimpl_size_overflow_tag ret = { false, 0, };
675676

676677
#if defined(ckd_add)
677-
ret.left = ckd_add(&ret.right, x, y);
678+
ret.overflowed = ckd_add(&ret.result, x, y);
678679

679680
#elif RBIMPL_HAS_BUILTIN(__builtin_add_overflow)
680-
ret.left = __builtin_add_overflow(x, y, &ret.right);
681+
ret.overflowed = __builtin_add_overflow(x, y, &ret.result);
681682

682683
#elif defined(DSIZE_T)
683684
RB_GNUC_EXTENSION DSIZE_T dx = x;
684685
RB_GNUC_EXTENSION DSIZE_T dy = y;
685686
RB_GNUC_EXTENSION DSIZE_T dz = dx + dy;
686-
ret.left = dz > SIZE_MAX;
687-
ret.right = (size_t)dz;
687+
ret.overflowed = dz > SIZE_MAX;
688+
ret.result = (size_t)dz;
688689

689690
#else
690-
ret.right = x + y;
691-
ret.left = ret.right < y;
691+
ret.result = x + y;
692+
ret.overflowed = ret.result < y;
692693

693694
#endif
694695

@@ -710,11 +711,11 @@ rbimpl_size_add_overflow(size_t x, size_t y)
710711
static inline size_t
711712
rbimpl_size_add_or_raise(size_t x, size_t y)
712713
{
713-
struct rbimpl_size_mul_overflow_tag size =
714+
struct rbimpl_size_overflow_tag size =
714715
rbimpl_size_add_overflow(x, y);
715716

716-
if (RB_LIKELY(!size.left)) {
717-
return size.right;
717+
if (RB_LIKELY(!size.overflowed)) {
718+
return size.result;
718719
}
719720
else {
720721
ruby_malloc_add_size_overflow(x, y);

io_buffer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,8 +1527,8 @@ VALUE rb_io_buffer_free_locked(VALUE self)
15271527
static bool
15281528
size_sum_is_bigger_than(size_t a, size_t b, size_t x)
15291529
{
1530-
struct rbimpl_size_mul_overflow_tag size = rbimpl_size_add_overflow(a, b);
1531-
return size.left || size.right > x;
1530+
struct rbimpl_size_overflow_tag size = rbimpl_size_add_overflow(a, b);
1531+
return size.overflowed || size.result > x;
15321532
}
15331533

15341534
// Validate that access to the buffer is within bounds, assuming you want to

0 commit comments

Comments
 (0)