os/mm/mmplugins/lib3gp/impl/src/list.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include "mp4list.h"
    17 #include "mp4memwrap.h"
    18 
    19 
    20 
    21 /*
    22  * Function:
    23  *
    24  *   list_s *listCreate()
    25  *
    26  * Description:
    27  *
    28  *   Create a list.
    29  *
    30  * Parameters:
    31  *
    32  *   None
    33  *
    34  * Return value:
    35  *
    36  *   Pointer to the allocated list.
    37  *
    38  */
    39 list_s *listCreate()
    40 {
    41   list_s *tmpList;
    42 
    43   if ((tmpList = (list_s *)mp4malloc(sizeof(list_s))) == NULL)
    44     return NULL;
    45 
    46   tmpList->elementsInList = 0;
    47   tmpList->bytesInList = 0;
    48   tmpList->cumulativeBytesInList = 0;
    49   tmpList->first = NULL;
    50   tmpList->last = NULL;
    51 
    52   return tmpList;
    53 }
    54 
    55 
    56 /*
    57  * Function:
    58  *
    59  *   void *listAppend(list_s *list,
    60  *                    void *data,
    61  *                    mp4_u32 dataSize)
    62  *
    63  * Description:
    64  *
    65  *   Append an element (void *) to the end of the list.
    66  *
    67  * Parameters:
    68  *
    69  *   list      Pointer to a list
    70  *   data      Pointer to add to the list
    71  *   dataSize  Size of data pointed to by data
    72  *
    73  * Return value:
    74  *
    75  *   NULL      Error
    76  *   Non-NULL  Success
    77  *
    78  */
    79 void *listAppend(list_s *list, void *data, mp4_u32 dataSize)
    80 {
    81   node_s *tmpNode;
    82 
    83   if ((tmpNode = (node_s *)mp4malloc(sizeof(node_s))) == NULL)
    84     return NULL;
    85 
    86   if (list->elementsInList == 0) /* List is empty */
    87     list->first = tmpNode;
    88   else
    89     list->last->next = tmpNode;
    90 
    91   list->last = tmpNode;
    92   list->elementsInList++;
    93   list->bytesInList += dataSize;
    94   list->cumulativeBytesInList += dataSize;
    95 
    96   tmpNode->next = NULL;
    97   tmpNode->data = data;
    98   tmpNode->dataSize = dataSize;
    99 
   100   return (void *)tmpNode;
   101 }
   102 
   103 
   104 /*
   105  * Function:
   106  *
   107  *   void listDeleteFirst(list_s *list)
   108  *
   109  * Description:
   110  *
   111  *   Delete the first element in the list.
   112  *
   113  * Parameters:
   114  *
   115  *   list   Pointer to a list
   116  *
   117  * Return value:
   118  *
   119  *   None
   120  *
   121  */
   122 void listDeleteFirst(list_s *list)
   123 {
   124   if (list->elementsInList == 1)
   125   {
   126 	mp4free(list->first->data);
   127     mp4free(list->first);
   128     list->first = NULL;
   129     list->last = NULL;
   130     list->elementsInList = 0;
   131     list->bytesInList = 0;
   132   }
   133 
   134   if (list->elementsInList > 1)
   135   {
   136     node_s *tmpNode;
   137 
   138     tmpNode = list->first;
   139     list->bytesInList -= tmpNode->dataSize;
   140     list->first = tmpNode->next;
   141 	mp4free(tmpNode->data);
   142     mp4free(tmpNode);
   143     list->elementsInList--;
   144   }
   145 }
   146 
   147 
   148 /*
   149  * Function:
   150  *
   151  *   void listDestroyList(list_s *list)
   152  *
   153  * Description:
   154  *
   155  *   Free list and all its elements from memory.
   156  *
   157  * Parameters:
   158  *
   159  *   list   Pointer to a list
   160  *
   161  * Return value:
   162  *
   163  *   None
   164  *
   165  */
   166 void listDestroyList(list_s *list)
   167 {
   168   while (list->elementsInList)
   169     listDeleteFirst(list);
   170 
   171   mp4free(list);
   172 }
   173 
   174 
   175 /*
   176  * Function:
   177  *
   178  *   mp4_u32 listElementsInList(list_s *list)
   179  *
   180  * Description:
   181  *
   182  *   Return the number of elements in the list.
   183  *
   184  * Parameters:
   185  *
   186  *   list   Pointer to a list
   187  *
   188  * Return value:
   189  *
   190  *   Number of elements in the list
   191  *
   192  */
   193 mp4_u32 listElementsInList(list_s *list)
   194 {
   195   return list->elementsInList;
   196 }
   197 
   198 
   199 /*
   200  * Function:
   201  *
   202  *   mp4_u32 listBytesInList(list_s *list)
   203  *
   204  * Description:
   205  *
   206  *   Return the number of bytes currently in list.
   207  *
   208  * Parameters:
   209  *
   210  *   list   Pointer to a list
   211  *
   212  * Return value:
   213  *
   214  *   Number of bytes in the list
   215  *
   216  */
   217 mp4_u32 listBytesInList(list_s *list)
   218 {
   219   return list->bytesInList;
   220 }
   221 
   222 
   223 /*
   224  * Function:
   225  *
   226  *   mp4_u32 listCumulativeBytesInList(list_s *list)
   227  *
   228  * Description:
   229  *
   230  *   Return the number of bytes that have been in the list since the list
   231  *   was created.
   232  *
   233  * Parameters:
   234  *
   235  *   list   Pointer to a list
   236  *
   237  * Return value:
   238  *
   239  *   Number of bytes in the list from the creation
   240  *
   241  */
   242 mp4_u32 listCumulativeBytesInList(list_s *list)
   243 {
   244   return list->cumulativeBytesInList;
   245 }
   246 // End of File