This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Patch: FYI: natFile -vs- stack


I'm checking this in.

This changes natFile.cc so that it doesn't stack allocate any buffers,
and so that the buffers it allocates are the right size.  It also
removes a global which I think isn't really needed.

I've kept the encoding of file names as UTF-8 for now.  I suspect this
is incorrect but I haven't found the time to find out.

Tom

Index: ChangeLog
from  Tom Tromey  <tromey@redhat.com>
	* java/io/natFile.cc (_access): Don't stack-allocate buffer.
	Size buffer based on real size of string.
	(_stat): Likewise.
	(attr): Likewise.
	(getCanonicalPath): Likewise.
	(performList): Likewise.
	(performMkdir): Likewise.
	(performSetReadOnly): Likewise.
	(unixroot): Removed.
	(performRenameTo): Likewise.
	(performSetLastModified): Likewise.
	(performCreate): Likewise.
	(performDelete): Likewise.
	(performListRoots): Always return new array.

Index: java/io/natFile.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/natFile.cc,v
retrieving revision 1.15
diff -u -r1.15 natFile.cc
--- java/io/natFile.cc 2002/02/07 03:24:12 1.15
+++ java/io/natFile.cc 2002/02/07 19:09:49
@@ -39,7 +39,7 @@
 jboolean
 java::io::File::_access (jint query)
 {
-  char buf[MAXPATHLEN];
+  char *buf = (char *) _Jv_AllocBytes (JvGetStringUTFLength (path) + 1);
   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
   buf[total] = '\0';
   JvAssert (query == READ || query == WRITE || query == EXISTS);
@@ -60,7 +60,7 @@
 jboolean
 java::io::File::_stat (jint query)
 {
-  char buf[MAXPATHLEN];
+  char *buf = (char *) _Jv_AllocBytes (JvGetStringUTFLength (path) + 1);
   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
   buf[total] = '\0';
 
@@ -83,7 +83,7 @@
 jlong
 java::io::File::attr (jint query)
 {
-  char buf[MAXPATHLEN];
+  char *buf = (char *) _Jv_AllocBytes (JvGetStringUTFLength (path) + 1);
   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
   buf[total] = '\0';
 
@@ -104,7 +104,8 @@
 jstring
 java::io::File::getCanonicalPath (void)
 {
-  char buf[MAXPATHLEN], buf2[MAXPATHLEN];
+  char *buf = (char *) _Jv_AllocBytes (JvGetStringUTFLength (path) + 1);
+  char buf2[MAXPATHLEN];
   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
   buf[total] = '\0';
 
@@ -134,7 +135,7 @@
   /* Some systems have dirent.h, but no directory reading functions like
      opendir.  */
 #if defined(HAVE_DIRENT_H) && defined(HAVE_OPENDIR)
-  char buf[MAXPATHLEN];
+  char *buf = (char *) _Jv_AllocBytes (JvGetStringUTFLength (path) + 1);
   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
   buf[total] = '\0';
 
@@ -142,7 +143,6 @@
   if (! dir)
     return NULL;
 
-
   java::util::ArrayList *list = new java::util::ArrayList ();
   struct dirent *d;
 #ifdef HAVE_READDIR_R
@@ -162,7 +162,7 @@
       jstring name = JvNewStringUTF (d->d_name);
       if (filter && ! filter->accept(this, name))
 	continue;
-      
+
       if (result_type == &java::io::File::class$)
         {
 	  java::io::File *file = new java::io::File (this, name);
@@ -188,7 +188,7 @@
 jboolean
 java::io::File::performMkdir (void)
 {
-  char buf[MAXPATHLEN];
+  char *buf = (char *) _Jv_AllocBytes (JvGetStringUTFLength (path) + 1);
   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
   buf[total] = '\0';
 
@@ -202,7 +202,7 @@
 jboolean
 java::io::File::performSetReadOnly (void)
 {
-  char buf[MAXPATHLEN];
+  char *buf = (char *) _Jv_AllocBytes (JvGetStringUTFLength (path) + 1);
   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
   buf[total] = '\0';
 
@@ -219,28 +219,24 @@
 #endif
 }
 
-static JArray<java::io::File *> *unixroot;
-
 JArray< ::java::io::File *>*
 java::io::File::performListRoots ()
 {
-  if (unixroot == NULL)
-    {
-      ::java::io::File *f = new ::java::io::File (JvNewStringLatin1 ("/"));
-      unixroot = reinterpret_cast <JArray<java::io::File *>*> 
-		   (JvNewObjectArray (1, &java::io::File::class$, f));
-      elements (unixroot) [0] = f;
-    }
+  ::java::io::File *f = new ::java::io::File (JvNewStringLatin1 ("/"));
+  JArray<java::io::File *> *unixroot
+    = reinterpret_cast <JArray<java::io::File *>*> 
+          (JvNewObjectArray (1, &java::io::File::class$, f));
+  elements (unixroot) [0] = f;
   return unixroot;
 }
 
 jboolean
 java::io::File::performRenameTo (File *dest)
 {
-  char buf[MAXPATHLEN];
+  char *buf = (char *) _Jv_AllocBytes (JvGetStringUTFLength (path) + 1);
   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
   buf[total] = '\0';
-  char buf2[MAXPATHLEN];
+  char *buf2 = (char *) _Jv_AllocBytes (JvGetStringUTFLength (dest->path) + 1);
   total = JvGetStringUTFRegion (dest->path, 0, dest->path->length(), buf2);
   buf2[total] = '\0';
 
@@ -257,7 +253,7 @@
 #ifdef HAVE_UTIME
   utimbuf tb;
 
-  char buf[MAXPATHLEN];
+  char *buf = (char *) _Jv_AllocBytes (JvGetStringUTFLength (path) + 1);
   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
   buf[total] = '\0';
   
@@ -272,7 +268,7 @@
 jboolean
 java::io::File::performCreate (void)
 {
-  char buf[MAXPATHLEN];
+  char *buf = (char *) _Jv_AllocBytes (JvGetStringUTFLength (path) + 1);
   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
   buf[total] = '\0';
 
@@ -294,7 +290,7 @@
 jboolean
 java::io::File::performDelete (void)
 {
-  char buf[MAXPATHLEN];
+  char *buf = (char *) _Jv_AllocBytes (JvGetStringUTFLength (path) + 1);
   jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
   buf[total] = '\0';
 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]