William Kane / Generic

Dependents:   LaserioLib

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers abcc_mem.c Source File

abcc_mem.c

00001 /*******************************************************************************
00002 ********************************************************************************
00003 **                                                                            **
00004 ** ABCC Driver version 4.01.01 (2015-12-14)                                   **
00005 **                                                                            **
00006 ** Delivered with:                                                            **
00007 **    ABP         7.16.01 (2015-10-14)                                        **
00008 **                                                                            */
00009 /*******************************************************************************
00010 ********************************************************************************
00011 ** COPYRIGHT NOTIFICATION (c) 2013 HMS Industrial Networks AB                 **
00012 **                                                                            **
00013 ** This code is the property of HMS Industrial Networks AB.                   **
00014 ** The source code may not be reproduced, distributed, or used without        **
00015 ** permission. When used together with a product from HMS, permission is      **
00016 ** granted to modify, reproduce and distribute the code in binary form        **
00017 ** without any restrictions.                                                  **
00018 **                                                                            **
00019 ** THE CODE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. HMS DOES NOT    **
00020 ** WARRANT THAT THE FUNCTIONS OF THE CODE WILL MEET YOUR REQUIREMENTS, OR     **
00021 ** THAT THE OPERATION OF THE CODE WILL BE UNINTERRUPTED OR ERROR-FREE, OR     **
00022 ** THAT DEFECTS IN IT CAN BE CORRECTED.                                       **
00023 ********************************************************************************
00024 ********************************************************************************
00025 ** File Description:
00026 ** Memory allocation implementation.
00027 ********************************************************************************
00028 ********************************************************************************
00029 */
00030 
00031 #include "abcc_drv_cfg.h"
00032 #include "abcc_td.h"
00033 #include "abcc.h"
00034 #include "abcc_mem.h"
00035 #include "abcc_sys_adapt.h"
00036 #include "abcc_port.h"
00037 #include "abcc_debug_err.h"
00038 
00039 /*******************************************************************************
00040 ** Constants
00041 ********************************************************************************
00042 */
00043 
00044 /*
00045 ** Set default value for maximum number of resources
00046 */
00047 #ifndef ABCC_CFG_MAX_NUM_MSG_RESOURCES
00048 #define ABCC_CFG_MAX_NUM_MSG_RESOURCES          ( ABCC_CFG_MAX_NUM_APPL_CMDS + ABCC_CFG_MAX_NUM_ABCC_CMDS )
00049 #endif
00050 
00051 /*
00052 ** Magic cookie
00053 */
00054 #define ABCC_MEM_MAGIC_COOKIE  0x5CC5
00055 
00056 /*******************************************************************************
00057 ** Typedefs
00058 ********************************************************************************
00059 */
00060 
00061 /*------------------------------------------------------------------------------
00062 ** Structure for defining size of memory message allocation
00063 **
00064 ** The magic cookie field is used to evaluate if the buffer status field
00065 ** is broken. The buffer status could be broken if the user writes outside the
00066 ** bounds of the message data area.
00067 **------------------------------------------------------------------------------
00068 */
00069 typedef struct
00070 {
00071    ABP_MsgHeaderType16 sHeader;
00072    UINT32   alData[ ( ABCC_CFG_MAX_MSG_SIZE + 3 ) >> 2 ];
00073    UINT16   iMagicCookie;
00074    UINT16   iBufferStatus;
00075 }
00076 PACKED_STRUCT ABCC_MemAllocType;
00077 
00078 /*------------------------------------------------------------------------------
00079 ** Union used for casting between memory ABCC_MemAllocType and ABP_MsgType.
00080 **------------------------------------------------------------------------------
00081 */
00082 typedef union
00083 {
00084    ABCC_MemAllocType* psAllocMsg;
00085    ABP_MsgType* psMsg;
00086 }
00087 ABCC_MemAllocUnion;
00088 
00089 /*------------------------------------------------------------------------------
00090 ** Memory pool structure
00091 **
00092 ** ------------------
00093 ** iNumFreeMsg = 3  |
00094 ** ------------------
00095 ** Msg 0 pointer    |---|
00096 ** ------------------   |
00097 ** Msg 1 pointer    |---+--|
00098 ** ------------------   |  |
00099 ** Msg 2 pointer    |---+--+--|
00100 ** ------------------   |  |  |
00101 ** Msg 0           |<--|  |  |
00102 ** ------------------      |  |
00103 ** Msg 1           |<-----|  |
00104 ** ------------------         |
00105 ** Msg 2           |<--------|
00106 ** ------------------
00107 **------------------------------------------------------------------------------
00108 */
00109 static UINT16 abcc_iNumFreeMsg;
00110 static ABCC_MemAllocUnion abcc_uFreeMsgStack[ ABCC_CFG_MAX_NUM_MSG_RESOURCES ];
00111 static ABCC_MemAllocType  abcc_asMsgPool[ ABCC_CFG_MAX_NUM_MSG_RESOURCES ];
00112 
00113 
00114 
00115 /*******************************************************************************
00116 ** Public Globals
00117 ********************************************************************************
00118 */
00119 
00120 /*******************************************************************************
00121 ** Private Globals
00122 ********************************************************************************
00123 */
00124 
00125 /*******************************************************************************
00126 ** Private Services
00127 ********************************************************************************
00128 */
00129 
00130 /*******************************************************************************
00131 ** Public Services
00132 ********************************************************************************
00133 */
00134 
00135 void ABCC_MemCreatePool( void )
00136 {
00137    UINT16 i;
00138 
00139    abcc_iNumFreeMsg = ABCC_CFG_MAX_NUM_MSG_RESOURCES;
00140 
00141    for( i = 0;  i < ABCC_CFG_MAX_NUM_MSG_RESOURCES ; i++ )
00142    {
00143       abcc_uFreeMsgStack[ i ].psAllocMsg = &abcc_asMsgPool[ i ];
00144       abcc_asMsgPool[ i ].iMagicCookie = ABCC_MEM_MAGIC_COOKIE;
00145       abcc_asMsgPool[ i ].iBufferStatus = ABCC_MEM_BUFSTAT_FREE;
00146    }
00147 }
00148 
00149 ABP_MsgType* ABCC_MemAlloc( void )
00150 {
00151    ABP_MsgType* pxItem = NULL;
00152    ABCC_PORT_UseCritical();
00153    ABCC_PORT_EnterCritical();
00154    if( abcc_iNumFreeMsg > 0 )
00155    {
00156       abcc_iNumFreeMsg--;
00157       pxItem = abcc_uFreeMsgStack[ abcc_iNumFreeMsg ].psMsg;
00158       ( (ABCC_MemAllocType*)pxItem )->iBufferStatus = ABCC_MEM_BUFSTAT_ALLOCATED;
00159    }
00160 
00161    ABCC_PORT_ExitCritical();
00162 
00163    ABCC_DEBUG_MSG_GENERAL( ( "Mem: Buffer allocated: 0x%08x\n", (UINT32)pxItem ) );
00164 
00165    return( pxItem );
00166 }
00167 
00168 void ABCC_MemFree( ABP_MsgType** pxItem)
00169 {
00170    ABCC_MemAllocType* const psBuf = (ABCC_MemAllocType*)(*pxItem);
00171    ABCC_PORT_UseCritical();
00172 
00173    ABCC_DEBUG_MSG_GENERAL( ( "Mem: Buffer returned:  0x%08x\n", (UINT32)*pxItem ) );
00174 
00175    if( psBuf->iMagicCookie != ABCC_MEM_MAGIC_COOKIE )
00176    {
00177       ABCC_CbfDriverError( ABCC_SEV_FATAL,
00178                            ABCC_EC_MSG_BUFFER_CORRUPTED,
00179                            (UINT32)psBuf );
00180       return;
00181    }
00182 
00183    if( psBuf->iBufferStatus == ABCC_MEM_BUFSTAT_FREE )
00184    {
00185       ABCC_CbfDriverError( ABCC_SEV_FATAL,
00186                            ABCC_EC_MSG_BUFFER_ALREADY_FREED,
00187                            (UINT32)psBuf );
00188       return;
00189    }
00190 
00191    ABCC_PORT_EnterCritical();
00192 
00193    abcc_uFreeMsgStack[ abcc_iNumFreeMsg ].psAllocMsg = psBuf;
00194    abcc_iNumFreeMsg++;
00195    psBuf->iBufferStatus = ABCC_MEM_BUFSTAT_FREE;
00196    *pxItem = NULL;
00197 
00198    ABCC_PORT_ExitCritical();
00199 }
00200 
00201 ABCC_MemBufferStatusType ABCC_MemGetBufferStatus( ABP_MsgType* psMsg )
00202 {
00203    const ABCC_MemAllocType* const psBuf = (ABCC_MemAllocType*)psMsg;
00204 
00205    if( psBuf->iMagicCookie != ABCC_MEM_MAGIC_COOKIE )
00206    {
00207       ABCC_CbfDriverError( ABCC_SEV_FATAL,
00208                            ABCC_EC_MSG_BUFFER_CORRUPTED,
00209                            (UINT32)psBuf );
00210 
00211       return( ABCC_MEM_BUFSTAT_UNKNOWN );
00212    }
00213 
00214    return( (ABCC_MemBufferStatusType)psBuf->iBufferStatus );
00215 }
00216 
00217 void ABCC_MemSetBufferStatus( ABP_MsgType* psMsg,
00218                               ABCC_MemBufferStatusType eStatus )
00219 {
00220    ABCC_MemAllocType* const psBuf = (ABCC_MemAllocType*)psMsg;
00221 
00222    if( psBuf->iMagicCookie != ABCC_MEM_MAGIC_COOKIE )
00223    {
00224       ABCC_CbfDriverError( ABCC_SEV_FATAL,
00225                            ABCC_EC_MSG_BUFFER_CORRUPTED,
00226                            (UINT32)psMsg );
00227       return;
00228    }
00229 
00230    psBuf->iBufferStatus = eStatus;
00231 }
00232 
00233 /*******************************************************************************
00234 ** Tasks
00235 ********************************************************************************
00236 */