Skip to content
Browse files

Make some of the StringObject functions member functions.

Change-Id: I72ed13c16f0cb24498772c453ba268a0f65f208a
  • Loading branch information...
1 parent 837eabb commit d8a3f9fa1951e552f5f65c2914689083cc0c46c2 @enh enh committed Jun 17, 2011
Showing with 70 additions and 85 deletions.
  1. +2 −2 vm/CheckJni.cpp
  2. +14 −9 vm/Ddm.cpp
  3. +24 −18 vm/Jni.cpp
  4. +1 −0 vm/ReferenceTable.cpp
  5. +13 −34 vm/UtfString.cpp
  6. +1 −22 vm/UtfString.h
  7. +15 −0 vm/oo/Object.h
View
4 vm/CheckJni.cpp
@@ -1569,7 +1569,7 @@ static const jchar* Check_GetStringChars(JNIEnv* env, jstring string, jboolean*
if (gDvmJni.forceCopy && result != NULL) {
ScopedJniThreadState ts(env);
StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, string);
- int byteCount = dvmStringLen(strObj) * 2;
+ int byteCount = strObj->length() * 2;
result = (const jchar*) GuardedCopy::create(result, byteCount, false);
if (isCopy != NULL) {
*isCopy = JNI_TRUE;
@@ -1832,7 +1832,7 @@ static const jchar* Check_GetStringCritical(JNIEnv* env, jstring string, jboolea
if (gDvmJni.forceCopy && result != NULL) {
ScopedJniThreadState ts(env);
StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, string);
- int byteCount = dvmStringLen(strObj) * 2;
+ int byteCount = strObj->length() * 2;
result = (const jchar*) GuardedCopy::create(result, byteCount, false);
if (isCopy != NULL) {
*isCopy = JNI_TRUE;
View
23 vm/Ddm.cpp
@@ -246,8 +246,9 @@ void dvmDdmSetThreadNotification(bool enable)
*/
void dvmDdmSendThreadNotification(Thread* thread, bool started)
{
- if (!gDvm.ddmThreadNotification)
+ if (!gDvm.ddmThreadNotification) {
return;
+ }
StringObject* nameObj = NULL;
Object* threadObj = thread->threadObj;
@@ -268,25 +269,27 @@ void dvmDdmSendThreadNotification(Thread* thread, bool started)
type = CHUNK_TYPE("THCR");
if (nameObj != NULL) {
- stringLen = dvmStringLen(nameObj);
- chars = dvmStringChars(nameObj);
+ stringLen = nameObj->length();
+ chars = nameObj->chars();
} else {
stringLen = 0;
chars = NULL;
}
/* leave room for the two integer fields */
- if (stringLen > (sizeof(buf) - sizeof(u4)*2) / 2)
+ if (stringLen > (sizeof(buf) - sizeof(u4)*2) / 2) {
stringLen = (sizeof(buf) - sizeof(u4)*2) / 2;
+ }
len = stringLen*2 + sizeof(u4)*2;
set4BE(&buf[0x00], thread->threadId);
set4BE(&buf[0x04], stringLen);
/* copy the UTF-16 string, transforming to big-endian */
outChars = (u2*)(void*)&buf[0x08];
- while (stringLen--)
+ while (stringLen--) {
set2BE((u1*) (outChars++), *chars++);
+ }
} else {
type = CHUNK_TYPE("THDE");
@@ -303,11 +306,12 @@ void dvmDdmSendThreadNotification(Thread* thread, bool started)
*/
void dvmDdmSendThreadNameChange(int threadId, StringObject* newName)
{
- if (!gDvm.ddmThreadNotification)
+ if (!gDvm.ddmThreadNotification) {
return;
+ }
- size_t stringLen = dvmStringLen(newName);
- const u2* chars = dvmStringChars(newName);
+ size_t stringLen = newName->length();
+ const u2* chars = newName->chars();
/*
* Output format:
@@ -321,8 +325,9 @@ void dvmDdmSendThreadNameChange(int threadId, StringObject* newName)
set4BE(&buf[0x00], threadId);
set4BE(&buf[0x04], stringLen);
u2* outChars = (u2*)(void*)&buf[0x08];
- while (stringLen--)
+ while (stringLen--) {
set2BE((u1*) (outChars++), *chars++);
+ }
dvmDbgDdmSendChunk(CHUNK_TYPE("THNM"), bufLen, buf);
}
View
42 vm/Jni.cpp
@@ -389,11 +389,14 @@ static jobject addLocalReference(JNIEnv* env, Object* obj) {
dvmDumpThread(dvmThreadSelf(), false);
dvmAbort(); // spec says call FatalError; this is equivalent
} else {
- LOGVV("LREF add %p (%s.%s) (ent=%d)", obj,
- dvmGetCurrentJNIMethod()->clazz->descriptor,
- dvmGetCurrentJNIMethod()->name,
- (int) dvmReferenceTableEntries(pRefTable));
+ if (false) {
+ LOGI("LREF add %p (%s.%s) (ent=%d)", obj,
+ dvmGetCurrentJNIMethod()->clazz->descriptor,
+ dvmGetCurrentJNIMethod()->name,
+ (int) dvmIndirectRefTableEntries(pRefTable));
+ }
}
+
return jobj;
}
@@ -2147,7 +2150,7 @@ static jstring NewString(JNIEnv* env, const jchar* unicodeChars, jsize len) {
static jsize GetStringLength(JNIEnv* env, jstring jstr) {
ScopedJniThreadState ts(env);
StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr);
- return dvmStringLen(strObj);
+ return strObj->length();
}
@@ -2161,11 +2164,11 @@ static const jchar* GetStringChars(JNIEnv* env, jstring jstr, jboolean* isCopy)
ScopedJniThreadState ts(env);
StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr);
- ArrayObject* strChars = dvmStringCharArray(strObj);
+ ArrayObject* strChars = strObj->array();
pinPrimitiveArray(strChars);
- const u2* data = dvmStringChars(strObj);
+ const u2* data = strObj->chars();
if (isCopy != NULL) {
*isCopy = JNI_FALSE;
}
@@ -2178,7 +2181,7 @@ static const jchar* GetStringChars(JNIEnv* env, jstring jstr, jboolean* isCopy)
static void ReleaseStringChars(JNIEnv* env, jstring jstr, const jchar* chars) {
ScopedJniThreadState ts(env);
StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr);
- ArrayObject* strChars = dvmStringCharArray(strObj);
+ ArrayObject* strChars = strObj->array();
unpinPrimitiveArray(strChars);
}
@@ -2206,7 +2209,10 @@ static jstring NewStringUTF(JNIEnv* env, const char* bytes) {
static jsize GetStringUTFLength(JNIEnv* env, jstring jstr) {
ScopedJniThreadState ts(env);
StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr);
- return dvmStringUtf8ByteLen(strObj);
+ if (strObj == NULL) {
+ return 0; // Should we throw something or assert?
+ }
+ return strObj->utfLength();
}
/*
@@ -2583,12 +2589,12 @@ static jint GetJavaVM(JNIEnv* env, JavaVM** vm) {
static void GetStringRegion(JNIEnv* env, jstring jstr, jsize start, jsize len, jchar* buf) {
ScopedJniThreadState ts(env);
StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr);
- int strLen = dvmStringLen(strObj);
- if (((start|len) < 0) || (start + len > dvmStringLen(strObj))) {
+ int strLen = strObj->length();
+ if (((start|len) < 0) || (start + len > strLen)) {
dvmThrowStringIndexOutOfBoundsExceptionWithRegion(strLen, start, len);
return;
}
- memcpy(buf, dvmStringChars(strObj) + start, len * sizeof(u2));
+ memcpy(buf, strObj->chars() + start, len * sizeof(u2));
}
/*
@@ -2598,12 +2604,12 @@ static void GetStringRegion(JNIEnv* env, jstring jstr, jsize start, jsize len, j
static void GetStringUTFRegion(JNIEnv* env, jstring jstr, jsize start, jsize len, char* buf) {
ScopedJniThreadState ts(env);
StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr);
- int strLen = dvmStringLen(strObj);
- if (((start|len) < 0) || (start + len > dvmStringLen(strObj))) {
+ int strLen = strObj->length();
+ if (((start|len) < 0) || (start + len > strLen)) {
dvmThrowStringIndexOutOfBoundsExceptionWithRegion(strLen, start, len);
return;
}
- dvmCreateCstrFromStringRegion(strObj, start, len, buf);
+ dvmGetStringUtfRegion(strObj, start, len, buf);
}
/*
@@ -2643,11 +2649,11 @@ static const jchar* GetStringCritical(JNIEnv* env, jstring jstr, jboolean* isCop
ScopedJniThreadState ts(env);
StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr);
- ArrayObject* strChars = dvmStringCharArray(strObj);
+ ArrayObject* strChars = strObj->array();
pinPrimitiveArray(strChars);
- const u2* data = dvmStringChars(strObj);
+ const u2* data = strObj->chars();
if (isCopy != NULL) {
*isCopy = JNI_FALSE;
}
@@ -2660,7 +2666,7 @@ static const jchar* GetStringCritical(JNIEnv* env, jstring jstr, jboolean* isCop
static void ReleaseStringCritical(JNIEnv* env, jstring jstr, const jchar* carray) {
ScopedJniThreadState ts(env);
StringObject* strObj = (StringObject*) dvmDecodeIndirectRef(env, jstr);
- ArrayObject* strChars = dvmStringCharArray(strObj);
+ ArrayObject* strChars = strObj->array();
unpinPrimitiveArray(strChars);
}
View
1 vm/ReferenceTable.cpp
@@ -284,6 +284,7 @@ void dvmDumpReferenceTableContents(Object* const* refs, size_t count,
extras += "\"";
} else {
StringAppendF(&extras, "... (%d chars)", dvmStringLen(str));
+ StringAppendF(&extras, "... (%d chars)", str->length());
}
free(s);
}
View
47 vm/UtfString.cpp
@@ -305,63 +305,42 @@ char* dvmCreateCstrFromString(const StringObject* jstr)
return newStr;
}
-/*
- * Create a UTF-8 C string from a region of a java/lang/String. (Used by
- * the JNI GetStringUTFRegion call.)
- */
-void dvmCreateCstrFromStringRegion(const StringObject* jstr,
+void dvmGetStringUtfRegion(const StringObject* jstr,
int start, int len, char* buf)
{
- const u2* data = dvmStringChars(jstr) + start;
+ const u2* data = jstr->chars() + start;
convertUtf16ToUtf8(buf, data, len);
}
-/*
- * Compute the length, in modified UTF-8, of a java/lang/String object.
- *
- * Does not include the terminating null byte.
- */
-int dvmStringUtf8ByteLen(const StringObject* jstr)
+int StringObject::utfLength() const
{
assert(gDvm.classJavaLangString != NULL);
- if (jstr == NULL) {
- return 0; // should we throw something? assert?
- }
- int len = dvmGetFieldInt(jstr, STRING_FIELDOFF_COUNT);
- int offset = dvmGetFieldInt(jstr, STRING_FIELDOFF_OFFSET);
+ int len = dvmGetFieldInt(this, STRING_FIELDOFF_COUNT);
+ int offset = dvmGetFieldInt(this, STRING_FIELDOFF_OFFSET);
ArrayObject* chars =
- (ArrayObject*) dvmGetFieldObject(jstr, STRING_FIELDOFF_VALUE);
+ (ArrayObject*) dvmGetFieldObject(this, STRING_FIELDOFF_VALUE);
const u2* data = (const u2*)(void*)chars->contents + offset;
assert(offset + len <= (int) chars->length);
return utf16_utf8ByteLen(data, len);
}
-/*
- * Get the string's length.
- */
-int dvmStringLen(const StringObject* jstr)
+int StringObject::length() const
{
- return dvmGetFieldInt(jstr, STRING_FIELDOFF_COUNT);
+ return dvmGetFieldInt(this, STRING_FIELDOFF_COUNT);
}
-/*
- * Get the char[] object from the String.
- */
-ArrayObject* dvmStringCharArray(const StringObject* jstr)
+ArrayObject* StringObject::array() const
{
- return (ArrayObject*) dvmGetFieldObject(jstr, STRING_FIELDOFF_VALUE);
+ return (ArrayObject*) dvmGetFieldObject(this, STRING_FIELDOFF_VALUE);
}
-/*
- * Get the string's data.
- */
-const u2* dvmStringChars(const StringObject* jstr)
+const u2* StringObject::chars() const
{
- int offset = dvmGetFieldInt(jstr, STRING_FIELDOFF_OFFSET);
+ int offset = dvmGetFieldInt(this, STRING_FIELDOFF_OFFSET);
ArrayObject* chars =
- (ArrayObject*) dvmGetFieldObject(jstr, STRING_FIELDOFF_VALUE);
+ (ArrayObject*) dvmGetFieldObject(this, STRING_FIELDOFF_VALUE);
return (const u2*)(void*)chars->contents + offset;
}
View
23 vm/UtfString.h
@@ -129,31 +129,10 @@ char* dvmCreateCstrFromString(const StringObject* jstr);
* Create a UTF-8 C string from a region of a java/lang/String. (Used by
* the JNI GetStringUTFRegion call.)
*/
-void dvmCreateCstrFromStringRegion(const StringObject* jstr,
+void dvmGetStringUtfRegion(const StringObject* jstr,
int start, int len, char* buf);
/*
- * Compute the length in bytes of the modified UTF-8 representation of a
- * string.
- */
-int dvmStringUtf8ByteLen(const StringObject* jstr);
-
-/*
- * Get the length in Unicode characters of a string.
- */
-int dvmStringLen(const StringObject* jstr);
-
-/*
- * Get the char[] object from the String.
- */
-ArrayObject* dvmStringCharArray(const StringObject* jstr);
-
-/*
- * Get a pointer to the Unicode data.
- */
-const u2* dvmStringChars(const StringObject* jstr);
-
-/*
* Compare two string objects. (This is a dvmHashTableLookup() callback.)
*/
int dvmHashcmpStrings(const void* vstrObj1, const void* vstrObj2);
View
15 vm/oo/Object.h
@@ -245,6 +245,21 @@ struct DataObject : Object {
struct StringObject : Object {
/* variable #of u4 slots; u8 uses 2 slots */
u4 instanceData[1];
+
+ /** Returns this string's length in characters. */
+ int length() const;
+
+ /**
+ * Returns this string's length in bytes when encoded as modified UTF-8.
+ * Does not include a terminating NUL byte.
+ */
+ int utfLength() const;
+
+ /** Returns this string's char[] as an ArrayObject. */
+ ArrayObject* array() const;
+
+ /** Returns this string's char[] as a u2*. */
+ const u2* chars() const;
};

0 comments on commit d8a3f9f

Please sign in to comment.
Something went wrong with that request. Please try again.