From c86ffdae0a3565f2c3522b4d4e513a2a22268808 Mon Sep 17 00:00:00 2001 From: "u.harbuz" Date: Thu, 4 May 2017 15:07:52 +0200 Subject: [PATCH] Fix platform dependent types casts. Change-Id: I50ac18635a3cb0adfc9852a1d69f22f40be381e3 --- osal/OsaIpc.c | 31 +++++++++++++++++++++----- osal/OsaSem.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 85 insertions(+), 16 deletions(-) diff --git a/osal/OsaIpc.c b/osal/OsaIpc.c index 57bb384..6f2824e 100755 --- a/osal/OsaIpc.c +++ b/osal/OsaIpc.c @@ -131,6 +131,27 @@ typedef struct { #define MAX_NAMEDSEM_MGR 256 +static UlOsaSem_t* sem[MAX_NAMEDSEM_MGR]={0}; + + +unsigned int addptr(UlOsaSem_t*s) { + for (int i=0; i < MAX_NAMEDSEM_MGR; ++i) { + if (sem[i]==NULL) {sem[i]=s; return i;} + } + return -1; +} + + +UlOsaSem_t* getptr(unsigned int id) { + return sem[id]; +} + + +void rmid(unsigned int id) { + sem[id]=NULL; +} + + static int UlOsaNamedSemCreate(const char pcName[10], int iCount, int iAttribute, unsigned int* puiSmid) { int iRetVal = OSAL_OK; @@ -175,7 +196,7 @@ static int UlOsaNamedSemCreate(const char pcName[10], int iCount, memcpy((void*)sem->bName, (const void*)pcName, (size_t)10); sem->bName[10] = '\0'; - *puiSmid = (unsigned int)sem; + *puiSmid = addptr(sem); return iRetVal; } @@ -207,7 +228,7 @@ static int UlOsaNamedSemGet(unsigned int uiSmid, int iFlags, int iTimeout) { struct timeval tv; int ret; - UlOsaSem_t *sem = (UlOsaSem_t*)uiSmid; + UlOsaSem_t *sem = getptr(uiSmid); if (!sem) { return OSAL_ERROR; @@ -267,7 +288,7 @@ static int UlOsaNamedSemGet(unsigned int uiSmid, int iFlags, int iTimeout) { } static int UlOsaNamedSemRelease(unsigned int uiSmid) { - UlOsaSem_t *sem = (UlOsaSem_t*)uiSmid; + UlOsaSem_t *sem = getptr(uiSmid); struct sembuf semBuf; if (!sem) { @@ -288,7 +309,7 @@ static int UlOsaNamedSemRelease(unsigned int uiSmid) { } static int UlOsaNamedSemReset(unsigned int uiSmid) { - UlOsaSem_t *sem = (UlOsaSem_t*)uiSmid; + UlOsaSem_t *sem = getptr(uiSmid); union semun semUnion; if (!sem) { @@ -306,7 +327,7 @@ static int UlOsaNamedSemReset(unsigned int uiSmid) { } static int UlOsaNamedSemGetval(unsigned int uiSmid) { - UlOsaSem_t *sem = (UlOsaSem_t*)uiSmid; + UlOsaSem_t *sem = getptr(uiSmid); int n; if (!sem) { diff --git a/osal/OsaSem.c b/osal/OsaSem.c index eaef3e1..c2865ec 100755 --- a/osal/OsaSem.c +++ b/osal/OsaSem.c @@ -37,6 +37,30 @@ typedef struct _UlOsaSem { *-----------------------------------------------------------------------------*/ /* TODO: apply iAttribute */ // COMMON_071008_1 + +#define MAX_NAMEDSEM_MGR 256 +static UlOsaSem_t* sem[MAX_NAMEDSEM_MGR]={0}; + + +unsigned int addptr(UlOsaSem_t*s) { + for (int i=0; i < MAX_NAMEDSEM_MGR; ++i) { + if (sem[i]==NULL) {sem[i]=s; return i;} + } + return -1; +} + + +UlOsaSem_t* getptr(unsigned int id) { + return sem[id]; +} + + +void rmid(unsigned int id) { + sem[id]=NULL; +} + + + static int UlOsaSemCreate(const char bName[10], int iCount, int iAttribute, unsigned int* puiSmid) { UlOsaSem_t* sem; @@ -59,13 +83,13 @@ static int UlOsaSemCreate(const char bName[10], int iCount, int iAttribute, memcpy((void*)sem->bName, (const void*)bName, (size_t)10); sem->bName[10] = '\0'; - *puiSmid = (unsigned int)sem; + *puiSmid = addptr(sem); return OSAL_OK; } static int UlOsaSemDelete(unsigned int uiSmid) { - UlOsaSem_t *sem = (UlOsaSem_t*)uiSmid; + UlOsaSem_t *sem = getptr(uiSmid); if (!sem) { return OSAL_ERROR; @@ -73,13 +97,14 @@ static int UlOsaSemDelete(unsigned int uiSmid) { sem_destroy(&sem->sem); free(sem); + rmid(uiSmid); return OSAL_OK; } static int UlOsaSemGet(unsigned int uiSmid, int iFlags, int iTimeout) { int ret; - UlOsaSem_t *sem = (UlOsaSem_t*)uiSmid; + UlOsaSem_t *sem = getptr(uiSmid); if (!sem) { return OSAL_ERROR; @@ -148,7 +173,7 @@ static int UlOsaSemGet(unsigned int uiSmid, int iFlags, int iTimeout) { } static int UlOsaSemRelease(unsigned int uiSmid) { - UlOsaSem_t *sem = (UlOsaSem_t*)uiSmid; + UlOsaSem_t *sem = getptr(uiSmid); if (!sem) { return OSAL_ERROR; } @@ -163,7 +188,7 @@ static int UlOsaSemRelease(unsigned int uiSmid) { } static int UlOsaSemReset(unsigned int uiSmid) { - UlOsaSem_t *sem = (UlOsaSem_t*)uiSmid; + UlOsaSem_t *sem = getptr(uiSmid); if (!sem) { return OSAL_ERROR; } @@ -183,7 +208,7 @@ static int UlOsaSemReset(unsigned int uiSmid) { } static int UlOsaSemGetval(unsigned int uiSmid) { - UlOsaSem_t *sem = (UlOsaSem_t*)uiSmid; + UlOsaSem_t *sem = getptr(uiSmid); int n; if (!sem) { return OSAL_ERROR; @@ -301,6 +326,28 @@ int OsaSemReset(unsigned int uiSmid) { //------------------------------------------------------------------------------ // $$$ */ + + +pthread_mutex_t* mutexes[MAX_NAMEDSEM_MGR] = {0}; + +unsigned int add_mutex(pthread_mutex_t* s) { + for (int i=0; i < MAX_NAMEDSEM_MGR; ++i) { + if (mutexes[i]==NULL) {mutexes[i]=s; return i;} + } + return -1; +} + + +pthread_mutex_t* get_mutex(unsigned int id) { + return mutexes[id]; +} + + +void rmid_mutex(unsigned int id) { + mutexes[id]=NULL; +} + + int OsaMutCreate(const char bName[10], int iAttributes, unsigned int* puiMutid) { pthread_mutexattr_t attr_t; pthread_mutex_t* pmutex_t; @@ -328,7 +375,7 @@ int OsaMutCreate(const char bName[10], int iAttributes, unsigned int* puiMutid) break; } - (*puiMutid) = (unsigned int)pmutex_t; + (*puiMutid) = add_mutex(pmutex_t); pthread_mutexattr_destroy(&attr_t); } else { @@ -358,7 +405,7 @@ int OsaMutCreate(const char bName[10], int iAttributes, unsigned int* puiMutid) int OsaMutDelete(unsigned int uiMutid) { int iRet; - pthread_mutex_t* pmutex_t = (pthread_mutex_t *)uiMutid; + pthread_mutex_t* pmutex_t = get_mutex(uiMutid); if (pmutex_t == NULL) { return OSAL_OK; } @@ -371,6 +418,7 @@ int OsaMutDelete(unsigned int uiMutid) { } free(pmutex_t); + rmid_mutex(uiMutid); return OSAL_OK; } @@ -390,7 +438,7 @@ int OsaMutDelete(unsigned int uiMutid) { int OsaMutRelease(unsigned int uiMutid) { int iRet; - pthread_mutex_t* pmutex_t = (pthread_mutex_t *)uiMutid; + pthread_mutex_t* pmutex_t = get_mutex(uiMutid); iRet = pthread_mutex_unlock(pmutex_t); if (iRet < 0) { perror("In OsaMutRelease() : failed "); @@ -414,7 +462,7 @@ int OsaMutRelease(unsigned int uiMutid) { */ int OsaMutGet(unsigned int uiMutid, int iFlags, int iTimeout) { int iRet; - pthread_mutex_t* pmutex_t = (pthread_mutex_t *)uiMutid; + pthread_mutex_t* pmutex_t = get_mutex(uiMutid); iRet = pthread_mutex_lock(pmutex_t); if (iRet < 0) { perror("In OsaMutGet() : failed "); @@ -438,7 +486,7 @@ int OsaMutGet(unsigned int uiMutid, int iFlags, int iTimeout) { int OsaMutTryGet(unsigned int uiMutid, int iFlags, int iTimeout) { int iRet; - pthread_mutex_t* pmutex_t = (pthread_mutex_t *)uiMutid; + pthread_mutex_t* pmutex_t = get_mutex(uiMutid); iRet = pthread_mutex_trylock(pmutex_t); if (iRet) { return ((int)iRet); -- 2.7.4