]> git.wh0rd.org Git - fontconfig.git/blob - src/fcinit.c
Inline FcDebug invocations and only initialize once, in FcInit*. Improve
[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     FcInitDebug ();
61     config = FcConfigCreate ();
62     if (!config)
63         return FcFalse;
64     
65     if (!FcConfigParseAndLoad (config, 0, FcTrue))
66     {
67         FcConfigDestroy (config);
68         return FcInitFallbackConfig ();
69     }
70
71     return config;
72 }
73
74 /*
75  * Load the configuration files and scan for available fonts
76  */
77 FcConfig *
78 FcInitLoadConfigAndFonts (void)
79 {
80     FcConfig    *config = FcInitLoadConfig ();
81
82     FcInitDebug ();
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  */
96 FcBool
97 FcInit (void)
98 {
99     FcConfig    *config;
100
101     if (_fcConfig)
102         return FcTrue;
103     config = FcInitLoadConfigAndFonts ();
104     if (!config)
105         return FcTrue;
106     FcConfigSetCurrent (config);
107     if (FcDebug() & FC_DBG_MEMORY)
108         FcMemReport ();
109     return FcTrue;
110 }
111
112 /*
113  * Free all library-allocated data structures.
114  */
115 void
116 FcFini (void)
117 {
118     if (_fcConfig)
119         FcConfigDestroy (_fcConfig);
120
121     FcPatternFini ();
122     FcCharSetThawAll ();
123 }
124
125 /*
126  * Reread the configuration and available font lists
127  */
128 FcBool
129 FcInitReinitialize (void)
130 {
131     FcConfig    *config;
132
133     config = FcInitLoadConfigAndFonts ();
134     if (!config)
135         return FcFalse;
136     FcConfigSetCurrent (config);
137     return FcTrue;
138 }
139
140 FcBool
141 FcInitBringUptoDate (void)
142 {
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 ();
163 }
164
165 static struct {
166     char    name[16];
167     int     alloc_count;
168     int     alloc_mem;
169     int     free_count;
170     int     free_mem;
171 } FcInUse[FC_MEM_NUM] = {
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" },
201     { "staticstr" },
202 };
203
204 static int  FcAllocCount, FcAllocMem;
205 static int  FcFreeCount, FcFreeMem;
206
207 static int  FcMemNotice = 1*1024*1024;
208
209 static int  FcAllocNotify, FcFreeNotify;
210
211 void
212 FcValueListReport (void);
213
214 void
215 FcMemReport (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++)
222         printf ("%16.16s%8d%8d%8d%8d%8d%8d\n",
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);
228     printf ("%16.16s%8d%8d%8d%8d%8d%8d\n",
229             "Total",
230             FcAllocCount, FcAllocMem,
231             FcFreeCount, FcFreeMem,
232             FcAllocCount - FcFreeCount,
233             FcAllocMem - FcFreeMem);
234     FcAllocNotify = 0;
235     FcFreeNotify = 0;
236     FcValueListReport ();
237 }
238
239 void
240 FcMemAlloc (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
254 void
255 FcMemFree (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 }