From ca60dce284a912a0e62500ed6b097c1960dce26e Mon Sep 17 00:00:00 2001 From: Bryce McKinlay Date: Fri, 28 May 2004 18:53:06 +0000 Subject: [PATCH] gcj.texi (Object allocation): Remove _Jv_AllocBytes. * gcj.texi (Object allocation): Remove _Jv_AllocBytes. (Mixing with C++): Document JvAllocBytes and RawDataManaged. * 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. From-SVN: r82372 --- gcc/java/ChangeLog | 5 ++++ gcc/java/gcj.texi | 39 +++++++++++++++++++++-------- libjava/ChangeLog | 11 ++++++++ libjava/Makefile.am | 1 + libjava/Makefile.in | 4 ++- libjava/gcj/cni.h | 7 ++++++ libjava/gnu/gcj/RawDataManaged.java | 23 +++++++++++++++++ libjava/java/lang/Thread.java | 3 ++- libjava/java/lang/natThread.cc | 7 ++---- 9 files changed, 83 insertions(+), 17 deletions(-) create mode 100644 libjava/gnu/gcj/RawDataManaged.java diff --git a/gcc/java/ChangeLog b/gcc/java/ChangeLog index b3a951b1d6c..e869a077859 100644 --- a/gcc/java/ChangeLog +++ b/gcc/java/ChangeLog @@ -1,3 +1,8 @@ +2004-05-28 Bryce McKinlay + + * gcj.texi (Object allocation): Remove _Jv_AllocBytes. + (Mixing with C++): Document JvAllocBytes and RawDataManaged. + 2004-05-26 Bryce McKinlay * decl.c (struct binding_level): Add GTY marker. Compile diff --git a/gcc/java/gcj.texi b/gcc/java/gcj.texi index b71568a3d6f..e5b37a0e077 100644 --- a/gcc/java/gcj.texi +++ b/gcc/java/gcj.texi @@ -1171,9 +1171,7 @@ macros start with the @code{Jv} prefix, for example the function @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 @@ using standard C++ overload resolution rules. 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 @@ jint @} @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 @@ void @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 diff --git a/libjava/ChangeLog b/libjava/ChangeLog index a4124d49e0a..e6270fe86a5 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,14 @@ +2004-05-28 Bryce McKinlay + + * 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. + 2004-05-27 Jerry Quinn * java/util/SimpleTimeZone.java: Reverting my last change until I diff --git a/libjava/Makefile.am b/libjava/Makefile.am index e8fac90557d..5a97d9b886f 100644 --- a/libjava/Makefile.am +++ b/libjava/Makefile.am @@ -2251,6 +2251,7 @@ gnu/classpath/ServiceFactory.java \ 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 \ diff --git a/libjava/Makefile.in b/libjava/Makefile.in index a68a718616c..cb924d58757 100644 --- a/libjava/Makefile.in +++ b/libjava/Makefile.in @@ -1923,6 +1923,7 @@ gnu/classpath/ServiceFactory.java \ 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 @@ DEP_FILES = .deps/$(srcdir)/$(CONVERT_DIR)/gen-from-JIS.P \ .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 \ diff --git a/libjava/gcj/cni.h b/libjava/gcj/cni.h index 59cf673b092..b9ee3829ce1 100644 --- a/libjava/gcj/cni.h +++ b/libjava/gcj/cni.h @@ -22,6 +22,7 @@ details. */ 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 @@ JvInitClass (jclass cls) return _Jv_InitClass (cls); } +extern inline void * +JvAllocBytes (jsize sz) +{ + return _Jv_AllocBytes (sz); +} + extern inline jstring JvAllocString (jsize sz) { diff --git a/libjava/gnu/gcj/RawDataManaged.java b/libjava/gnu/gcj/RawDataManaged.java new file mode 100644 index 00000000000..91a36acb952 --- /dev/null +++ b/libjava/gnu/gcj/RawDataManaged.java @@ -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 + RawData type, fields declared as RawDataManaged + will be "marked" by the memory manager and considered for garbage + collection. + + Native data which is allocated using CNI's JvAllocBytes() + function and stored in a RawDataManaged will be automatically + freed when the Java object it is associated with becomes unreachable. */ + +public final class RawDataManaged +{ + private RawDataManaged() { } +} diff --git a/libjava/java/lang/Thread.java b/libjava/java/lang/Thread.java index 0232a1d6afd..f631cc5aaea 100644 --- a/libjava/java/lang/Thread.java +++ b/libjava/java/lang/Thread.java @@ -39,6 +39,7 @@ exception statement from your version. */ 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 @@ public class Thread implements Runnable RawData interp_frame; // Our native data - points to an instance of struct natThread. - private Object data; + private RawDataManaged data; /** * Allocates a new Thread object. This constructor has diff --git a/libjava/java/lang/natThread.cc b/libjava/java/lang/natThread.cc index a131f058be7..ea235ce3789 100644 --- a/libjava/java/lang/natThread.cc +++ b/libjava/java/lang/natThread.cc @@ -16,6 +16,7 @@ details. */ #include #include +#include #include #include #include @@ -59,11 +60,7 @@ java::lang::Thread::initialize_native (void) { 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 (nt); + data = (gnu::gcj::RawDataManaged *) nt; // Register a finalizer to clean up the native thread resources. _Jv_RegisterFinalizer (data, finalize_native); -- 2.30.2