Patch for Review: _Jv_TempUTFString + JV_TEMP_UTF_STRING

Mohan Embar gnustuff@thisiscool.com
Thu Aug 14 22:49:00 GMT 2003


Hi People,

>> In conclusion, shall I just cash in my chips, not touch cni.h and
>> simply submit a patch for jvm.h which uses the existing CNI
>> functions?
>
>Sounds good. Meanwhile I'll see if I can come up with a prototype 
>StringConverter implementation, for discussion.

Will the nth time be a charm?

Here is the patch, rewritten to use the existing CNI functions.
To recap this supersedes and nullifies the cni.h + gcj.texi
patches as well as all other jvm.h patches.

Tested with love on (i686-pc-linux-gnu,i686-pc-linux-gnu),
(i686-pc-linux-gnu,i686-pc-mingw32) and (i686-pc-mingw32,i686-pc-mingw32).
The testcase is attached.

-- Mohan
http://www.thisiscool.com/
http://www.animalsong.org/

ChangeLog
2003-08-14  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	14 Aug 2003 21:44:04 -0000
@@ -150,4 +150,77 @@
 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;
+  jsize len = JvGetStringUTFLength (jstr);
+  if (buf)
+    buf_ = buf;
+  else
+    {
+      buf_ = (char*) _Jv_Malloc (len+1);
+      heapAllocated_ = true;
+    }
+
+  JvGetStringUTFRegion (jstr, 0, jstr->length(), buf_);
+  buf_[len] = '\0';
+}
+
+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 = utfstr##thejstr ? JvGetStringUTFLength (utfstr##thejstr) + 1 : 0; \
+  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$

-------------- next part --------------
A non-text attachment was scrubbed...
Name: TempUTFStrTest.tar.bz2
Type: application/bzip2
Size: 3996 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/java-patches/attachments/20030814/4c606bc2/attachment.bin>


More information about the Java-patches mailing list