Commits

Anonymous committed 3e93a51 Merge

Merge "use longs instead of ints to store pointers in ExpatParser"

Comments (0)

Files changed (3)

luni/src/main/java/org/apache/harmony/xml/ExpatAttributes.java

      * Gets the pointer to the parser. We need this so we can get to the
      * interned string pool.
      */
-    abstract int getParserPointer();
+    abstract long getParserPointer();
 
     /**
      * Gets the pointer to the underlying attribute array. Can be 0 if the
      * length is 0.
      */
-    public abstract int getPointer();
+    public abstract long getPointer();
 
     public String getURI(int index) {
         if (index < 0 || index >= getLength()) {
         if (localName == null) {
             throw new NullPointerException("localName == null");
         }
-        int pointer = getPointer();
+        long pointer = getPointer();
         if (pointer == 0) {
             return -1;
         }
         if (qName == null) {
             throw new NullPointerException("qName == null");
         }
-        int pointer = getPointer();
+        long pointer = getPointer();
         if (pointer == 0) {
             return -1;
         }
         if (localName == null) {
             throw new NullPointerException("localName == null");
         }
-        int pointer = getPointer();
+        long pointer = getPointer();
         if (pointer == 0) {
             return null;
         }
         if (qName == null) {
             throw new NullPointerException("qName == null");
         }
-        int pointer = getPointer();
+        long pointer = getPointer();
         if (pointer == 0) {
             return null;
         }
         return getValueForQName(pointer, qName);
     }
 
-    private static native String getURI(int pointer, int attributePointer, int index);
-    private static native String getLocalName(int pointer, int attributePointer, int index);
-    private static native String getQName(int pointer, int attributePointer, int index);
-    private static native String getValueByIndex(int attributePointer, int index);
-    private static native int getIndex(int attributePointer, String uri, String localName);
-    private static native int getIndexForQName(int attributePointer, String qName);
-    private static native String getValue(int attributePointer, String uri, String localName);
-    private static native String getValueForQName(int attributePointer, String qName);
-    protected native void freeAttributes(int pointer);
+    private static native String getURI(long pointer, long attributePointer, int index);
+    private static native String getLocalName(long pointer, long attributePointer, int index);
+    private static native String getQName(long pointer, long attributePointer, int index);
+    private static native String getValueByIndex(long attributePointer, int index);
+    private static native int getIndex(long attributePointer, String uri, String localName);
+    private static native int getIndexForQName(long attributePointer, String qName);
+    private static native String getValue(long attributePointer, String uri, String localName);
+    private static native String getValueForQName(long attributePointer, String qName);
+    protected native void freeAttributes(long pointer);
 }

luni/src/main/java/org/apache/harmony/xml/ExpatParser.java

     private static final int BUFFER_SIZE = 8096; // in bytes
 
     /** Pointer to XML_Parser instance. */
-    private int pointer;
+    private long pointer;
 
     private boolean inStartElement = false;
     private int attributeCount = -1;
     /**
      * Used by {@link EntityParser}.
      */
-    private ExpatParser(String encoding, ExpatReader xmlReader, int pointer,
+    private ExpatParser(String encoding, ExpatReader xmlReader, long pointer,
             String publicId, String systemId) {
         this.encoding = encoding;
         this.xmlReader = xmlReader;
      *
      * @return the pointer to the native parser
      */
-    private native int initialize(String encoding, boolean namespacesEnabled);
+    private native long initialize(String encoding, boolean namespacesEnabled);
 
     /**
      * Called at the start of an element.
         }
 
         String encoding = pickEncoding(inputSource);
-        int pointer = createEntityParser(this.pointer, context);
+        long pointer = createEntityParser(this.pointer, context);
         try {
             EntityParser entityParser = new EntityParser(encoding, xmlReader,
                     pointer, inputSource.getPublicId(),
      * @param context passed to {@link #handleExternalEntity}
      * @return pointer to native parser
      */
-    private static native int createEntityParser(int parentPointer, String context);
+    private static native long createEntityParser(long parentPointer, String context);
 
     /**
      * Appends part of an XML document. This parser will parse the given XML to
         }
     }
 
-    private native void appendString(int pointer, String xml, boolean isFinal)
+    private native void appendString(long pointer, String xml, boolean isFinal)
             throws SAXException, ExpatException;
 
     /**
         }
     }
 
-    private native void appendChars(int pointer, char[] xml, int offset,
+    private native void appendChars(long pointer, char[] xml, int offset,
             int length) throws SAXException, ExpatException;
 
     /**
         }
     }
 
-    private native void appendBytes(int pointer, byte[] xml, int offset,
+    private native void appendBytes(long pointer, byte[] xml, int offset,
             int length) throws SAXException, ExpatException;
 
     /**
     /**
      * Releases all native objects.
      */
-    private native void release(int pointer);
+    private native void release(long pointer);
 
     /**
      * Releases native parser only.
      */
-    private static native void releaseParser(int pointer);
+    private static native void releaseParser(long pointer);
 
     /**
      * Initialize static resources.
         return line(this.pointer);
     }
 
-    private static native int line(int pointer);
+    private static native int line(long pointer);
 
     /**
      * Gets the current column number within the XML file.
         return column(this.pointer);
     }
 
-    private static native int column(int pointer);
+    private static native int column(long pointer);
 
     /**
      * Clones the current attributes so they can be used outside of
             return ClonedAttributes.EMPTY;
         }
 
-        int clonePointer
+        long clonePointer
                 = cloneAttributes(this.attributePointer, this.attributeCount);
         return new ClonedAttributes(pointer, clonePointer, attributeCount);
     }
 
-    private static native int cloneAttributes(int pointer, int attributeCount);
+    private static native long cloneAttributes(long pointer, int attributeCount);
 
     /**
      * Used for cloned attributes.
 
         private static final Attributes EMPTY = new ClonedAttributes(0, 0, 0);
 
-        private final int parserPointer;
-        private int pointer;
+        private final long parserPointer;
+        private long pointer;
         private final int length;
 
         /**
          *  length is 0.
          * @param length number of attributes
          */
-        private ClonedAttributes(int parserPointer, int pointer, int length) {
+        private ClonedAttributes(long parserPointer, long pointer, int length) {
             this.parserPointer = parserPointer;
             this.pointer = pointer;
             this.length = length;
         }
 
         @Override
-        public int getParserPointer() {
+        public long getParserPointer() {
             return this.parserPointer;
         }
 
         @Override
-        public int getPointer() {
+        public long getPointer() {
             return pointer;
         }
 
     private class CurrentAttributes extends ExpatAttributes {
 
         @Override
-        public int getParserPointer() {
+        public long getParserPointer() {
             return pointer;
         }
 
         @Override
-        public int getPointer() {
+        public long getPointer() {
             if (!inStartElement) {
                 throw new IllegalStateException(OUTSIDE_START_ELEMENT);
             }
         private int depth = 0;
 
         private EntityParser(String encoding, ExpatReader xmlReader,
-                int pointer, String publicId, String systemId) {
+                long pointer, String publicId, String systemId) {
             super(encoding, xmlReader, pointer, publicId, systemId);
         }
 

luni/src/main/native/org_apache_harmony_xml_ExpatParser.cpp

             if (javaBuffer == NULL) return NULL;
 
             // Create a global reference.
-            javaBuffer = (jcharArray) env->NewGlobalRef(javaBuffer);
+            javaBuffer = reinterpret_cast<jcharArray>(env->NewGlobalRef(javaBuffer));
             if (javaBuffer == NULL) return NULL;
 
             buffer = javaBuffer;
     return reinterpret_cast<ParsingContext*>(XML_GetUserData(parser));
 }
 
+static XML_Parser toXMLParser(jlong address) {
+  return reinterpret_cast<XML_Parser>(address);
+}
+
+static jlong fromXMLParser(XML_Parser parser) {
+  return reinterpret_cast<uintptr_t>(parser);
+}
+
 static jmethodID commentMethod;
 static jmethodID endCdataMethod;
 static jmethodID endDtdMethod;
     }
 
     // Create a global reference to the interned string.
-    wrapper->interned = (jstring) env->NewGlobalRef(interned.get());
+    wrapper->interned = reinterpret_cast<jstring>(env->NewGlobalRef(interned.get()));
     if (env->ExceptionCheck()) {
         return NULL;
     }
     env->CallVoidMethod(javaParser, method, buffer, utf16length);
 }
 
-static const char** toAttributes(jint attributePointer) {
+static const char** toAttributes(jlong attributePointer) {
     return reinterpret_cast<const char**>(static_cast<uintptr_t>(attributePointer));
 }
 
  */
 class ExpatElementName {
 public:
-    ExpatElementName(JNIEnv* env, ParsingContext* parsingContext, jint attributePointer, jint index) {
+    ExpatElementName(JNIEnv* env, ParsingContext* parsingContext, jlong attributePointer, jint index) {
         const char** attributes = toAttributes(attributePointer);
         const char* name = attributes[index * 2];
         init(env, parsingContext, name);
  * @param javaContext that was provided to handleExternalEntity
  * @returns the pointer to the C Expat entity parser
  */
-static jint ExpatParser_createEntityParser(JNIEnv* env, jobject, jint parentParser, jstring javaContext) {
+static jlong ExpatParser_createEntityParser(JNIEnv* env, jobject, jlong parentParser, jstring javaContext) {
     ScopedUtfChars context(env, javaContext);
     if (context.c_str() == NULL) {
         return 0;
     }
 
-    XML_Parser parent = (XML_Parser) parentParser;
+    XML_Parser parent = toXMLParser(parentParser);
     XML_Parser entityParser = XML_ExternalEntityParserCreate(parent, context.c_str(), NULL);
     if (entityParser == NULL) {
         jniThrowOutOfMemoryError(env, NULL);
     }
 
-    return (jint) entityParser;
+    return fromXMLParser(entityParser);
 }
 
 /**
  * @param processNamespaces true if the parser should handle namespaces
  * @returns the pointer to the C Expat parser
  */
-static jint ExpatParser_initialize(JNIEnv* env, jobject object, jstring javaEncoding,
+static jlong ExpatParser_initialize(JNIEnv* env, jobject object, jstring javaEncoding,
         jboolean processNamespaces) {
     // Allocate parsing context.
     UniquePtr<ParsingContext> context(new ParsingContext(object));
         return 0;
     }
 
-    context->processNamespaces = (bool) processNamespaces;
+    context->processNamespaces = processNamespaces;
 
     // Create a parser.
     XML_Parser parser;
         return 0;
     }
 
-    return (jint) parser;
+    return fromXMLParser(parser);
 }
 
 /**
  * can be reinterpreted as a UTF-16 encoded byte[]. appendBytes, appendChars
  * and appendString all call through this method.
  */
-static void append(JNIEnv* env, jobject object, jint pointer,
+static void append(JNIEnv* env, jobject object, jlong pointer,
         const char* bytes, size_t byteOffset, size_t byteCount, jboolean isFinal) {
-    XML_Parser parser = (XML_Parser) pointer;
+    XML_Parser parser = toXMLParser(pointer);
     ParsingContext* context = toParsingContext(parser);
     context->env = env;
     context->object = object;
     context->env = NULL;
 }
 
-static void ExpatParser_appendBytes(JNIEnv* env, jobject object, jint pointer,
+static void ExpatParser_appendBytes(JNIEnv* env, jobject object, jlong pointer,
         jbyteArray xml, jint byteOffset, jint byteCount) {
     ScopedByteArrayRO byteArray(env, xml);
     if (byteArray.get() == NULL) {
     append(env, object, pointer, bytes, byteOffset, byteCount, XML_FALSE);
 }
 
-static void ExpatParser_appendChars(JNIEnv* env, jobject object, jint pointer,
+static void ExpatParser_appendChars(JNIEnv* env, jobject object, jlong pointer,
         jcharArray xml, jint charOffset, jint charCount) {
     ScopedCharArrayRO charArray(env, xml);
     if (charArray.get() == NULL) {
     append(env, object, pointer, bytes, byteOffset, byteCount, XML_FALSE);
 }
 
-static void ExpatParser_appendString(JNIEnv* env, jobject object, jint pointer, jstring javaXml, jboolean isFinal) {
+static void ExpatParser_appendString(JNIEnv* env, jobject object, jlong pointer, jstring javaXml, jboolean isFinal) {
     ScopedStringChars xml(env, javaXml);
     if (xml.get() == NULL) {
         return;
 
 /**
  * Releases parser only.
- *
- * @param object the Java ExpatParser instance
- * @param i pointer to the C expat parser
  */
-static void ExpatParser_releaseParser(JNIEnv*, jobject, jint i) {
-    XML_Parser parser = (XML_Parser) i;
-    XML_ParserFree(parser);
+static void ExpatParser_releaseParser(JNIEnv*, jobject, jlong address) {
+  XML_ParserFree(toXMLParser(address));
 }
 
 /**
  * Cleans up after the parser. Called at garbage collection time.
- *
- * @param object the Java ExpatParser instance
- * @param i pointer to the C expat parser
  */
-static void ExpatParser_release(JNIEnv* env, jobject, jint i) {
-    XML_Parser parser = (XML_Parser) i;
+static void ExpatParser_release(JNIEnv* env, jobject, jlong address) {
+  XML_Parser parser = toXMLParser(address);
 
-    ParsingContext* context = toParsingContext(parser);
-    context->env = env;
-    delete context;
+  ParsingContext* context = toParsingContext(parser);
+  context->env = env;
+  delete context;
 
-    XML_ParserFree(parser);
+  XML_ParserFree(parser);
 }
 
-/**
- * Gets the current line.
- *
- * @param object the Java ExpatParser instance
- * @param pointer to the C expat parser
- * @returns current line number
- */
-static int ExpatParser_line(JNIEnv*, jobject, jint pointer) {
-    XML_Parser parser = (XML_Parser) pointer;
-    return XML_GetCurrentLineNumber(parser);
+static int ExpatParser_line(JNIEnv*, jobject, jlong address) {
+  return XML_GetCurrentLineNumber(toXMLParser(address));
 }
 
-/**
- * Gets the current column.
- *
- * @param object the Java ExpatParser instance
- * @param pointer to the C expat parser
- * @returns current column number
- */
-static int ExpatParser_column(JNIEnv*, jobject, jint pointer) {
-    XML_Parser parser = (XML_Parser) pointer;
-    return XML_GetCurrentColumnNumber(parser);
+static int ExpatParser_column(JNIEnv*, jobject, jlong address) {
+  return XML_GetCurrentColumnNumber(toXMLParser(address));
 }
 
 /**
  * Gets the URI of the attribute at the given index.
  *
- * @param object Java ExpatParser instance
- * @param pointer to the C expat parser
  * @param attributePointer to the attribute array
  * @param index of the attribute
  * @returns interned Java string containing attribute's URI
  */
-static jstring ExpatAttributes_getURI(JNIEnv* env, jobject, jint pointer,
-        jint attributePointer, jint index) {
-    XML_Parser parser = (XML_Parser) pointer;
-    ParsingContext* context = toParsingContext(parser);
-    return ExpatElementName(env, context, attributePointer, index).uri();
+static jstring ExpatAttributes_getURI(JNIEnv* env, jobject, jlong address,
+        jlong attributePointer, jint index) {
+  ParsingContext* context = toParsingContext(toXMLParser(address));
+  return ExpatElementName(env, context, attributePointer, index).uri();
 }
 
 /**
  * Gets the local name of the attribute at the given index.
  *
- * @param object Java ExpatParser instance
- * @param pointer to the C expat parser
  * @param attributePointer to the attribute array
  * @param index of the attribute
  * @returns interned Java string containing attribute's local name
  */
-static jstring ExpatAttributes_getLocalName(JNIEnv* env, jobject, jint pointer,
-        jint attributePointer, jint index) {
-    XML_Parser parser = (XML_Parser) pointer;
-    ParsingContext* context = toParsingContext(parser);
-    return ExpatElementName(env, context, attributePointer, index).localName();
+static jstring ExpatAttributes_getLocalName(JNIEnv* env, jobject, jlong address,
+        jlong attributePointer, jint index) {
+  ParsingContext* context = toParsingContext(toXMLParser(address));
+  return ExpatElementName(env, context, attributePointer, index).localName();
 }
 
 /**
  * Gets the qualified name of the attribute at the given index.
  *
- * @param object Java ExpatParser instance
- * @param pointer to the C expat parser
  * @param attributePointer to the attribute array
  * @param index of the attribute
  * @returns interned Java string containing attribute's local name
  */
-static jstring ExpatAttributes_getQName(JNIEnv* env, jobject, jint pointer,
-        jint attributePointer, jint index) {
-    XML_Parser parser = (XML_Parser) pointer;
-    ParsingContext* context = toParsingContext(parser);
-    return ExpatElementName(env, context, attributePointer, index).qName();
+static jstring ExpatAttributes_getQName(JNIEnv* env, jobject, jlong address,
+        jlong attributePointer, jint index) {
+  ParsingContext* context = toParsingContext(toXMLParser(address));
+  return ExpatElementName(env, context, attributePointer, index).qName();
 }
 
 /**
  * @returns Java string containing attribute's value
  */
 static jstring ExpatAttributes_getValueByIndex(JNIEnv* env, jobject,
-        jint attributePointer, jint index) {
+        jlong attributePointer, jint index) {
     const char** attributes = toAttributes(attributePointer);
     const char* value = attributes[(index * 2) + 1];
     return env->NewStringUTF(value);
  *  found
  */
 static jint ExpatAttributes_getIndexForQName(JNIEnv* env, jobject,
-        jint attributePointer, jstring qName) {
+        jlong attributePointer, jstring qName) {
     ScopedUtfChars qNameBytes(env, qName);
     if (qNameBytes.c_str() == NULL) {
         return -1;
  * @returns index of attribute with the given uri and local name or -1 if not
  *  found
  */
-static jint ExpatAttributes_getIndex(JNIEnv* env, jobject, jint attributePointer,
+static jint ExpatAttributes_getIndex(JNIEnv* env, jobject, jlong attributePointer,
         jstring uri, jstring localName) {
     ScopedUtfChars uriBytes(env, uri);
     if (uriBytes.c_str() == NULL) {
  *  found
  */
 static jstring ExpatAttributes_getValueForQName(JNIEnv* env, jobject clazz,
-        jint attributePointer, jstring qName) {
+        jlong attributePointer, jstring qName) {
     jint index = ExpatAttributes_getIndexForQName(env, clazz, attributePointer, qName);
     return index == -1 ? NULL
             : ExpatAttributes_getValueByIndex(env, clazz, attributePointer, index);
  *  found
  */
 static jstring ExpatAttributes_getValue(JNIEnv* env, jobject clazz,
-        jint attributePointer, jstring uri, jstring localName) {
+        jlong attributePointer, jstring uri, jstring localName) {
     jint index = ExpatAttributes_getIndex(env, clazz, attributePointer, uri, localName);
     return index == -1 ? NULL
             : ExpatAttributes_getValueByIndex(env, clazz, attributePointer, index);
  * @param address char** to clone
  * @param count number of attributes
  */
-static jint ExpatParser_cloneAttributes(JNIEnv* env, jobject, jint address, jint count) {
+static jlong ExpatParser_cloneAttributes(JNIEnv* env, jobject, jlong address, jint count) {
     const char** source = reinterpret_cast<const char**>(static_cast<uintptr_t>(address));
     count *= 2;
 
         destinationString += stringLength + 1;
     }
 
-    return static_cast<jint>(reinterpret_cast<uintptr_t>(buffer));
+    return reinterpret_cast<uintptr_t>(buffer);
 }
 
 /**
  * Frees cloned attributes.
  */
-static void ExpatAttributes_freeAttributes(JNIEnv*, jobject, jint pointer) {
+static void ExpatAttributes_freeAttributes(JNIEnv*, jobject, jlong pointer) {
     delete[] reinterpret_cast<char*>(static_cast<uintptr_t>(pointer));
 }
 
     if (internMethod == NULL) return;
 
     // Reference to "".
-    emptyString = (jstring) env->NewGlobalRef(empty);
+    emptyString = reinterpret_cast<jstring>(env->NewGlobalRef(empty));
 }
 
 static JNINativeMethod parserMethods[] = {
-    NATIVE_METHOD(ExpatParser, appendString, "(ILjava/lang/String;Z)V"),
-    NATIVE_METHOD(ExpatParser, appendBytes, "(I[BII)V"),
-    NATIVE_METHOD(ExpatParser, appendChars, "(I[CII)V"),
-    NATIVE_METHOD(ExpatParser, cloneAttributes, "(II)I"),
-    NATIVE_METHOD(ExpatParser, column, "(I)I"),
-    NATIVE_METHOD(ExpatParser, createEntityParser, "(ILjava/lang/String;)I"),
-    NATIVE_METHOD(ExpatParser, initialize, "(Ljava/lang/String;Z)I"),
-    NATIVE_METHOD(ExpatParser, line, "(I)I"),
-    NATIVE_METHOD(ExpatParser, release, "(I)V"),
-    NATIVE_METHOD(ExpatParser, releaseParser, "(I)V"),
+    NATIVE_METHOD(ExpatParser, appendString, "(JLjava/lang/String;Z)V"),
+    NATIVE_METHOD(ExpatParser, appendBytes, "(J[BII)V"),
+    NATIVE_METHOD(ExpatParser, appendChars, "(J[CII)V"),
+    NATIVE_METHOD(ExpatParser, cloneAttributes, "(JI)J"),
+    NATIVE_METHOD(ExpatParser, column, "(J)I"),
+    NATIVE_METHOD(ExpatParser, createEntityParser, "(JLjava/lang/String;)J"),
+    NATIVE_METHOD(ExpatParser, initialize, "(Ljava/lang/String;Z)J"),
+    NATIVE_METHOD(ExpatParser, line, "(J)I"),
+    NATIVE_METHOD(ExpatParser, release, "(J)V"),
+    NATIVE_METHOD(ExpatParser, releaseParser, "(J)V"),
     NATIVE_METHOD(ExpatParser, staticInitialize, "(Ljava/lang/String;)V"),
 };
 
 static JNINativeMethod attributeMethods[] = {
-    NATIVE_METHOD(ExpatAttributes, freeAttributes, "(I)V"),
-    NATIVE_METHOD(ExpatAttributes, getIndexForQName, "(ILjava/lang/String;)I"),
-    NATIVE_METHOD(ExpatAttributes, getIndex, "(ILjava/lang/String;Ljava/lang/String;)I"),
-    NATIVE_METHOD(ExpatAttributes, getLocalName, "(III)Ljava/lang/String;"),
-    NATIVE_METHOD(ExpatAttributes, getQName, "(III)Ljava/lang/String;"),
-    NATIVE_METHOD(ExpatAttributes, getURI, "(III)Ljava/lang/String;"),
-    NATIVE_METHOD(ExpatAttributes, getValueByIndex, "(II)Ljava/lang/String;"),
-    NATIVE_METHOD(ExpatAttributes, getValueForQName, "(ILjava/lang/String;)Ljava/lang/String;"),
-    NATIVE_METHOD(ExpatAttributes, getValue, "(ILjava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
+    NATIVE_METHOD(ExpatAttributes, freeAttributes, "(J)V"),
+    NATIVE_METHOD(ExpatAttributes, getIndexForQName, "(JLjava/lang/String;)I"),
+    NATIVE_METHOD(ExpatAttributes, getIndex, "(JLjava/lang/String;Ljava/lang/String;)I"),
+    NATIVE_METHOD(ExpatAttributes, getLocalName, "(JJI)Ljava/lang/String;"),
+    NATIVE_METHOD(ExpatAttributes, getQName, "(JJI)Ljava/lang/String;"),
+    NATIVE_METHOD(ExpatAttributes, getURI, "(JJI)Ljava/lang/String;"),
+    NATIVE_METHOD(ExpatAttributes, getValueByIndex, "(JI)Ljava/lang/String;"),
+    NATIVE_METHOD(ExpatAttributes, getValueForQName, "(JLjava/lang/String;)Ljava/lang/String;"),
+    NATIVE_METHOD(ExpatAttributes, getValue, "(JLjava/lang/String;Ljava/lang/String;)Ljava/lang/String;"),
 };
 void register_org_apache_harmony_xml_ExpatParser(JNIEnv* env) {
     jniRegisterNativeMethods(env, "org/apache/harmony/xml/ExpatParser", parserMethods, NELEM(parserMethods));