classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] RFC: Some iconv provider (build) fixes


From: Mark Wielaard
Subject: [cp-patches] RFC: Some iconv provider (build) fixes
Date: Sun, 24 Apr 2005 23:49:29 +0200

Hi,

Here are some fixes to make the iconv provider build on more systems
(hopefully), get rid of an obnoxious gcc warning, be a little bit more
efficient and fix for a memory leak. Since I only have access to
GNU/Linux systems I am looking for people wanting to try this out on
some other system like darwin, freebsd or solaris.

2005-04-24  Mark Wielaard  <address@hidden>

        * iconv.m4: New file.
        * configure.ac: Use AM_ICONV.
        * native/jni/java-nio/Makefile.am (libjavanio_la_LIBADD):
        Add LTLIBICONV.
        * native/jni/java-nio/gnu_java_nio_charset_iconv_IconvDecoder.c:
        Include config.h and jcl.h, use if defined(HAVE_ICONV), add new
        static variables infid and outfid, add char_union.
        (Java_gnu_java_nio_charset_iconv_IconvDecoder_openIconv):
        Guard with if defined(HAVE_ICONV), use JCL_jstring_to_cstring,
        JCL_free_cstring and JCL_ThrowException, cache inremaining and
        outremaining fieldIDs.
        (Java_gnu_java_nio_charset_iconv_IconvDecoder_decode):
        Guard with if defined(HAVE_ICONV), use ICONV_CONST for iconv
        argument, used cached fieldIDs, use char_union.
        (Java_gnu_java_nio_charset_iconv_IconvDecoder_closeIconv):
        Guard with if defined(HAVE_ICONV).
        * native/jni/java-nio/gnu_java_nio_charset_iconv_IconvEncoder.c:
        Include config.h and jcl.h, use if defined(HAVE_ICONV), add new
        static variables infid and outfid, add char_union.
        (Java_gnu_java_nio_charset_iconv_IconvEncoder_openIconv):
        Guard with if defined(HAVE_ICONV), use JCL_jstring_to_cstring,
        JCL_free_cstring and JCL_ThrowException, cache inremaining and
        outremaining fieldIDs.
        (Java_gnu_java_nio_charset_iconv_IconvEncoder_encode):
        Guard with if defined(HAVE_ICONV), use ICONV_CONST for iconv
        argument, used cached fieldIDs, use char_union.
        (Java_gnu_java_nio_charset_iconv_IconvEncoder_closeIconv):
        Guard with if defined(HAVE_ICONV).
        * gnu/java/nio/charset/iconv/IconvDecoder.java (dispose): Renamed.
        (finalize): Renamed from dispose.
        * gnu/java/nio/charset/iconv/IconvEncoder.java (dispose): Renamed.
        (finalize): Renamed from dispose.

Please let me know if this (doesn't) build for you.
Note that you have to run ./autogen.sh after applying the patch.
Results with --enable-Werror are also appreciated.

Thanks,

Mark
Index: configure.ac
===================================================================
RCS file: /cvsroot/classpath/classpath/configure.ac,v
retrieving revision 1.82
diff -u -r1.82 configure.ac
--- configure.ac        24 Apr 2005 19:41:50 -0000      1.82
+++ configure.ac        24 Apr 2005 21:33:19 -0000
@@ -217,6 +217,9 @@
 
   AC_C_CONST
 
+  dnl See if we HAVE_ICONV, how ICONV_CONST is set and LTLIBICONV
+  AM_ICONV
+
   dnl When using gcc we want warnings, lots of warnings :-)
   if test "x${GCC}" = xyes; then
     dnl We want ISO C90 pedantic ansi, but with longlong (jlong) support
Index: iconv.m4
===================================================================
RCS file: iconv.m4
diff -N iconv.m4
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ iconv.m4    24 Apr 2005 21:33:19 -0000
@@ -0,0 +1,103 @@
+# iconv.m4 serial AM4 (gettext-0.11.3)
+dnl Copyright (C) 2000-2002 Free Software Foundation, Inc.
+dnl This file is free software, distributed under the terms of the GNU
+dnl General Public License.  As a special exception to the GNU General
+dnl Public License, this file may be distributed as part of a program
+dnl that contains a configuration script generated by Autoconf, under
+dnl the same distribution terms as the rest of that program.
+
+dnl From Bruno Haible.
+
+AC_DEFUN([AM_ICONV_LINKFLAGS_BODY],
+[
+  dnl Prerequisites of AC_LIB_LINKFLAGS_BODY.
+  AC_REQUIRE([AC_LIB_PREPARE_PREFIX])
+  AC_REQUIRE([AC_LIB_RPATH])
+
+  dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
+  dnl accordingly.
+  AC_LIB_LINKFLAGS_BODY([iconv])
+])
+
+AC_DEFUN([AM_ICONV_LINK],
+[
+  dnl Some systems have iconv in libc, some have it in libiconv (OSF/1 and
+  dnl those with the standalone portable GNU libiconv installed).
+
+  dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
+  dnl accordingly.
+  AC_REQUIRE([AM_ICONV_LINKFLAGS_BODY])
+
+  dnl Add $INCICONV to CPPFLAGS before performing the following checks,
+  dnl because if the user has installed libiconv and not disabled its use
+  dnl via --without-libiconv-prefix, he wants to use it. The first
+  dnl AC_TRY_LINK will then fail, the second AC_TRY_LINK will succeed.
+  am_save_CPPFLAGS="$CPPFLAGS"
+  AC_LIB_APPENDTOVAR([CPPFLAGS], [$INCICONV])
+
+  AC_CACHE_CHECK(for iconv, am_cv_func_iconv, [
+    am_cv_func_iconv="no, consider installing GNU libiconv"
+    am_cv_lib_iconv=no
+    AC_TRY_LINK([#include <stdlib.h>
+#include <iconv.h>],
+      [iconv_t cd = iconv_open("","");
+       iconv(cd,NULL,NULL,NULL,NULL);
+       iconv_close(cd);],
+      am_cv_func_iconv=yes)
+    if test "$am_cv_func_iconv" != yes; then
+      am_save_LIBS="$LIBS"
+      LIBS="$LIBS $LIBICONV"
+      AC_TRY_LINK([#include <stdlib.h>
+#include <iconv.h>],
+        [iconv_t cd = iconv_open("","");
+         iconv(cd,NULL,NULL,NULL,NULL);
+         iconv_close(cd);],
+        am_cv_lib_iconv=yes
+        am_cv_func_iconv=yes)
+      LIBS="$am_save_LIBS"
+    fi
+  ])
+  if test "$am_cv_func_iconv" = yes; then
+    AC_DEFINE(HAVE_ICONV, 1, [Define if you have the iconv() function.])
+  fi
+  if test "$am_cv_lib_iconv" = yes; then
+    AC_MSG_CHECKING([how to link with libiconv])
+    AC_MSG_RESULT([$LIBICONV])
+  else
+    dnl If $LIBICONV didn't lead to a usable library, we don't need $INCICONV
+    dnl either.
+    CPPFLAGS="$am_save_CPPFLAGS"
+    LIBICONV=
+    LTLIBICONV=
+  fi
+  AC_SUBST(LIBICONV)
+  AC_SUBST(LTLIBICONV)
+])
+
+AC_DEFUN([AM_ICONV],
+[
+  AM_ICONV_LINK
+  if test "$am_cv_func_iconv" = yes; then
+    AC_MSG_CHECKING([for iconv declaration])
+    AC_CACHE_VAL(am_cv_proto_iconv, [
+      AC_TRY_COMPILE([
+#include <stdlib.h>
+#include <iconv.h>
+extern
+#ifdef __cplusplus
+"C"
+#endif
+#if defined(__STDC__) || defined(__cplusplus)
+size_t iconv (iconv_t cd, char * *inbuf, size_t *inbytesleft, char * *outbuf, 
size_t *outbytesleft);
+#else
+size_t iconv();
+#endif
+], [], am_cv_proto_iconv_arg1="", am_cv_proto_iconv_arg1="const")
+      am_cv_proto_iconv="extern size_t iconv (iconv_t cd, 
$am_cv_proto_iconv_arg1 char * *inbuf, size_t *inbytesleft, char * *outbuf, 
size_t *outbytesleft);"])
+    am_cv_proto_iconv=`echo "[$]am_cv_proto_iconv" | tr -s ' ' | sed -e 's/( 
/(/'`
+    AC_MSG_RESULT([$]{ac_t:-
+         }[$]am_cv_proto_iconv)
+    AC_DEFINE_UNQUOTED(ICONV_CONST, $am_cv_proto_iconv_arg1,
+      [Define as const if the declaration of iconv() needs const.])
+  fi
+])
Index: native/jni/java-nio/Makefile.am
===================================================================
RCS file: /cvsroot/classpath/classpath/native/jni/java-nio/Makefile.am,v
retrieving revision 1.17
diff -u -r1.17 Makefile.am
--- native/jni/java-nio/Makefile.am     18 Apr 2005 11:35:13 -0000      1.17
+++ native/jni/java-nio/Makefile.am     24 Apr 2005 21:33:19 -0000
@@ -8,7 +8,8 @@
                        java_nio_MappedByteBufferImpl.c \
                        java_nio_VMDirectByteBuffer.c
 
-libjavanio_la_LIBADD = $(top_builddir)/native/jni/classpath/jcl.lo
+libjavanio_la_LIBADD = $(top_builddir)/native/jni/classpath/jcl.lo \
+                      $(LTLIBICONV) 
 
 AM_LDFLAGS = @CLASSPATH_MODULE@
 AM_CPPFLAGS = @CLASSPATH_INCLUDES@
Index: native/jni/java-nio/gnu_java_nio_charset_iconv_IconvDecoder.c
===================================================================
RCS file: 
/cvsroot/classpath/classpath/native/jni/java-nio/gnu_java_nio_charset_iconv_IconvDecoder.c,v
retrieving revision 1.1
diff -u -r1.1 gnu_java_nio_charset_iconv_IconvDecoder.c
--- native/jni/java-nio/gnu_java_nio_charset_iconv_IconvDecoder.c       18 Apr 
2005 11:35:13 -0000      1.1
+++ native/jni/java-nio/gnu_java_nio_charset_iconv_IconvDecoder.c       24 Apr 
2005 21:33:19 -0000
@@ -35,24 +35,55 @@
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */
 
+#include <config.h>
+#include <jcl.h>
+
 #include <stdio.h>
 #include <assert.h>
 #include <errno.h>
+
+#if defined(HAVE_ICONV)
 #include <iconv.h>
+#endif
+
 #include "gnu_java_nio_charset_iconv_IconvDecoder.h"
 
 static void createRawData (JNIEnv * env, jobject obj, void *ptr);
 static void *getData (JNIEnv * env, jobject obj);
 
+static jfieldID infid = NULL;
+static jfieldID outfid = NULL;
+
+/* Union used for type punning. */
+union char_union
+{
+  jbyte **jb;
+  jchar **jc;
+  char **c;
+};
+
 JNIEXPORT void JNICALL
 Java_gnu_java_nio_charset_iconv_IconvDecoder_openIconv (JNIEnv * env,
                                                        jobject obj,
                                                        jstring jname)
 {
-  jclass exception;
+#if defined(HAVE_ICONV)
   iconv_t iconv_object;
+  jclass cls;
+
+  const char *name = JCL_jstring_to_cstring (env, jname);
+  if (name == NULL)
+    return;
 
-  const char *name = (*env)->GetStringUTFChars (env, jname, 0);
+  /* Cache fieldIDs for use in decode function. */
+  if (infid == NULL || outfid == NULL)
+    {
+      cls = (*env)->GetObjectClass (env, obj);
+      infid = (*env)->GetFieldID (env, cls, "inremaining", "I");
+      assert (infid != 0);
+      outfid = (*env)->GetFieldID (env, cls, "outremaining", "I");
+      assert (outfid != 0);
+    }
 
   /* to java from "name", native java format depends on endianness */
 #ifdef WORDS_BIGENDIAN
@@ -61,19 +92,18 @@
   iconv_object = iconv_open ("UTF-16LE", name);
 #endif
 
-  (*env)->ReleaseStringUTFChars (env, jname, name);
+  JCL_free_cstring (env, jname, name);
   if ((long) iconv_object == -1L)
     {
-      /* Throw an exception if charset not available */
-      (*env)->ExceptionDescribe (env);
-      (*env)->ExceptionClear (env);
-      exception =
-       (*env)->FindClass (env, "java/lang/IllegalArgumentException");
-      assert (exception != 0);
-      (*env)->ThrowNew (env, exception, "Charset not available.");
+      JCL_ThrowException (env, "java/lang/IllegalArgumentException",
+                         "Charset not available");
       return;
     }
   createRawData (env, obj, (void *) iconv_object);
+#else
+  JCL_ThrowException (env, "java/lang/IllegalArgumentException",
+                     "iconv not available");
+#endif
 }
 
 JNIEXPORT jint JNICALL
@@ -84,11 +114,10 @@
                                                     jint posIn, jint remIn,
                                                     jint posOut, jint remOut)
 {
+#if defined(HAVE_ICONV)
   iconv_t iconv_object = getData (env, obj);
-  jclass cls;
-  jfieldID fid;
   size_t retval;
-  char **in, **out;
+  union char_union in, out;
   jbyte *input, *inputcopy;
   jchar *output, *outputcopy;
   size_t lenIn = (size_t) remIn;
@@ -100,9 +129,10 @@
   input += posIn;
   output += posOut * 2;
 
-  in = (char **) &input;
-  out = (char **) &output;
-  retval = iconv (iconv_object, in, &lenIn, out, &lenOut);
+  in.jb = &input;
+  out.jc = &output;
+  retval = iconv (iconv_object, (ICONV_CONST char **) in.c, &lenIn,
+                 out.c, &lenOut);
 
   /* XXX: Do we need to relase the input array? It's not modified. */
   (*env)->ReleaseByteArrayElements (env, inArr, inputcopy, 0);
@@ -118,24 +148,24 @@
   else
     retval = 0;
 
-  cls = (*env)->GetObjectClass (env, obj);
-  fid = (*env)->GetFieldID (env, cls, "inremaining", "I");
-  assert (fid != 0);
-  (*env)->SetIntField (env, obj, fid, (jint) lenIn);
-  fid = (*env)->GetFieldID (env, cls, "outremaining", "I");
-  assert (fid != 0);
-  (*env)->SetIntField (env, obj, fid, (jint) (lenOut >> 1));
+  (*env)->SetIntField (env, obj, infid, (jint) lenIn);
+  (*env)->SetIntField (env, obj, outfid, (jint) (lenOut >> 1));
 
   return (jint) retval;
+#else
+  return -1;
+#endif
 }
 
 JNIEXPORT void JNICALL
 Java_gnu_java_nio_charset_iconv_IconvDecoder_closeIconv (JNIEnv * env,
                                                         jobject obj)
 {
+#if defined(HAVE_ICONV)
   iconv_t iconv_object;
   iconv_object = getData (env, obj);
   iconv_close (iconv_object);
+#endif
 }
 
 
Index: native/jni/java-nio/gnu_java_nio_charset_iconv_IconvEncoder.c
===================================================================
RCS file: 
/cvsroot/classpath/classpath/native/jni/java-nio/gnu_java_nio_charset_iconv_IconvEncoder.c,v
retrieving revision 1.1
diff -u -r1.1 gnu_java_nio_charset_iconv_IconvEncoder.c
--- native/jni/java-nio/gnu_java_nio_charset_iconv_IconvEncoder.c       18 Apr 
2005 11:35:13 -0000      1.1
+++ native/jni/java-nio/gnu_java_nio_charset_iconv_IconvEncoder.c       24 Apr 
2005 21:33:19 -0000
@@ -35,24 +35,55 @@
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */
 
+#include <config.h>
+#include <jcl.h>
+
 #include <stdio.h>
 #include <assert.h>
 #include <errno.h>
+
+#if defined(HAVE_ICONV)
 #include <iconv.h>
+#endif
+
 #include "gnu_java_nio_charset_iconv_IconvEncoder.h"
 
 static void createRawData (JNIEnv * env, jobject obj, void *ptr);
 static void *getData (JNIEnv * env, jobject obj);
 
+static jfieldID infid = NULL;
+static jfieldID outfid = NULL;
+
+/* Union used for type punning. */
+union char_union
+{
+  jbyte **jb;
+  jchar **jc;
+  char **c;
+};
+
 JNIEXPORT void JNICALL
 Java_gnu_java_nio_charset_iconv_IconvEncoder_openIconv (JNIEnv * env,
                                                        jobject obj,
                                                        jstring jname)
 {
-  jclass exception;
+#if defined(HAVE_ICONV)
   iconv_t iconv_object;
+  jclass cls;
+
+  const char *name = JCL_jstring_to_cstring (env, jname);
+  if (name == NULL)
+    return;
 
-  const char *name = (*env)->GetStringUTFChars (env, jname, 0);
+  /* Cache fieldIDs for use in encode function. */
+  if (infid == NULL || outfid == NULL)
+    {
+      cls = (*env)->GetObjectClass (env, obj);
+      infid = (*env)->GetFieldID (env, cls, "inremaining", "I");
+      assert (infid != 0);
+      outfid = (*env)->GetFieldID (env, cls, "outremaining", "I");
+      assert (outfid != 0);
+    }
 
   /* to "name" from java, native java format depends on endianness */
 #ifdef WORDS_BIGENDIAN
@@ -61,19 +92,18 @@
   iconv_object = iconv_open (name, "UTF-16LE");
 #endif
 
-  (*env)->ReleaseStringUTFChars (env, jname, name);
+  JCL_free_cstring (env, jname, name);
   if ((long) iconv_object == -1L)
     {
-      /* Throw an exception if charset not available */
-      (*env)->ExceptionDescribe (env);
-      (*env)->ExceptionClear (env);
-      exception =
-       (*env)->FindClass (env, "java/lang/IllegalArgumentException");
-      assert (exception != 0);
-      (*env)->ThrowNew (env, exception, "Charset not available.");
+      JCL_ThrowException (env, "java/lang/IllegalArgumentException",
+                         "Charset not available");
       return;
     }
   createRawData (env, obj, (void *) iconv_object);
+#else
+  JCL_ThrowException (env, "java/lang/IllegalArgumentException",
+                     "iconv not available");
+#endif
 }
 
 JNIEXPORT jint JNICALL
@@ -84,11 +114,10 @@
                                                     jint posIn, jint remIn,
                                                     jint posOut, jint remOut)
 {
+#if defined(HAVE_ICONV)
   iconv_t iconv_object = getData (env, obj);
-  jclass cls;
-  jfieldID fid;
   size_t retval;
-  char **in, **out;
+  union char_union in, out;
   jchar *input, *inputcopy;
   jbyte *output, *outputcopy;
   size_t lenIn = (size_t) remIn * 2;
@@ -100,9 +129,10 @@
   input += posIn * 2;
   output += posOut;
 
-  in = (char **) &input;
-  out = (char **) &output;
-  retval = iconv (iconv_object, in, &lenIn, out, &lenOut);
+  in.jc = &input;
+  out.jb = &output;
+  retval = iconv (iconv_object, (ICONV_CONST char **) in.c, &lenIn,
+                 out.c, &lenOut);
 
   /* XXX: Do we need to relase the input array? It's not modified. */
   (*env)->ReleaseCharArrayElements (env, inArr, inputcopy, 0);
@@ -118,24 +148,24 @@
   else
     retval = 0;
 
-  cls = (*env)->GetObjectClass (env, obj);
-  fid = (*env)->GetFieldID (env, cls, "inremaining", "I");
-  assert (fid != 0);
-  (*env)->SetIntField (env, obj, fid, (jint) (lenIn >> 1));
-  fid = (*env)->GetFieldID (env, cls, "outremaining", "I");
-  assert (fid != 0);
-  (*env)->SetIntField (env, obj, fid, (jint) lenOut);
+  (*env)->SetIntField (env, obj, infid, (jint) (lenIn >> 1));
+  (*env)->SetIntField (env, obj, outfid, (jint) lenOut);
 
   return (jint) retval;
+#else
+  return -1;
+#endif
 }
 
 JNIEXPORT void JNICALL
 Java_gnu_java_nio_charset_iconv_IconvEncoder_closeIconv (JNIEnv * env,
                                                         jobject obj)
 {
+#if defined(HAVE_ICONV)
   iconv_t iconv_object;
   iconv_object = getData (env, obj);
   iconv_close (iconv_object);
+#endif
 }
 
 
Index: gnu/java/nio/charset/iconv/IconvDecoder.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/gnu/java/nio/charset/iconv/IconvDecoder.java,v
retrieving revision 1.1
diff -u -r1.1 IconvDecoder.java
--- gnu/java/nio/charset/iconv/IconvDecoder.java        18 Apr 2005 11:35:14 
-0000      1.1
+++ gnu/java/nio/charset/iconv/IconvDecoder.java        24 Apr 2005 21:33:19 
-0000
@@ -104,7 +104,7 @@
     return CoderResult.OVERFLOW;
   }
 
-  public void dispose()
+  protected void finalize()
   {
     closeIconv();
   }
Index: gnu/java/nio/charset/iconv/IconvEncoder.java
===================================================================
RCS file: 
/cvsroot/classpath/classpath/gnu/java/nio/charset/iconv/IconvEncoder.java,v
retrieving revision 1.1
diff -u -r1.1 IconvEncoder.java
--- gnu/java/nio/charset/iconv/IconvEncoder.java        18 Apr 2005 11:35:14 
-0000      1.1
+++ gnu/java/nio/charset/iconv/IconvEncoder.java        24 Apr 2005 21:33:19 
-0000
@@ -104,7 +104,7 @@
     return CoderResult.OVERFLOW;
   }
 
-  public void dispose()
+  protected void finalize()
   {
     closeIconv();
   }

Attachment: signature.asc
Description: This is a digitally signed message part


reply via email to

[Prev in Thread] Current Thread [Next in Thread]