📄 class.c
字号:
fprintf(stdout, "Instance %lx contents:\n", (long)thisInstance); printClassName((CLASS)thisClass); for (i = 0; i < instSize; i++) { long contents = thisInstance->data[i].cell; fprintf(stdout, "Field %d: %lx (%ld)\n", i, contents, contents); }}#endif /* INCLUDEDEBUGCODE *//*========================================================================= * Operations on array instances *=======================================================================*//*========================================================================= * FUNCTION: instantiateArray() * TYPE: constructor * OVERVIEW: Create an array of given type and size. * INTERFACE: * parameters: array class, array size * returns: pointer to array object instance * NOTE: This operation only allocates the space for the * array and initializes its slots to zero. * Actual constructor for initializing the array * must be invoked separately. *=======================================================================*/ARRAY instantiateArray(ARRAY_CLASS arrayClass, long length){ if (length < 0) { raiseException(NegativeArraySizeException); return NULL; } else if (length > 0x1000000) { /* Don't even try. dataSize above may have overflowed */ throwException(&OutOfMemoryObject); return NULL; } else { /* Does this array contain pointers or not? */ GCT_ObjectType gctype = arrayClass->gcType; /* How big is each item */ long slotSize = arrayClass->itemSize; /* What's the total data size */ long dataSize = (length * slotSize + (CELL - 1)) >> log2CELL; /* What's the total size */ long arraySize = SIZEOF_ARRAY(dataSize); ARRAY newArray; newArray = (ARRAY)mallocHeapObject(arraySize, gctype); if (newArray == NULL) { throwException(&OutOfMemoryObject); } else { memset(newArray, 0, arraySize << log2CELL); newArray->ofClass = arrayClass; newArray->length = length; } return newArray; }}/*========================================================================= * FUNCTION: instantiateMultiArray() * TYPE: constructor * OVERVIEW: Create an n-dimensional array (recursively). * INTERFACE: * parameters: array type index, integer array containing the * different dimensions, number of dimensions * returns: pointer to array object instance *=======================================================================*/ARRAYinstantiateMultiArray(ARRAY_CLASS arrayClass, long* lengthsArg, int dimensions){ long currDepth; ARRAY result; for (currDepth = 0; currDepth < dimensions; currDepth++) { long dim = lengthsArg[currDepth]; if (dim < 0) { raiseException(NegativeArraySizeException); return NIL; } } START_TEMPORARY_ROOTS STACK stack = getFP()->stack; /* volatile */ /* lengthsArg is pointing into the stack */ DECLARE_TEMPORARY_ROOT_FROM_BASE(long*, lengths, lengthsArg, stack); DECLARE_TEMPORARY_ROOT(ARRAY, rootArray, instantiateArray(getArrayClass(1, JavaLangObject, 0), 1)); DECLARE_TEMPORARY_ROOT(ARRAY, prevArraySet, NULL); DECLARE_TEMPORARY_ROOT(ARRAY, currArraySet, rootArray); DECLARE_TEMPORARY_ROOT(ARRAY, prevArray, NULL); DECLARE_TEMPORARY_ROOT(ARRAY, currArray, NULL); long prevArrayWidth, currArrayWidth; CLASS currClass; if (rootArray == NULL) { /* Out of memory exception has already been thrown */ return NULL; } currArrayWidth = 1; /* We have rootArray acting as the previous level to level 0. * It has only one slot, and the allocator has already set it to 0, * so it will appear as a linked list with one node alone. * * Now go through all the lengths and do the work */ for (currDepth = 0, currClass = (CLASS)arrayClass; ; currDepth++, currClass = ((ARRAY_CLASS)currClass)->u.elemClass) { bool_t lastIteration; prevArraySet = currArraySet; prevArrayWidth = currArrayWidth; currArraySet = NULL; currArrayWidth = lengths[currDepth]; lastIteration = (currDepth == dimensions - 1 || currArrayWidth == 0); /* Go through each of the previous level's nodes, and allocate * and link the current nodes, if needed. Fill them in the slots * of the previous level's nodes. */ do { long index; /* Get an item from the chain, and advance the chain. */ prevArray = prevArraySet; prevArraySet = (ARRAY)prevArraySet->data[0].cellp; /* Fill in each index of prevArray */ for (index = 0; index < prevArrayWidth; index++) { /* Allocate the node, and fill in the parent's slot. */ currArray = instantiateArray((ARRAY_CLASS)currClass, currArrayWidth); if (currArray == NULL) { /* OutOfMemoryError has already been thrown */ rootArray->data[0].cellp = NULL; goto done; } prevArray->data[index].cellp = (cell *)currArray; if (!lastIteration) { /* Link it in, for the next time through the loop. */ currArray->data[0].cellp = (cell *)currArraySet; currArraySet = currArray; } } } while (prevArraySet != NULL); if (lastIteration) { break; } } done: result = (ARRAY)rootArray->data[0].cellp; END_TEMPORARY_ROOTS return result;}/*========================================================================= * FUNCTION: arrayItemSize() * TYPE: private helper operation for array manipulation * OVERVIEW: Get the item size of the array on the basis of its type. * INTERFACE: * parameters: array type index * returns: array item size *=======================================================================*/long arrayItemSize(int arrayType){ long itemSize; switch (arrayType) { case T_BOOLEAN: case T_BYTE: itemSize = 1; break; case T_CHAR: case T_SHORT: itemSize = SHORTSIZE; break; case T_INT: case T_FLOAT: itemSize = CELL; break; case T_DOUBLE: case T_LONG: itemSize = CELL*2; break; default: /* Array of reference type (T_REFERENCE or class pointer) */ itemSize = CELL; break; } return itemSize;}/*========================================================================= * FUNCTION: printArray() * TYPE: public instance-level operation * OVERVIEW: Print the contents of an array object instance * for debugging purposes. * INTERFACE: * parameters: array object pointer * returns: <nothing> *=======================================================================*/#if INCLUDEDEBUGCODEvoid printArray(ARRAY thisArray){ ARRAY_CLASS thisClass = thisArray->ofClass; if (!IS_ARRAY_CLASS(thisClass)) { fprintf(stdout, "Object %lx is not an array\n", (long)thisArray); return; } fprintf(stdout, "Array %lx contents:\n", (long)thisArray); printClassName((CLASS)thisClass); fprintf(stdout, "Length: %ld\n", thisArray->length); if (thisClass->gcType == GCT_ARRAY) { int length, i; if (thisClass->u.primType == T_CHAR) { SHORTARRAY shortArray = (SHORTARRAY)thisArray; fprintf(stdout, "Contents....: '\n"); length = (thisArray->length < 40) ? thisArray->length : 40; for (i = 0; i < length; i++) { putchar((char)shortArray->sdata[i]); } fprintf(stdout, "'\n"); } }}#endif /* INCLUDEDEBUGCODE *//*========================================================================= * Operations on string instances *=======================================================================*//*========================================================================= * FUNCTION: createCharArray() * TYPE: constructor (internal use only) * OVERVIEW: Create a character array. Handle Utf8 input properly. *=======================================================================*/static SHORTARRAY createCharArray(const char *utf8stringArg, int utf8length, int *unicodelengthP, bool_t isPermanent){ int unicodelength = 0; int i; SHORTARRAY newArray; const char *p, *end; int size, objSize; START_TEMPORARY_ROOTS DECLARE_TEMPORARY_ROOT(const char *, utf8string, utf8stringArg); for (p = utf8string, end = p + utf8length; p < end; ) { utf2unicode(&p); unicodelength++; } size = (unicodelength * sizeof(short) + CELL - 1) >> log2CELL; objSize = SIZEOF_ARRAY(size); /* Allocate room for the character array; */ newArray = isPermanent ? (SHORTARRAY)callocPermanentObject(objSize) : (SHORTARRAY)callocObject(objSize, GCT_ARRAY); newArray->ofClass = PrimitiveArrayClasses[T_CHAR]; newArray->length = unicodelength; /* Initialize the array with string contents; */ for (p = utf8string, i = 0; i < unicodelength; i++) { newArray->sdata[i] = utf2unicode(&p); } *unicodelengthP = unicodelength; END_TEMPORARY_ROOTS return newArray;}/*========================================================================= * FUNCTION: instantiateString(), instantiateInternedString() * TYPE: constructor * OVERVIEW: Create an initialized Java-level string object. * INTERFACE: * parameters: UTF8 String containing the value of the new string object * number of bytes in the UTF8 string * returns: pointer to array object instance * NOTE: String arrays are not intended to be manipulated * directly; they are manipulated from within instances * of 'java.lang.String'. *=======================================================================*/STRING_INSTANCEinstantiateString(const char *stringArg, int utflength){ int unicodelength; STRING_INSTANCE result; START_TEMPORARY_ROOTS DECLARE_TEMPORARY_ROOT(const char *, string, stringArg); DECLARE_TEMPORARY_ROOT(SHORTARRAY, chars, createCharArray(string, utflength, &unicodelength, FALSE)); result = (STRING_INSTANCE)instantiate(JavaLangString); /* We can't do any garbage collection, since result isn't protected */ result->offset = 0; result->length = unicodelength; result->array = chars; END_TEMPORARY_ROOTS return result;}INTERNED_STRING_INSTANCEinstantiateInternedString(const char *stringArg, int utflength){ int unicodelength; INTERNED_STRING_INSTANCE result; START_TEMPORARY_ROOTS DECLARE_TEMPORARY_ROOT(const char *, string, stringArg); SHORTARRAY chars = createCharArray(string, utflength, &unicodelength, TRUE); result = (INTERNED_STRING_INSTANCE) callocPermanentObject(SIZEOF_INTERNED_STRING_INSTANCE); /* We can't do any garbage collection, since result isn't protected */ result->ofClass = JavaLangString; result->offset = 0; result->length = unicodelength; result->array = chars; END_TEMPORARY_ROOTS return result;}/*========================================================================= * FUNCTION: getStringContents() * TYPE: internal operation on string objects * OVERVIEW: Get the contents of a string object in C/C++ * string format. * INTERFACE: * parameters: String object pointer * returns: char* to the string in C/C++ format * * NOTE: This operation uses an internal string buffer * that is shared and has a fixed length. Thus, * the contents of the returned string may (will) * change over time. *=======================================================================*/char* getStringContents(STRING_INSTANCE string){ return getStringContentsSafely(string, str_buffer, STRINGBUFFERSIZE);}/*========================================================================= * FUNCTION: getStringContentsSafely() * TYPE: internal operation on string objects * OVERVIEW: Get the contents of a string object in C/C++ * string format. * INTERFACE: * parameters: String object pointer * returns: char* to the string in C/C++ format *=======================================================================*/char* getStringContentsSafely(STRING_INSTANCE string, char *buf, int lth){ /* Get the internal unicode array containing the string */ SHORTARRAY thisArray = string->array; int offset = string->offset; int length = string->length; int i; if ((length+1) > lth) { fatalError(KVM_MSG_STRINGBUFFER_OVERFLOW); } /* Copy contents of the unicode array to the C/C++ string */ for (i = 0; i < length; i++) { buf[i] = (char)thisArray->sdata[offset + i]; } /* Terminating C/C++ string with zero */ buf[length] = 0; return buf;}#if INCLUDEDEBUGCODEvoidprintString(INSTANCE string){ SHORTARRAY data = (SHORTARRAY)string->data[0].cellp; int offset = string->data[1].cell; int length = string->data[2].cell; int i; putchar('"'); for (i = 0; i < length; i++) { putchar((char)data->sdata[offset + i]); } putchar('"'); putchar('\n'); }#endif /* INCLUDEDEBUGCODE */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -