?? memlib.c
字號:
partId, pBlock, 2* (pHdr->nWords), nBytes);#endif partId->curWordsAllocated += pNextHdr->nWords; /* fix stats */ partId->cumWordsAllocated += pNextHdr->nWords; NEXT_HDR (pHdr)->pPrevHdr = pHdr; /* fix next */ /* if this combined block is too big, it will get fixed below */ } } /* split off any extra and give it back; * note that this covers both the case of a realloc for smaller space * and the case of a realloc for bigger space that caused a coalesce * with the next block that resulted in larger than required block */ pNextHdr = memBlockSplit (pHdr, pHdr->nWords - nWords, partId->minBlockWords); semGive (&partId->sem); if (pNextHdr != NULL) { (void) memPartFree (partId, HDR_TO_BLOCK (pNextHdr)); partId->curBlocksAllocated++; /* memPartFree() is going to subtract one from the curBlocksAllocated * statistic. Since memPartRealloc() should not change the value of * curBlocksAllocated, it has to be corrected (SPR 1407 & 3564) */ } return ((void *) pBlock); }/********************************************************************************* memPartFindMax - find the size of the largest available free block** This routine searches for the largest block in the memory partition free* list and returns its size.** RETURNS: The size, in bytes, of the largest available block.** ERRNO: S_smObjLib_NOT_INITIALIZED** SEE ALSO: smMemLib*/int memPartFindMax ( FAST PART_ID partId /* partition ID */ ) { FAST BLOCK_HDR * pHdr; FAST DL_NODE * pNode; FAST unsigned biggestWords = 0; if (ID_IS_SHARED (partId)) /* partition is shared? */ { if (smMemPartFindMaxRtn == NULL) { errno = S_smObjLib_NOT_INITIALIZED; return (ERROR); } return ((*smMemPartFindMaxRtn) (SM_OBJ_ID_TO_ADRS (partId))); } /* partition is local */ if (OBJ_VERIFY (partId, memPartClassId) != OK) return (ERROR); semTake (&partId->sem, WAIT_FOREVER); /* go through free list and find largest free */ for (pNode = DLL_FIRST (&partId->freeList); pNode != NULL; pNode = DLL_NEXT (pNode)) { pHdr = NODE_TO_HDR (pNode); if (pHdr->nWords > biggestWords) biggestWords = pHdr->nWords; } semGive (&partId->sem); return (2 * biggestWords - sizeof (BLOCK_HDR)); }/********************************************************************************* memPartAllocError - handle allocation error*/LOCAL void memPartAllocError ( PART_ID pPart, unsigned nBytes ) { if ((_func_logMsg != NULL) && (pPart->options & MEM_ALLOC_ERROR_LOG_FLAG)) (* _func_logMsg) (memMsgBlockTooBig, nBytes, (int) pPart, 0, 0, 0, 0); }/********************************************************************************* memPartBlockError - handle invalid block error*/LOCAL void memPartBlockError ( PART_ID pPart, char *pBlock, char *label ) { if ((_func_logMsg != NULL) && (pPart->options & MEM_BLOCK_ERROR_LOG_FLAG)) (* _func_logMsg) (memMsgBlockError, (int)label, (int) pBlock, (int) pPart, 0, 0, 0); }/******************************************************************************* memBlockSplit - split a block into two blocks** This routine splits the block pointed to into two blocks. The second* block will have nWords words in it. A pointer is returned to this block.* If either resultant block would end up having less than minWords in it,* NULL is returned.** RETURNS: A pointer to the second block, or NULL.*/LOCAL BLOCK_HDR *memBlockSplit ( FAST BLOCK_HDR *pHdr, unsigned nWords, /* number of words in second block */ unsigned minWords /* min num of words allowed in a block */ ) { FAST unsigned wordsLeft; FAST BLOCK_HDR *pNewHdr; /* check if block can be split */ if ((nWords < minWords) || ((wordsLeft = (pHdr->nWords - nWords)) < minWords)) return (NULL); /* not enough space left */ /* adjust original block size and create new block */ pHdr->nWords = wordsLeft; pNewHdr = NEXT_HDR (pHdr); pNewHdr->pPrevHdr = pHdr; pNewHdr->nWords = nWords; pNewHdr->free = pHdr->free; /* fix next block */ NEXT_HDR (pNewHdr)->pPrevHdr = pNewHdr; return (pNewHdr); }/********************************************************************************* memOptionsSet - set the debug options for the system memory partition** This routine sets the debug options for the system memory partition.* Two kinds of errors are detected: attempts to allocate more memory than* is available, and bad blocks found when memory is freed. In both cases,* the following options can be selected for actions to be taken when the error* is detected: (1) return the error status, (2) log an error message and* return the error status, or (3) log an error message and suspend the* calling task.** These options are discussed in detail in the library manual* entry for memLib.** RETURNS: N/A** SEE ALSO: memPartOptionsSet()*/void memOptionsSet ( unsigned options /* options for system partition */ ) { memPartOptionsSet (memSysPartId, options); }/********************************************************************************* calloc - allocate space for an array (ANSI)** This routine allocates a block of memory for an array that contains* <elemNum> elements of size <elemSize>. This space is initialized to* zeros.** RETURNS:* A pointer to the block, or NULL if the call fails.** SEE ALSO:* .I "American National Standard for Information Systems -"* .I "Programming Language - C, ANSI X3.159-1989: General Utilities (stdlib.h)"*/void *calloc ( size_t elemNum, /* number of elements */ size_t elemSize /* size of elements */ ) { FAST void *pMem; FAST size_t nBytes = elemNum * elemSize; if ((pMem = memPartAlloc (memSysPartId, (unsigned) nBytes)) != NULL) bzero ((char *) pMem, (int) nBytes); return (pMem); }/********************************************************************************* realloc - reallocate a block of memory (ANSI)** This routine changes the size of a specified block of memory and returns a* pointer to the new block of memory. The contents that fit inside the new* size (or old size if smaller) remain unchanged. The memory alignment of* the new block is not guaranteed to be the same as the original block.** RETURNS:* A pointer to the new block of memory, or NULL if the call fails.** SEE ALSO:* .I "American National Standard for Information Systems -"* .I "Programming Language - C, ANSI X3.159-1989: General Utilities (stdlib.h)"*/void *realloc ( void *pBlock, /* block to reallocate */ size_t newSize /* new block size */ ) { return (memPartRealloc (memSysPartId, (char *) pBlock, (unsigned) newSize)); }/********************************************************************************* cfree - free a block of memory** This routine returns to the free memory pool a block of memory * previously allocated with calloc().** It is an error to free a memory block that was not previously allocated.** RETURNS: OK, or ERROR if the the block is invalid.*/STATUS cfree ( char *pBlock /* pointer to block of memory to free */ ) { return (memPartFree (memSysPartId, pBlock)); }/********************************************************************************* memFindMax - find the largest free block in the system memory partition** This routine searches for the largest block in the system memory partition* free list and returns its size.** RETURNS: The size, in bytes, of the largest available block.** SEE ALSO: memPartFindMax()*/int memFindMax (void) { return (memPartFindMax (memSysPartId)); }/********************************************************************************* memSemInit - initialize the partition semaphore as type mutex** This routine initializes the partition semaphore as a mutex semaphore with* the mutex options stored in mutexOptionsMemLib. This function is called* indirectly from memPartInit (). This coupling allows alternative semaphore* types to serve as the basis for memory partition interlocking.*/LOCAL void memSemInit ( PART_ID partId /* partition to initialize semaphore for */ ) { semMInit (&partId->sem, mutexOptionsMemLib); }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -