]> git.wh0rd.org - fontconfig.git/blob - src/fcinit.c
More complete memory tracking. Install always overwrites header files
[fontconfig.git] / src / fcinit.c
1 /*
2 * $XFree86: xc/lib/fontconfig/src/fcinit.c,v 1.7 2002/08/22 07:36:44 keithp Exp $
3 *
4 * Copyright © 2001 Keith Packard, member of The XFree86 Project, Inc.
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
15 *
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
23 */
24
25 #include <stdlib.h>
26 #include "fcint.h"
27
28 static FcConfig *
29 FcInitFallbackConfig (void)
30 {
31 FcConfig *config;
32
33 config = FcConfigCreate ();
34 if (!config)
35 goto bail0;
36 if (!FcConfigAddDir (config, (FcChar8 *) FC_DEFAULT_FONTS))
37 goto bail1;
38 return config;
39
40 bail1:
41 FcConfigDestroy (config);
42 bail0:
43 return 0;
44 }
45
46 int
47 FcGetVersion (void)
48 {
49 return FC_VERSION;
50 }
51
52 /*
53 * Load the configuration files
54 */
55 FcConfig *
56 FcInitLoadConfig (void)
57 {
58 FcConfig *config;
59
60 config = FcConfigCreate ();
61 if (!config)
62 return FcFalse;
63
64 if (!FcConfigParseAndLoad (config, 0, FcTrue))
65 {
66 FcConfigDestroy (config);
67 return FcInitFallbackConfig ();
68 }
69
70 return config;
71 }
72
73 /*
74 * Load the configuration files and scan for available fonts
75 */
76 FcConfig *
77 FcInitLoadConfigAndFonts (void)
78 {
79 FcConfig *config = FcInitLoadConfig ();
80
81 if (!config)
82 return 0;
83 if (!FcConfigBuildFonts (config))
84 {
85 FcConfigDestroy (config);
86 return 0;
87 }
88 return config;
89 }
90
91 /*
92 * Initialize the default library configuration
93 */
94 FcBool
95 FcInit (void)
96 {
97 FcConfig *config;
98
99 if (_fcConfig)
100 return FcTrue;
101 config = FcInitLoadConfigAndFonts ();
102 if (!config)
103 return FcTrue;
104 FcConfigSetCurrent (config);
105 if (FcDebug() & FC_DBG_MEMORY)
106 FcMemReport ();
107 return FcTrue;
108 }
109
110 /*
111 * Reread the configuration and available font lists
112 */
113 FcBool
114 FcInitReinitialize (void)
115 {
116 FcConfig *config;
117
118 config = FcInitLoadConfigAndFonts ();
119 if (!config)
120 return FcFalse;
121 FcConfigSetCurrent (config);
122 return FcTrue;
123 }
124
125 FcBool
126 FcInitBringUptoDate (void)
127 {
128 FcConfig *config = FcConfigGetCurrent ();
129 time_t now;
130
131 /*
132 * rescanInterval == 0 disables automatic up to date
133 */
134 if (config->rescanInterval == 0)
135 return FcTrue;
136 /*
137 * Check no more often than rescanInterval seconds
138 */
139 now = time (0);
140 if (config->rescanTime + config->rescanInterval - now > 0)
141 return FcTrue;
142 /*
143 * If up to date, don't reload configuration
144 */
145 if (FcConfigUptoDate (0))
146 return FcTrue;
147 return FcInitReinitialize ();
148 }
149
150 static struct {
151 char *name;
152 int alloc_count;
153 int alloc_mem;
154 int free_count;
155 int free_mem;
156 } FcInUse[FC_MEM_NUM] = {
157 { "charset" },
158 { "charleaf" },
159 { "fontset" },
160 { "fontptr" },
161 { "objectset" },
162 { "objectptr" },
163 { "matrix" },
164 { "pattern" },
165 { "patelt" },
166 { "vallist" },
167 { "substate" },
168 { "string" },
169 { "listbuck" },
170 { "strset" },
171 { "strlist" },
172 { "config" },
173 { "langset" },
174 { "atomic" },
175 { "blanks" },
176 { "cache" },
177 { "strbuf" },
178 { "subst" },
179 { "objecttype" },
180 { "constant" },
181 { "test" },
182 { "expr" },
183 { "vstack" },
184 { "attr" },
185 { "pstack" },
186 };
187
188 static int FcAllocCount, FcAllocMem;
189 static int FcFreeCount, FcFreeMem;
190
191 static int FcMemNotice = 1*1024*1024;
192
193 static int FcAllocNotify, FcFreeNotify;
194
195 void
196 FcValueListReport (void);
197
198 void
199 FcMemReport (void)
200 {
201 int i;
202 printf ("Fc Memory Usage:\n");
203 printf ("\t Which Alloc Free Active\n");
204 printf ("\t count bytes count bytes count bytes\n");
205 for (i = 0; i < FC_MEM_NUM; i++)
206 printf ("\t%8.8s%8d%8d%8d%8d%8d%8d\n",
207 FcInUse[i].name,
208 FcInUse[i].alloc_count, FcInUse[i].alloc_mem,
209 FcInUse[i].free_count, FcInUse[i].free_mem,
210 FcInUse[i].alloc_count - FcInUse[i].free_count,
211 FcInUse[i].alloc_mem - FcInUse[i].free_mem);
212 printf ("\t%8.8s%8d%8d%8d%8d%8d%8d\n",
213 "Total",
214 FcAllocCount, FcAllocMem,
215 FcFreeCount, FcFreeMem,
216 FcAllocCount - FcFreeCount,
217 FcAllocMem - FcFreeMem);
218 FcAllocNotify = 0;
219 FcFreeNotify = 0;
220 FcValueListReport ();
221 }
222
223 void
224 FcMemAlloc (int kind, int size)
225 {
226 if (FcDebug() & FC_DBG_MEMORY)
227 {
228 FcInUse[kind].alloc_count++;
229 FcInUse[kind].alloc_mem += size;
230 FcAllocCount++;
231 FcAllocMem += size;
232 FcAllocNotify += size;
233 if (FcAllocNotify > FcMemNotice)
234 FcMemReport ();
235 }
236 }
237
238 void
239 FcMemFree (int kind, int size)
240 {
241 if (FcDebug() & FC_DBG_MEMORY)
242 {
243 FcInUse[kind].free_count++;
244 FcInUse[kind].free_mem += size;
245 FcFreeCount++;
246 FcFreeMem += size;
247 FcFreeNotify += size;
248 if (FcFreeNotify > FcMemNotice)
249 FcMemReport ();
250 }
251 }