]> git.wh0rd.org Git - fontconfig.git/blob - src/fcinit.c
More autoconf cleanup for fontconfig
[fontconfig.git] / src / fcinit.c
1 /*
2  * $XFree86: xc/lib/fontconfig/src/fcinit.c,v 1.4 2002/05/21 17:06:22 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 /*
47  * Load the configuration files
48  */
49 FcConfig *
50 FcInitLoadConfig (void)
51 {
52     FcConfig    *config;
53     
54     config = FcConfigCreate ();
55     if (!config)
56         return FcFalse;
57     
58     if (!FcConfigParseAndLoad (config, 0, FcTrue))
59     {
60         FcConfigDestroy (config);
61         return FcInitFallbackConfig ();
62     }
63
64     return config;
65 }
66
67 /*
68  * Load the configuration files and scan for available fonts
69  */
70 FcConfig *
71 FcInitLoadConfigAndFonts (void)
72 {
73     FcConfig    *config = FcInitLoadConfig ();
74
75     if (!config)
76         return 0;
77     if (!FcConfigBuildFonts (config))
78     {
79         FcConfigDestroy (config);
80         return 0;
81     }
82     return config;
83 }
84
85 /*
86  * Initialize the default library configuration
87  */
88 FcBool
89 FcInit (void)
90 {
91     FcConfig    *config;
92
93     if (_fcConfig)
94         return FcTrue;
95     config = FcInitLoadConfigAndFonts ();
96     if (!config)
97         return FcTrue;
98     FcConfigSetCurrent (config);
99     return FcTrue;
100 }
101
102 /*
103  * Reread the configuration and available font lists
104  */
105 FcBool
106 FcInitReinitialize (void)
107 {
108     FcConfig    *config;
109
110     config = FcInitLoadConfigAndFonts ();
111     if (!config)
112         return FcFalse;
113     FcConfigSetCurrent (config);
114     return FcTrue;
115 }
116
117 FcBool
118 FcInitBringUptoDate (void)
119 {
120     FcConfig    *config = FcConfigGetCurrent ();
121     time_t      now;
122
123     /*
124      * rescanInterval == 0 disables automatic up to date
125      */
126     if (config->rescanInterval == 0)
127         return FcTrue;
128     /*
129      * Check no more often than rescanInterval seconds
130      */
131     now = time (0);
132     if (config->rescanTime + config->rescanInterval - now > 0)
133         return FcTrue;
134     /*
135      * If up to date, don't reload configuration
136      */
137     if (FcConfigUptoDate (0))
138         return FcTrue;
139     return FcInitReinitialize ();
140 }
141
142 static struct {
143     char    *name;
144     int     alloc_count;
145     int     alloc_mem;
146     int     free_count;
147     int     free_mem;
148 } FcInUse[FC_MEM_NUM] = {
149     { "charset", 0, 0 },
150     { "charnode", 0 ,0 },
151     { "fontset", 0, 0 },
152     { "fontptr", 0, 0 },
153     { "objectset", 0, 0 },
154     { "objectptr", 0, 0 },
155     { "matrix", 0, 0 },
156     { "pattern", 0, 0 },
157     { "patelt", 0, 0 },
158     { "vallist", 0, 0 },
159     { "substate", 0, 0 },
160     { "string", 0, 0 },
161     { "listbuck", 0, 0 },
162 };
163
164 static int  FcAllocCount, FcAllocMem;
165 static int  FcFreeCount, FcFreeMem;
166
167 static int  FcMemNotice = 1*1024*1024;
168
169 static int  FcAllocNotify, FcFreeNotify;
170
171 void
172 FcMemReport (void)
173 {
174     int i;
175     printf ("Fc Memory Usage:\n");
176     printf ("\t   Which       Alloc           Free           Active\n");
177     printf ("\t           count   bytes   count   bytes   count   bytes\n");
178     for (i = 0; i < FC_MEM_NUM; i++)
179         printf ("\t%8.8s%8d%8d%8d%8d%8d%8d\n",
180                 FcInUse[i].name,
181                 FcInUse[i].alloc_count, FcInUse[i].alloc_mem,
182                 FcInUse[i].free_count, FcInUse[i].free_mem,
183                 FcInUse[i].alloc_count - FcInUse[i].free_count,
184                 FcInUse[i].alloc_mem - FcInUse[i].free_mem);
185     printf ("\t%8.8s%8d%8d%8d%8d%8d%8d\n",
186             "Total",
187             FcAllocCount, FcAllocMem,
188             FcFreeCount, FcFreeMem,
189             FcAllocCount - FcFreeCount,
190             FcAllocMem - FcFreeMem);
191     FcAllocNotify = 0;
192     FcFreeNotify = 0;
193 }
194
195 void
196 FcMemAlloc (int kind, int size)
197 {
198     if (FcDebug() & FC_DBG_MEMORY)
199     {
200         FcInUse[kind].alloc_count++;
201         FcInUse[kind].alloc_mem += size;
202         FcAllocCount++;
203         FcAllocMem += size;
204         FcAllocNotify += size;
205         if (FcAllocNotify > FcMemNotice)
206             FcMemReport ();
207     }
208 }
209
210 void
211 FcMemFree (int kind, int size)
212 {
213     if (FcDebug() & FC_DBG_MEMORY)
214     {
215         FcInUse[kind].free_count++;
216         FcInUse[kind].free_mem += size;
217         FcFreeCount++;
218         FcFreeMem += size;
219         FcFreeNotify += size;
220         if (FcFreeNotify > FcMemNotice)
221             FcMemReport ();
222     }
223 }