Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

sfmemcap.c

Go to the documentation of this file.
00001 /*
00002   sfmemcap.c
00003 
00004   These functions wrap the malloc & free functions. They enforce a memory cap using
00005   the MEMCAP structure.  The MEMCAP structure tracks memory usage.  Each allocation
00006   has 4 bytes added to it so we can store the allocation size.  This allows us to 
00007   free a block and accurately track how much memory was recovered.
00008   
00009   Marc Norton  
00010 */
00011 #include <stdlib.h>
00012 #include <stdio.h>
00013 #include <string.h>
00014 
00015 #include "sfmemcap.h"
00016 
00017 /*
00018 *   Set max # bytes & init other variables.
00019 */
00020 void sfmemcap_init( MEMCAP * mc, unsigned nbytes )
00021 {
00022         mc->memcap = nbytes;
00023         mc->memused= 0;
00024         mc->nblocks= 0;
00025 }
00026 
00027 /*
00028 *   Create and Init a MEMCAP -  use free to release it
00029 */
00030 MEMCAP * sfmemcap_new( unsigned nbytes )
00031 {
00032          MEMCAP * mc;
00033 
00034          mc = (MEMCAP*)calloc(1,sizeof(MEMCAP));
00035 
00036          if( mc ) sfmemcap_init( mc, nbytes );
00037          
00038          return mc;
00039 }
00040 
00041 /*
00042 *  Release the memcap structure
00043 */
00044 void sfmemcap_delete( MEMCAP * p )
00045 {
00046      if(p)free( p );
00047 }
00048 
00049 /*
00050 *  Allocate some memory
00051 */
00052 void * sfmemcap_alloc( MEMCAP * mc, unsigned nbytes )
00053 {
00054    long * data;
00055 
00056    //printf("sfmemcap_alloc: %d bytes requested, memcap=%d, used=%d\n",nbytes,mc->memcap,mc->memused);
00057 
00058    nbytes += sizeof(long);
00059 
00060 
00061    /* Check if we are limiting memory use */
00062    if( mc->memcap > 0 )
00063    {
00064       /* Check if we've maxed out our memory - if we are tracking memory */
00065       if( (mc->memused + nbytes) > mc->memcap )
00066       {
00067               return 0;
00068       }
00069    }
00070 
00071    data = (long *) malloc( nbytes );
00072    if( data == NULL )
00073    {
00074         return 0;
00075    }
00076 
00077    *data++ = (long)nbytes;
00078 
00079    mc->memused += nbytes;
00080    mc->nblocks++;
00081 
00082    return data;
00083 }
00084 
00085 /*
00086 *   Free some memory
00087 */
00088 void sfmemcap_free( MEMCAP * mc, void * p )
00089 {
00090    long * q;
00091 
00092    q = (long*)p;
00093    q--;
00094    mc->memused -= (unsigned)(*q);
00095    mc->nblocks--;
00096 
00097    free(q);
00098 }
00099 
00100 /*
00101 *   For debugging.
00102 */
00103 void sfmemcap_showmem( MEMCAP * mc )
00104 {
00105      fprintf(stderr, "memcap: memcap = %u bytes,",mc->memcap);
00106      fprintf(stderr, " memused= %u bytes,",mc->memused);
00107      fprintf(stderr, " nblocks= %d blocks\n",mc->nblocks);
00108 }
00109 
00110 /*
00111 *  String Dup Some memory.
00112 */
00113 char * sfmemcap_strdup( MEMCAP * mc, const char *str )
00114 {
00115     char * data = (char *)sfmemcap_alloc( mc, strlen(str) + 1 );
00116     if(data == NULL)
00117     {
00118         return  0 ;
00119     }
00120     strcpy(data,str);
00121     return data;
00122 }
00123 
00124 /*
00125 *  Dup Some memory.
00126 */
00127 void * sfmemcap_dupmem( MEMCAP * mc, void * src, int n )
00128 {
00129     void * data = (char *)sfmemcap_alloc( mc, n );
00130     if(data == NULL)
00131     {
00132         return  0;
00133     }
00134 
00135     memcpy( data, src, n );
00136 
00137     return data;
00138 }

Generated on Sun May 14 14:51:18 2006 by  doxygen 1.4.2