]>
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 | ||
64 | if (!FcConfigParseAndLoad (config, 0, FcTrue)) | |
65 | { | |
66 | FcConfigDestroy (config); | |
67 | return FcInitFallbackConfig (); | |
68 | } | |
179c3995 KP |
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); | |
d8d73958 KP |
105 | if (FcDebug() & FC_DBG_MEMORY) |
106 | FcMemReport (); | |
179c3995 KP |
107 | return FcTrue; |
108 | } | |
109 | ||
34cd0514 CW |
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 | ||
179c3995 KP |
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; | |
24330d27 KP |
134 | FcConfigSetCurrent (config); |
135 | return FcTrue; | |
136 | } | |
137 | ||
138 | FcBool | |
179c3995 | 139 | FcInitBringUptoDate (void) |
24330d27 | 140 | { |
179c3995 KP |
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 (); | |
24330d27 KP |
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] = { | |
9dac3c59 KP |
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" }, | |
1c52c0f0 | 199 | { "staticstr" }, |
24330d27 KP |
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 | ||
d8d73958 KP |
209 | void |
210 | FcValueListReport (void); | |
211 | ||
24330d27 KP |
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++) | |
1c52c0f0 | 220 | printf ("%16.16s%8d%8d%8d%8d%8d%8d\n", |
24330d27 KP |
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); | |
1c52c0f0 | 226 | printf ("%16.16s%8d%8d%8d%8d%8d%8d\n", |
24330d27 KP |
227 | "Total", |
228 | FcAllocCount, FcAllocMem, | |
229 | FcFreeCount, FcFreeMem, | |
230 | FcAllocCount - FcFreeCount, | |
231 | FcAllocMem - FcFreeMem); | |
232 | FcAllocNotify = 0; | |
233 | FcFreeNotify = 0; | |
d8d73958 | 234 | FcValueListReport (); |
24330d27 KP |
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 | } |