[前][次][番号順一覧][スレッド一覧]

ruby-changes:35557

From: nobu <ko1@a...>
Date: Fri, 19 Sep 2014 14:53:07 +0900 (JST)
Subject: [ruby-changes:35557] nobu:r47639 (trunk): string.c: rb_str_new_static

nobu	2014-09-19 14:53:00 +0900 (Fri, 19 Sep 2014)

  New Revision: 47639

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=47639

  Log:
    string.c: rb_str_new_static
    
    * string.c (rb_str_new_static): create string object with static
      buffer.  incorporated from mruby.
    
    * string.c (rb_{usascii,utf8,enc}_str_new_static): ditto with
      encodings.

  Modified files:
    trunk/include/ruby/encoding.h
    trunk/include/ruby/intern.h
    trunk/string.c
Index: include/ruby/intern.h
===================================================================
--- include/ruby/intern.h	(revision 47638)
+++ include/ruby/intern.h	(revision 47639)
@@ -710,6 +710,9 @@ VALUE rb_usascii_str_new(const char*, lo https://github.com/ruby/ruby/blob/trunk/include/ruby/intern.h#L710
 VALUE rb_usascii_str_new_cstr(const char*);
 VALUE rb_utf8_str_new(const char*, long);
 VALUE rb_utf8_str_new_cstr(const char*);
+VALUE rb_str_new_static(const char *, long);
+VALUE rb_usascii_str_new_static(const char *, long);
+VALUE rb_utf8_str_new_static(const char *, long);
 void rb_str_free(VALUE);
 void rb_str_shared_replace(VALUE, VALUE);
 VALUE rb_str_buf_append(VALUE, VALUE);
@@ -771,13 +774,32 @@ long rb_str_offset(VALUE, long); https://github.com/ruby/ruby/blob/trunk/include/ruby/intern.h#L774
 size_t rb_str_capacity(VALUE);
 VALUE rb_str_ellipsize(VALUE, long);
 VALUE rb_str_scrub(VALUE, VALUE);
+
 #if defined(__GNUC__) && !defined(__PCC__)
+#define rb_str_new(str, len) __extension__ (	\
+{						\
+    (__builtin_constant_p(str) && __builtin_constant_p(len)) ? \
+	rb_str_new_static((str), (len)) : \
+	rb_str_new((str), (len));	  \
+})
 #define rb_str_new_cstr(str) __extension__ (	\
 {						\
     (__builtin_constant_p(str)) ?		\
-	rb_str_new((str), (long)strlen(str)) :	\
+	rb_str_new_static((str), (long)strlen(str)) : \
 	rb_str_new_cstr(str);			\
 })
+#define rb_usascii_str_new(str, len) __extension__ ( \
+{						\
+    (__builtin_constant_p(str) && __builtin_constant_p(len)) ? \
+	rb_usascii_str_new_static((str), (len)) : \
+	rb_usascii_str_new((str), (len));	  \
+})
+#define rb_utf8_str_new(str, len) __extension__ ( \
+{						\
+    (__builtin_constant_p(str) && __builtin_constant_p(len)) ? \
+	rb_utf8_str_new_static((str), (len)) : \
+	rb_utf8_str_new((str), (len));	  \
+})
 #define rb_tainted_str_new_cstr(str) __extension__ ( \
 {					       \
     (__builtin_constant_p(str)) ?	       \
@@ -787,13 +809,13 @@ VALUE rb_str_scrub(VALUE, VALUE); https://github.com/ruby/ruby/blob/trunk/include/ruby/intern.h#L809
 #define rb_usascii_str_new_cstr(str) __extension__ ( \
 {					       \
     (__builtin_constant_p(str)) ?	       \
-	rb_usascii_str_new((str), (long)strlen(str)) : \
+	rb_usascii_str_new_static((str), (long)strlen(str)) : \
 	rb_usascii_str_new_cstr(str);	       \
 })
 #define rb_utf8_str_new_cstr(str) __extension__ ( \
 {						\
     (__builtin_constant_p(str)) ?		\
-	rb_utf8_str_new((str), (long)strlen(str)) : \
+	rb_utf8_str_new_static((str), (long)strlen(str)) : \
 	rb_utf8_str_new_cstr(str);		\
 })
 #define rb_external_str_new_cstr(str) __extension__ ( \
Index: include/ruby/encoding.h
===================================================================
--- include/ruby/encoding.h	(revision 47638)
+++ include/ruby/encoding.h	(revision 47639)
@@ -91,6 +91,7 @@ void rb_enc_copy(VALUE dst, VALUE src); https://github.com/ruby/ruby/blob/trunk/include/ruby/encoding.h#L91
 
 VALUE rb_enc_str_new(const char*, long, rb_encoding*);
 VALUE rb_enc_str_new_cstr(const char*, rb_encoding*);
+VALUE rb_enc_str_new_static(const char*, long, rb_encoding*);
 VALUE rb_enc_reg_new(const char*, long, rb_encoding*, int);
 PRINTF_ARGS(VALUE rb_enc_sprintf(rb_encoding *, const char*, ...), 2, 3);
 VALUE rb_enc_vsprintf(rb_encoding *, const char*, va_list);
@@ -106,10 +107,16 @@ VALUE rb_str_conv_enc(VALUE str, rb_enco https://github.com/ruby/ruby/blob/trunk/include/ruby/encoding.h#L107
 VALUE rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts);
 
 #if defined(__GNUC__) && !defined(__PCC__)
+#define rb_enc_str_new(str, len, enc) __extension__ ( \
+{					       \
+    (__builtin_constant_p(str) && __builtin_constant_p(len)) ? \
+	rb_enc_str_new_static((str), (len), (enc)) : \
+	rb_enc_str_new((str), (len), (enc)); \
+})
 #define rb_enc_str_new_cstr(str, enc) __extension__ (	\
 {					       \
     (__builtin_constant_p(str)) ?	       \
-	rb_enc_str_new((str), (long)strlen(str), (enc)) : \
+	rb_enc_str_new_static((str), (long)strlen(str), (enc)) : \
 	rb_enc_str_new_cstr((str), (enc)); \
 })
 #endif
Index: string.c
===================================================================
--- string.c	(revision 47638)
+++ string.c	(revision 47639)
@@ -31,6 +31,10 @@ https://github.com/ruby/ruby/blob/trunk/string.c#L31
 
 #define STRING_ENUMERATORS_WANTARRAY 0 /* next major */
 
+#undef rb_str_new
+#undef rb_usascii_str_new
+#undef rb_utf8_str_new
+#undef rb_enc_str_new
 #undef rb_str_new_cstr
 #undef rb_tainted_str_new_cstr
 #undef rb_usascii_str_new_cstr
@@ -52,6 +56,7 @@ VALUE rb_cSymbol; https://github.com/ruby/ruby/blob/trunk/string.c#L56
 
 #define RUBY_MAX_CHAR_LEN 16
 #define STR_TMPLOCK FL_USER7
+#define STR_NOFREE FL_USER18
 
 #define STR_SET_NOEMBED(str) do {\
     FL_SET((str), STR_NOEMBED);\
@@ -507,7 +512,7 @@ rb_str_capacity(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L512
     if (STR_EMBED_P(str)) {
 	return RSTRING_EMBED_LEN_MAX;
     }
-    else if (FL_TEST(str, STR_SHARED)) {
+    else if (FL_TEST(str, STR_SHARED|STR_NOFREE)) {
 	return RSTRING(str)->as.heap.len;
     }
     else {
@@ -642,6 +647,57 @@ rb_enc_str_new_cstr(const char *ptr, rb_ https://github.com/ruby/ruby/blob/trunk/string.c#L647
     return rb_enc_str_new(ptr, strlen(ptr), enc);
 }
 
+static VALUE
+str_new_static(VALUE klass, const char *ptr, long len, int encindex)
+{
+    VALUE str;
+
+    if (len < 0) {
+	rb_raise(rb_eArgError, "negative string size (or size too big)");
+    }
+
+    if (!ptr) {
+	str = str_new(klass, ptr, len);
+    }
+    else {
+	if (RUBY_DTRACE_STRING_CREATE_ENABLED()) {
+	    RUBY_DTRACE_STRING_CREATE(len, rb_sourcefile(), rb_sourceline());
+	}
+	str = str_alloc(klass);
+	RSTRING(str)->as.heap.len = len;
+	RSTRING(str)->as.heap.ptr = (char *)ptr;
+	RSTRING(str)->as.heap.aux.capa = len;
+	STR_SET_NOEMBED(str);
+	RBASIC(str)->flags |= STR_NOFREE;
+    }
+    rb_enc_associate_index(str, encindex);
+    return str;
+}
+
+VALUE
+rb_str_new_static(const char *ptr, long len)
+{
+    return str_new_static(rb_cString, ptr, len, 0);
+}
+
+VALUE
+rb_usascii_str_new_static(const char *ptr, long len)
+{
+    return str_new_static(rb_cString, ptr, len, ENCINDEX_US_ASCII);
+}
+
+VALUE
+rb_utf8_str_new_static(const char *ptr, long len)
+{
+    return str_new_static(rb_cString, ptr, len, ENCINDEX_UTF_8);
+}
+
+VALUE
+rb_enc_str_new_static(const char *ptr, long len, rb_encoding *enc)
+{
+    return str_new_static(rb_cString, ptr, len, rb_enc_to_index(enc));
+}
+
 VALUE
 rb_tainted_str_new(const char *ptr, long len)
 {
@@ -892,6 +948,8 @@ rb_str_new_frozen(VALUE orig) https://github.com/ruby/ruby/blob/trunk/string.c#L948
 	    RSTRING(str)->as.heap.len = RSTRING_LEN(orig);
 	    RSTRING(str)->as.heap.ptr = RSTRING_PTR(orig);
 	    RSTRING(str)->as.heap.aux.capa = RSTRING(orig)->as.heap.aux.capa;
+	    RBASIC(str)->flags |= RBASIC(orig)->flags & STR_NOFREE;
+	    RBASIC(orig)->flags &= ~STR_NOFREE;
 	    STR_SET_SHARED(orig, str);
 	}
     }
@@ -977,7 +1035,7 @@ rb_str_free(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L1035
 	st_delete(rb_vm_fstring_table(), &fstr, NULL);
     }
 
-    if (!STR_EMBED_P(str) && !FL_TEST(str, STR_SHARED)) {
+    if (!STR_EMBED_P(str) && !FL_TEST(str, STR_SHARED|STR_NOFREE)) {
 	ruby_sized_xfree(STR_HEAP_PTR(str), STR_HEAP_SIZE(str));
     }
 }
@@ -985,7 +1043,7 @@ rb_str_free(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L1043
 RUBY_FUNC_EXPORTED size_t
 rb_str_memsize(VALUE str)
 {
-    if (FL_TEST(str, STR_NOEMBED|STR_SHARED) == STR_NOEMBED) {
+    if (FL_TEST(str, STR_NOEMBED|STR_SHARED|STR_NOFREE) == STR_NOEMBED) {
 	return STR_HEAP_SIZE(str);
     }
     else {
@@ -1488,7 +1546,7 @@ static inline int https://github.com/ruby/ruby/blob/trunk/string.c#L1546
 str_independent(VALUE str)
 {
     str_modifiable(str);
-    if (STR_EMBED_P(str) || !FL_TEST(str, STR_SHARED)) {
+    if (STR_EMBED_P(str) || !FL_TEST(str, STR_SHARED|STR_NOFREE)) {
 	return 1;
     }
     else {
@@ -1505,6 +1563,15 @@ str_make_independent_expand(VALUE str, l https://github.com/ruby/ruby/blob/trunk/string.c#L1563
     long capa = len + expand;
 
     if (len > capa) len = capa;
+
+    if (capa <= RSTRING_EMBED_LEN_MAX && !STR_EMBED_P(str)) {
+	ptr = RSTRING(str)->as.heap.ptr;
+	STR_SET_EMBED(str);
+	memcpy(RSTRING(str)->as.ary, ptr, len);
+	STR_SET_EMBED_LEN(str, len);
+	return;
+    }
+
     ptr = ALLOC_N(char, capa + termlen);
     if (RSTRING_PTR(str)) {
 	memcpy(ptr, RSTRING_PTR(str), len);
@@ -3600,7 +3667,7 @@ rb_str_drop_bytes(VALUE str, long len) https://github.com/ruby/ruby/blob/trunk/string.c#L3667
     nlen = olen - len;
     if (nlen <= RSTRING_EMBED_LEN_MAX) {
 	char *oldptr = ptr;
-	int fl = (int)(RBASIC(str)->flags & (STR_NOEMBED|STR_SHARED));
+	int fl = (int)(RBASIC(str)->flags & (STR_NOEMBED|STR_SHARED|STR_NOFREE));
 	STR_SET_EMBED(str);
 	STR_SET_EMBED_LEN(str, nlen);
 	ptr = RSTRING(str)->as.ary;

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]