Beispiel #1
0
/**
 * FUNCTION:      JVM_FinalizeMemory()
 * TYPE:          public operation
 * OVERVIEW:      Finalize the JVM memory pool
 * INTERFACE:
 *   parameters:  <none>
 *   returns:     <nothing>
 *                
 */
  void
JVM_FinalizeMemory(void) {
    int count, size, ret;

    ret = jvmEndMemory(&count, &size);

#ifdef REPORT_LEVEL
#if REPORT_LEVEL <= LOG_WARNING
    if (ret > 0) {
        reportToLog(LOG_WARNING, LC_MALLOC, 
		  "WARNING: %d memory leak(s); %d bytes!\n",
		  count, size);
    }
    reportToLog(LOG_WARNING, LC_MALLOC, 
		"** Total memory: %d\n** Highwater mark:%d",
		JVM_GetTotalHeap(), JvmMemoryHighWaterMark);
#endif 
#endif

#ifndef JVM_MALLOC_USE_STATIC
#ifdef JVM_MALLOC_USE_CLDC_HI
    OsMemory_free(JvmMemory);
#else
    free(JvmMemory);
#endif
    JvmMemory = NULL;
#endif

    JvmMemoryStart = NULL;
    JvmMemoryEnd = NULL;
}
/** Repaint custom item upon system notification */
void CustomItem::paintEvent(QPaintEvent *e)
{
#if REPORT_LEVEL <= LOG_INFORMATION
  QRect r(e->rect());
  reportToLog(LOG_INFORMATION, LC_HIGHUI, 
 	      "CustomItem::paintEvent (%d,%d,%d,%d)\n",
 	      r.left(), r.top(), r.right(), r.bottom());
#endif

  Item::paintEvent(e);

    if (hasFocus()) {
      QPainter painter(this);
      QRect focusRect = drawable->geometry();
      
      // focusRect.setWidth(focusRect.width() + 3);
      // focusRect.setHeight(focusRect.height() + 3);

      focusRect.moveBy(-2, -2);
      focusRect.rRight() += 4;
      focusRect.rBottom() += 4;

      style().drawFocusRect(&painter, focusRect, colorGroup());
    }
}
Beispiel #3
0
/* Set countMemoryLeaksOnly = 0 in order to get more verbose information */
 int JVM_MallocDumpMemory(int countMemoryLeaksOnly)
{
    char *localjvmMallocMemPtr = NULL;
    char *localjvmMallocMemStart = JvmMemoryStart;
    char *localjvmMallocMemEnd = JvmMemoryEnd;
    _JvmMemHdrPtr localjvmMallocMemHdr = NULL;

    int numberOfAllocatedBlocks = 0;

    tty->print_cr(
		 "JvmMemory=0x%p JvmMemoryStart=0x%p JvmMemoryEnd=0x%p", 
		 JvmMemory, JvmMemoryStart, JvmMemoryEnd);

    for (localjvmMallocMemPtr = localjvmMallocMemStart; 
        localjvmMallocMemPtr < localjvmMallocMemEnd;
        localjvmMallocMemPtr += localjvmMallocMemHdr->size + sizeof(_JvmMemHdr)) {

        localjvmMallocMemHdr = (_JvmMemHdrPtr) localjvmMallocMemPtr;
        if (localjvmMallocMemHdr->magic != MAGIC) {
            tty->print_cr( 
			  "ERROR: memory corruption at 0x%p", 
			  localjvmMallocMemPtr);
            return -1;
        } else {

            if (countMemoryLeaksOnly == 0) {
                tty->print_cr( 
			     "hdr 0x%p free=%d size=%d address=0x%p",
			     localjvmMallocMemHdr, 
			     localjvmMallocMemHdr->free, 
			     localjvmMallocMemHdr->size,
			     (void *)(((char *)localjvmMallocMemHdr) + 
				      sizeof(_JvmMemHdr)));
            }

            if (localjvmMallocMemHdr->free != 1) {
                numberOfAllocatedBlocks += 1;
#ifdef REPORT_LEVEL
#if REPORT_LEVEL <= LOG_WARNING
		reportToLog(LOG_WARNING, LC_MALLOC, 
			    "WARNING: memory leak: size=%d  address=0x%p",
			    localjvmMallocMemHdr->size, 
			    (void*)((char*)localjvmMallocMemHdr + 
				    sizeof(_JvmMemHdr)));
		printAllocation(LOG_WARNING, "allocated", 
				localjvmMallocMemHdr->filename, 
				localjvmMallocMemHdr->lineno);
#endif
#endif
            }
        }
    }
    return numberOfAllocatedBlocks;
}
/** 
 * Refresh the area specified.
 * @param x relative to drawable
 * @param y relative to drawable
 * @param width width of the area to be updated
 * @param height height of the area to be updated 
 */
MidpError
CustomItem::refresh(int x,
		    int y,
		    int width,
		    int height)
{
#if REPORT_LEVEL <= LOG_INFORMATION
  PlatformMScreen * mscreen = PlatformMScreen::getMScreen();
  reportToLog(LOG_INFORMATION, LC_HIGHUI, 
	      "+ CustomItem::refresh: (%d,%d) (%dx%d)\n",
	      x, y, width, height);
  
  reportToLog(LOG_INFORMATION, LC_HIGHUI, 
	      "+       QWidget::pos() = (%d,%d)  drawable->pos() = (%d,%d)\n",
	      QWidget::pos().x(), QWidget::pos().y(),
	      drawable->pos().x(), drawable->pos().y());
  
  reportToLog(LOG_INFORMATION, LC_HIGHUI, 
	      "+           scroll position = %d\n",
	      mscreen->scrollPosition());

  reportToLog(LOG_INFORMATION, LC_HIGHUI, 
	      "+\t\tbitBlt(\tdst, %d, %d,\n\t\t\tsrc, %d, %d, %d, %d); ",
	      x, // dx
	      y, // dy

	      QWidget::pos().x() + drawable->pos().x() + x,
	      QWidget::pos().y() + drawable->pos().y() + y -
	      mscreen->scrollPosition(),
	      
	      width,
	      height);
#endif
   
  drawable->refresh(x, y, width, height);

  REPORT_INFO(LC_HIGHUI, "bitBlt complete.\n\n");

  return KNI_OK;  
}
/**
 * Send hint to VM about the begin of MIDlet startup phase
 * to allow the VM to fine tune its internal parameters to
 * achieve optimal peformance
 *
 * @param midletIsolateId ID of the started MIDlet isolate
 */
KNIEXPORT KNI_RETURNTYPE_VOID
KNIDECL(com_sun_midp_main_MIDletSuiteUtils_vmBeginStartUp) {
    int midletIsolateId = KNI_GetParameterAsInt(1);
#if !ENABLE_CDC
    JVM_SetHint(midletIsolateId, JVM_HINT_BEGIN_STARTUP_PHASE, 0);
#endif
#if REPORT_LEVEL <= LOG_INFORMATION
    reportToLog(LOG_INFORMATION, LC_AMS,
        "Hint VM about MIDlet startup begin within isolate %d\n",
        midletIsolateId);
#endif
    KNI_ReturnVoid();
}
void DrawableArea::paintEvent(QPaintEvent *e)
{
#if REPORT_LEVEL <= LOG_INFORMATION
  QRect r(e->rect());
  reportToLog(LOG_INFORMATION, LC_HIGHUI, 
 	      "DrawableArea::paintEvent (%d,%d,%d,%d)\n",
 	      r.left(), r.top(), r.right(), r.bottom());
#endif

  if (bodyPix != NULL) {
    const QRect r = e->rect();
    bitBlt( this, r.topLeft(), bodyPix, r);
  }
}
Beispiel #7
0
/**
 * @internal
 *
 * FUNCTION:      jvmEndMemory()
 * TYPE:          private operation
 * OVERVIEW:      Finalize the JVM memory pool
 * INTERFACE:
 *   parameters:  count   address to store memory leak count
 *                size    address to store totol bytes of memory leaked
 *   returns:     the number of memory leaks detected
 *                
 */
 int
jvmEndMemory(int* count, int* size) {
    _JvmMemHdrPtr jvmMemoryHdr;
    char*          jvmMemoryPtr;

    *count = 0;
    *size  = 0;

    for (jvmMemoryPtr = JvmMemoryStart; 
         jvmMemoryPtr < JvmMemoryEnd;
         jvmMemoryPtr += jvmMemoryHdr->size + sizeof(_JvmMemHdr)) {

        jvmMemoryHdr = (_JvmMemHdrPtr)jvmMemoryPtr;

        if (jvmMemoryHdr->magic != MAGIC) {
            tty->print_cr( 
			  "ERROR: memory corruption at 0x%p", 
			  jvmMemoryPtr);
            return -1;
        } else if (jvmMemoryHdr->free != 1) {
#ifdef REPORT_LEVEL
#if REPORT_LEVEL <= LOG_WARNING
	    reportToLog(LOG_WARNING, LC_MALLOC, 
			"WARNING: memory leak: size= %d  address= 0x%p",
			jvmMemoryHdr->size,
			(void*)((char*)jvmMemoryHdr + sizeof(_JvmMemHdr)));
            printAllocation(LOG_WARNING, "allocated", 
                            jvmMemoryHdr->filename, jvmMemoryHdr->lineno);
#endif
#endif
            JVM_Free((void*)((char*)jvmMemoryHdr + sizeof(_JvmMemHdr)));
            *count += 1;
            *size  += jvmMemoryHdr->size;
        }
    }
    return *count;
}
MidpProperties mf_main(char* mfbuf, int mflength) {

    MidpProperties mfsmp      = {0, ALL_OK, NULL};
    jchar* save_jchar_buffer  = NULL;
    jchar* jchar_buffer       = NULL;
    int jbufsize              = -1;
#if REPORT_LEVEL <= LOG_INFORMATION
    int res                   = 0;
#endif
    if ((mflength <= 0) || (!mfbuf)) {
        mfsmp.status = OUT_OF_MEMORY;
        return mfsmp;
    }

    jbufsize = mflength * sizeof(jchar);

    jchar_buffer = (jchar*)midpMalloc(jbufsize+2);
    if (!jchar_buffer) {
        midpFree(mfbuf);
        mfsmp.status = OUT_OF_MEMORY;
        return mfsmp;
    }
    memset(jchar_buffer,0,(jbufsize + 2));

    convertChar2JChar(mfbuf,jchar_buffer,mflength);

    midpFree(mfbuf);

    save_jchar_buffer = jchar_buffer;

    REPORT_INFO(LC_AMS,
		"#########################  Start of manifest parsing");

    /* during execution of this, jchar_buffer pointer will be changed */
    mfsmp = midpParseMf(jchar_buffer);
    midpFree(save_jchar_buffer);
    switch (mfsmp.status) {

    case NO_SUITE_NAME_PROP:
        midp_free_properties(&mfsmp);
        return mfsmp;

    case NO_SUITE_VENDOR_PROP:
        midp_free_properties(&mfsmp);
        return mfsmp;

    case NO_SUITE_VERSION_PROP:
        midp_free_properties(&mfsmp);
        return mfsmp;

    case NO_MIDLET_ONE_PROP:
        midp_free_properties(&mfsmp);
        return mfsmp;

    case NO_MICROEDITION_PROFILE_PROP:
        midp_free_properties(&mfsmp);
        return mfsmp;

    case NO_MICROEDITION_CONFIGURATION_PROP:
        REPORT_INFO1(LC_AMS,
		     "Manifest MUST property missing %d", mfsmp.status);
        return mfsmp;

    case BAD_PARAMS:
    case BAD_MF_KEY:
    case BAD_MF_VALUE:
        REPORT_INFO1(LC_AMS,
		     "Some NOT MUST Manifest property is not valid %d",
		     mfsmp.status);
        break;

    case ALL_OK:
        REPORT_INFO1(LC_AMS, "Manifest ALL_OK %d", mfsmp.status);
        break;

    default:
        REPORT_INFO1(LC_AMS, "MF parse OUT_OF_MEMORY %d", mfsmp.status);
        /* for unknown result assuming OUT_OF_MEMORY */
        return mfsmp;

    } /* end of switch */

#if REPORT_LEVEL <= LOG_INFORMATION
    reportToLog(LOG_INFORMATION, LC_AMS, "%s %s %d",
		"###########################  End   of manifest parsing:  ",
		"mf_main() : number of Manifest properties = ",
		mfsmp.numberOfProperties);
    for (res = 0; res < mfsmp.numberOfProperties * 2; res += 2) {
        printPcslStringWithMessage(" ", &mfsmp.pStringArr[res]);
        printPcslStringWithMessage(" ", &mfsmp.pStringArr[res+1]);
    }
#endif

    return mfsmp;
} /* end of mf_main */
MidpProperties jad_main(char* jadbuf, int jadsize) {

    MidpProperties jadsmp = {0,ALL_OK,NULL};
    jchar* jchar_buffer = NULL;
    jchar* save_jchar_buffer = NULL;
    int jbufsize = -1;
#if REPORT_LEVEL <= LOG_INFORMATION
    int res = 0;
#endif
    jbufsize = jadsize * sizeof(jchar);

    jchar_buffer = (jchar*)midpMalloc(jbufsize+2);
    if (!jchar_buffer) {
        midpFree(jadbuf);
        jadsmp.status = OUT_OF_MEMORY;
        return jadsmp;
    }
    memset(jchar_buffer,0,(jbufsize + 2));

    convertChar2JChar(jadbuf,jchar_buffer,jadsize);
    midpFree(jadbuf);
    save_jchar_buffer = jchar_buffer;



    REPORT_INFO(LC_AMS, "####################### Start JAD parsing");

    /* during execution of this function jadbuf pointer will be changed
       status will be set during midpParseJad() execution */
    jadsmp = midpParseJad(jchar_buffer);
    midpFree(save_jchar_buffer);
    switch (jadsmp.status) {
    case OUT_OF_STORAGE:
        REPORT_WARN1(LC_AMS, "OUT_OF_STORAGE by JAD %d", jadsmp.status);
        midp_free_properties(&jadsmp);
        return jadsmp;

    case NO_JAR_URL_PROP:
        REPORT_WARN1(LC_AMS, "Jad property missing %d", jadsmp.status);
        midp_free_properties(&jadsmp);
        return jadsmp;

    case NO_SUITE_NAME_PROP:
        REPORT_WARN1(LC_AMS, "Jad property missing %d", jadsmp.status);
        midp_free_properties(&jadsmp);
        return jadsmp;

    case NO_SUITE_VENDOR_PROP:
        REPORT_WARN1(LC_AMS, "Jad property missing %d", jadsmp.status);
        midp_free_properties(&jadsmp);
        return jadsmp;

    case NO_SUITE_VERSION_PROP:
        REPORT_WARN1(LC_AMS, "Jad property missing %d", jadsmp.status);
        midp_free_properties(&jadsmp);
        return jadsmp;

    case NO_JAR_SIZE_PROP:
        REPORT_WARN1(LC_AMS, "Jad property missing %d", jadsmp.status);
        midp_free_properties(&jadsmp);
        return jadsmp;

    case NUMBER_ERROR:
        REPORT_INFO1(LC_AMS,
		     "Error during parsing JAR size written in JAD %d",
		     jadsmp.status);
        midp_free_properties(&jadsmp);
        return jadsmp;

    case BAD_PARAMS:
    case BAD_JAD_KEY:
    case BAD_JAD_VALUE:
        REPORT_INFO1(LC_AMS,
		     "Some NOT mandatory Jad properties is not valid %d",
		     jadsmp.status);
        break;

    case ALL_OK:
        REPORT_INFO1(LC_AMS, "Jad ALL_OK %d", jadsmp.status);
        break;

    default:
        /* for an unknown result assuming OUT_OF_MEMORY */
        REPORT_INFO1(LC_AMS, "JAD parse OUT_OF_MEMORY %d", jadsmp.status);
        return jadsmp;
    } /* end of switch */

#if REPORT_LEVEL <= LOG_INFORMATION
    reportToLog(LOG_INFORMATION, LC_AMS,
		"######################### End of JAD parsing\n"
		"jad_main() : number of JAD properties = %d",
		jadsmp.numberOfProperties);

    for (res = 0; res < jadsmp.numberOfProperties*2; res+=2 ) {
        printPcslStringWithMessage(" ", &jadsmp.pStringArr[res]);
        printPcslStringWithMessage(" ", &jadsmp.pStringArr[res+1]);
    }
#endif

    return jadsmp;
} /* end of jad_main */
Beispiel #10
0
/**
 * check the SMS header against the push filter.
 * @param filter The filter string to be used
 * @param cmsidn The caller's MSIDN number to be tested by the filter
 * @return <code>1</code> if the comparison is successful; <code>0</code>,
 *     otherwise.
 */
static int checkfilter(char *filter, char *cmsidn) {
    char *p1 = NULL;
    char *p2 = NULL;

#if REPORT_LEVEL <= LOG_INFORMATION
    if (filter != NULL && cmsidn != NULL) {
        reportToLog(LOG_INFORMATION, LC_PROTOCOL,
                    "in checkfilter[%s , %s]",
                    filter, cmsidn);
    }
#endif
    if ((cmsidn == NULL) || (filter == NULL)) return 0;

    /* Filter is exactly "*", then all MSIDN numbers are allowed. */
    if (strcmp(filter, "*") == 0) return 1;

    /*
     * Otherwise walk through the filter string looking for character
     * matches and wildcard matches.
     * The filter pointer is incremented in the main loop and the
     * MSIDN pointer is incremented as characters and wildcards
     * are matched. Checking continues until there are no more filter or
     * MSIDN characters available.
     */
    for (p1=filter, p2=cmsidn; *p1 && *p2; p1++) {
        /*
         * For an asterisk, consume all the characters up to
         * a matching next character.
         */
        if (*p1 == '*') {
            /* Initialize the next two filter characters. */
            char f1 = *(p1+1);
            char f2 = '\0';
            if (f1 != '\0') {
                f2 = *(p1+2);
            }

            /* Skip multiple wild cards. */
            if (f1 == '*') {
                continue;
            }

            /*
             * Consume all the characters up to a match of the next
             * character from the filter string. Stop consuming
             * characters, if the address is fully consumed.
             */
            while (*p2) {
                /*
                 * When the next character matches, check the second character
                 * from the filter string. If it does not match, continue
                 * consuming characters from the address string.
                 */
                if(*p2 == f1 || f1 == '?') {
                    if (*(p2+1) == f2 || f2 == '?' || f2 == '*') {
                        /* Always consume an address character. */
                        p2++;
                        if (f2 != '?' || *(p2+1) == '.' || *(p2+1) == '\0') {
                            /* Also, consume a filter character. */
                            p1++;
                        }
                        break;
                    }
                }
                p2++;
            }
        } else if (*p1 == '?') {
            p2 ++;
        } else if (*p1 != *p2) {
            /* If characters do not match, filter failed. */
            return 0;
        } else {
            p2 ++;
        }
    }

    if (!(*p1)  && !(*p2) ) {
        /*
         * All available filter and MSIDN characters were checked.
         */
        return 1;
    } else {
        /*
         * Mismatch in length of filter and MSIDN string
         */
        return 0;
    }
}
Beispiel #11
0
/**
 * FUNCTION:      jvmFreeImpl()
 * TYPE:          public operation
 * OVERVIEW:      Free memory allocated from the private JVM memory pool
 * INTERFACE:
 *   parameters:  ptr        Pointer to allocated memory
 *                filename   Filename where allocation occured
 *                lineno     Line number where allocation occured
 *   returns:     <nothing>
 *                
 */
 void
jvmFreeImpl(void *ptr, char *filename, int lineno) {
    _JvmMemHdrPtr jvmMemoryHdr;
    int guardSize = 0;

    if(jvm_tag_malloc) {
       guardSize = 4;
    }

    if (ptr == NULL) {
#ifdef REPORT_LEVEL
#if REPORT_LEVEL <= LOG_INFORMATION
        reportToLog(LOG_INFORMATION, LC_MALLOC, 
		    "DEBUG: Attempt to free NULL pointer");
        printAllocation(LOG_INFORMATION, "freed", filename, lineno);
#endif
#endif
    } else if (((char*)ptr > JvmMemoryEnd) || 
               ((char*)ptr < JvmMemoryStart)) {
#ifdef REPORT_LEVEL
#if REPORT_LEVEL <= LOG_ERROR
        reportToLog(LOG_ERROR, LC_MALLOC, 
		    "ERROR: Attempt to free memory out of scope: 0x%p", ptr);
	printAllocation(LOG_ERROR, "freed", filename, lineno);
#endif
#endif
    } else {

	jvmMemoryHdr = (_JvmMemHdrPtr)((char*)ptr -sizeof(_JvmMemHdr));

        if (jvm_trace_malloc) {
            tty->print("|%x| Free, total=%d, %s:%d    ", ptr, jvm_malloc_total,
              file_tail(filename), lineno);
        }
		
        if (jvmMemoryHdr->magic != MAGIC) {

	    if(jvm_tag_malloc) {
		tty->print_cr(" **** BAD TAG (0x%x) **** \n", jvmMemoryHdr->magic);
	    }
#ifdef REPORT_LEVEL
#if REPORT_LEVEL <= LOG_ERROR
	    reportToLog(LOG_ERROR, LC_MALLOC, 
			"ERROR: Attempt to free corrupted memory: 0x%p", 
			ptr);
            printAllocation(LOG_ERROR, "freed", filename, lineno);
#endif
#endif
        } else if (jvmMemoryHdr->free != 0) {
#ifdef REPORT_LEVEL
#if REPORT_LEVEL <= LOG_ERROR
	    reportToLog(LOG_ERROR, LC_MALLOC, 
			"ERROR: Attempt to free memory twice: 0x%p", ptr);
            printAllocation(LOG_ERROR, "freed", filename, lineno);
#endif
#endif
        } else {
	    if(jvm_tag_malloc) {
	       if (*(((char*)ptr) + jvmMemoryHdr->size - guardSize + 0) != (char)0x01 ||
                 *(((char*)ptr) + jvmMemoryHdr->size - guardSize + 1) != (char)0xcd ||
                 *(((char*)ptr) + jvmMemoryHdr->size - guardSize + 2) != (char)0x02 ||
                 *(((char*)ptr) + jvmMemoryHdr->size - guardSize + 3) != (char)0xef) {
                 tty->print_cr(" **** BAD POST TAG **** ");
		 }
	    } else {
#ifdef REPORT_LEVEL
#if REPORT_LEVEL <= LOG_WARNING
                JvmMemoryAllocated -= jvmMemoryHdr->size;
                /* The memory block header is valid, now check the guard data */
                if (jvmMemoryHdr->guard != GUARD_WORD) {
	            reportToLog(LOG_WARNING, LC_MALLOC, 
			        "ERROR: Possible memory underrun: 0x%p", ptr);
                    printAllocation(LOG_WARNING, "allocated", 
				    jvmMemoryHdr->filename, 
				    jvmMemoryHdr->lineno);
                    printAllocation(LOG_WARNING, "freed", filename, lineno);
                } else if (verifyTailGuardData(jvmMemoryHdr)) {
                   reportToLog(LOG_WARNING, LC_MALLOC, 
			       "ERROR: Possible memory overrun: 0x%p", ptr);
                    printAllocation(LOG_WARNING, "allocated", 
				   jvmMemoryHdr->filename, 
				   jvmMemoryHdr->lineno);
                    printAllocation(LOG_WARNING, "freed", filename, lineno);
                }
#endif 
#endif
	    }
#ifdef REPORT_LEVEL
#if REPORT_LEVEL <= LOG_INFORMATION
            reportToLog(LOG_INFORMATION, LC_MALLOC, 
			"DEBUG: free %d bytes: 0x%p", 
			jvmMemoryHdr->size, ptr);
            printAllocation(LOG_INFORMATION, "allocated", 
                            jvmMemoryHdr->filename, jvmMemoryHdr->lineno);
            printAllocation(LOG_INFORMATION, "freed", filename, lineno);
#else
            (void)filename; /* No-op */
            (void)lineno;   /* No-op */
#endif
#endif
            jvmMemoryHdr->free = 1;
	    if(jvm_tag_malloc) {
	       jvm_malloc_total -= (jvmMemoryHdr->size - guardSize);
            }
	    if (jvm_trace_malloc) {
              tty->print_cr("OK");
            }
        }
	if (jvm_trash_malloc)
	{
          if (jvmMemoryHdr->size < 1024)
	  {
             JVM_Memset(ptr, 0xaf, jvmMemoryHdr->size);
          } 
	  
        }

    } /* end of else */

}
Beispiel #12
0
/**
 * FUNCTION:      jvmMallocImpl()
 * TYPE:          public operation
 * OVERVIEW:      Allocate memory from the private JVM memory pool
 * INTERFACE:
 *   parameters:  size       Number of byte to allocate
 *                filename   Filename where allocation occured
 *                lineno     Line number where allocation occured
 *   returns:     pointer to the newly allocated memory
 *                
 */
 void*
jvmMallocImpl(unsigned int size, char* filename, int lineno) {
    unsigned int   numBytesToAllocate = size;
    void*          loc     = NULL;
    _JvmMemHdrPtr tempHdr = NULL;
    char*          temp    = NULL;
    char*          jvmMemoryPtr;
    char*          jvmMemoryLast;
    _JvmMemHdrPtr jvmMemoryHdr;
    int   guardSize = 0;
    void* guardPos;


    if (jvm_tag_malloc) {
        guardSize = 4;
    } else {
#ifdef REPORT_LEVEL
#if REPORT_LEVEL <= LOG_WARNING
        guardSize = GUARD_SIZE;
#endif
#endif
    }

    while ( (numBytesToAllocate & ALIGNMENT) != 0 ) {
        numBytesToAllocate++;
	size++;
    }

    numBytesToAllocate += guardSize;
    jvmMemoryLast = JvmMemoryStart;

    /* find a free slot */
    for (jvmMemoryPtr = JvmMemoryStart;
         jvmMemoryPtr < JvmMemoryEnd;
         jvmMemoryLast = jvmMemoryPtr,
         jvmMemoryPtr += jvmMemoryHdr->size + sizeof(_JvmMemHdr)) {
        
        jvmMemoryHdr = (_JvmMemHdrPtr)jvmMemoryPtr;
        if (jvmMemoryHdr->magic != MAGIC) {
	    tty->print_cr("ERROR: Memory corruption at 0x%p, in chunk 0x%p", 
			  jvmMemoryPtr, jvmMemoryLast); 
            return((void *) 0);
        } else {
            while ( 1 ) {
                /* coalescing */
                if (jvmMemoryHdr->free == 1) {
                    /* if current block is free */
                    temp = (char*)jvmMemoryHdr;
                    temp += jvmMemoryHdr->size + sizeof(_JvmMemHdr);
                    tempHdr = (_JvmMemHdrPtr)temp;

                    if ((temp < JvmMemoryEnd) && 
			(tempHdr->free == 1) && (tempHdr->magic == MAGIC)) {
		        /* and the next block is free too */
		        /* then coalesce */
		        jvmMemoryHdr->size += tempHdr->size
			    + sizeof(_JvmMemHdr);
#ifdef REPORT_LEVEL
#if REPORT_LEVEL <= LOG_INFORMATION
                        reportToLog(LOG_INFORMATION, LC_MALLOC,
                           "DEBUG: Coalescing blocks 0x%p and 0x%p",
				     jvmMemoryHdr, tempHdr);
#endif
#endif

                    } else {
		        break;
                    }
                } else {
		    break;
                }
            } /* while */

            /* allocating */
            if ((jvmMemoryHdr->free == 1) && 
                (jvmMemoryHdr->size >= numBytesToAllocate)) {
                if (jvmMemoryHdr->size > (numBytesToAllocate 
                                              + sizeof(_JvmMemHdr) + 4)) {
                    /* split block */
                    _JvmMemHdrPtr nextHdr;
                    nextHdr = (_JvmMemHdrPtr)((char *)jvmMemoryPtr
                                               + numBytesToAllocate
                                               + sizeof(_JvmMemHdr));
                    nextHdr->magic = MAGIC;
                    nextHdr->free = 1;
                    nextHdr->size = jvmMemoryHdr->size 
                                    - numBytesToAllocate 
                                    - sizeof(_JvmMemHdr);
                    jvmMemoryHdr->size     = numBytesToAllocate;
                } else
                {
                    size = jvmMemoryHdr->size - guardSize;
                }
                jvmMemoryHdr->free     = 0;
                loc = (void*)((char*)jvmMemoryHdr + sizeof(_JvmMemHdr));

                if (jvm_tag_malloc) {
                    /* Add tail guard */
	            guardPos = (void*)((char*)loc + jvmMemoryHdr->size - guardSize);
		    ((unsigned char*)guardPos)[0] = (char)0x01;
                    ((unsigned char*)guardPos)[1] = (char)0xcd;
                    ((unsigned char*)guardPos)[2] = (char)0x02;
                    ((unsigned char*)guardPos)[3] = (char)0xef;
		} else {
#ifdef REPORT_LEVEL
#if REPORT_LEVEL <= LOG_WARNING
                  jvmMemoryHdr->guard    = GUARD_WORD;      /* Add head guard */
                  jvmMemoryHdr->filename = filename;
                  jvmMemoryHdr->lineno   = lineno;

                  /* Add tail guard */
                  guardSize = jvmMemoryHdr->size - size;

                  jvmMemoryHdr->guardSize = guardSize;
                  guardPos = (void*)((char*)loc + jvmMemoryHdr->size 
                                   - guardSize);
                  for(i=0; i<guardSize; i++) {
                      ((unsigned char*)guardPos)[i] = GUARD_BYTE;
                  }
                
                  JvmMemoryAllocated += numBytesToAllocate;
                  if (JvmMemoryAllocated > JvmMemoryHighWaterMark) {
                      JvmMemoryHighWaterMark = JvmMemoryAllocated;
                  }
#endif 
#endif
		}
               
	 	if (loc != NULL && jvm_tag_malloc) {
		    jvm_malloc_total += size;
		}

                if (jvm_trace_malloc) {
                    tty->print_cr("|%x| Malloc(%d): total=%d, %s:%d ", loc, size, 
                    jvm_malloc_total, file_tail(filename), lineno);
                }

                if (jvm_trash_malloc && loc != NULL) {
		   JVM_Memset(loc, 0xab, size);
		}
#ifdef REPORT_LEVEL
#if REPORT_LEVEL <= LOG_INFORMATION
                reportToLog(LOG_INFORMATION, LC_MALLOC, 
			    "DEBUG: Requested %d provided %d at 0x%p",
			    numBytesToAllocate, jvmMemoryHdr->size, loc);
                printAllocation(LOG_INFORMATION, "allocated", filename, lineno);
#else
                (void)filename; /* No-op */
                (void)lineno;   /* No-op */
#endif
#endif
                return(loc);
            } /* end of allocating */
        } /* end of else */
    } /* end of for */
    tty->print_cr("DEBUG: Unable to allocate %d bytes", 
		 numBytesToAllocate);
    return((void *)0);
}
Beispiel #13
0
/**
 * An internal helper function.
 * Calls to <code>printAllocation()</code> should be wrapped like this, 
 * as if they were <code>reportToLog()</code> calls.
 *
 * #if REPORT_LEVEL <= severity
 * printAllocation(severity, what, filename, lineno);
 * #endif
 */
 void
printAllocation(int severity, const char* what, char* filename, int lineno) {
    reportToLog(severity, LC_MALLOC, "alloc: %s at %s line %d", 
		what, filename, lineno);
}