"Fossies" - the Fresh Open Source Software Archive

Member "tidy-html5-5.8.0/src/alloc.c" (16 Jul 2021, 2855 Bytes) of package /linux/www/tidy-html5-5.8.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "alloc.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 5.4.0_vs_5.6.0.

    1 /* alloc.c -- Default memory allocation routines.
    2 
    3   (c) 1998-2006 (W3C) MIT, ERCIM, Keio University
    4   See tidy.h for the copyright notice.
    5 
    6 */
    7 
    8 /* #define DEBUG_MEMORY very NOISY extra DEBUG of memory allocation, reallocation and free */
    9 
   10 #include "tidy.h"
   11 #include "forward.h"
   12 #include "sprtf.h"
   13 
   14 static TidyMalloc  g_malloc  = NULL;
   15 static TidyRealloc g_realloc = NULL;
   16 static TidyFree    g_free    = NULL;
   17 static TidyPanic   g_panic   = NULL;
   18 
   19 #if defined(ENABLE_DEBUG_LOG) && defined(DEBUG_MEMORY)
   20 static int alloccnt = 0;
   21 static int realloccnt = 0;
   22 static int freecnt = 0;
   23 #endif
   24 
   25 
   26 Bool TIDY_CALL tidySetMallocCall( TidyMalloc fmalloc )
   27 {
   28   g_malloc  = fmalloc;
   29   return yes;
   30 }
   31 Bool TIDY_CALL tidySetReallocCall( TidyRealloc frealloc )
   32 {
   33   g_realloc = frealloc;
   34   return yes;
   35 }
   36 Bool TIDY_CALL tidySetFreeCall( TidyFree ffree )
   37 {
   38   g_free    = ffree;
   39   return yes;
   40 }
   41 Bool TIDY_CALL tidySetPanicCall( TidyPanic fpanic )
   42 {
   43   g_panic   = fpanic;
   44   return yes;
   45 }
   46 
   47 static void TIDY_CALL defaultPanic( TidyAllocator* ARG_UNUSED(allocator), ctmbstr msg )
   48 {
   49   if ( g_panic )
   50     g_panic( msg );
   51   else
   52   {
   53     /* 2 signifies a serious error */
   54     fprintf( stderr, "Fatal error: %s\n", msg );
   55 #ifdef _DEBUG
   56     assert(0);
   57 #endif
   58     exit(2);
   59   }
   60 }
   61 
   62 static void* TIDY_CALL defaultAlloc( TidyAllocator* allocator, size_t size )
   63 {
   64     void *p = ( g_malloc ? g_malloc(size) : malloc(size) );
   65     if ( !p )
   66         defaultPanic( allocator,"Out of memory!");
   67 #if defined(ENABLE_DEBUG_LOG) && defined(DEBUG_MEMORY)
   68     alloccnt++;
   69     SPRTF("%d: alloc   MEM %p, size %d\n", alloccnt, p, (int)size );
   70     if (size == 0) {
   71         SPRTF("NOTE: An allocation of ZERO bytes!!!!!!\n");
   72     }
   73 #endif
   74     return p;
   75 }
   76 
   77 static void* TIDY_CALL defaultRealloc( TidyAllocator* allocator, void* mem, size_t newsize )
   78 {
   79     void *p;
   80     if ( mem == NULL )
   81         return defaultAlloc( allocator, newsize );
   82 
   83     p = ( g_realloc ? g_realloc(mem, newsize) : realloc(mem, newsize) );
   84     if (!p)
   85         defaultPanic( allocator, "Out of memory!");
   86 #if defined(ENABLE_DEBUG_LOG) && defined(DEBUG_MEMORY)
   87     realloccnt++;
   88     SPRTF("%d: realloc MEM %p, size %d\n", realloccnt, p, (int)newsize );
   89 #endif
   90     return p;
   91 }
   92 
   93 static void TIDY_CALL defaultFree( TidyAllocator* ARG_UNUSED(allocator), void* mem )
   94 {
   95     if ( mem )
   96     {
   97 #if defined(ENABLE_DEBUG_LOG) && defined(DEBUG_MEMORY)
   98         freecnt++;
   99         SPRTF("%d: free    MEM %p\n", freecnt, mem );
  100 #endif
  101         if ( g_free )
  102             g_free( mem );
  103         else
  104             free( mem );
  105     }
  106 }
  107 
  108 static const TidyAllocatorVtbl defaultVtbl = {
  109     defaultAlloc,
  110     defaultRealloc,
  111     defaultFree,
  112     defaultPanic
  113 };
  114 
  115 TidyAllocator TY_(g_default_allocator) = {
  116     &defaultVtbl
  117 };
  118 
  119 /*
  120  * local variables:
  121  * mode: c
  122  * indent-tabs-mode: nil
  123  * c-basic-offset: 4
  124  * eval: (c-set-offset 'substatement-open 0)
  125  * end:
  126  */