xml/cxmllibrary/src/utils/src/cxml_mem.c
branchRCL_3
changeset 20 889504eac4fb
equal deleted inserted replaced
19:6bcc0aa4be39 20:889504eac4fb
       
     1 /*
       
     2 * Copyright (c) 2003 - 2004 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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 /***************************************************************************
       
    20 **   File: cxml_mem.c
       
    21 **   Purpose:  Provides the implementation to memory management
       
    22 **************************************************************************/
       
    23 
       
    24 #include "cxml_internal.h"
       
    25 
       
    26 #ifdef FEATURE_MEMORY_PROFILE
       
    27 typedef struct _MemProfile
       
    28 {
       
    29     struct _MemProfile  *next;  // ptr to next mem block
       
    30     struct _MemProfile  *prev;  // ptr to previous mem block
       
    31     char                *file;  // filename of memory allocator
       
    32     CXML_Int32               line;   // line # in filename of memory allocator
       
    33     CXML_Int32               size;   // size of requested memory
       
    34     CXML_Int32               seq;    // sequence number for allocation 
       
    35 } MemProfile;
       
    36 #endif
       
    37 
       
    38 /*
       
    39 **-------------------------------------------------------------------------
       
    40 **  External Public (Exported) Functions
       
    41 **-------------------------------------------------------------------------
       
    42 */
       
    43 
       
    44 void *CXML_Mem_memset(void *s, CXML_Uint32 c, CXML_Uint32 n)
       
    45 {
       
    46   unsigned char *myPtr  = (unsigned char *)s;
       
    47   unsigned char fill    = (unsigned char)c;
       
    48   while (n > 0) {
       
    49     *myPtr = fill;
       
    50     myPtr++;
       
    51     n--;
       
    52   }
       
    53   return s;
       
    54 }
       
    55 
       
    56 
       
    57 void *CXML_Mem_memcpy(void *s1, const void *s2, CXML_Uint32 n)
       
    58 {
       
    59   unsigned char *myDest   = (unsigned char *)s1;
       
    60   unsigned char *mySource = (unsigned char *)s2;
       
    61 
       
    62   if (n==0) return NULL;
       
    63 
       
    64   while (n > 0) {
       
    65     *myDest = *mySource;
       
    66     n--;
       
    67     myDest++;
       
    68     mySource++;
       
    69   }
       
    70   return s1;
       
    71 }
       
    72 
       
    73 
       
    74 void *CXML_Mem_memmove(void *s1, const void *s2, CXML_Uint32 n)
       
    75 {
       
    76   unsigned char *myDest;
       
    77   unsigned char *mySource;
       
    78 
       
    79   if (n == 0) return NULL;
       
    80 
       
    81   if (s1 <= s2) {
       
    82     /* We can always copy from higher address to */
       
    83     /* lower address, even if buffer overlaps */
       
    84     myDest    = (unsigned char *)s1;
       
    85     mySource  = (unsigned char *)s2;
       
    86     while (n > 0) {
       
    87       *myDest = *mySource;
       
    88       myDest++;
       
    89       mySource++;
       
    90       n--;
       
    91     }
       
    92 
       
    93   } else {
       
    94     /* We can always copy in reverse order from */
       
    95     /* lower address to higher address, even if */
       
    96     /* buffer overlaps */
       
    97     myDest    = (unsigned char *)s1 + n;
       
    98     mySource  = (unsigned char *)s2 + n;
       
    99     while (n > 0) {
       
   100       n--;
       
   101       myDest--;
       
   102       mySource--;
       
   103       *myDest = *mySource;
       
   104     }
       
   105 
       
   106   }
       
   107 
       
   108   return s1;
       
   109 }
       
   110 
       
   111 
       
   112 CXML_Int32 CXML_Mem_memcmp(const void *s1, const void *s2, CXML_Uint32 n)
       
   113 {
       
   114   unsigned char *myDest   = (unsigned char *)s1;
       
   115   unsigned char *mySource = (unsigned char *)s2;
       
   116 
       
   117   /* If s1 == s2, then immediately return 0. */
       
   118   if ( s1 == s2 )
       
   119     return 0 ;
       
   120 
       
   121   while (n > 0) {
       
   122     if (*myDest != *mySource) {
       
   123       return (*myDest - *mySource);
       
   124     }
       
   125     myDest++;
       
   126     mySource++;
       
   127     n--;
       
   128   }
       
   129   
       
   130   return 0;
       
   131 }
       
   132 
       
   133 /*********************************************************
       
   134 **  Name:   CXML_Mem_Malloc
       
   135 **  Description:  Allocates memory.
       
   136 **  Parameters:   nbytes - number of bytes to allocate
       
   137 **  Return Value: pointer to the allocated memory or NULL 
       
   138 **********************************************************/
       
   139 void *CXML_Mem_Malloc(CXML_Uint32 nbytes)
       
   140 {
       
   141 	return malloc(nbytes);
       
   142 }
       
   143 
       
   144 /************************************************************************
       
   145 **  Name:   CXML_Mem_Free
       
   146 **  Description:  Frees allocated memory.
       
   147 **  Parameters:   mem - pointer to the memory to free
       
   148 **  Return Value: void
       
   149 **************************************************************************/
       
   150 void CXML_Mem_Free(void *buffer)
       
   151 {
       
   152   if (buffer != NULL) {
       
   153     /* Note: os_block_dealloc() will free both heap and block-table memory */
       
   154 		free(buffer);
       
   155   }
       
   156   return;
       
   157 }
       
   158 
       
   159 
       
   160 #ifdef FEATURE_MEMORY_PROFILE
       
   161 CXML_Uint32 Asc_strlen(const char *s)
       
   162 {
       
   163   CXML_Uint32 i = 0;
       
   164   while (*s != '\0')
       
   165   {
       
   166     s++;
       
   167     i++;
       
   168   }
       
   169   return i;
       
   170 }
       
   171 
       
   172 /*****************************************************************
       
   173 **  Name:  Asc_strcpy
       
   174 **  Description:  Copies ascii string
       
   175 **  Parameters:   s1 destination string
       
   176 **                s2 source string
       
   177 **  Return Value: pointer to s1
       
   178 ******************************************************************/
       
   179 char *Asc_strcpy(char *s1, const char *s2)
       
   180 {
       
   181   while (*s2 != '\0') {
       
   182     *s1 = *s2;
       
   183     s1++;
       
   184     s2++;
       
   185   }
       
   186   *s1 = *s2;
       
   187   return s1;
       
   188 }
       
   189 
       
   190 /*********************************************************
       
   191 **  Name:   CXML_Mem_Malloc
       
   192 **  Description:  Allocates memory.
       
   193 **  Parameters:   nbytes - number of bytes to allocate
       
   194 **  Return Value: pointer to the allocated memory or NULL 
       
   195 **********************************************************/
       
   196 void *CXML_Mem_Malloc_Pro(CXML_Uint32 nbytes, char *file, CXML_Uint32 line)
       
   197 {
       
   198 	void *buf;
       
   199     MemProfile  *pblock, *last;  
       
   200     CXML_Int32   len = nbytes + sizeof(MemProfile);
       
   201     CXML_Int32   seqno;
       
   202 
       
   203     pblock = malloc(len);
       
   204     
       
   205     // Store profile block data 
       
   206 
       
   207     if ((CXML_Uint32) file > 0xf000)
       
   208     {
       
   209         pblock->file = (char*)malloc(Asc_strlen(file) + 1);
       
   210         Asc_strcpy(pblock->file, file);
       
   211     }
       
   212     else
       
   213     {
       
   214         // no filename since this may be called via a function ptr
       
   215         pblock->file = NULL;
       
   216     }
       
   217     pblock->line = line;
       
   218     pblock->size = nbytes;
       
   219 
       
   220     // Set List Pointers
       
   221     // Get last ptr
       
   222     last = (MemProfile *) NW_Ctx_Get(NW_CTX_MEM_PRO_LAST_PTR, 0); 
       
   223 
       
   224     if (last == NULL)
       
   225     {
       
   226         pblock->prev = NULL;
       
   227         pblock->seq = 0;
       
   228     }
       
   229     else
       
   230     {
       
   231         pblock->prev = last;
       
   232         last->next = pblock;
       
   233         // can't use last-> because free my remove block (along with
       
   234         // seq numbers, need a context
       
   235         seqno = (CXML_Uint32) NW_Ctx_Get(NW_CTX_MEM_PRO_SEQ_NO, 0); 
       
   236         seqno += 1;
       
   237         pblock->seq = seqno;
       
   238         NW_Ctx_Set(NW_CTX_MEM_PRO_SEQ_NO, 0, seqno); 
       
   239     }
       
   240     pblock->next = NULL;
       
   241 
       
   242     // last = pblock
       
   243     last = pblock;
       
   244     NW_Ctx_Set(NW_CTX_MEM_PRO_LAST_PTR, 0, last);
       
   245 
       
   246     
       
   247     // return mem block to caller              
       
   248     buf = (void *) ((CXML_Byte *) pblock + sizeof(MemProfile));
       
   249     return (buf);
       
   250 }
       
   251 
       
   252 /************************************************************************
       
   253 **  Name:   CXML_Mem_Free
       
   254 **  Description:  Frees allocated memory.
       
   255 **  Parameters:   mem - pointer to the memory to free
       
   256 **  Return Value: void
       
   257 **************************************************************************/
       
   258 void CXML_Mem_Free_Pro(void *buffer, char *file, CXML_Uint32 line)
       
   259 {
       
   260  MemProfile  *pblock, *last, *prevp, *nextp;  
       
   261     
       
   262     if (buffer == NULL)
       
   263     {
       
   264         return;
       
   265     }
       
   266                                                          
       
   267     pblock = (MemProfile *) ((CXML_Byte *) buffer - sizeof(MemProfile));
       
   268 
       
   269     // Get last ptr
       
   270     last = (MemProfile *) NW_Ctx_Get(NW_CTX_MEM_PRO_LAST_PTR, 0); 
       
   271 
       
   272     if ( pblock == last )
       
   273     {
       
   274         last = last->prev;
       
   275         NW_Ctx_Set(NW_CTX_MEM_PRO_LAST_PTR, 0, last);
       
   276     }
       
   277     else
       
   278     {
       
   279         prevp = pblock->prev; 
       
   280         nextp = pblock->next;
       
   281         if (prevp)
       
   282         {
       
   283             prevp->next = nextp;
       
   284         }
       
   285         if (nextp)
       
   286         {
       
   287             nextp->prev = prevp;
       
   288         }
       
   289     }
       
   290 
       
   291     // Free Filename pointer and memory block
       
   292     free(pblock->file);
       
   293     free(pblock);
       
   294     return;
       
   295 }
       
   296 #endif