This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Patch: Add JvAllocBytes() and RawDataManaged to CNI
- From: Bryce McKinlay <mckinlay at redhat dot com>
- To: java-patches at gcc dot gnu dot org
- Date: Fri, 28 May 2004 14:52:19 -0400
- Subject: Patch: Add JvAllocBytes() and RawDataManaged to CNI
This patch adds a new RawDataManaged type to CNI. This is used like
RawData except the pointer will be marked by the garbage collector, so
memory allocated by GC functions such as JvAllocBytes() can be stored in
RawDataManaged fields and may be automatically freed when the object
itself becomes unreachable. This gives us a way to explicity specify
native data fields rather than using "Object", which is ugly and
potentially unsafe.
The patch also officially adds JvAllocBytes() to the CNI interface and
documents it.
I'm checking this in to mainline.
Regards
Bryce
2004-05-28 Bryce McKinlay <mckinlay@redhat.com>
* gcj.texi (Object allocation): Remove _Jv_AllocBytes.
(Mixing with C++): Document JvAllocBytes and RawDataManaged.
2004-05-28 Bryce McKinlay <mckinlay@redhat.com>
* gcj/cni.h (JvAllocBytes): New public CNI function. Calls
_Jv_AllocBytes.
* gnu/gcj/RawDataManaged.java: New file.
* java/lang/Thread.java (data): Declare as RawDataManaged.
* java/lang/natThread.cc (init_native): Cast natThread data to
RawDataManaged, not jobject.
* Makefile.am (ordinary_java_source_files): Add RawDataManaged.
* Makefile.in: Rebuilt.
Index: gcc/java/gcj.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/gcj.texi,v
retrieving revision 1.61
diff -u -r1.61 gcj.texi
--- gcc/java/gcj.texi 23 Mar 2004 17:49:22 -0000 1.61
+++ gcc/java/gcj.texi 28 May 2004 18:45:29 -0000
@@ -1171,9 +1171,7 @@
@code{JvNewObjectArray}. This convention is used to avoid conflicts
with other libraries. Internal functions in CNI start with the prefix
@code{_Jv_}. You should not call these; if you find a need to, let us
-know and we will try to come up with an alternate solution. (This
-manual lists @code{_Jv_AllocBytes} as an example; CNI should instead
-provide a @code{JvAllocBytes} function.)
+know and we will try to come up with an alternate solution.
@subsection Limitations
@@ -1488,11 +1486,6 @@
java::util::Hashtable *ht = new java::util::Hashtable(120);
@end example
-@deftypefun void* _Jv_AllocBytes (jsize @var{size})
-Allocates @var{size} bytes from the heap. The memory is not scanned
-by the garbage collector but it freed if no references to it are discovered.
-@end deftypefun
-
@node Arrays
@section Arrays
@@ -1784,11 +1777,13 @@
@}
@end example
-But this restriction can cause a problem so @acronym{CNI} includes the
+@subsection RawData
+
+The above restriction can be problematic, so @acronym{CNI} includes the
@code{gnu.gcj.RawData} class. The @code{RawData} class is a
@dfn{non-scanned reference} type. In other words variables declared
of type @code{RawData} can contain any data and are not checked by the
-compiler in any way.
+compiler or memory manager in any way.
This means that you can put C/C++ data structures (including classes)
in your @acronym{CNI} classes, as long as you use the appropriate cast.
@@ -1826,6 +1821,30 @@
@end example
+@subsection RawDataManaged
+
+@code{gnu.gcj.RawDataManaged} is another type used to indicate special data used
+by native code. Unlike the @code{RawData} type, fields declared as
+@code{RawDataManaged} will be "marked" by the memory manager and
+considered for garbage collection.
+
+Native data which is allocated using CNI's @code{JvAllocBytes()}
+function and stored in a @code{RawDataManaged} will be automatically
+freed when the Java object it is associated with becomes unreachable.
+
+@subsection Native memory allocation
+
+@deftypefun void* JvAllocBytes (jsize @var{size})
+Allocates @var{size} bytes from the heap. The memory returned is zeroed.
+This memory is not scanned for pointers by the garbage collector, but will
+be freed if no references to it are discovered.
+
+This function can be useful if you need to associate some native data with a
+Java object. Using a CNI's special @code{RawDataManaged} type, native data
+allocated with @code{JvAllocBytes} will be automatically freed when the Java
+object itself becomes unreachable.
+@end deftypefun
+
@node Exception Handling
@section Exception Handling
Index: libjava/gcj/cni.h
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gcj/cni.h,v
retrieving revision 1.12
diff -u -r1.12 cni.h
--- libjava/gcj/cni.h 20 Apr 2004 01:38:45 -0000 1.12
+++ libjava/gcj/cni.h 28 May 2004 18:45:29 -0000
@@ -22,6 +22,7 @@
extern "C" jstring _Jv_NewStringUTF (const char *bytes);
extern "C" void _Jv_InitClass (jclass);
+extern "C" void *_Jv_AllocBytes (jsize size) __attribute__((__malloc__));
extern inline void
JvInitClass (jclass cls)
@@ -29,6 +30,12 @@
return _Jv_InitClass (cls);
}
+extern inline void *
+JvAllocBytes (jsize sz)
+{
+ return _Jv_AllocBytes (sz);
+}
+
extern inline jstring
JvAllocString (jsize sz)
{
Index: libjava/gnu/gcj/RawDataManaged.java
===================================================================
RCS file: libjava/gnu/gcj/RawDataManaged.java
diff -N libjava/gnu/gcj/RawDataManaged.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ libjava/gnu/gcj/RawDataManaged.java 28 May 2004 18:45:29 -0000
@@ -0,0 +1,23 @@
+/* Copyright (C) 2004 Free Software Foundation
+
+ This file is part of libgcj.
+
+This software is copyrighted work licensed under the terms of the
+Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
+details. */
+
+package gnu.gcj;
+
+/** A type used to indicate special data used by native code. Unlike the
+ <code>RawData</code> type, fields declared as <code>RawDataManaged</code>
+ will be "marked" by the memory manager and considered for garbage
+ collection.
+
+ Native data which is allocated using CNI's <code>JvAllocBytes()</code>
+ function and stored in a <code>RawDataManaged</code> will be automatically
+ freed when the Java object it is associated with becomes unreachable. */
+
+public final class RawDataManaged
+{
+ private RawDataManaged() { }
+}
Index: libjava/java/lang/Thread.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/Thread.java,v
retrieving revision 1.32
diff -u -r1.32 Thread.java
--- libjava/java/lang/Thread.java 9 Mar 2004 21:02:52 -0000 1.32
+++ libjava/java/lang/Thread.java 28 May 2004 18:45:29 -0000
@@ -39,6 +39,7 @@
package java.lang;
import gnu.gcj.RawData;
+import gnu.gcj.RawDataManaged;
/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
* "The Java Language Specification", ISBN 0-201-63451-1
@@ -127,7 +128,7 @@
RawData interp_frame;
// Our native data - points to an instance of struct natThread.
- private Object data;
+ private RawDataManaged data;
/**
* Allocates a new <code>Thread</code> object. This constructor has
Index: libjava/java/lang/natThread.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/natThread.cc,v
retrieving revision 1.26
diff -u -r1.26 natThread.cc
--- libjava/java/lang/natThread.cc 9 Mar 2004 21:02:52 -0000 1.26
+++ libjava/java/lang/natThread.cc 28 May 2004 18:45:29 -0000
@@ -16,6 +16,7 @@
#include <jvm.h>
#include <java-threads.h>
+#include <gnu/gcj/RawDataManaged.h>
#include <java/lang/Thread.h>
#include <java/lang/ThreadGroup.h>
#include <java/lang/IllegalArgumentException.h>
@@ -59,11 +60,7 @@
{
natThread *nt = (natThread *) _Jv_AllocBytes (sizeof (natThread));
- // The native thread data is kept in a Object field, not a RawData, so that
- // the GC allocator can be used and a finalizer run after the thread becomes
- // unreachable. Note that this relies on the GC's ability to finalize
- // non-Java objects. FIXME?
- data = reinterpret_cast<jobject> (nt);
+ data = (gnu::gcj::RawDataManaged *) nt;
// Register a finalizer to clean up the native thread resources.
_Jv_RegisterFinalizer (data, finalize_native);
Index: libjava/Makefile.am
===================================================================
RCS file: /cvs/gcc/gcc/libjava/Makefile.am,v
retrieving revision 1.380
diff -u -r1.380 Makefile.am
--- libjava/Makefile.am 27 May 2004 06:17:30 -0000 1.380
+++ libjava/Makefile.am 28 May 2004 18:45:29 -0000
@@ -2251,6 +2251,7 @@
gnu/classpath/ServiceProviderLoadingAction.java \
gnu/gcj/Core.java \
gnu/gcj/RawData.java \
+gnu/gcj/RawDataManaged.java \
gnu/gcj/io/DefaultMimeTypes.java \
gnu/gcj/io/MimeTypes.java \
gnu/gcj/io/SimpleSHSStream.java \
Index: libjava/Makefile.in
===================================================================
RCS file: /cvs/gcc/gcc/libjava/Makefile.in,v
retrieving revision 1.404
diff -u -r1.404 Makefile.in
--- libjava/Makefile.in 27 May 2004 06:17:31 -0000 1.404
+++ libjava/Makefile.in 28 May 2004 18:45:30 -0000
@@ -1923,6 +1923,7 @@
gnu/classpath/ServiceProviderLoadingAction.java \
gnu/gcj/Core.java \
gnu/gcj/RawData.java \
+gnu/gcj/RawDataManaged.java \
gnu/gcj/io/DefaultMimeTypes.java \
gnu/gcj/io/MimeTypes.java \
gnu/gcj/io/SimpleSHSStream.java \
@@ -3075,7 +3076,8 @@
.deps/gnu/awt/xlib/XToolkit.P .deps/gnu/classpath/Configuration.P \
.deps/gnu/classpath/ServiceFactory.P \
.deps/gnu/classpath/ServiceProviderLoadingAction.P .deps/gnu/gcj/Core.P \
-.deps/gnu/gcj/RawData.P .deps/gnu/gcj/convert/BytesToUnicode.P \
+.deps/gnu/gcj/RawData.P .deps/gnu/gcj/RawDataManaged.P \
+.deps/gnu/gcj/convert/BytesToUnicode.P \
.deps/gnu/gcj/convert/Convert.P .deps/gnu/gcj/convert/IOConverter.P \
.deps/gnu/gcj/convert/Input_8859_1.P \
.deps/gnu/gcj/convert/Input_ASCII.P \