Patch for Review: _Jv_TempUTFString and JV_TEMP_UTF_STRING

Mohan Embar gnustuff@thisiscool.com
Wed Aug 13 08:23:00 GMT 2003


Hi All,

This patch leverages:

http://gcc.gnu.org/ml/java-patches/2003-q3/msg00371.html

and tries to do this:

http://gcc.gnu.org/ml/java-patches/2003-q3/msg00361.html

Tested 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 here:

http://gcc.gnu.org/ml/java-patches/2003-q3/msg00371.html

It was tricky to test this since I wanted a standalone testcase
which used functionality inside of jvm.h, but I think I nailed it.

I separated this from the cni.h patch because I'm not sure whether
this will be received with shouts of joy or screams of horror.

-- 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 05:32:44 -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 less than
+   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$






More information about the Java-patches mailing list