OSDN Git Service

フォルダ名変更
[wordring-tm/wordring-tm.git] / third-party / tidy-html5-master / src / alloc.c
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 #include "tidy.h"
9 #include "forward.h"
10
11 static TidyMalloc  g_malloc  = NULL;
12 static TidyRealloc g_realloc = NULL;
13 static TidyFree    g_free    = NULL;
14 static TidyPanic   g_panic   = NULL;
15
16 Bool TIDY_CALL tidySetMallocCall( TidyMalloc fmalloc )
17 {
18   g_malloc  = fmalloc;
19   return yes;
20 }
21 Bool TIDY_CALL tidySetReallocCall( TidyRealloc frealloc )
22 {
23   g_realloc = frealloc;
24   return yes;
25 }
26 Bool TIDY_CALL tidySetFreeCall( TidyFree ffree )
27 {
28   g_free    = ffree;
29   return yes;
30 }
31 Bool TIDY_CALL tidySetPanicCall( TidyPanic fpanic )
32 {
33   g_panic   = fpanic;
34   return yes;
35 }
36
37 static void TIDY_CALL defaultPanic( TidyAllocator* ARG_UNUSED(allocator), ctmbstr msg )
38 {
39   if ( g_panic )
40     g_panic( msg );
41   else
42   {
43     /* 2 signifies a serious error */
44     fprintf( stderr, "Fatal error: %s\n", msg );
45 #ifdef _DEBUG
46     assert(0);
47 #endif
48     exit(2);
49   }
50 }
51
52 static void* TIDY_CALL defaultAlloc( TidyAllocator* allocator, size_t size )
53 {
54     void *p = ( g_malloc ? g_malloc(size) : malloc(size) );
55     if ( !p )
56         defaultPanic( allocator,"Out of memory!");
57     return p;
58 }
59
60 static void* TIDY_CALL defaultRealloc( TidyAllocator* allocator, void* mem, size_t newsize )
61 {
62     void *p;
63     if ( mem == NULL )
64         return defaultAlloc( allocator, newsize );
65
66     p = ( g_realloc ? g_realloc(mem, newsize) : realloc(mem, newsize) );
67     if (!p)
68         defaultPanic( allocator, "Out of memory!");
69     return p;
70 }
71
72 static void TIDY_CALL defaultFree( TidyAllocator* ARG_UNUSED(allocator), void* mem )
73 {
74     if ( mem )
75     {
76         if ( g_free )
77             g_free( mem );
78         else
79             free( mem );
80     }
81 }
82
83 static const TidyAllocatorVtbl defaultVtbl = {
84     defaultAlloc,
85     defaultRealloc,
86     defaultFree,
87     defaultPanic
88 };
89
90 TidyAllocator TY_(g_default_allocator) = {
91     &defaultVtbl
92 };
93
94 /*
95  * local variables:
96  * mode: c
97  * indent-tabs-mode: nil
98  * c-basic-offset: 4
99  * eval: (c-set-offset 'substatement-open 0)
100  * end:
101  */