⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 midpmalloc.c

📁 用于移动设备上的java虚拟机源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
                        /* then coalesce */                        midpMemoryHdr->size += tempHdr->size			                       + sizeof(_MidpMemHdr);#ifdef MIDP_MALLOC_DEBUG                                                printf("DEBUG: Coalescing blocks 0x%08x and 0x%08x\n",			       midpMemoryHdr, tempHdr);#endif /* MIDP_MALLOC_DEBUG */                    } else {                        break;                    }                } else {                    break;                }            } /* while */            /* allocating */            if ((midpMemoryHdr->free == 1) && 		(midpMemoryHdr->size >= numBytesToAllocate)) {                if (midpMemoryHdr->size > (numBytesToAllocate 					      + sizeof(_MidpMemHdr) + 4)) {                    /* split block */                    _MidpMemHdrPtr nextHdr;                    nextHdr = (_MidpMemHdrPtr)((char *)midpMemoryPtr					       + numBytesToAllocate                                               + sizeof(_MidpMemHdr));                    nextHdr->magic = MAGIC;                    nextHdr->free = 1;                    nextHdr->size = midpMemoryHdr->size 			            - numBytesToAllocate 			            - sizeof(_MidpMemHdr);		    midpMemoryHdr->size     = numBytesToAllocate;                }                midpMemoryHdr->free     = 0;                loc = (void*)((char*)midpMemoryHdr + sizeof(_MidpMemHdr));#ifdef MIDP_MALLOC_TRACE                midpMemoryHdr->guard    = GUARD_WORD;      /* Add head guard */                midpMemoryHdr->filename = filename;                midpMemoryHdr->lineno   = lineno;		{		    /* Add tail guard */		    int   guardSize = midpMemoryHdr->size - size;		    void* guardPos;		    int   i;                    midpMemoryHdr->guardSize = guardSize;		    guardPos = (void*)((char*)loc + midpMemoryHdr->size 				       - guardSize);		    for(i=0; i<guardSize; i++) {			((unsigned char*)guardPos)[i] = GUARD_BYTE;		    }		}		MidpMemoryAllocated += numBytesToAllocate;		if (MidpMemoryAllocated > MidpMemoryHighWaterMark) {		    MidpMemoryHighWaterMark = MidpMemoryAllocated;		}#endif /* MIDP_MALLOC_TRACE */#ifdef MIDP_MALLOC_DEBUG                                printf("DEBUG: Requested %d provided %d at 0x%08x\n",		       numBytesToAllocate, midpMemoryHdr->size, loc);		printAllocation("allocated", filename, lineno);#endif /* MIDP_MALLOC_DEBUG */                return(loc);            } /* end of allocating */        } /* end of else */    } /* end of for */#ifdef MIDP_MALLOC_DEBUG    printf("DEBUG: Unable to allocate %d bytes\n", numBytesToAllocate);#endif /* MIDP_MALLOC_DEBUG */    return((void *)0);}/*========================================================================= * FUNCTION:      midpCallocImpl() * TYPE:          public operation * OVERVIEW:      Allocate memory from the private MIDP memory pool, *                 memory contents are cleared * INTERFACE: *   parameters:  nelem      Number of elements to allocate *                elsize     Size of one element *                filename   Filename where allocation occured *                lineno     Line number where allocation occured *   returns:     pointer to the newly allocated and cleared memory *                 *=======================================================================*/void*midpCallocImpl(int nelem, int elsize, char* filename, int lineno) {    void *loc = NULL;    if ((loc = midpMallocImpl(nelem * elsize, filename, lineno)) != NULL) {        memset(loc, 0, nelem * elsize);    }    return loc;}/*========================================================================= * FUNCTION:      midpReallocImpl() * TYPE:          public operation * OVERVIEW:      Re-allocate memory from the private MIDP memory pool * INTERFACE: *   parameters:  ptr        Original memory pointer *                size       New size *                filename   Filename where allocation occured *                lineno     Line number where allocation occured *   returns:     pointer to the re-allocated memory *                 *=======================================================================*/void*midpReallocImpl(void* ptr, int size, char* filename, int lineno) {    void*          newPtr = NULL;    _MidpMemHdrPtr memHdr;    if (ptr == NULL) {	return ptr;    }    memHdr = (_MidpMemHdrPtr)((char*)ptr - sizeof(_MidpMemHdr));    if (memHdr->size != size) {	if (size != 0) {	    newPtr = midpMallocImpl(size, filename, lineno);	    if (newPtr != NULL) {		if (memHdr->size < size) {		    memcpy(newPtr, ptr, memHdr->size);		} else {		    memcpy(newPtr, ptr, size);		}		midpFreeImpl(ptr, filename, lineno);	    }	} else {	    /* When size == 0, realloc() acts just like free() */	    midpFreeImpl(ptr, filename, lineno);	}    } else {	/* sizes are the same, just return the same pointer */	newPtr = ptr;    }	    return newPtr;}/*========================================================================= * FUNCTION:      midpStrdupImpl() * TYPE:          public operation * OVERVIEW:      Duplicate the given string * INTERFACE: *   parameters:  s1         String to duplicate *                filename   Filename where allocation occured *                lineno     Line number where allocation occured *   returns:     pointer to the duplicate string *                 *=======================================================================*/char*midpStrdupImpl(const char *s1, char* filename, int lineno) {    char *p = (char *)midpMallocImpl(strlen(s1) + 1, filename, lineno);    if ( p != NULL ) {        strcpy(p, s1);    }    return(p);}/*========================================================================= * FUNCTION:      midpFreeImpl() * TYPE:          public operation * OVERVIEW:      Free memory allocated from the private MIDP memory pool * INTERFACE: *   parameters:  ptr        Pointer to allocated memory *                filename   Filename where allocation occured *                lineno     Line number where allocation occured *   returns:     <nothing> *                 *=======================================================================*/voidmidpFreeImpl(void *ptr, char *filename, int lineno) {    _MidpMemHdrPtr midpMemoryHdr;    if (ptr == NULL) {#ifdef MIDP_MALLOC_DEBUG        printf("DEBUG: Attempt to free NULL pointer\n");        printAllocation("freed", filename, lineno);#endif /* MIDP_MALLOC_DEBUG */    } else if (((char*)ptr > MidpMemoryEnd) || 	       ((char*)ptr < MidpMemoryStart)) {        printf("ERROR: Attempt to free memory out of scope: 0x%08x\n", ptr);        printAllocation("freed", filename, lineno);    } else {        midpMemoryHdr = (_MidpMemHdrPtr)((char*)ptr -sizeof(_MidpMemHdr));        if (midpMemoryHdr->magic != MAGIC) {            printf("ERROR: Attempt to free corrupted memory: 0x%08x\n", ptr);	    printAllocation("freed", filename, lineno);#ifdef MIDP_MALLOC_DEBUG        } else if (midpMemoryHdr->free != 0) {            printf("DEBUG: Attempt to free memory twice: 0x%08x\n", ptr);	    printAllocation("freed", filename, lineno);#endif /* MIDP_MALLOC_DEBUG */        } else {#ifdef MIDP_MALLOC_TRACE	    MidpMemoryAllocated -= midpMemoryHdr->size;	    /* The memory block header is valid, now check the guard data */	    if (midpMemoryHdr->guard != GUARD_WORD) {		printf("ERROR: Possible memory underrun: 0x%08x\n", ptr);		printAllocation("allocated", midpMemoryHdr->filename, 				             midpMemoryHdr->lineno);		printAllocation("freed", filename, lineno);	    } else if (verifyTailGuardData(midpMemoryHdr)) {		printf("ERROR: Possible memory overrun: 0x%08x\n", ptr);		printAllocation("allocated", midpMemoryHdr->filename, 				             midpMemoryHdr->lineno);		printAllocation("freed", filename, lineno);	    }#endif /* MIDP_MALLOC_TRACE */#ifdef MIDP_MALLOC_DEBUG            printf("DEBUG: free %d bytes: 0x%08x\n", midpMemoryHdr->size, ptr);	    printAllocation("allocated", 			    midpMemoryHdr->filename, midpMemoryHdr->lineno);	    printAllocation("freed", filename, lineno);#endif            midpMemoryHdr->free = 1;        }    } /* end of else */}#if MIDP_MALLOC_TRACE/*========================================================================= * FUNCTION:      verifyTailGuardData() * TYPE:          private operation * OVERVIEW:      Verify guard data at the end of the memory is valid * INTERFACE: *   parameters:  midpMemoryHdr   Pointer to memory block header *   returns:     0 if guard data is valid; otherwise, the byte position *                 of the first incorrect guard data byte *                 *=======================================================================*/static intverifyTailGuardData(_MidpMemHdrPtr midpMemoryHdr) {    void* guardPos;    int   guardSize;    int   i;    guardSize = midpMemoryHdr->guardSize;    guardPos = (void*)((char*)midpMemoryHdr		       + sizeof(_MidpMemHdr)		       + midpMemoryHdr->size - guardSize - 1);    for(i = 1; i <= guardSize; i++) {	if (((unsigned char*)guardPos)[i] != GUARD_BYTE) {	    return i;	}    }    return 0;}#endif /* MIDP_MALLOC_TRACE *//*========================================================================= * FUNCTION:      midpGetTotalHeap() * TYPE:          public operation * OVERVIEW:      Get the total amount of available heap * INTERFACE: *   parameters:  <none> *   returns:     The total amount of available heap *                 *=======================================================================*/intmidpGetTotalHeap() {    return (MidpMemoryEnd - MidpMemoryStart);}/*========================================================================= * FUNCTION:      midpGetFreeHeap() * TYPE:          public operation * OVERVIEW:      Get the current amount of unused heap * INTERFACE: *   parameters:  <none> *   returns:     The current amount of unused heap *                 *=======================================================================*/intmidpGetFreeHeap() {    _MidpMemHdrPtr midpMemoryHdr;    char*          midpMemoryPtr;    int            size = 0;    for (midpMemoryPtr = MidpMemoryStart;          midpMemoryPtr < MidpMemoryEnd;         midpMemoryPtr += midpMemoryHdr->size + sizeof(_MidpMemHdr)) {        midpMemoryHdr = (_MidpMemHdrPtr)midpMemoryPtr;        if (midpMemoryHdr->free != 1) {            size += midpMemoryHdr->size;        }    }    return (midpGetTotalHeap() - size);}

⌨️ 快捷键说明

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