]> git.wh0rd.org - fontconfig.git/blob - src/fcinit.c
A few random fontconfig build fixes
[fontconfig.git] / src / fcinit.c
1 /*
2 * $XFree86: xc/lib/fontconfig/src/fcinit.c,v 1.5 2002/05/21 17:48:15 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 return FcTrue;
106 }
107
108 /*
109 * Reread the configuration and available font lists
110 */
111 FcBool
112 FcInitReinitialize (void)
113 {
114 FcConfig *config;
115
116 config = FcInitLoadConfigAndFonts ();
117 if (!config)
118 return FcFalse;
119 FcConfigSetCurrent (config);
120 return FcTrue;
121 }
122
123 FcBool
124 FcInitBringUptoDate (void)
125 {
126 FcConfig *config = FcConfigGetCurrent ();
127 time_t now;
128
129 /*
130 * rescanInterval == 0 disables automatic up to date
131 */
132 if (config->rescanInterval == 0)
133 return FcTrue;
134 /*
135 * Check no more often than rescanInterval seconds
136 */
137 now = time (0);
138 if (config->rescanTime + config->rescanInterval - now > 0)
139 return FcTrue;
140 /*
141 * If up to date, don't reload configuration
142 */
143 if (FcConfigUptoDate (0))
144 return FcTrue;
145 return FcInitReinitialize ();
146 }
147
148 static struct {
149 char *name;
150 int alloc_count;
151 int alloc_mem;
152 int free_count;
153 int free_mem;
154 } FcInUse[FC_MEM_NUM] = {
155 { "charset", 0, 0 },
156 { "charnode", 0 ,0 },
157 { "fontset", 0, 0 },
158 { "fontptr", 0, 0 },
159 { "objectset", 0, 0 },
160 { "objectptr", 0, 0 },
161 { "matrix", 0, 0 },
162 { "pattern", 0, 0 },
163 { "patelt", 0, 0 },
164 { "vallist", 0, 0 },
165 { "substate", 0, 0 },
166 { "string", 0, 0 },
167 { "listbuck", 0, 0 },
168 };
169
170 static int FcAllocCount, FcAllocMem;
171 static int FcFreeCount, FcFreeMem;
172
173 static int FcMemNotice = 1*1024*1024;
174
175 static int FcAllocNotify, FcFreeNotify;
176
177 void
178 FcMemReport (void)
179 {
180 int i;
181 printf ("Fc Memory Usage:\n");
182 printf ("\t Which Alloc Free Active\n");
183 printf ("\t count bytes count bytes count bytes\n");
184 for (i = 0; i < FC_MEM_NUM; i++)
185 printf ("\t%8.8s%8d%8d%8d%8d%8d%8d\n",
186 FcInUse[i].name,
187 FcInUse[i].alloc_count, FcInUse[i].alloc_mem,
188 FcInUse[i].free_count, FcInUse[i].free_mem,
189 FcInUse[i].alloc_count - FcInUse[i].free_count,
190 FcInUse[i].alloc_mem - FcInUse[i].free_mem);
191 printf ("\t%8.8s%8d%8d%8d%8d%8d%8d\n",
192 "Total",
193 FcAllocCount, FcAllocMem,
194 FcFreeCount, FcFreeMem,
195 FcAllocCount - FcFreeCount,
196 FcAllocMem - FcFreeMem);
197 FcAllocNotify = 0;
198 FcFreeNotify = 0;
199 }
200
201 void
202 FcMemAlloc (int kind, int size)
203 {
204 if (FcDebug() & FC_DBG_MEMORY)
205 {
206 FcInUse[kind].alloc_count++;
207 FcInUse[kind].alloc_mem += size;
208 FcAllocCount++;
209 FcAllocMem += size;
210 FcAllocNotify += size;
211 if (FcAllocNotify > FcMemNotice)
212 FcMemReport ();
213 }
214 }
215
216 void
217 FcMemFree (int kind, int size)
218 {
219 if (FcDebug() & FC_DBG_MEMORY)
220 {
221 FcInUse[kind].free_count++;
222 FcInUse[kind].free_mem += size;
223 FcFreeCount++;
224 FcFreeMem += size;
225 FcFreeNotify += size;
226 if (FcFreeNotify > FcMemNotice)
227 FcMemReport ();
228 }
229 }