gcj.texi

来自「理解和实践操作系统的一本好书」· TEXI 代码 · 共 1,908 行 · 第 1/5 页

TEXI
1,908
字号
  [@option{-v}]  [@option{--verbose}]  [@option{-p} @var{tool-prefix}]  [@option{-d} @var{directory}]  [@option{--version}]  [@option{--help}]@end ignore@c man end@c man begin DESCRIPTION gc-analyze@command{gc-analyze} prints an analysis of a GC memory dump tostandard out.The memory dumps may be created by calling@code{gnu.gcj.util.GCInfo.enumerate(String namePrefix)} from javacode.  A memory dump will be created on an out of memory condition if@code{gnu.gcj.util.GCInfo.setOOMDump(String namePrefix)} is calledbefore the out of memory occurs.Running this program will create two files: @file{TestDump001} and@file{TestDump001.bytes}.@exampleimport gnu.gcj.util.*;import java.util.*;public class GCDumpTest@{    static public void main(String args[])    @{        ArrayList<String> l = new ArrayList<String>(1000);                for (int i = 1; i < 1500; i++) @{            l.add("This is string #" + i);        @}        GCInfo.enumerate("TestDump");    @}@}@end exampleThe memory dump may then be displayed by running:@examplegc-analyze -v TestDump001@end example@c FIXME: Add real information here.@c This really isn't much more than the --help output.@c man end@c man begin OPTIONS gc-analyze@table @gcctabopt@item --verbose@itemx -vVerbose output.@item -p @var{tool-prefix}Prefix added to the names of the @command{nm} and @command{readelf} commands.@item -d @var{directory}Directory that contains the executable and shared libraries used whenthe dump was generated.@item --helpPrint a help message, then exit.@item --versionPrint version information, then exit.@end table@c man end@node About CNI@chapter About CNIThis documents CNI, the Compiled Native Interface,which is is a convenient way to write Java native methods using C++.This is a more efficient, more convenient, but less portablealternative to the standard JNI (Java Native Interface).@menu* Basic concepts::              Introduction to using CNI@.* Packages::                    How packages are mapped to C++.* Primitive types::             Handling primitive Java types in C++.* Reference types::             Handling Java reference types in C++.* Interfaces::                  How Java interfaces map to C++.* Objects and Classes::         C++ and Java classes.* Class Initialization::        How objects are initialized.* Object allocation::           How to create Java objects in C++.* Memory allocation::           How to allocate and free memory.* Arrays::                      Dealing with Java arrays in C++.* Methods::                     Java methods in C++.* Strings::                     Information about Java Strings.* Mixing with C++::             How CNI can interoperate with C++.* Exception Handling::          How exceptions are handled.* Synchronization::             Synchronizing between Java and C++.* Invocation::			Starting the Java runtime from C++.* Reflection::                  Using reflection from C++.@end menu@node Basic concepts@section Basic conceptsIn terms of languages features, Java is mostly a subsetof C++.  Java has a few important extensions, plus a powerful standardclass library, but on the whole that does not change the basic similarity.Java is a hybrid object-oriented language, with a few native types,in addition to class types.  It is class-based, where a class may havestatic as well as per-object fields, and static as well as instance methods.Non-static methods may be virtual, and may be overloaded.  Overloading isresolved at compile time by matching the actual argument types againstthe parameter types.  Virtual methods are implemented using indirect callsthrough a dispatch table (virtual function table).  Objects areallocated on the heap, and initialized using a constructor method.Classes are organized in a package hierarchy.All of the listed attributes are also true of C++, though C++ hasextra features (for example in C++ objects may be allocated not juston the heap, but also statically or in a local stack frame).  Because@command{gcj} uses the same compiler technology as G++ (the GNUC++ compiler), it is possible to make the intersection of the twolanguages use the same ABI (object representation and callingconventions).  The key idea in CNI is that Java objects are C++objects, and all Java classes are C++ classes (but not the other wayaround).  So the most important task in integrating Java and C++ is toremove gratuitous incompatibilities.You write CNI code as a regular C++ source file.  (You do have to usea Java/CNI-aware C++ compiler, specifically a recent version of G++.)@noindent A CNI C++ source file must have:@example#include <gcj/cni.h>@end example@noindent and then must include one header file for each Java class it uses, e.g.:@example#include <java/lang/Character.h>#include <java/util/Date.h>#include <java/lang/IndexOutOfBoundsException.h>@end example@noindent These header files are automatically generated by @code{gcjh}.CNI provides some functions and macros to make using Java objects andprimitive types from C++ easier.  In general, these CNI functions andmacros start with the @code{Jv} prefix, for example the function@code{JvNewObjectArray}.  This convention is used to avoid conflictswith other libraries.  Internal functions in CNI start with the prefix@code{_Jv_}.  You should not call these; if you find a need to, let usknow and we will try to come up with an alternate solution.@subsection LimitationsWhilst a Java class is just a C++ class that doesn't mean that you arefreed from the shackles of Java, a @acronym{CNI} C++ class must adhere to therules of the Java programming language.For example: it is not possible to declare a method in a CNI classthat will take a C string (@code{char*}) as an argument, or to declare amember variable of some non-Java datatype.@node Packages@section PackagesThe only global names in Java are class names, and packages.  A@dfn{package} can contain zero or more classes, and also zero or moresub-packages.  Every class belongs to either an unnamed package or apackage that has a hierarchical and globally unique name.A Java package is mapped to a C++ @dfn{namespace}.  The Java class@code{java.lang.String} is in the package @code{java.lang}, which is asub-package of @code{java}.  The C++ equivalent is the class@code{java::lang::String}, which is in the namespace @code{java::lang}which is in the namespace @code{java}.@noindent Here is how you could express this:@example(// @r{Declare the class(es), possibly in a header file:}namespace java @{  namespace lang @{    class Object;    class String;    ...  @}@}class java::lang::String : public java::lang::Object@{  ...@};@end example@noindent The @code{gcjh} tool automatically generates the necessary namespacedeclarations.@subsection Leaving out package namesAlways using the fully-qualified name of a java class can betiresomely verbose.  Using the full qualified name also ties the codeto a single package making code changes necessary should the classmove from one package to another.  The Java @code{package} declarationspecifies that the following class declarations are in the namedpackage, without having to explicitly name the full packagequalifiers.  The @code{package} declaration can befollowed by zero or more @code{import} declarations, whichallows either a single class or all the classes in a package to benamed by a simple identifier.  C++ provides something similar with the@code{using} declaration and directive.@noindent In Java:@exampleimport @var{package-name}.@var{class-name};@end example@noindent allows the program text to refer to @var{class-name} as a shorthand for the fully qualified name: @code{@var{package-name}.@var{class-name}}.@noindent To achieve the same effect C++, you have to do this:@exampleusing @var{package-name}::@var{class-name};@end example@noindent Java can also cause imports on demand, like this:@exampleimport @var{package-name}.*;@end example@noindent Doing this allows any class from the package @var{package-name} to bereferred to only by its class-name within the program text.@noindent The same effect can be achieved in C++ like this:@exampleusing namespace @var{package-name};@end example@node Primitive types@section Primitive typesJava provides 8 @dfn{primitives} types which represent integers, floats, characters and booleans (and also the void type).  C++ has its ownvery similar concrete types.  Such types in C++ however are not alwaysimplemented in the same way (an int might be 16, 32 or 64 bits for example) so CNI provides a special C++ type for each primitive Java type:@multitable @columnfractions .20 .25 .60@item @strong{Java type}   @tab @strong{C/C++ typename} @tab @strong{Description}@item @code{char}        @tab @code{jchar}          @tab 16 bit Unicode character@item @code{boolean}     @tab @code{jboolean}       @tab logical (true or false) values@item @code{byte}        @tab @code{jbyte}          @tab 8-bit signed integer@item @code{short}       @tab @code{jshort}         @tab 16 bit signed integer@item @code{int}         @tab @code{jint}           @tab 32 bit signed integer@item @code{long}        @tab @code{jlong}          @tab 64 bit signed integer@item @code{float}       @tab @code{jfloat}         @tab 32 bit IEEE floating point number@item @code{double}      @tab @code{jdouble}        @tab 64 bit IEEE floating point number@item @code{void}        @tab @code{void}           @tab no value@end multitableWhen referring to a Java type You should always use these C++ typenames (e.g.: @code{jint})to avoid disappointment.@subsection Reference types associated with primitive typesIn Java each primitive type has an associated reference type, e.g.: @code{boolean} has an associated @code{java.lang.Boolean.TYPE} class.In order to make working with such classes easier GCJ provides the macro@code{JvPrimClass}:@deffn macro JvPrimClass typeReturn a pointer to the @code{Class} object corresponding to the type supplied.@exampleJvPrimClass(void) @result{} java.lang.Void.TYPE@end example@end deffn@node Reference types@section Reference typesA Java reference type is treated as a class in C++.  Classes andinterfaces are handled this way.  A Java reference is translated to aC++ pointer, so for instance a Java @code{java.lang.String} becomes,in C++, @code{java::lang::String *}.CNI provides a few built-in typedefs for the most common classes:@multitable @columnfractions .30 .25 .60@item @strong{Java type} @tab @strong{C++ typename} @tab @strong{Description}@item @code{java.lang.Object} @tab @code{jobject} @tab Object type@item @code{java.lang.String} @tab @code{jstring} @tab String type@item @code{java.lang.Class} @tab @code{jclass} @tab Class type@end multitable@cindex jobject@cindex jstring@cindex jclassEvery Java class or interface has a corresponding @code{Class}instance.  These can be accessed in CNI via the static @code{class$}field of a class.  The @code{class$} field is of type @code{Class}(and not @code{Class *}), so you will typically take the address ofit.@cindex class$Here is how you can refer to the class of @code{String}, which inJava would be written @code{String.class}:@exampleusing namespace java::lang;doSomething (&String::class$);@end example@node Interfaces@section InterfacesA Java class can @dfn{implement} zero or more@dfn{interfaces}, in addition to inheriting froma single base class. @acronym{CNI} allows CNI code to implement methods of interfaces.You can also call methods through interface references, with somelimitations.@acronym{CNI} doesn't understand interface inheritance at all yet.  So,you can only call an interface method when the declared type of thefield being called matches the interface which declares thatmethod.  The workaround is to cast the interface reference to the rightsuperinterface. For example if you have: @example interface A @{   void a(); @}  interface B extends A @{   void b(); @} @end example and declare a variable of type @code{B} in C++, you can't call@code{a()} unless you cast it to an @code{A} first.@node Objects and Classes@section Objects and Classes@subsection ClassesAll Java classes are derived from @code{java.lang.Object}.  C++ doesnot have a unique root class, but we use the C++ class@code{java::lang::Object} as the C++ version of the@code{java.lang.Object} Java class.  All other Java classes are mappedinto corresponding C++ classes derived from @code{java::lang::Object}.Interface inheritance (the @code{implements} keyword) is currently notreflected in the C++ mapping.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?