]> git.wh0rd.org - fontconfig.git/blob - src/fcinit.c
Reviewed by: Keith Packard <keithp@keithp.com>
[fontconfig.git] / src / fcinit.c
1 /*
2 * $RCSId: xc/lib/fontconfig/src/fcinit.c,v 1.7 2002/08/22 07:36:44 keithp Exp $
3 *
4 * Copyright © 2001 Keith Packard
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 * Free all library-allocated data structures.
112 */
113 void
114 FcFini (void)
115 {
116 if (_fcConfig)
117 FcConfigDestroy (_fcConfig);
118
119 FcPatternThawAll ();
120 FcCharSetThawAll ();
121 }
122
123 /*
124 * Reread the configuration and available font lists
125 */
126 FcBool
127 FcInitReinitialize (void)
128 {
129 FcConfig *config;
130
131 config = FcInitLoadConfigAndFonts ();
132 if (!config)
133 return FcFalse;
134 FcConfigSetCurrent (config);
135 return FcTrue;
136 }
137
138 FcBool
139 FcInitBringUptoDate (void)
140 {
141 FcConfig *config = FcConfigGetCurrent ();
142 time_t now;
143
144 /*
145 * rescanInterval == 0 disables automatic up to date
146 */
147 if (config->rescanInterval == 0)
148 return FcTrue;
149 /*
150 * Check no more often than rescanInterval seconds
151 */
152 now = time (0);
153 if (config->rescanTime + config->rescanInterval - now > 0)
154 return FcTrue;
155 /*
156 * If up to date, don't reload configuration
157 */
158 if (FcConfigUptoDate (0))
159 return FcTrue;
160 return FcInitReinitialize ();
161 }
162
163 static struct {
164 char *name;
165 int alloc_count;
166 int alloc_mem;
167 int free_count;
168 int free_mem;
169 } FcInUse[FC_MEM_NUM] = {
170 { "charset" },
171 { "charleaf" },
172 { "fontset" },
173 { "fontptr" },
174 { "objectset" },
175 { "objectptr" },
176 { "matrix" },
177 { "pattern" },
178 { "patelt" },
179 { "vallist" },
180 { "substate" },
181 { "string" },
182 { "listbuck" },
183 { "strset" },
184 { "strlist" },
185 { "config" },
186 { "langset" },
187 { "atomic" },
188 { "blanks" },
189 { "cache" },
190 { "strbuf" },
191 { "subst" },
192 { "objecttype" },
193 { "constant" },
194 { "test" },
195 { "expr" },
196 { "vstack" },
197 { "attr" },
198 { "pstack" },
199 { "staticstr" },
200 };
201
202 static int FcAllocCount, FcAllocMem;
203 static int FcFreeCount, FcFreeMem;
204
205 static int FcMemNotice = 1*1024*1024;
206
207 static int FcAllocNotify, FcFreeNotify;
208
209 void
210 FcValueListReport (void);
211
212 void
213 FcMemReport (void)
214 {
215 int i;
216 printf ("Fc Memory Usage:\n");
217 printf ("\t Which Alloc Free Active\n");
218 printf ("\t count bytes count bytes count bytes\n");
219 for (i = 0; i < FC_MEM_NUM; i++)
220 printf ("%16.16s%8d%8d%8d%8d%8d%8d\n",
221 FcInUse[i].name,
222 FcInUse[i].alloc_count, FcInUse[i].alloc_mem,
223 FcInUse[i].free_count, FcInUse[i].free_mem,
224 FcInUse[i].alloc_count - FcInUse[i].free_count,
225 FcInUse[i].alloc_mem - FcInUse[i].free_mem);
226 printf ("%16.16s%8d%8d%8d%8d%8d%8d\n",
227 "Total",
228 FcAllocCount, FcAllocMem,
229 FcFreeCount, FcFreeMem,
230 FcAllocCount - FcFreeCount,
231 FcAllocMem - FcFreeMem);
232 FcAllocNotify = 0;
233 FcFreeNotify = 0;
234 FcValueListReport ();
235 }
236
237 void
238 FcMemAlloc (int kind, int size)
239 {
240 if (FcDebug() & FC_DBG_MEMORY)
241 {
242 FcInUse[kind].alloc_count++;
243 FcInUse[kind].alloc_mem += size;
244 FcAllocCount++;
245 FcAllocMem += size;
246 FcAllocNotify += size;
247 if (FcAllocNotify > FcMemNotice)
248 FcMemReport ();
249 }
250 }
251
252 void
253 FcMemFree (int kind, int size)
254 {
255 if (FcDebug() & FC_DBG_MEMORY)
256 {
257 FcInUse[kind].free_count++;
258 FcInUse[kind].free_mem += size;
259 FcFreeCount++;
260 FcFreeMem += size;
261 FcFreeNotify += size;
262 if (FcFreeNotify > FcMemNotice)
263 FcMemReport ();
264 }
265 }