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

📄 rzkfreesegment.c

📁 zilog的实时操作系统RZK,可以移植到多种处理器上
💻 C
字号:
/*
* File			:	RZKFreeSegment.c
*
* Description	:	This file contains RZKFreeSegment function which frees a 
*					segment of memory to the region specified. 
* 
* Copyright 2004 ZiLOG Inc.  ALL RIGHTS RESERVED.
*
* This file contains unpublished confidential and proprietary information
* of ZiLOG, Inc.
* NO PART OF THIS WORK MAY BE DUPLICATED, STORED, PUBLISHED OR DISCLOSED 
* IN ANY FORM WITHOUT THE PRIOR WRITTEN CONSENT OF ZiLOG, INC.
* This is not a license and no use of any kind of this work is authorized
* in the absence of a written license granted by ZiLOG, Inc. in ZiLOG's 
* sole discretion 
*/


#include "ZSysgen.h"
#include "ZTypes.h"
#include "ZThread.h"
#include "ZQueue.h" 
#include "ZInterrupt.h"
#include "ZRegion.h"
#include "ZScheduler.h"

#define pCurrentThread  ((RZK_TCB_t *) hCurrentThread)
#define pRegion  ((RZK_REGION_t *) hRegion)

/** extern functions */
//extern void RZKScheduler();
extern void AddSegToOwnerQueue(RZK_HANDLE_t,pRZK_SEGMENT_t);
extern void RemoveSegFromOwnerQueue(RZK_HANDLE_t,pRZK_SEGMENT_t);
//extern void UpdateThreadStatus( RZK_TCB_t * hThread,UINT errNum);
extern void Clear_Segment_Entry(pRZK_SEGMENT_t pSeg);
extern pRZK_SEGMENT_t Is_Segment_Valid(RZK_REGIONHANDLE_t, void *start);
extern void Segment_Remove(pRZK_SEGMENT_t pSeg, pRZK_SEGMENT_t * pSegHead);
extern void Segment_Insert(pRZK_SEGMENT_t pSeg, pRZK_SEGMENT_t * pSegHead);
extern void *Mem_Alloc_Seg(RZK_REGIONHANDLE_t,RZK_LENGTH_t size);	// IAR port: changed from COUNT_t to RZK_LENGTH_t

/** extern variables */
extern RZK_THREADHANDLE_t hCurrentThread;


/*
* Function		:	RZKFreeSegment
*
* Description	:	This function updates the thread statistics parameters of the given thread.
* 
* Inputs		:	hRegion - Specifies a handle to the Region.  
*					pSegment -  Specifies the segment to be freed.
*
* Outputs		:	RZKERR_SUCCESS - If the free operation completed successfully.
*					RZKERR_INVALID_ARGUMENTS - If anyone of the specified arguments is invalid.         
*					RZKERR_CB_BUSY - If the region control block is being exclusively used by another thread.
*
* Dependencies	:	All externs
*/

RZK_STATUS_t RZKFreeSegment
				 ( 
					 RZK_REGIONHANDLE_t	hRegion,
					 RZK_PTR_t pSegment  
				 )
{
	/* Local Variable */
	RZK_TCB_t	*pTemp,*pTempRelease;
	RZK_PTR_t pAllocated_Mem;
	pRZK_SEGMENT_t pSegToFree, pSegTemp;
	RZK_STATE_t uState;

	/* Check the validity of arguments */
#ifdef RZK_DEBUG
		if ((pRegion == NULL) || (pRegion->uState == 0) || (pRegion->magic_num != MAGIC_NUM_REGION))
			return RZKERR_INVALID_HANDLE;
#endif	/* End of validity */

	if (!(pRegion->uState & OBJECT_CREATED))
		return RZKERR_CB_BUSY;

	if((RZK_PTR_t)(pSegToFree = Is_Segment_Valid(hRegion,pSegment)) == NULL) 
		return RZKERR_INVALID_ARGUMENTS;

	/* Preemption disabled - The amount of time Preemption are disabled is more. Need to check */
	uState = RZKDisablePreemption();
	
   /* Move Segment from allocated to free list */
	Segment_Remove(pSegToFree, &(pRegion->Alloc_List));
	Segment_Insert(pSegToFree,&(pRegion->Free_List));

    /* Clear ownership */
//	RemoveSegFromOwnerQueue(pSegToFree->thread,pSegToFree);
    pSegToFree->thread = ( RZK_TCB_t * ) NULL;
	

    /* Merge with previous segments */
    for (pSegTemp = pSegToFree->prev;pSegTemp != NULL && pSegTemp->start + pSegTemp->len == pSegToFree->start; pSegToFree = pSegTemp, pSegTemp = pSegTemp->prev)
	{
		pSegTemp->len += pSegToFree->len;
		Segment_Remove(pSegToFree, &(pRegion->Free_List));
		Clear_Segment_Entry(pSegToFree);
	}

    /* Merge with next regions */
    for (pSegTemp = pSegToFree->next; pSegTemp != NULL && pSegToFree->start + pSegToFree->len == pSegTemp->start; pSegTemp = pSegToFree->next)
	{	
		pSegToFree->len += pSegTemp->len;
		Segment_Remove(pSegTemp, &(pRegion->Free_List));
		Clear_Segment_Entry(pSegTemp);
    }
	
	pTemp = pRegion->pResNext;

	while(pTemp != NULL)
	{
		pAllocated_Mem = Mem_Alloc_Seg(hRegion,pTemp->uSegSize);
		pTempRelease = pTemp;
		pTemp = pTemp->pResNext;
		if(pAllocated_Mem != NULL)
		{
			pTempRelease->pSeg = pAllocated_Mem;

			/* Now remove the thread from this resource queue */
			UpdateThreadStatus(pTempRelease,RZKERR_SUCCESS);
			RemoveFromResourceQueue(pRegion,pTempRelease);
		}
		/* Call the scheduler */
	}
	
	if(pRegion->pResNext == NULL)
		pRegion->etWaitingQueue = MESSAGE_NONE;
	
	RZKRestorePreemption(uState);
	return RZKERR_SUCCESS;

} /* End of RZKFreeSegment */

⌨️ 快捷键说明

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