webengine/wmlengine/src/utils/src/nwx_datastruct.c
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Provides interfaces to data structure routines.
       
    15 *                These routines support a number of different data structures.
       
    16 *        
       
    17 *                NW_Ds_Dar - DynamicArray of pointers. Creates an array of pointers
       
    18 *                to void *. Elements are inserted and retrieved by index.
       
    19 *                The array is fixed size put will automatically grow if
       
    20 *                the append function is called and the array needs to grow.
       
    21 *
       
    22 */
       
    23 
       
    24 /*
       
    25 **-------------------------------------------------------------------------
       
    26 **  Include Files
       
    27 **-------------------------------------------------------------------------
       
    28 */
       
    29 #include "nwx_defs.h"
       
    30 #include "nwx_datastruct.h"
       
    31 #include "BrsrStatusCodes.h"
       
    32 
       
    33 
       
    34 /*
       
    35 **-------------------------------------------------------------------------
       
    36 **  Internal Functions
       
    37 **-------------------------------------------------------------------------
       
    38 */
       
    39 static TBrowserStatusCode DarGrow(NW_Ds_DynamicArray_t *);
       
    40 
       
    41 /*
       
    42 **-------------------------------------------------------------------------
       
    43 **  External Public (Exported) Functions
       
    44 **-------------------------------------------------------------------------
       
    45 */
       
    46 
       
    47 /*lint -save -e64 -e119 Type mismatch, Too many arguments*/
       
    48 
       
    49 /*************************************************************************
       
    50 **          Dynamic Array of Pointers Routines
       
    51 **************************************************************************/
       
    52 
       
    53 /*****************************************************************
       
    54 **  Name:   NW_Ds_DarNew_DefaultHeap
       
    55 **  Description:  Helper function which creates the array on the
       
    56 **                default heap
       
    57 **  Parameters:   size - number of elements in the initial
       
    58 **                allocation
       
    59 **  Return Value: pointer to the dynamic array or NULL if failure
       
    60 ******************************************************************/
       
    61 NW_Ds_DynamicArray_t *NW_Ds_DarNew_DefaultHeap(const NW_Uint16 size)
       
    62 {
       
    63   return NW_Ds_DarNew(size, NW_Mem_Malloc_Address, NW_Mem_Free_Address);
       
    64 }
       
    65 
       
    66 /*****************************************************************
       
    67 **  Name:   NW_Ds_DarNew
       
    68 **  Description:  Creates a dyamic array of pointers. The size of the
       
    69 **                array returned is always greater then or equal to one.
       
    70 **  Parameters:   size - number of elements in the initial allocation
       
    71 **                *fpAlloc -- function pointer to the memory allocation routine
       
    72 **                *fpFree --  function pointer to the free allocation routine
       
    73 **  Return Value: pointer to the dynamic array or NULL if failure
       
    74 ******************************************************************/
       
    75 NW_Ds_DynamicArray_t *NW_Ds_DarNew(const NW_Uint16 size,
       
    76                                    NW_Ds_Mem_MallocHandler *fpAlloc,
       
    77                                    NW_Ds_Mem_FreeHandler *fpFree)
       
    78 {
       
    79   NW_Uint16 i;
       
    80   NW_Ds_DynamicArray_t *array;
       
    81 
       
    82   NW_ASSERT(fpAlloc != NULL);
       
    83   NW_ASSERT(fpFree != NULL);
       
    84 
       
    85 #ifndef FEATURE_MEMORY_PROFILE
       
    86   array = (fpAlloc)(sizeof(NW_Ds_DynamicArray_t));
       
    87 #else
       
    88   array = NW_Mem_Malloc(sizeof(NW_Ds_DynamicArray_t));
       
    89 #endif
       
    90   if (array != NULL) {
       
    91     array->size = NW_UINT16_CAST(NW_MAX(size, 1));
       
    92     array->numElements = 0;
       
    93 #ifndef FEATURE_MEMORY_PROFILE
       
    94     array->elements = (fpAlloc)(array->size * sizeof(void *));
       
    95 #else
       
    96     array->elements = NW_Mem_Malloc(array->size * sizeof(void *));
       
    97 #endif
       
    98     if (array->elements == NULL) {
       
    99 #ifndef FEATURE_MEMORY_PROFILE
       
   100       (fpFree)(array);
       
   101 #else
       
   102       NW_Mem_Free(array);
       
   103 #endif
       
   104 
       
   105       return NULL;
       
   106     }
       
   107     for (i = 0; i < array->size; i++)
       
   108       array->elements[i] = NULL;
       
   109     array->mallocHandler = fpAlloc;
       
   110     array->freeHandler   = fpFree;
       
   111   }
       
   112 
       
   113   return array;
       
   114 }
       
   115 
       
   116 /*****************************************************************
       
   117 **  Name:   NW_Ds_DarDelete
       
   118 **  Description:  Deletes a dyamic array of pointers.
       
   119 **  Parameters:   *array - a pointer to the dynamic array
       
   120 **  Return Value: void
       
   121 ******************************************************************/
       
   122 void NW_Ds_DarDelete(NW_Ds_DynamicArray_t *array)
       
   123 {
       
   124  if ((array != NULL) && (array->elements != NULL) && (array->freeHandler != NULL))
       
   125   {
       
   126 #ifndef FEATURE_MEMORY_PROFILE
       
   127   (array->freeHandler)(array->elements);
       
   128   (array->freeHandler)(array);
       
   129 #else
       
   130   NW_Mem_Free(array->elements);
       
   131   NW_Mem_Free(array);
       
   132 #endif
       
   133   }
       
   134   return;
       
   135 }
       
   136 
       
   137 /*****************************************************************
       
   138 **  Name:   NW_Ds_DarAppend
       
   139 **  Description:  adds a pointer to an element to the end of the array.
       
   140 **                The array will grow as needed.
       
   141 **  Parameters:   *array - a pointer to the dynamic array
       
   142 **                *element - the data to append
       
   143 **  Return Value: KBrsrSuccess if succeeds, else KBrsrOutOfMemory
       
   144 ******************************************************************/
       
   145 TBrowserStatusCode NW_Ds_DarAppend(NW_Ds_DynamicArray_t *array, void *element)
       
   146 {
       
   147   NW_ASSERT(array != NULL);
       
   148   NW_ASSERT(array->elements != NULL);
       
   149 
       
   150   if (array->numElements >= array->size) {
       
   151     if (DarGrow(array) == KBrsrOutOfMemory)
       
   152       return KBrsrOutOfMemory;
       
   153   }
       
   154   array->elements[array->numElements++] = element;
       
   155 
       
   156   return KBrsrSuccess;
       
   157 }
       
   158 
       
   159 /*****************************************************************
       
   160 **  Name:   DarGrow
       
   161 **  Description:  Grow the array by 1/2 the current size
       
   162 **  Parameters:   *array - a pointer to the dynamic array
       
   163 **  Return Value: KBrsrSuccess if succeeds, else KBrsrOutOfMemory
       
   164 ******************************************************************/
       
   165 static TBrowserStatusCode DarGrow(NW_Ds_DynamicArray_t *array)
       
   166 {
       
   167   NW_Int32 i;
       
   168   NW_Uint16 newsize;
       
   169   void **temp;
       
   170 
       
   171   NW_ASSERT(array != NULL);
       
   172   NW_ASSERT(array->elements != NULL);
       
   173 
       
   174   newsize = NW_UINT16_CAST(array->size + NW_MAX((array->size/2), 1));
       
   175 #ifndef FEATURE_MEMORY_PROFILE
       
   176   temp = (array->mallocHandler)(newsize * sizeof(void *));
       
   177 #else
       
   178   temp = NW_Mem_Malloc(newsize * sizeof(void *));
       
   179 #endif
       
   180   if (temp == NULL) {
       
   181     return KBrsrOutOfMemory;
       
   182   } else {
       
   183     array->size = newsize;
       
   184     for (i = 0; i < array->numElements; i++)
       
   185       temp[i] = array->elements[i];
       
   186     for (i = array->numElements; i < array->size; i++)
       
   187       temp[i] = NULL;
       
   188 #ifndef FEATURE_MEMORY_PROFILE
       
   189     (array->freeHandler)(array->elements);
       
   190 #else
       
   191     NW_Mem_Free(array->elements);
       
   192 #endif
       
   193     array->elements = temp;
       
   194     return KBrsrSuccess;
       
   195   }
       
   196 }
       
   197 
       
   198 
       
   199 /*****************************************************************
       
   200 **  Name:   NW_Ds_DarGetElement
       
   201 **  Description:  gets the pointer to the specified element
       
   202 **  Parameters:   *array - a pointer to the dynamic array
       
   203 **                index - the index of the element to get
       
   204 **  Return Value: a pointer to the data or NULL if array out of bounds
       
   205 ******************************************************************/
       
   206 void *NW_Ds_DarGetElement(const NW_Ds_DynamicArray_t *array, const NW_Uint32 index)
       
   207 {
       
   208   NW_ASSERT(array != NULL);
       
   209   NW_ASSERT(array->elements != NULL);
       
   210   NW_ASSERT(index < array->numElements);
       
   211 
       
   212   if (index < array->numElements)
       
   213     return array->elements[index];
       
   214   else
       
   215     return NULL;
       
   216 }
       
   217 /*****************************************************************
       
   218 **  Name:   NW_Ds_DarGetCount
       
   219 **  Description:  gets the number of elements in the array
       
   220 **  Parameters:   *array - a pointer to the dynamic array
       
   221 **  Return Value: the actual number of elements stored in the array
       
   222 ******************************************************************/
       
   223 NW_Uint16 NW_Ds_DarGetCount(const NW_Ds_DynamicArray_t *array)
       
   224 {
       
   225   if(array != NULL)
       
   226     return array->numElements;
       
   227   return 0;
       
   228 }
       
   229 
       
   230 /*lint -restore*/