/*** BeginHeader */
#ifndef __MCOS_MEM_LIB
#define __MCOS_MEM_LIB

// uC/OS-2 version: 1.05

/*** EndHeader */

/*
*********************************************************************************************************
*                                                uC/OS-II
*                                          The Real-Time Kernel
*                                            MEMORY MANAGEMENT
*
*                          (c) Copyright 1992-2001, Jean J. Labrosse, Weston, FL
*                                           All Rights Reserved
*
* File : OS_MEM.C
* By   : Jean J. Labrosse
*********************************************************************************************************
*/

/*** BeginHeader OSMemCreate, OSMemGet, OSMemPut, OSMemQuery, OS_MemInit */
#if (OS_MEM_EN > 0) && (OS_MAX_MEM_PART > 0)
OS_MEM *OSMemCreate (void *addr, INT32U nblks, INT32U blksize, INT8U *err);
extern  OS_MEM      *OSMemFreeList;	 	// Storage for memory partition manager
extern  OS_MEM       OSMemTbl[];			// Pointer to free list of memory partitions
void *OSMemGet (OS_MEM *pmem, INT8U *err);
INT8U OSMemPut (OS_MEM  *pmem, void *pblk);
void OS_MemInit (void);

#if OS_MEM_QUERY_EN > 0
INT8U OSMemQuery (OS_MEM *pmem, OS_MEM_DATA *pdata);
#endif

#endif
/*** EndHeader */

#if (OS_MEM_EN > 0) && (OS_MAX_MEM_PART > 0)
OS_MEM      *OSMemFreeList;	 				// Storage for memory partition manager
OS_MEM       OSMemTbl[OS_MAX_MEM_PART];	// Pointer to free list of memory partitions

/*
*********************************************************************************************************
*                                        CREATE A MEMORY PARTITION
*********************************************************************************************************
*/


/* START FUNCTION DESCRIPTION ********************************************
OSMemCreate                                                 <UCOS.LIB>

SYNTAX:       	OS_MEM *OSMemCreate (void *addr, INT32U nblks,
												INT32U blksize, INT8U *err);

DESCRIPTION:	Create a fixed-sized memory partition that will be managed
					by uC/OS-II.

PARAMETER1:    The starting address of the memory partition.

PARAMETER2:    The number of memory blocks to create from the partition.

PARAMETER3:    The size (in bytes) of each block in the memory partition.

PARAMETER4:    Pointer to a variable containing an error message which will
					be set by this function to either:
               OS_NO_ERR            if the memory partition has been
                                    created correctly.
               OS_MEM_INVALID_ADDR  you are specifying an invalid address
                                    for the memory storage of the
                                    partition.
               OS_MEM_INVALID_PART  no free partitions available
               OS_MEM_INVALID_BLKS  user specified an invalid number of
               							blocks (must be >= 2)
               OS_MEM_INVALID_SIZE  user specified an invalid block size
                                    (must be greater than the size of a
                                    pointer)

RETURN VALUE:  != (OS_MEM *)0  if the partition was created
               == (OS_MEM *)0  if the partition was not created because of
               					 invalid arguments or, no free partition is
               					 available.
END DESCRIPTION **********************************************************/

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

#if OS_ARG_CHK_EN > 0
    if (addr == (void *)0) {                          /* Must pass a valid address for the memory part. */
        *err = OS_MEM_INVALID_ADDR;
        return ((OS_MEM *)0);
    }
    if (nblks < 2) {                                  /* Must have at least 2 blocks per partition      */
        *err = OS_MEM_INVALID_BLKS;
        return ((OS_MEM *)0);
    }
    if (blksize < sizeof(void *)) {                   /* Must contain space for at least a pointer      */
        *err = OS_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             */
        *err = OS_MEM_INVALID_PART;
        return ((OS_MEM *)0);
    }
    plink = (void **)addr;                            /* Create linked list of free memory blocks      */
    pblk  = (INT8U *)addr + (INT16U)blksize;
    for (i = 0; i < (nblks - 1); i++) {
        *plink = (void *)pblk;
        plink  = (void **)pblk;
        pblk   = pblk + (INT16U)blksize;
    }
    *plink = (void *)0;                               /* Last memory block points to NULL              */
    OS_ENTER_CRITICAL();
    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        */
    OS_EXIT_CRITICAL();
    *err   = OS_NO_ERR;
    return (pmem);
}

/*
*********************************************************************************************************
*                                          GET A MEMORY BLOCK
*********************************************************************************************************
*/

/* START FUNCTION DESCRIPTION ********************************************
OSMemGet                                                    <UCOS.LIB>

SYNTAX:    		void *OSMemGet (OS_MEM *pmem, INT8U *err);

DESCRIPTION:	Get a memory block from a partition.

PARAMETER1:    Pointer to the memory partition control block.

PARAMETER2:    Pointer to a variable containing an error message which will
               be set by this function to either:
               OS_NO_ERR           if the memory partition has been created
                                   correctly.
               OS_MEM_NO_FREE_BLKS if there are no more free memory blocks
               						  to allocate to caller
               OS_MEM_INVALID_PMEM if you passed a NULL pointer for 'pmem'

RETURN VALUE:  A pointer to a memory block if no error is detected
               A pointer to NULL if an error is detected
END DESCRIPTION **********************************************************/

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

#if OS_ARG_CHK_EN > 0
    if (pmem == (OS_MEM *)0) {                        /* Must point to a valid memory partition         */
        *err = OS_MEM_INVALID_PMEM;
        return ((OS_MEM *)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();
        *err = OS_NO_ERR;                             /*      No error                                 */
        return (pblk);                                /*      Return memory block to caller            */
    }
    OS_EXIT_CRITICAL();
    *err = OS_MEM_NO_FREE_BLKS;                       /* No,  Notify caller of empty memory partition  */
    return ((void *)0);                               /*      Return NULL pointer to caller            */
}

/*
*********************************************************************************************************
*                                         RELEASE A MEMORY BLOCK
*********************************************************************************************************
*/

/* START FUNCTION DESCRIPTION ********************************************
OSMemPut                                                    <UCOS.LIB>

SYNTAX:    		INT8U OSMemPut (OS_MEM  *pmem, void *pblk);

DESCRIPTION:	Returns a memory block to a partition.

PARAMETER1:    Pointer to the memory partition control block.

PARAMETER2:    Pointer to the memory block being released.

RETURN VALUE:  OS_NO_ERR            if the memory block was inserted into
                                    the partition
               OS_MEM_FULL          if returning a memory block to an
                                    already FULL memory partition (More
                                    blocks were freed than allocated!)
               OS_MEM_INVALID_PMEM  if you passed a NULL pointer for 'pmem'
               OS_MEM_INVALID_PBLK  if you passed a NULL pointer for the
                                    block to release.
END DESCRIPTION **********************************************************/

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

#if OS_ARG_CHK_EN > 0
    if (pmem == (OS_MEM *)0) {                   /* Must point to a valid memory partition             */
        return (OS_MEM_INVALID_PMEM);
    }
    if (pblk == (void *)0) {                     /* Must release a valid block                         */
        return (OS_MEM_INVALID_PBLK);
    }
#endif
    OS_ENTER_CRITICAL();
    if (pmem->OSMemNFree >= pmem->OSMemNBlks) {  /* Make sure all blocks not already returned          */
        OS_EXIT_CRITICAL();
        return (OS_MEM_FULL);
    }
    *(void **)pblk      = pmem->OSMemFreeList;   /* Insert released block into free block list         */
    pmem->OSMemFreeList = pblk;
    pmem->OSMemNFree++;                          /* One more memory block in this partition            */
    OS_EXIT_CRITICAL();
    return (OS_NO_ERR);                          /* Notify caller that memory block was released       */
}

/*
*********************************************************************************************************
*                                          QUERY MEMORY PARTITION
*********************************************************************************************************
*/

/* START FUNCTION DESCRIPTION ********************************************
OSMemQuery                                                  <UCOS.LIB>

SYNTAX:    		INT8U OSMemQuery (OS_MEM *pmem, OS_MEM_DATA *pdata);

DESCRIPTION:	This function is used to determine the number of free memory
               blocks and the number of used memory blocks from a memory
               partition.

PARAMETER1:    Pointer to the memory partition control block.

PARAMETER2:    Pointer to a structure that will contain information about
					the memory partition.

RETURN VALUE:  OS_NO_ERR            If no errors were found.
               OS_MEM_INVALID_PMEM  if you passed a NULL pointer for 'pmem'
               OS_MEM_INVALID_PDATA if you passed a NULL pointer for the
               block to release.
END DESCRIPTION **********************************************************/

#if OS_MEM_QUERY_EN > 0
nodebug
INT8U  OSMemQuery (OS_MEM *pmem, OS_MEM_DATA *pdata)
{
#if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */
    OS_CPU_SR  cpu_sr;
#endif

#if OS_ARG_CHK_EN > 0
    if (pmem == (OS_MEM *)0) {                   /* Must point to a valid memory partition             */
        return (OS_MEM_INVALID_PMEM);
    }
    if (pdata == (OS_MEM_DATA *)0) {             /* Must release a valid storage area for the data     */
        return (OS_MEM_INVALID_PDATA);
    }
#endif
    OS_ENTER_CRITICAL();
    pdata->OSAddr     = pmem->OSMemAddr;
    pdata->OSFreeList = pmem->OSMemFreeList;
    pdata->OSBlkSize  = pmem->OSMemBlkSize;
    pdata->OSNBlks    = pmem->OSMemNBlks;
    pdata->OSNFree    = pmem->OSMemNFree;
    OS_EXIT_CRITICAL();
    pdata->OSNUsed    = pdata->OSNBlks - pdata->OSNFree;
    return (OS_NO_ERR);
}
#endif

/*
*********************************************************************************************************
*                                    INITIALIZE MEMORY PARTITION MANAGER
*********************************************************************************************************
*/

/* START _FUNCTION DESCRIPTION ********************************************
OS_MemInit                                                   <UCOS.LIB>

SYNTAX:    		void OS_MemInit ();

DESCRIPTION:	This function is called by uC/OS-II to initialize the memory
               partition manager.  Application code MUST NOT call this
               function.

END DESCRIPTION **********************************************************/

nodebug
void  OS_MemInit (void)
{
#if OS_MAX_MEM_PART == 1
    OSMemFreeList                = (OS_MEM *)&OSMemTbl[0]; /* Point to beginning of free list          */
    OSMemFreeList->OSMemFreeList = (void *)0;              /* Initialize last node                     */
    OSMemFreeList->OSMemAddr     = (void *)0;              /* Store start address of memory partition  */
    OSMemFreeList->OSMemNFree    = 0;                      /* No free blocks                           */
    OSMemFreeList->OSMemNBlks    = 0;                      /* No blocks                                */
    OSMemFreeList->OSMemBlkSize  = 0;                      /* Zero size                                */
#endif

#if OS_MAX_MEM_PART >= 2
    OS_MEM  *pmem;
    INT16U   i;


    pmem = (OS_MEM *)&OSMemTbl[0];                    /* Point to memory control block (MCB)           */
    for (i = 0; i < (OS_MAX_MEM_PART - 1); i++) {     /* Init. list of free memory partitions          */
        pmem->OSMemFreeList = (void *)&OSMemTbl[i+1]; /* Chain list of free partitions                 */
        pmem->OSMemAddr     = (void *)0;              /* Store start address of memory partition       */
        pmem->OSMemNFree    = 0;                      /* No free blocks                                */
        pmem->OSMemNBlks    = 0;                      /* No blocks                                     */
        pmem->OSMemBlkSize  = 0;                      /* Zero size                                     */
        pmem++;
    }
    pmem->OSMemFreeList = (void *)0;                  /* Initialize last node                          */
    pmem->OSMemAddr     = (void *)0;                  /* Store start address of memory partition       */
    pmem->OSMemNFree    = 0;                          /* No free blocks                                */
    pmem->OSMemNBlks    = 0;                          /* No blocks                                     */
    pmem->OSMemBlkSize  = 0;                          /* Zero size                                     */

    OSMemFreeList       = (OS_MEM *)&OSMemTbl[0];     /* Point to beginning of free list               */
#endif
}

#endif

/*** BeginHeader */
#endif
/*** EndHeader */
