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

📄 rzkfreefixedsizememory.c

📁 zilog的实时操作系统RZK,可以移植到多种处理器上
💻 C
字号:
/*
* File				:	RZKFreeFixedSizeMemory.c
*
* Description		:	This file defines RZKFreeFixedSizeMemory API.
* 
* 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)
extern RZK_PARTITION_CB_t  nPartition[];
extern UINT MAX_PARTITIONS;
/* Function			:	RZKFreeFixedSizeMemory
*
* Description		:	This function frees a fixed size of memory from the
*						 specified partition.
* 
* Inputs			:	hPartition - This parameter specifies the handle of 
*									 the partition area for freeing a 
*									 memory block.
*						pBlock - Pointer to the memory block to be freed.
*							
* Outputs			:	RZKERR_INVALID_HANDLE - This error occurs when the 
*											hPartition handle is invalid.
*						RZKERR_CB_BUSY- The partition is being used exclusively
*										 by a thread.
*						RZKERR_INVALID_OPERATION - This error occurs when 
*													overflow is detected.
*
* Dependencies		:	None.
*/
RZK_STATUS_t RZKFreeFixedSizeMemory
					(
						RZK_PARTITIONHANDLE_t hPartition,
						RZK_PTR_t pBlock
					)
{
	RZK_PTR_t *ptr ;
	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))
			return RZKERR_INVALID_HANDLE;
	if(pBlock == NULL)
			return 	RZKERR_INVALID_ARGUMENTS;

	if( 
	    ( ( UINT8* ) pBlock < ( UINT8* ) pPartition->pEnd )
	  )
	  {
		return RZKERR_INVALID_ARGUMENTS ;
          }
     
	if( 
	    ( ( UINT8* ) pBlock > ( ( UINT8* ) pPartition->pEnd + (UINT32)( (UINT32)pPartition->uMemoryBlocks * (UINT32)pPartition->uBlockSize ) ) ) 
	  )
	  {
		return RZKERR_INVALID_ARGUMENTS ;
          }
          
	if( 
            ( ( ( ( UINT8* ) pBlock - ( UINT8* ) pPartition->pEnd ) % pPartition->uBlockSize ) != 0 ) 
	  )
	  {
		return RZKERR_INVALID_ARGUMENTS ;
          }                            
	/** check whether the block to be freed is a valid one or not */
// For IAR port	
	if( 
	    ( ( UINT8* ) pBlock < ( UINT8* ) pPartition->pEnd ) || 
	    ( ( UINT8* ) pBlock > ( ( UINT8* ) pPartition->pEnd + (UINT32)( (UINT32)pPartition->uMemoryBlocks * (UINT32)pPartition->uBlockSize ) ) )  || 
            ( ( ( ( UINT8* ) pBlock - ( UINT8* ) pPartition->pEnd ) % pPartition->uBlockSize ) != 0 ) 
	  )
	  {
		return RZKERR_INVALID_ARGUMENTS ;
          }
      
/*	
	if( ( ( UINT ) pBlock < ( UINT ) pPartition->pEnd ) || 
			( ( UINT ) pBlock > ( ( UINT ) pPartition->pEnd + ( pPartition->uMemoryBlocks * pPartition->uBlockSize ) ) )  || 
			( ( ( UINT ) pBlock - ( UINT ) pPartition->pEnd ) % pPartition->uBlockSize ) != 0 ) {
		return RZKERR_INVALID_ARGUMENTS ;
	}
*/	
#endif

#ifndef RZK_PERFORMANCE
	if(!(pPartition->uState & OBJECT_CREATED))
			return RZKERR_CB_BUSY;	
#endif	
	if (pPartition->uMemoryBlocksLeft == pPartition->uMemoryBlocks )
			return RZKERR_INVALID_OPERATION;

	/** 
	 * traverse through the circular buffer to find the pBlock present or not.
	 * if present, the block is already deleted and return an error
	 */
	if( pPartition->pRead != pPartition->pWrite ) {
		ptr = pPartition->pRead ;
		do {
			if( *(ptr) == pBlock ) { /** already freed */
				return RZKERR_INVALID_OPERATION ;
			}
			ptr ++ ;
			if( ptr == pPartition->pEnd )  {
				ptr = ( RZK_PTR_t * ) pPartition->pStart ;
			}
		} while( ptr != pPartition->pWrite ) ;
	}
	
	uState = RZKDisablePreemption();

	*(pPartition->pWrite) = pBlock;
	pPartition->uMemoryBlocksLeft ++;
	pPartition->pWrite ++;

	if (pPartition->pWrite == pPartition->pEnd )
			pPartition->pWrite = ( RZK_PTR_t * ) pPartition->pStart;

	RZKRestorePreemption(uState);
	return RZKERR_SUCCESS;
}  /* end of RZKFreeFixedSizeMemory.*/

⌨️ 快捷键说明

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