This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Re: Patch for Review: _Jv_TempUTFString and JV_TEMP_UTF_STRING (Updated)
- From: Mohan Embar <gnustuff at thisiscool dot com>
- To: tromey at redhat dot com, gnustuff at thisiscool dot com
- Cc: GCJ Patches <java-patches at gcc dot gnu dot org>, Bryce McKinlay <bryce at mckinlay dot net dot nz>, Andrew Haley <aph at redhat dot com>
- Date: Wed, 13 Aug 2003 03:31:49 -0500
- Subject: Re: Patch for Review: _Jv_TempUTFString and JV_TEMP_UTF_STRING (Updated)
- Reply-to: gnustuff at thisiscool dot com
Hi All,
Waaaaah! Now I forgot the ChangeLog!:
http://gcc.gnu.org/ml/java-patches/2003-q3/msg00374.html
I <i>promise</i> that after I hit the [Send]
button, that I'll make myself a bowl of cereal and
watch some TV.
To recap, this supersedes these two botched attempts:
- http://gcc.gnu.org/ml/java-patches/2003-q3/msg00373.html
- http://gcc.gnu.org/ml/java-patches/2003-q3/msg00374.html
-- Mohan
http://www.thisiscool.com/
http://www.animalsong.org/
ChangeLog
2003-08-13 Mohan Embar <gnustuff@thisiscool.com>
* include/jvm.h: New class _Jv_TempUTFString (helper class for
getting a temporary C string from a jstring)
New macro JV_TEMP_UTF_STRING, which leverages _Jv_TempUTFString
but uses a stack buffer if the string length is less than 256
bytes.
Index: include/jvm.h
===================================================================
RCS file: /cvsroot/gcc/gcc/libjava/include/jvm.h,v
retrieving revision 1.56
diff -u -2 -r1.56 jvm.h
--- include/jvm.h 21 Jul 2003 01:54:05 -0000 1.56
+++ include/jvm.h 13 Aug 2003 08:18:33 -0000
@@ -150,4 +150,76 @@
extern jboolean _Jv_equaln (_Jv_Utf8Const *, jstring, jint);
+/* Helper class which converts a jstring to a temporary char*.
+ Uses the supplied buffer, if non-null. Otherwise, allocates
+ the buffer on the heap. Use the JV_TEMP_UTF_STRING macro,
+ which follows, to automatically allocate a stack buffer if
+ the string is small enough. */
+class _Jv_TempUTFString
+{
+public:
+ _Jv_TempUTFString(jstring jstr, char* buf=0);
+ ~_Jv_TempUTFString();
+
+// Accessors
+ operator const char*() const
+ {
+ return buf_;
+ }
+ const char* buf() const
+ {
+ return buf_;
+ }
+ char* buf()
+ {
+ return buf_;
+ }
+
+private:
+ char* buf_;
+ bool heapAllocated_;
+};
+
+inline _Jv_TempUTFString::_Jv_TempUTFString (jstring jstr, char* buf)
+ : buf_(0), heapAllocated_(false)
+{
+ if (!jstr) return;
+ if (buf)
+ buf_ = buf;
+ else
+ {
+ jsize len = JvGetStringUTFLength (jstr);
+ buf_ = (char*) _Jv_Malloc (len+1);
+ heapAllocated_ = true;
+ }
+
+ JvGetStringUTFChars (jstr, buf_);
+}
+
+inline _Jv_TempUTFString::~_Jv_TempUTFString ()
+{
+ if (heapAllocated_)
+ _Jv_Free (buf_);
+}
+
+/* Macro which uses _Jv_TempUTFString. Allocates a stack-based
+ buffer if the string and its null terminator are <= 256
+ characters in length. Otherwise, a heap-based buffer is
+ used. The parameters to this macro are the variable name
+ which is an instance of _Jv_TempUTFString (above) and a
+ jstring.
+
+ Sample Usage:
+
+ jstring jstr = getAJString();
+ JV_TEMP_UTF_STRING(utfstr, jstr);
+ printf("The string is: %s\n", utfstr.buf());
+
+ */
+#define JV_TEMP_UTF_STRING(utfstr, jstr) \
+ jstring utfstr##thejstr = (jstr); \
+ jsize utfstr##_len = JvGetStringUTFLength (utfstr##thejstr) + 1; \
+ char utfstr##_buf[utfstr##_len <= 256 ? utfstr##_len : 0]; \
+ _Jv_TempUTFString utfstr(utfstr##thejstr, sizeof(utfstr##_buf)==0 ? 0 : utfstr##_buf)
+
// FIXME: remove this define.
#define StringClass java::lang::String::class$