/src/tidy-html5/src/alloc.c
Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
28 | 0 | g_malloc = fmalloc; |
29 | 0 | return yes; |
30 | 0 | } |
31 | | Bool TIDY_CALL tidySetReallocCall( TidyRealloc frealloc ) |
32 | 0 | { |
33 | 0 | g_realloc = frealloc; |
34 | 0 | return yes; |
35 | 0 | } |
36 | | Bool TIDY_CALL tidySetFreeCall( TidyFree ffree ) |
37 | 0 | { |
38 | 0 | g_free = ffree; |
39 | 0 | return yes; |
40 | 0 | } |
41 | | Bool TIDY_CALL tidySetPanicCall( TidyPanic fpanic ) |
42 | 0 | { |
43 | 0 | g_panic = fpanic; |
44 | 0 | return yes; |
45 | 0 | } |
46 | | |
47 | | static void TIDY_CALL defaultPanic( TidyAllocator* ARG_UNUSED(allocator), ctmbstr msg ) |
48 | 0 | { |
49 | 0 | if ( g_panic ) |
50 | 0 | g_panic( msg ); |
51 | 0 | else |
52 | 0 | { |
53 | | /* 2 signifies a serious error */ |
54 | 0 | fprintf( stderr, "Fatal error: %s\n", msg ); |
55 | | #ifdef _DEBUG |
56 | | assert(0); |
57 | | #endif |
58 | 0 | exit(2); |
59 | 0 | } |
60 | 0 | } |
61 | | |
62 | | static void* TIDY_CALL defaultAlloc( TidyAllocator* allocator, size_t size ) |
63 | 43.2M | { |
64 | 43.2M | void *p = ( g_malloc ? g_malloc(size) : malloc(size) ); |
65 | 43.2M | if ( !p ) |
66 | 0 | 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 | 43.2M | return p; |
75 | 43.2M | } |
76 | | |
77 | | static void* TIDY_CALL defaultRealloc( TidyAllocator* allocator, void* mem, size_t newsize ) |
78 | 71.9k | { |
79 | 71.9k | void *p; |
80 | 71.9k | if ( mem == NULL ) |
81 | 38.9k | return defaultAlloc( allocator, newsize ); |
82 | | |
83 | 32.9k | p = ( g_realloc ? g_realloc(mem, newsize) : realloc(mem, newsize) ); |
84 | 32.9k | if (!p) |
85 | 0 | 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 | 32.9k | return p; |
91 | 71.9k | } |
92 | | |
93 | | static void TIDY_CALL defaultFree( TidyAllocator* ARG_UNUSED(allocator), void* mem ) |
94 | 43.4M | { |
95 | 43.4M | if ( mem ) |
96 | 42.8M | { |
97 | | #if defined(ENABLE_DEBUG_LOG) && defined(DEBUG_MEMORY) |
98 | | freecnt++; |
99 | | SPRTF("%d: free MEM %p\n", freecnt, mem ); |
100 | | #endif |
101 | 42.8M | if ( g_free ) |
102 | 0 | g_free( mem ); |
103 | 42.8M | else |
104 | 42.8M | free( mem ); |
105 | 42.8M | } |
106 | 43.4M | } |
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 | | */ |