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

📄 os_mem.c

📁 MCB2300_ucgui_LCD320240.rar LPC2368的uc/gui的移植
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
*********************************************************************************************************
*   											 uC/OS-II
*   									   The Real-Time Kernel
*   										 MEMORY MANAGEMENT
*
*   					   (c) Copyright 1992-2007, Jean J. Labrosse, Weston, FL
*   										All Rights Reserved
*
* File    : OS_MEM.C
* By	  : Jean J. Labrosse
* Version : V2.85
*
* LICENSING TERMS:
* ---------------
*   uC/OS-II is provided in source form for FREE evaluation, for educational use or for peaceful research.  
* If you plan on using  uC/OS-II  in a commercial product you need to contact Micri祄 to properly license 
* its use in your product. We provide ALL the source code for your convenience and to help you experience 
* uC/OS-II.   The fact that the  source is provided does  NOT  mean that you can use it without  paying a 
* licensing fee.
*********************************************************************************************************
*/

#ifndef  OS_MASTER_FILE
#include <ucos_ii.h>
#endif

#if (OS_MEM_EN > 0) && (OS_MAX_MEM_PART > 0)
/*
*********************************************************************************************************
*   									 CREATE A MEMORY PARTITION
*
* Description : Create a fixed-sized memory partition that will be managed by uC/OS-II.
*
* Arguments   : addr	 is the starting address of the memory partition
*
*   			nblks    is the number of memory blocks to create from the partition.
*
*   			blksize  is the size (in bytes) of each block in the memory partition.
*
*   			perr	 is a pointer to a variable containing an error message which will be set by
*   					 this function to either:
*
*   					 OS_ERR_NONE			  if the memory partition has been created correctly.
*   					 OS_ERR_MEM_INVALID_ADDR  if you are specifying an invalid address for the memory
*   											  storage of the partition or, the block does not align
*   											  on a pointer boundary
*   					 OS_ERR_MEM_INVALID_PART  no free partitions available
*   					 OS_ERR_MEM_INVALID_BLKS  user specified an invalid number of blocks (must be >= 2)
*   					 OS_ERR_MEM_INVALID_SIZE  user specified an invalid block size
*   												- must be greater than the size of a pointer
*   												- must be able to hold an integral number of pointers
* Returns    : != (OS_MEM *)0  is the partition was created
*   		   == (OS_MEM *)0  if the partition was not created because of invalid arguments or, no
*   						   free partition is available.
*********************************************************************************************************
*/

OS_MEM * OSMemCreate(void *addr, INT32U nblks, INT32U blksize, INT8U *perr)
{
	OS_MEM    *pmem;
	INT8U     *pblk;
	void	 **plink;
	INT32U     i;
#if OS_CRITICAL_METHOD == 3 						  /* Allocate storage for CPU status register      */
	OS_CPU_SR  cpu_sr = 0;
#endif



#if OS_ARG_CHK_EN > 0   		   
	if (perr == (INT8U *) 0)
	{
		/* Validate 'perr'  							 */
		return ((OS_MEM *) 0);
	}
	if (addr == (void *) 0)
	{
		/* Must pass a valid address for the memory part.*/
		*perr = OS_ERR_MEM_INVALID_ADDR;
		return ((OS_MEM *) 0);
	}
	if (((INT32U) addr & (sizeof(void *) - 1)) != 0)
	{
		/* Must be pointer size aligned 				 */
		*perr = OS_ERR_MEM_INVALID_ADDR;
		return ((OS_MEM *) 0);
	}
	if (nblks < 2)
	{
		/* Must have at least 2 blocks per partition	 */
		*perr = OS_ERR_MEM_INVALID_BLKS;
		return ((OS_MEM *) 0);
	}
	if (blksize < sizeof(void *))
	{
		/* Must contain space for at least a pointer	 */
		*perr = OS_ERR_MEM_INVALID_SIZE;
		return ((OS_MEM *) 0);
	}
#endif
	OS_ENTER_CRITICAL();
	pmem = OSMemFreeList;   						  /* Get next free memory partition 			   */
	if (OSMemFreeList != (OS_MEM *) 0)
	{
		/* See if pool of free partitions was empty 	 */
		OSMemFreeList = (OS_MEM *) OSMemFreeList->OSMemFreeList;
	}
	OS_EXIT_CRITICAL();
	if (pmem == (OS_MEM *) 0)
	{
		/* See if we have a memory partition			 */
		*perr = OS_ERR_MEM_INVALID_PART;
		return ((OS_MEM *) 0);
	}
	plink = (void * *) addr;							/* Create linked list of free memory blocks 	 */
	pblk = (INT8U *) ((INT32U) addr + blksize);
	for (i = 0; i < (nblks - 1); i++)
	{
		*plink = (void *) pblk; 						/* Save pointer to NEXT block in CURRENT block   */
		plink = (void * *) pblk;						/* Position to  NEXT	  block 				 */
		pblk = (INT8U *) ((INT32U) pblk + blksize);    /* Point to the FOLLOWING block  				*/
	}
	*plink = (void *) 0;				  /* Last memory block points to NULL   		   */
	pmem->OSMemAddr = addr; 					  /* Store start address of memory partition	   */
	pmem->OSMemFreeList = addr; 					  /* Initialize pointer to pool of free blocks     */
	pmem->OSMemNFree = nblks;   				   /* Store number of free blocks in MCB			*/
	pmem->OSMemNBlks = nblks;
	pmem->OSMemBlkSize = blksize;   				 /* Store block size of each memory blocks  	  */
	*perr = OS_ERR_NONE;
	return (pmem);
}
/*$PAGE*/
/*
*********************************************************************************************************
*   									   GET A MEMORY BLOCK
*
* Description : Get a memory block from a partition
*
* Arguments   : pmem	is a pointer to the memory partition control block
*
*   			perr	is a pointer to a variable containing an error message which will be set by this
*   					function to either:
*
*   					OS_ERR_NONE 			if the memory partition has been created correctly.
*   					OS_ERR_MEM_NO_FREE_BLKS if there are no more free memory blocks to allocate to caller
*   					OS_ERR_MEM_INVALID_PMEM if you passed a NULL pointer for 'pmem'
*
* Returns     : A pointer to a memory block if no error is detected
*   			A pointer to NULL if an error is detected
*********************************************************************************************************
*/

void * OSMemGet(OS_MEM *pmem, INT8U *perr)
{
	void	  *pblk;
#if OS_CRITICAL_METHOD == 3 						  /* Allocate storage for CPU status register      */
	OS_CPU_SR  cpu_sr = 0;
#endif



#if OS_ARG_CHK_EN > 0
	if (perr == (INT8U *) 0)
	{
		/* Validate 'perr'  							 */
		return ((void *) 0);
	}
	if (pmem == (OS_MEM *) 0)
	{
		/* Must point to a valid memory partition   	 */
		*perr = OS_ERR_MEM_INVALID_PMEM;
		return ((void *) 0);
	}
#endif
	OS_ENTER_CRITICAL();
	if (pmem->OSMemNFree > 0)
	{
		/* See if there are any free memory blocks  	 */
		pblk = pmem->OSMemFreeList;    /* Yes, point to next free memory block  		*/
		pmem->OSMemFreeList = *(void * *) pblk; 		/*  	Adjust pointer to new free list 		 */
		pmem->OSMemNFree--; 						  /*	  One less memory block in this partition  */
		OS_EXIT_CRITICAL();
		*perr = OS_ERR_NONE;						  /*	  No error  							   */
		return (pblk);  							  /*	  Return memory block to caller 		   */
	}
	OS_EXIT_CRITICAL();
	*perr = OS_ERR_MEM_NO_FREE_BLKS;				  /* No,  Notify caller of empty memory partition  */
	return ((void *) 0);							   /*      Return NULL pointer to caller			*/
}
/*$PAGE*/
/*
*********************************************************************************************************
*   								GET THE NAME OF A MEMORY PARTITION
*
* Description: This function is used to obtain the name assigned to a memory partition.
*
* Arguments  : pmem 	 is a pointer to the memory partition
*
*   		   pname	 is a pointer to an ASCII string that will receive the name of the memory partition.
*
*   		   perr 	 is a pointer to an error code that can contain one of the following values:
*
*   					 OS_ERR_NONE				if the name was copied to 'pname'
*   					 OS_ERR_MEM_INVALID_PMEM	if you passed a NULL pointer for 'pmem'
*   					 OS_ERR_PNAME_NULL  		You passed a NULL pointer for 'pname'
*   					 OS_ERR_NAME_GET_ISR		You called this function from an ISR
*
* Returns    : The length of the string or 0 if 'pmem' is a NULL pointer.
*********************************************************************************************************
*/

#if OS_MEM_NAME_SIZE > 1
INT8U OSMemNameGet(OS_MEM *pmem, INT8U *pname, INT8U *perr)
{
	INT8U      len;
#if OS_CRITICAL_METHOD == 3 					 /* Allocate storage for CPU status register		   */
	OS_CPU_SR  cpu_sr = 0;
#endif



#if OS_ARG_CHK_EN > 0
	if (perr == (INT8U *) 0)
	{
		/* Validate 'perr'  								  */
		return (0);
	}
	if (pmem == (OS_MEM *) 0)
	{
		/* Is 'pmem' a NULL pointer?						  */
		*perr = OS_ERR_MEM_INVALID_PMEM;
		return (0);
	}
	if (pname == (INT8U *) 0)
	{
		/* Is 'pname' a NULL pointer?   					  */
		*perr = OS_ERR_PNAME_NULL;
		return (0);
	}
#endif
	if (OSIntNesting > 0)
	{

⌨️ 快捷键说明

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