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

📄 rzkallocsegment.c

📁 zilog的实时操作系统RZK,可以移植到多种处理器上
💻 C
字号:
/*
* File			:	RZKAllocSegment.c
*
* Description	:	This file contains RZKAllocSegment function that allocates 
*					a segment of memory from 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 "ZSemaphore.h"
#include "ZScheduler.h"


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

/** extern functions */
//extern void RZKScheduler();
extern void *Mem_Alloc_Seg(RZK_REGIONHANDLE_t, RZK_LENGTH_t size);	// IAR port: changed from COUNT_t to RZK_LENGTH_t
extern void AppendToResourceQueue(RZK_HANDLE_t hObject,RZK_THREADHANDLE_t hThread);
extern void WaitOnResourceQueue( RZK_HANDLE_t hControlBlock,UINT16 uBlockingReason,
							TICK_t tBlockTime);

/** extern variables */
extern RZK_THREADHANDLE_t hCurrentThread;

/*
* Function		:	RZKAllocSegment
*
* Description	:	This Function allocates a segment of memory from
*					the region specified. The amount of memory to be
*					allocated should be greater than or equal to uUnit_Size
*					of the region.
* 
* Inputs		:	hRegion - Specifies a handle to the region.  
*					uSize -  Specifies the size of memory to be allocated.
*					tBlockTime - Specifies the maximum time to wait if memory is not  available.
*
* Outputs		:	RZK_PTR_t - Returns the address of memory allocated.
*
* Dependencies	:	All externs
*/
extern RZK_SEMAPHOREHANDLE_t hSem_Region;

RZK_PTR_t RZKAllocSegment
				 ( 
					 RZK_REGIONHANDLE_t	hRegion,  
					 COUNT_t uSize,	// IAR port, changed from COUNT_t to RZK_LENGTH_t
					 TICK_t tBlockTime
				 )

{
	/* Local Variable */
//	RZK_TCB_t * pInsertionPos;
	RZK_PTR_t pAllocated_Mem;
	RZK_STATE_t uState;
	
	/* Check the validity of arguments */
#ifdef RZK_DEBUG
		if ((pRegion == NULL) || (pRegion->uState == 0) || (pRegion->magic_num != MAGIC_NUM_REGION))
		{
			pCurrentThread->errNum = RZKERR_INVALID_HANDLE;
			return NULL;
		}

		if ( ( uSize == 0) )
		{
			pCurrentThread->errNum = RZKERR_INVALID_ARGUMENTS;
			return NULL;
		}
#endif	/* End of validity */

	if (!(pRegion->uState & OBJECT_CREATED))
	{
		pCurrentThread->errNum = RZKERR_CB_BUSY;
		return NULL;
	}

		// IAR port added
#ifdef _IAR_CODE
		uSize &= 0xFFFFFF ;
#endif

	pAllocated_Mem = Mem_Alloc_Seg(hRegion, uSize);
	
	uState = RZKDisablePreemption();
	if(pAllocated_Mem != NULL)
	{
		pCurrentThread->errNum = RZKERR_SUCCESS;
		RZKRestorePreemption(uState);
		return pAllocated_Mem;
	}
	else
	{ 
		/* RZKERR_SCB_UNAVAILABLE is returned, if the segment control
		block is not available for creating the split off memory entry
		in the free list of the region.*/
		if(pCurrentThread->errNum == RZKERR_SCB_UNAVAILABLE)
		{
			RZKRestorePreemption(uState);
			return NULL;
		}

		/* if blocking time is 0, then return immediately */
		if(tBlockTime == 0)
		{
			pCurrentThread->errNum = RZKERR_TIMEOUT;
			RZKRestorePreemption(uState);
			return NULL;
		}
			
		/* Put the information about the memory to allocate in the thread control block */
		pCurrentThread->uSegSize = uSize;
		pCurrentThread->pSeg = NULL;

		AppendToResourceQueue(pRegion,hCurrentThread);	
		pRegion -> etWaitingQueue = MESSAGE_RECEIVE;
		
		
		WaitOnResourceQueue(pRegion, BLOCK_REGIONWAIT, tBlockTime);
		/* Reset the running bit in the TCB */
		pCurrentThread -> uState &= ~(THREAD_RUNNING);
		RZKRestorePreemption(uState);
		return pCurrentThread->pSeg;
	}

} /* End of RZKAllocSegment */

⌨️ 快捷键说明

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