]> git.wh0rd.org - fontconfig.git/blame - src/fcinit.c
Prevent terrible perf regression by getting the if-condition right
[fontconfig.git] / src / fcinit.c
CommitLineData
24330d27 1/*
4bd4418a 2 * $RCSId: xc/lib/fontconfig/src/fcinit.c,v 1.7 2002/08/22 07:36:44 keithp Exp $
24330d27 3 *
46b51147 4 * Copyright © 2001 Keith Packard
24330d27
KP
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
179c3995 28static FcConfig *
24330d27
KP
29FcInitFallbackConfig (void)
30{
31 FcConfig *config;
32
33 config = FcConfigCreate ();
34 if (!config)
35 goto bail0;
446bb9c9 36 if (!FcConfigAddDir (config, (FcChar8 *) FC_DEFAULT_FONTS))
24330d27 37 goto bail1;
179c3995 38 return config;
24330d27
KP
39
40bail1:
41 FcConfigDestroy (config);
42bail0:
179c3995 43 return 0;
24330d27
KP
44}
45
48db40f6
KP
46int
47FcGetVersion (void)
48{
49 return FC_VERSION;
50}
51
24330d27 52/*
179c3995 53 * Load the configuration files
24330d27 54 */
179c3995
KP
55FcConfig *
56FcInitLoadConfig (void)
24330d27 57{
179c3995 58 FcConfig *config;
24330d27 59
b97a34b5 60 FcInitDebug ();
24330d27
KP
61 config = FcConfigCreate ();
62 if (!config)
63 return FcFalse;
64
65 if (!FcConfigParseAndLoad (config, 0, FcTrue))
66 {
67 FcConfigDestroy (config);
68 return FcInitFallbackConfig ();
69 }
179c3995
KP
70
71 return config;
72}
73
74/*
75 * Load the configuration files and scan for available fonts
76 */
77FcConfig *
78FcInitLoadConfigAndFonts (void)
79{
80 FcConfig *config = FcInitLoadConfig ();
81
b97a34b5 82 FcInitDebug ();
179c3995
KP
83 if (!config)
84 return 0;
85 if (!FcConfigBuildFonts (config))
86 {
87 FcConfigDestroy (config);
88 return 0;
89 }
90 return config;
91}
92
93/*
94 * Initialize the default library configuration
95 */
96FcBool
97FcInit (void)
98{
99 FcConfig *config;
100
101 if (_fcConfig)
102 return FcTrue;
103 config = FcInitLoadConfigAndFonts ();
104 if (!config)
105 return FcTrue;
106 FcConfigSetCurrent (config);
d8d73958
KP
107 if (FcDebug() & FC_DBG_MEMORY)
108 FcMemReport ();
179c3995
KP
109 return FcTrue;
110}
111
34cd0514
CW
112/*
113 * Free all library-allocated data structures.
114 */
115void
116FcFini (void)
117{
118 if (_fcConfig)
119 FcConfigDestroy (_fcConfig);
120
e1b9d091 121 FcPatternFini ();
34cd0514
CW
122 FcCharSetThawAll ();
123}
124
179c3995
KP
125/*
126 * Reread the configuration and available font lists
127 */
128FcBool
129FcInitReinitialize (void)
130{
131 FcConfig *config;
132
133 config = FcInitLoadConfigAndFonts ();
134 if (!config)
135 return FcFalse;
24330d27
KP
136 FcConfigSetCurrent (config);
137 return FcTrue;
138}
139
140FcBool
179c3995 141FcInitBringUptoDate (void)
24330d27 142{
179c3995
KP
143 FcConfig *config = FcConfigGetCurrent ();
144 time_t now;
145
146 /*
147 * rescanInterval == 0 disables automatic up to date
148 */
149 if (config->rescanInterval == 0)
150 return FcTrue;
151 /*
152 * Check no more often than rescanInterval seconds
153 */
154 now = time (0);
155 if (config->rescanTime + config->rescanInterval - now > 0)
156 return FcTrue;
157 /*
158 * If up to date, don't reload configuration
159 */
160 if (FcConfigUptoDate (0))
161 return FcTrue;
162 return FcInitReinitialize ();
24330d27
KP
163}
164
165static struct {
67accef4 166 char name[16];
24330d27
KP
167 int alloc_count;
168 int alloc_mem;
169 int free_count;
170 int free_mem;
171} FcInUse[FC_MEM_NUM] = {
9dac3c59
KP
172 { "charset" },
173 { "charleaf" },
174 { "fontset" },
175 { "fontptr" },
176 { "objectset" },
177 { "objectptr" },
178 { "matrix" },
179 { "pattern" },
180 { "patelt" },
181 { "vallist" },
182 { "substate" },
183 { "string" },
184 { "listbuck" },
185 { "strset" },
186 { "strlist" },
187 { "config" },
188 { "langset" },
189 { "atomic" },
190 { "blanks" },
191 { "cache" },
192 { "strbuf" },
193 { "subst" },
194 { "objecttype" },
195 { "constant" },
196 { "test" },
197 { "expr" },
198 { "vstack" },
199 { "attr" },
200 { "pstack" },
1c52c0f0 201 { "staticstr" },
24330d27
KP
202};
203
204static int FcAllocCount, FcAllocMem;
205static int FcFreeCount, FcFreeMem;
206
207static int FcMemNotice = 1*1024*1024;
208
209static int FcAllocNotify, FcFreeNotify;
210
d8d73958
KP
211void
212FcValueListReport (void);
213
24330d27
KP
214void
215FcMemReport (void)
216{
217 int i;
218 printf ("Fc Memory Usage:\n");
219 printf ("\t Which Alloc Free Active\n");
220 printf ("\t count bytes count bytes count bytes\n");
221 for (i = 0; i < FC_MEM_NUM; i++)
1c52c0f0 222 printf ("%16.16s%8d%8d%8d%8d%8d%8d\n",
24330d27
KP
223 FcInUse[i].name,
224 FcInUse[i].alloc_count, FcInUse[i].alloc_mem,
225 FcInUse[i].free_count, FcInUse[i].free_mem,
226 FcInUse[i].alloc_count - FcInUse[i].free_count,
227 FcInUse[i].alloc_mem - FcInUse[i].free_mem);
1c52c0f0 228 printf ("%16.16s%8d%8d%8d%8d%8d%8d\n",
24330d27
KP
229 "Total",
230 FcAllocCount, FcAllocMem,
231 FcFreeCount, FcFreeMem,
232 FcAllocCount - FcFreeCount,
233 FcAllocMem - FcFreeMem);
234 FcAllocNotify = 0;
235 FcFreeNotify = 0;
d8d73958 236 FcValueListReport ();
24330d27
KP
237}
238
239void
240FcMemAlloc (int kind, int size)
241{
242 if (FcDebug() & FC_DBG_MEMORY)
243 {
244 FcInUse[kind].alloc_count++;
245 FcInUse[kind].alloc_mem += size;
246 FcAllocCount++;
247 FcAllocMem += size;
248 FcAllocNotify += size;
249 if (FcAllocNotify > FcMemNotice)
250 FcMemReport ();
251 }
252}
253
254void
255FcMemFree (int kind, int size)
256{
257 if (FcDebug() & FC_DBG_MEMORY)
258 {
259 FcInUse[kind].free_count++;
260 FcInUse[kind].free_mem += size;
261 FcFreeCount++;
262 FcFreeMem += size;
263 FcFreeNotify += size;
264 if (FcFreeNotify > FcMemNotice)
265 FcMemReport ();
266 }
267}