mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Refactoring integer ranges.
- Remove `mrb_ssize`
- Fix `MRB_FIXNUM_{MIN,MAX}` to 32 bits on `MRB_NAN_BOXING`
This commit is contained in:
@@ -17,7 +17,7 @@ MRB_BEGIN_DECL
|
||||
|
||||
typedef struct mrb_shared_array {
|
||||
int refcnt;
|
||||
mrb_ssize len;
|
||||
mrb_int len;
|
||||
mrb_value *ptr;
|
||||
} mrb_shared_array;
|
||||
|
||||
@@ -33,9 +33,9 @@ struct RArray {
|
||||
MRB_OBJECT_HEADER;
|
||||
union {
|
||||
struct {
|
||||
mrb_ssize len;
|
||||
mrb_int len;
|
||||
union {
|
||||
mrb_ssize capa;
|
||||
mrb_int capa;
|
||||
mrb_shared_array *shared;
|
||||
} aux;
|
||||
mrb_value *ptr;
|
||||
|
||||
@@ -21,6 +21,8 @@
|
||||
|
||||
#define MRB_FIXNUM_SHIFT 0
|
||||
#define MRB_SYMBOL_SHIFT 0
|
||||
#define MRB_FIXNUM_MIN INT32_MIN
|
||||
#define MRB_FIXNUM_MAX INT32_MAX
|
||||
|
||||
/* value representation by nan-boxing:
|
||||
* float : FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
|
||||
#define MRB_FIXNUM_SHIFT 0
|
||||
#define MRB_SYMBOL_SHIFT 0
|
||||
#define MRB_FIXNUM_MIN MRB_INT_MIN
|
||||
#define MRB_FIXNUM_MAX MRB_INT_MAX
|
||||
|
||||
union mrb_value_union {
|
||||
#ifndef MRB_NO_FLOAT
|
||||
|
||||
@@ -33,6 +33,14 @@ enum mrb_special_consts {
|
||||
#endif
|
||||
#define MRB_SYMBOL_SHIFT BOXWORD_SYMBOL_SHIFT
|
||||
|
||||
#if defined(MRB_64BIT) && defined(MRB_INT64)
|
||||
# define MRB_FIXNUM_MIN (INT64_MIN>>MRB_FIXNUM_SHIFT)
|
||||
# define MRB_FIXNUM_MAX (INT64_MAX>>MRB_FIXNUM_SHIFT)
|
||||
#else
|
||||
# define MRB_FIXNUM_MIN (INT32_MIN>>MRB_FIXNUM_SHIFT)
|
||||
# define MRB_FIXNUM_MAX (INT32_MAX>>MRB_FIXNUM_SHIFT)
|
||||
#endif
|
||||
|
||||
#define BOXWORD_FIXNUM_BIT_POS 1
|
||||
#define BOXWORD_SYMBOL_BIT_POS 2
|
||||
#define BOXWORD_FIXNUM_SHIFT BOXWORD_FIXNUM_BIT_POS
|
||||
|
||||
@@ -23,9 +23,9 @@ struct RString {
|
||||
MRB_OBJECT_HEADER;
|
||||
union {
|
||||
struct {
|
||||
mrb_ssize len;
|
||||
mrb_int len;
|
||||
union {
|
||||
mrb_ssize capa;
|
||||
mrb_int capa;
|
||||
struct mrb_shared_string *shared;
|
||||
struct RString *fshared;
|
||||
} aux;
|
||||
@@ -54,7 +54,7 @@ struct RStringEmbed {
|
||||
RSTR_SET_EMBED_LEN((s),(n));\
|
||||
}\
|
||||
else {\
|
||||
(s)->as.heap.len = (mrb_ssize)(n);\
|
||||
(s)->as.heap.len = (mrb_int)(n);\
|
||||
}\
|
||||
} while (0)
|
||||
#define RSTR_EMBED_PTR(s) (((struct RStringEmbed*)(s))->ary)
|
||||
|
||||
@@ -69,14 +69,6 @@ struct mrb_state;
|
||||
# define MRB_PRIx PRIx32
|
||||
#endif
|
||||
|
||||
#if defined(MRB_64BIT) && defined(MRB_INT64)
|
||||
# define MRB_FIXNUM_MIN (INT64_MIN>>MRB_FIXNUM_SHIFT)
|
||||
# define MRB_FIXNUM_MAX (INT64_MAX>>MRB_FIXNUM_SHIFT)
|
||||
#else
|
||||
# define MRB_FIXNUM_MIN (INT32_MIN>>MRB_FIXNUM_SHIFT)
|
||||
# define MRB_FIXNUM_MAX (INT32_MAX>>MRB_FIXNUM_SHIFT)
|
||||
#endif
|
||||
|
||||
#ifdef MRB_ENDIAN_BIG
|
||||
# define MRB_ENDIAN_LOHI(a,b) a b
|
||||
#else
|
||||
@@ -175,15 +167,6 @@ struct RCptr {
|
||||
#endif
|
||||
|
||||
#define MRB_SYMBOL_BIT (sizeof(mrb_sym) * CHAR_BIT - MRB_SYMBOL_SHIFT)
|
||||
#define MRB_SYMBOL_MAX (UINT32_MAX >> MRB_SYMBOL_SHIFT)
|
||||
|
||||
#if INTPTR_MAX < MRB_INT_MAX
|
||||
typedef intptr_t mrb_ssize;
|
||||
# define MRB_SSIZE_MAX (INTPTR_MAX>>MRB_FIXNUM_SHIFT)
|
||||
#else
|
||||
typedef mrb_int mrb_ssize;
|
||||
# define MRB_SSIZE_MAX MRB_INT_MAX
|
||||
#endif
|
||||
|
||||
#ifndef mrb_immediate_p
|
||||
#define mrb_immediate_p(o) (mrb_type(o) < MRB_TT_FREE)
|
||||
|
||||
+20
-20
@@ -25,7 +25,7 @@
|
||||
|
||||
typedef struct mrb_shared_string {
|
||||
int refcnt;
|
||||
mrb_ssize capa;
|
||||
mrb_int capa;
|
||||
char *ptr;
|
||||
} mrb_shared_string;
|
||||
|
||||
@@ -41,8 +41,8 @@ str_init_normal_capa(mrb_state *mrb, struct RString *s,
|
||||
if (p) memcpy(dst, p, len);
|
||||
dst[len] = '\0';
|
||||
s->as.heap.ptr = dst;
|
||||
s->as.heap.len = (mrb_ssize)len;
|
||||
s->as.heap.aux.capa = (mrb_ssize)capa;
|
||||
s->as.heap.len = (mrb_int)len;
|
||||
s->as.heap.aux.capa = (mrb_int)capa;
|
||||
RSTR_UNSET_TYPE_FLAG(s);
|
||||
return s;
|
||||
}
|
||||
@@ -67,7 +67,7 @@ static struct RString*
|
||||
str_init_nofree(struct RString *s, const char *p, size_t len)
|
||||
{
|
||||
s->as.heap.ptr = (char *)p;
|
||||
s->as.heap.len = (mrb_ssize)len;
|
||||
s->as.heap.len = (mrb_int)len;
|
||||
s->as.heap.aux.capa = 0; /* nofree */
|
||||
RSTR_SET_TYPE_FLAG(s, NOFREE);
|
||||
return s;
|
||||
@@ -119,7 +119,7 @@ str_new_static(mrb_state *mrb, const char *p, size_t len)
|
||||
if (RSTR_EMBEDDABLE_P(len)) {
|
||||
return str_init_embed(mrb_obj_alloc_string(mrb), p, len);
|
||||
}
|
||||
if (len >= MRB_SSIZE_MAX) {
|
||||
if (len >= (size_t)MRB_INT_MAX) {
|
||||
mrb_raise(mrb, E_ARGUMENT_ERROR, "string size too big");
|
||||
}
|
||||
return str_init_nofree(mrb_obj_alloc_string(mrb), p, len);
|
||||
@@ -131,7 +131,7 @@ str_new(mrb_state *mrb, const char *p, size_t len)
|
||||
if (RSTR_EMBEDDABLE_P(len)) {
|
||||
return str_init_embed(mrb_obj_alloc_string(mrb), p, len);
|
||||
}
|
||||
if (len >= MRB_SSIZE_MAX) {
|
||||
if (len >= (size_t)MRB_INT_MAX) {
|
||||
mrb_raise(mrb, E_ARGUMENT_ERROR, "string size too big");
|
||||
}
|
||||
if (p && mrb_ro_data_p(p)) {
|
||||
@@ -163,7 +163,7 @@ mrb_str_new_capa(mrb_state *mrb, size_t capa)
|
||||
if (RSTR_EMBEDDABLE_P(capa)) {
|
||||
s = str_init_embed(mrb_obj_alloc_string(mrb), NULL, 0);
|
||||
}
|
||||
else if (capa >= MRB_SSIZE_MAX) {
|
||||
else if (capa >= (size_t)MRB_INT_MAX) {
|
||||
mrb_raise(mrb, E_ARGUMENT_ERROR, "string capacity size too big");
|
||||
/* not reached */
|
||||
s = NULL;
|
||||
@@ -191,8 +191,8 @@ mrb_str_buf_new(mrb_state *mrb, size_t capa)
|
||||
static void
|
||||
resize_capa(mrb_state *mrb, struct RString *s, size_t capacity)
|
||||
{
|
||||
#if SIZE_MAX > MRB_SSIZE_MAX
|
||||
mrb_assert(capacity < MRB_SSIZE_MAX);
|
||||
#if SIZE_MAX > MRB_INT_MAX
|
||||
mrb_assert(capacity <= MRB_INT_MAX);
|
||||
#endif
|
||||
if (RSTR_EMBED_P(s)) {
|
||||
if (!RSTR_EMBEDDABLE_P(capacity)) {
|
||||
@@ -201,7 +201,7 @@ resize_capa(mrb_state *mrb, struct RString *s, size_t capacity)
|
||||
}
|
||||
else {
|
||||
s->as.heap.ptr = (char*)mrb_realloc(mrb, RSTR_PTR(s), capacity+1);
|
||||
s->as.heap.aux.capa = (mrb_ssize)capacity;
|
||||
s->as.heap.aux.capa = (mrb_int)capacity;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -586,7 +586,7 @@ str_share(mrb_state *mrb, struct RString *orig, struct RString *s)
|
||||
else {
|
||||
if (orig->as.heap.aux.capa > orig->as.heap.len) {
|
||||
orig->as.heap.ptr = (char *)mrb_realloc(mrb, orig->as.heap.ptr, len+1);
|
||||
orig->as.heap.aux.capa = (mrb_ssize)len;
|
||||
orig->as.heap.aux.capa = (mrb_int)len;
|
||||
}
|
||||
str_init_shared(mrb, orig, s, NULL);
|
||||
str_init_shared(mrb, orig, orig, s->as.heap.aux.shared);
|
||||
@@ -605,8 +605,8 @@ mrb_str_byte_subseq(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len)
|
||||
}
|
||||
else {
|
||||
str_share(mrb, orig, s);
|
||||
s->as.heap.ptr += (mrb_ssize)beg;
|
||||
s->as.heap.len = (mrb_ssize)len;
|
||||
s->as.heap.ptr += beg;
|
||||
s->as.heap.len = len;
|
||||
}
|
||||
RSTR_COPY_ASCII_FLAG(s, orig);
|
||||
return mrb_obj_value(s);
|
||||
@@ -953,7 +953,7 @@ mrb_str_times(mrb_state *mrb, mrb_value self)
|
||||
if (times < 0) {
|
||||
mrb_raise(mrb, E_ARGUMENT_ERROR, "negative argument");
|
||||
}
|
||||
if (times && MRB_SSIZE_MAX / times < RSTRING_LEN(self)) {
|
||||
if (times && MRB_INT_MAX / times < RSTRING_LEN(self)) {
|
||||
mrb_raise(mrb, E_ARGUMENT_ERROR, "argument too big");
|
||||
}
|
||||
|
||||
@@ -1270,9 +1270,9 @@ str_replace_partial(mrb_state *mrb, mrb_value src, mrb_int pos, mrb_int end, mrb
|
||||
}
|
||||
|
||||
replen = (mrb_nil_p(rep) ? 0 : RSTRING_LEN(rep));
|
||||
newlen = replen + len - (end - pos);
|
||||
newlen = replen + (len - (end - pos));
|
||||
|
||||
if (newlen >= MRB_SSIZE_MAX || newlen < replen /* overflowed */) {
|
||||
if (newlen < replen) { /* overflowed */
|
||||
mrb_raise(mrb, E_RUNTIME_ERROR, "string size too big");
|
||||
}
|
||||
|
||||
@@ -2689,21 +2689,21 @@ mrb_str_cat(mrb_state *mrb, mrb_value str, const char *ptr, size_t len)
|
||||
|
||||
capa = RSTR_CAPA(s);
|
||||
total = RSTR_LEN(s)+len;
|
||||
if (total >= MRB_SSIZE_MAX) {
|
||||
if (total > (size_t)MRB_INT_MAX) {
|
||||
size_error:
|
||||
mrb_raise(mrb, E_ARGUMENT_ERROR, "string size too big");
|
||||
}
|
||||
if (capa <= total) {
|
||||
if (capa == 0) capa = 1;
|
||||
while (capa <= total) {
|
||||
if (capa <= MRB_SSIZE_MAX / 2) {
|
||||
if (capa <= (size_t)MRB_INT_MAX / 2) {
|
||||
capa *= 2;
|
||||
}
|
||||
else {
|
||||
capa = total+1;
|
||||
}
|
||||
}
|
||||
if (capa <= total || capa > MRB_SSIZE_MAX) {
|
||||
if (capa <= total || capa > (size_t)MRB_INT_MAX) {
|
||||
goto size_error;
|
||||
}
|
||||
resize_capa(mrb, s, capa);
|
||||
@@ -2712,7 +2712,7 @@ mrb_str_cat(mrb_state *mrb, mrb_value str, const char *ptr, size_t len)
|
||||
ptr = RSTR_PTR(s) + off;
|
||||
}
|
||||
memcpy(RSTR_PTR(s) + RSTR_LEN(s), ptr, len);
|
||||
mrb_assert_int_fit(size_t, total, mrb_ssize, MRB_SSIZE_MAX);
|
||||
mrb_assert_int_fit(size_t, total, mrb_int, MRB_INT_MAX);
|
||||
RSTR_SET_LEN(s, total);
|
||||
RSTR_PTR(s)[total] = '\0'; /* sentinel */
|
||||
return str;
|
||||
|
||||
+1
-1
@@ -94,7 +94,7 @@ static const char pack_table[] = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS
|
||||
static mrb_sym
|
||||
sym_inline_pack(const char *name, size_t len)
|
||||
{
|
||||
const size_t pack_length_max = (MRB_SYMBOL_BIT - 2) / 6;
|
||||
const size_t pack_length_max = (MRB_SYMBOL_BIT - 2) / 6;
|
||||
|
||||
char c;
|
||||
const char *p;
|
||||
|
||||
Reference in New Issue
Block a user