Home | History | Annotate | Download | only in sec_osal
      1 /*
      2  *
      3  * Copyright 2010 Samsung Electronics S.LSI Co. LTD
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 /*
     19  * @file        SEC_OSAL_Queue.c
     20  * @brief
     21  * @author      SeungBeom Kim (sbcrux.kim (at) samsung.com)
     22  * @version     1.0
     23  * @history
     24  *   2010.7.15 : Create
     25  */
     26 
     27 
     28 #include <stdio.h>
     29 #include <stdlib.h>
     30 #include <string.h>
     31 
     32 #include "SEC_OSAL_Memory.h"
     33 #include "SEC_OSAL_Mutex.h"
     34 #include "SEC_OSAL_Queue.h"
     35 
     36 
     37 OMX_ERRORTYPE SEC_OSAL_QueueCreate(SEC_QUEUE *queueHandle)
     38 {
     39     int i = 0;
     40     SEC_QElem *newqelem = NULL;
     41     SEC_QElem *currentqelem = NULL;
     42     SEC_QUEUE *queue = (SEC_QUEUE *)queueHandle;
     43 
     44     OMX_ERRORTYPE ret = OMX_ErrorNone;
     45 
     46     if (!queue)
     47         return OMX_ErrorBadParameter;
     48 
     49     ret = SEC_OSAL_MutexCreate(&queue->qMutex);
     50     if (ret != OMX_ErrorNone)
     51         return ret;
     52 
     53     queue->first = (SEC_QElem *)SEC_OSAL_Malloc(sizeof(SEC_QElem));
     54     if (queue->first == NULL)
     55         return OMX_ErrorInsufficientResources;
     56 
     57     SEC_OSAL_Memset(queue->first, 0, sizeof(SEC_QElem));
     58     currentqelem = queue->last = queue->first;
     59     queue->numElem = 0;
     60 
     61     for (i = 0; i < (MAX_QUEUE_ELEMENTS - 2); i++) {
     62         newqelem = (SEC_QElem *)SEC_OSAL_Malloc(sizeof(SEC_QElem));
     63         if (newqelem == NULL) {
     64             while (queue->first != NULL) {
     65                 currentqelem = queue->first->qNext;
     66                 SEC_OSAL_Free((OMX_PTR)queue->first);
     67                 queue->first = currentqelem;
     68             }
     69             return OMX_ErrorInsufficientResources;
     70         } else {
     71             SEC_OSAL_Memset(newqelem, 0, sizeof(SEC_QElem));
     72             currentqelem->qNext = newqelem;
     73             currentqelem = newqelem;
     74         }
     75     }
     76 
     77     currentqelem->qNext = queue->first;
     78 
     79     return OMX_ErrorNone;
     80 }
     81 
     82 OMX_ERRORTYPE SEC_OSAL_QueueTerminate(SEC_QUEUE *queueHandle)
     83 {
     84     int i = 0;
     85     SEC_QElem *currentqelem = NULL;
     86     SEC_QUEUE *queue = (SEC_QUEUE *)queueHandle;
     87     OMX_ERRORTYPE ret = OMX_ErrorNone;
     88 
     89     if (!queue)
     90         return OMX_ErrorBadParameter;
     91 
     92     for ( i = 0; i < (MAX_QUEUE_ELEMENTS - 2); i++) {
     93         currentqelem = queue->first->qNext;
     94         SEC_OSAL_Free(queue->first);
     95         queue->first = currentqelem;
     96     }
     97 
     98     if(queue->first) {
     99         SEC_OSAL_Free(queue->first);
    100         queue->first = NULL;
    101     }
    102 
    103     ret = SEC_OSAL_MutexTerminate(queue->qMutex);
    104 
    105     return ret;
    106 }
    107 
    108 int SEC_OSAL_Queue(SEC_QUEUE *queueHandle, void *data)
    109 {
    110     SEC_QUEUE *queue = (SEC_QUEUE *)queueHandle;
    111     if (queue == NULL)
    112         return -1;
    113 
    114     SEC_OSAL_MutexLock(queue->qMutex);
    115 
    116     if ((queue->last->data != NULL) || (queue->numElem >= MAX_QUEUE_ELEMENTS)) {
    117         SEC_OSAL_MutexUnlock(queue->qMutex);
    118         return -1;
    119     }
    120     queue->last->data = data;
    121     queue->last = queue->last->qNext;
    122     queue->numElem++;
    123 
    124     SEC_OSAL_MutexUnlock(queue->qMutex);
    125     return 0;
    126 }
    127 
    128 void *SEC_OSAL_Dequeue(SEC_QUEUE *queueHandle)
    129 {
    130     void *data = NULL;
    131     SEC_QUEUE *queue = (SEC_QUEUE *)queueHandle;
    132     if (queue == NULL)
    133         return NULL;
    134 
    135     SEC_OSAL_MutexLock(queue->qMutex);
    136 
    137     if ((queue->first->data == NULL) || (queue->numElem <= 0)) {
    138         SEC_OSAL_MutexUnlock(queue->qMutex);
    139         return NULL;
    140     }
    141     data = queue->first->data;
    142     queue->first->data = NULL;
    143     queue->first = queue->first->qNext;
    144     queue->numElem--;
    145 
    146     SEC_OSAL_MutexUnlock(queue->qMutex);
    147     return data;
    148 }
    149 
    150 int SEC_OSAL_GetElemNum(SEC_QUEUE *queueHandle)
    151 {
    152     int ElemNum = 0;
    153     SEC_QUEUE *queue = (SEC_QUEUE *)queueHandle;
    154     if (queue == NULL)
    155         return -1;
    156 
    157     SEC_OSAL_MutexLock(queue->qMutex);
    158     ElemNum = queue->numElem;
    159     SEC_OSAL_MutexUnlock(queue->qMutex);
    160     return ElemNum;
    161 }
    162 
    163 int SEC_OSAL_SetElemNum(SEC_QUEUE *queueHandle, int ElemNum)
    164 {
    165     SEC_QUEUE *queue = (SEC_QUEUE *)queueHandle;
    166     if (queue == NULL)
    167         return -1;
    168 
    169     SEC_OSAL_MutexLock(queue->qMutex);
    170     queue->numElem = ElemNum;
    171     SEC_OSAL_MutexUnlock(queue->qMutex);
    172     return ElemNum;
    173 }
    174 
    175