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