65702b4421991ca73ec8ae89a54bc5be355adf89
[platform/upstream/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 "fcint.h"
26 #include <stdlib.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     if (!FcConfigAddCacheDir (config, (FcChar8 *) FC_CACHEDIR))
39         goto bail1;
40     return config;
41
42 bail1:
43     FcConfigDestroy (config);
44 bail0:
45     return 0;
46 }
47
48 int
49 FcGetVersion (void)
50 {
51     return FC_VERSION;
52 }
53
54 /*
55  * Load the configuration files
56  */
57 FcConfig *
58 FcInitLoadConfig (void)
59 {
60     FcConfig    *config;
61     
62     FcInitDebug ();
63     config = FcConfigCreate ();
64     if (!config)
65         return FcFalse;
66     
67     if (!FcConfigParseAndLoad (config, 0, FcTrue))
68     {
69         FcConfigDestroy (config);
70         return FcInitFallbackConfig ();
71     }
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     FcInitDebug ();
85     if (!config)
86         return 0;
87     if (!FcConfigBuildFonts (config))
88     {
89         FcConfigDestroy (config);
90         return 0;
91     }
92     return config;
93 }
94
95 /*
96  * Initialize the default library configuration
97  */
98 FcBool
99 FcInit (void)
100 {
101     FcConfig    *config;
102
103     if (_fcConfig)
104         return FcTrue;
105     config = FcInitLoadConfigAndFonts ();
106     if (!config)
107         return FcTrue;
108     FcConfigSetCurrent (config);
109     if (FcDebug() & FC_DBG_MEMORY)
110         FcMemReport ();
111     return FcTrue;
112 }
113
114 /*
115  * Free all library-allocated data structures.
116  */
117 void
118 FcFini (void)
119 {
120     if (_fcConfig)
121         FcConfigDestroy (_fcConfig);
122
123     FcPatternFini ();
124 }
125
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;
137     FcConfigSetCurrent (config);
138     return FcTrue;
139 }
140
141 FcBool
142 FcInitBringUptoDate (void)
143 {
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 ();
164 }
165
166 static struct {
167     char    name[16];
168     int     alloc_count;
169     int     alloc_mem;
170     int     free_count;
171     int     free_mem;
172 } FcInUse[FC_MEM_NUM] = {
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" },
202     { "staticstr" },
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
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 }
235
236 void
237 FcMemAlloc (int kind, int size)
238 {
239     if (FcDebug() & FC_DBG_MEMORY)
240     {
241         FcInUse[kind].alloc_count++;
242         FcInUse[kind].alloc_mem += size;
243         FcAllocCount++;
244         FcAllocMem += size;
245         FcAllocNotify += size;
246         if (FcAllocNotify > FcMemNotice)
247             FcMemReport ();
248     }
249 }
250
251 void
252 FcMemFree (int kind, int size)
253 {
254     if (FcDebug() & FC_DBG_MEMORY)
255     {
256         FcInUse[kind].free_count++;
257         FcInUse[kind].free_mem += size;
258         FcFreeCount++;
259         FcFreeMem += size;
260         FcFreeNotify += size;
261         if (FcFreeNotify > FcMemNotice)
262             FcMemReport ();
263     }
264 }