root/libswish3/trunk/src/libswish3/mem.c

Revision 2171, 3.1 kB (checked in by karpet, 2 months ago)

get rid of the circular reference in TokenList/Token?; make swish_init() a separate call from swish_init_swish3() so it can be called as class method rather than once-per-object; clean up some ref_cnt logic and mem debugging

Line 
1 /*
2  * This file is part of libswish3
3  * Copyright (C) 2007 Peter Karman
4  *
5  *  libswish3 is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  libswish3 is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with libswish3; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20 /* mem.c -- graceful memory handling */
21
22 #include <sys/types.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <libxml/xmlstring.h>
27 #include <err.h>
28
29 #include "libswish3.h"
30
31 extern int SWISH_DEBUG;
32
33 static long int memcount = 0;
34
35 void
36 swish_init_memory(
37 )
38 {
39     memcount = 0;
40 }
41
42 long int
43 swish_get_memcount(
44 )
45 {
46     return memcount;
47 }
48
49 /* PUBLIC */
50 /* realloc a block of memory */
51 void *
52 swish_xrealloc(
53     void *ptr,
54     size_t size
55 )
56 {
57     void *new_ptr = realloc(ptr, size);
58
59     if (new_ptr == NULL)
60         SWISH_CROAK("Out of memory (could not reallocate %lu more bytes)!",
61                     (unsigned long)size);
62
63     return new_ptr;
64 }
65
66 /* PUBLIC */
67 void *
68 swish_xmalloc(
69     size_t size
70 )
71 {
72     void *ptr = malloc(size);
73
74     if (ptr == NULL)
75         SWISH_CROAK("Out of memory! Can't malloc %lu bytes",
76                     (unsigned long)size);
77
78     memcount++;
79     if (SWISH_DEBUG & SWISH_DEBUG_MEMORY) {
80         SWISH_DEBUG_MSG("memcount = %ld", memcount);
81         SWISH_DEBUG_MSG("xmalloc address: 0x%x", (int)ptr);
82     }
83
84     return ptr;
85 }
86
87 xmlChar *
88 swish_xstrdup(
89     const xmlChar *ptr
90 )
91 {
92     xmlChar *copy;
93     memcount++;
94     if (SWISH_DEBUG & SWISH_DEBUG_MEMORY)
95         SWISH_DEBUG_MSG("memcount = %ld", memcount);
96     copy = xmlStrdup(ptr);
97     if (copy == NULL)
98         SWISH_CROAK("strdup returned NULL for %s", ptr);
99
100     return copy;
101 }
102
103 xmlChar *
104 swish_xstrndup(
105     const xmlChar *ptr,
106     int len
107 )
108 {
109     memcount++;
110     if (SWISH_DEBUG & SWISH_DEBUG_MEMORY)
111         SWISH_DEBUG_MSG("memcount = %ld", memcount);
112     return (xmlStrndup(ptr, len));
113 }
114
115 void
116 swish_xfree(
117     void *ptr
118 )
119 {
120     if (ptr == NULL) {
121         SWISH_WARN
122             (" >>>>>>>>>>>>>> attempt to free NULL pointer <<<<<<<<<<<<<<");
123         return;
124     }
125    
126     if (SWISH_DEBUG & SWISH_DEBUG_MEMORY)
127         SWISH_DEBUG_MSG("freeing %s 0x%x", (char*)ptr, (int)ptr);
128
129     xmlFree(ptr);
130
131     memcount--;
132
133     if (SWISH_DEBUG & SWISH_DEBUG_MEMORY)
134         SWISH_DEBUG_MSG("memcount = %ld", memcount);
135 }
136
137 void
138 swish_mem_debug(
139 )
140 {
141 /* SWISH_DEBUG_MSG("memcount = %ld", memcount); */
142     if (memcount > 0)
143         SWISH_WARN
144             ("%ld more swish_xmalloc()s or swish_xstrdup()s than swish_xfree()s",
145              memcount);
146
147     if (memcount < 0)
148         SWISH_WARN("too many swish_xfree()s %d", memcount);
149 }
Note: See TracBrowser for help on using the browser.