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

📄 rzkallocatefixedsizememory.c

📁 zilog的实时操作系统RZK,可以移植到多种处理器上
💻 C
字号:
/*
* File				:	RZKAllocateFixedSizeMemory
*
* Description		:	This file defines allocate function of partitions
* 
* 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 "ZMemory.h"
#include "ZInterrupt.h"

#define pPartition ((RZK_PARTITION_t *) hPartition)
#define pCurrentThread ((RZK_TCB_t *) hCurrentThread) 

/** extern functions */
extern RZK_HANDLE_t  hCurrentThread;

/* Function			:	RZKAllocFixedSizeMemory
*
* Description		:	This function allocates a fixed size 
*						of memory from the specified
*						partition if available  and returns
*						pointer to the memory block.
* 
* Inputs			:	hPartition -This parameter specifies
*						the handle of the partition area
*						required for allocating a memory block. 
*
* Outputs			:	RZKERR_INVALID_HANDLE - This error occurs when the 
*												hPartition handle is invalid.
*						RZKERR_CB_BUSY- When the partition is being used 
*										exclusively by a thread.
*						RZKERR_OUT_OF_MEMORY - This error occurs when there
*												 is no memory to allocate.
*							
*
* Dependencies		:	hCurrentThread
*/
RZK_PTR_t RZKAllocFixedSizeMemory
						(
							RZK_PARTITIONHANDLE_t  hPartition
						)
{
	/* Local Variables */
	RZK_PTR_t pMemory;
	RZK_STATE_t uState;
#ifdef RZK_DEBUG
			/*Validate the handle that has been passed */
	if((pPartition == NULL)||(pPartition ->uState == 0 ) || 
		(pPartition->magic_num != MAGIC_NUM_MEMORY))
	{
		pCurrentThread->errNum = RZKERR_INVALID_HANDLE;
		return NULL;
	}

#endif

#ifndef RZK_PERFORMANCE
	if(!(pPartition->uState & OBJECT_CREATED))
	{
		pCurrentThread->errNum = RZKERR_CB_BUSY;
		return NULL;
	} 	
#endif

	if(pPartition->uMemoryBlocksLeft == 0 )
	{
		pCurrentThread->errNum = RZKERR_OUT_OF_MEMORY;
		return NULL;
	}


	/* Make pMemory point to allocated block */
	uState = RZKDisablePreemption();
	pMemory = *(pPartition->pRead);
	pPartition->uMemoryBlocksLeft --;
	pPartition->pRead ++;
	
	/* Circular buffer mechanism */
	if(pPartition->pRead == pPartition->pEnd )
		pPartition->pRead = (RZK_PTR_t *) pPartition->pStart;

	RZKRestorePreemption(uState);
	pCurrentThread->errNum = RZKERR_SUCCESS;
	return pMemory;
}   /* end of RZKAllocFixedSizeMemory */

⌨️ 快捷键说明

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