mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Introduce BIGNUM_EMBED_P to check BIGNUM_EMBED_FLAG (#2802)
* bignum.h: Add BIGNUM_EMBED_P * bignum.c: Use macros for handling BIGNUM_EMBED_FLAG
This commit is contained in:
parent
4ce28b58cb
commit
e082f41611
Notes:
git
2019-12-31 22:48:53 +09:00
Merged-By: mrkn <mrkn@ruby-lang.org>
3 changed files with 17 additions and 9 deletions
12
bignum.c
12
bignum.c
|
@ -2957,7 +2957,7 @@ rb_cmpint(VALUE val, VALUE a, VALUE b)
|
||||||
}
|
}
|
||||||
|
|
||||||
#define BIGNUM_SET_LEN(b,l) \
|
#define BIGNUM_SET_LEN(b,l) \
|
||||||
((RBASIC(b)->flags & BIGNUM_EMBED_FLAG) ? \
|
(BIGNUM_EMBED_P(b) ? \
|
||||||
(void)(RBASIC(b)->flags = \
|
(void)(RBASIC(b)->flags = \
|
||||||
(RBASIC(b)->flags & ~BIGNUM_EMBED_LEN_MASK) | \
|
(RBASIC(b)->flags & ~BIGNUM_EMBED_LEN_MASK) | \
|
||||||
((l) << BIGNUM_EMBED_LEN_SHIFT)) : \
|
((l) << BIGNUM_EMBED_LEN_SHIFT)) : \
|
||||||
|
@ -2967,19 +2967,19 @@ static void
|
||||||
rb_big_realloc(VALUE big, size_t len)
|
rb_big_realloc(VALUE big, size_t len)
|
||||||
{
|
{
|
||||||
BDIGIT *ds;
|
BDIGIT *ds;
|
||||||
if (RBASIC(big)->flags & BIGNUM_EMBED_FLAG) {
|
if (BIGNUM_EMBED_P(big)) {
|
||||||
if (BIGNUM_EMBED_LEN_MAX < len) {
|
if (BIGNUM_EMBED_LEN_MAX < len) {
|
||||||
ds = ALLOC_N(BDIGIT, len);
|
ds = ALLOC_N(BDIGIT, len);
|
||||||
MEMCPY(ds, RBIGNUM(big)->as.ary, BDIGIT, BIGNUM_EMBED_LEN_MAX);
|
MEMCPY(ds, RBIGNUM(big)->as.ary, BDIGIT, BIGNUM_EMBED_LEN_MAX);
|
||||||
RBIGNUM(big)->as.heap.len = BIGNUM_LEN(big);
|
RBIGNUM(big)->as.heap.len = BIGNUM_LEN(big);
|
||||||
RBIGNUM(big)->as.heap.digits = ds;
|
RBIGNUM(big)->as.heap.digits = ds;
|
||||||
RBASIC(big)->flags &= ~BIGNUM_EMBED_FLAG;
|
FL_UNSET_RAW(big, BIGNUM_EMBED_FLAG);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (len <= BIGNUM_EMBED_LEN_MAX) {
|
if (len <= BIGNUM_EMBED_LEN_MAX) {
|
||||||
ds = RBIGNUM(big)->as.heap.digits;
|
ds = RBIGNUM(big)->as.heap.digits;
|
||||||
RBASIC(big)->flags |= BIGNUM_EMBED_FLAG;
|
FL_SET_RAW(big, BIGNUM_EMBED_FLAG);
|
||||||
BIGNUM_SET_LEN(big, len);
|
BIGNUM_SET_LEN(big, len);
|
||||||
(void)VALGRIND_MAKE_MEM_UNDEFINED((void*)RBIGNUM(big)->as.ary, sizeof(RBIGNUM(big)->as.ary));
|
(void)VALGRIND_MAKE_MEM_UNDEFINED((void*)RBIGNUM(big)->as.ary, sizeof(RBIGNUM(big)->as.ary));
|
||||||
if (ds) {
|
if (ds) {
|
||||||
|
@ -3011,8 +3011,8 @@ bignew_1(VALUE klass, size_t len, int sign)
|
||||||
NEWOBJ_OF(big, struct RBignum, klass, T_BIGNUM | (RGENGC_WB_PROTECTED_BIGNUM ? FL_WB_PROTECTED : 0));
|
NEWOBJ_OF(big, struct RBignum, klass, T_BIGNUM | (RGENGC_WB_PROTECTED_BIGNUM ? FL_WB_PROTECTED : 0));
|
||||||
BIGNUM_SET_SIGN((VALUE)big, sign);
|
BIGNUM_SET_SIGN((VALUE)big, sign);
|
||||||
if (len <= BIGNUM_EMBED_LEN_MAX) {
|
if (len <= BIGNUM_EMBED_LEN_MAX) {
|
||||||
RBASIC(big)->flags |= BIGNUM_EMBED_FLAG;
|
FL_SET_RAW(big, BIGNUM_EMBED_FLAG);
|
||||||
BIGNUM_SET_LEN(big, len);
|
BIGNUM_SET_LEN((VALUE)big, len);
|
||||||
(void)VALGRIND_MAKE_MEM_UNDEFINED((void*)RBIGNUM(big)->as.ary, sizeof(RBIGNUM(big)->as.ary));
|
(void)VALGRIND_MAKE_MEM_UNDEFINED((void*)RBIGNUM(big)->as.ary, sizeof(RBIGNUM(big)->as.ary));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
2
gc.c
2
gc.c
|
@ -2815,7 +2815,7 @@ obj_free(rb_objspace_t *objspace, VALUE obj)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case T_BIGNUM:
|
case T_BIGNUM:
|
||||||
if (!(RBASIC(obj)->flags & BIGNUM_EMBED_FLAG) && BIGNUM_DIGITS(obj)) {
|
if (!BIGNUM_EMBED_P(obj) && BIGNUM_DIGITS(obj)) {
|
||||||
xfree(BIGNUM_DIGITS(obj));
|
xfree(BIGNUM_DIGITS(obj));
|
||||||
RB_DEBUG_COUNTER_INC(obj_bignum_ptr);
|
RB_DEBUG_COUNTER_INC(obj_bignum_ptr);
|
||||||
}
|
}
|
||||||
|
|
|
@ -139,6 +139,7 @@ static inline void BIGNUM_NEGATE(VALUE b);
|
||||||
static inline size_t BIGNUM_LEN(VALUE b);
|
static inline size_t BIGNUM_LEN(VALUE b);
|
||||||
static inline BDIGIT *BIGNUM_DIGITS(VALUE b);
|
static inline BDIGIT *BIGNUM_DIGITS(VALUE b);
|
||||||
static inline int BIGNUM_LENINT(VALUE b);
|
static inline int BIGNUM_LENINT(VALUE b);
|
||||||
|
static inline bool BIGNUM_EMBED_P(VALUE b);
|
||||||
|
|
||||||
RUBY_SYMBOL_EXPORT_BEGIN
|
RUBY_SYMBOL_EXPORT_BEGIN
|
||||||
/* bignum.c (export) */
|
/* bignum.c (export) */
|
||||||
|
@ -207,7 +208,7 @@ BIGNUM_NEGATE(VALUE b)
|
||||||
static inline size_t
|
static inline size_t
|
||||||
BIGNUM_LEN(VALUE b)
|
BIGNUM_LEN(VALUE b)
|
||||||
{
|
{
|
||||||
if (! FL_TEST_RAW(b, BIGNUM_EMBED_FLAG)) {
|
if (! BIGNUM_EMBED_P(b)) {
|
||||||
return RBIGNUM(b)->as.heap.len;
|
return RBIGNUM(b)->as.heap.len;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -228,11 +229,18 @@ BIGNUM_LENINT(VALUE b)
|
||||||
static inline BDIGIT *
|
static inline BDIGIT *
|
||||||
BIGNUM_DIGITS(VALUE b)
|
BIGNUM_DIGITS(VALUE b)
|
||||||
{
|
{
|
||||||
if (FL_TEST_RAW(b, BIGNUM_EMBED_FLAG)) {
|
if (BIGNUM_EMBED_P(b)) {
|
||||||
return RBIGNUM(b)->as.ary;
|
return RBIGNUM(b)->as.ary;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return RBIGNUM(b)->as.heap.digits;
|
return RBIGNUM(b)->as.heap.digits;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline bool
|
||||||
|
BIGNUM_EMBED_P(VALUE b)
|
||||||
|
{
|
||||||
|
return FL_TEST_RAW(b, BIGNUM_EMBED_FLAG);
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* INTERNAL_BIGNUM_H */
|
#endif /* INTERNAL_BIGNUM_H */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue