80324fbe71ae1c8194fb30ee677a51c5f11370a0
[platform/upstream/fontconfig.git] / src / fcinit.c
1 /*
2  * $XFree86: xc/lib/fontconfig/src/fcinit.c,v 1.2 2002/02/15 06:01:28 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 FcBool
29 FcInitFonts (void)
30 {
31     FcConfig    *config;
32
33     config = FcConfigGetCurrent ();
34     if (!config)
35         return FcFalse;
36
37     if (FcConfigGetFonts (config, FcSetSystem))
38         return FcTrue;
39
40     return FcConfigBuildFonts (config);
41 }
42
43 static FcBool
44 FcInitFallbackConfig (void)
45 {
46     FcConfig    *config;
47
48     config = FcConfigCreate ();
49     if (!config)
50         goto bail0;
51     if (!FcConfigAddDir (config, (FcChar8 *) FC_FALLBACK_FONTS))
52         goto bail1;
53     FcConfigSetCurrent (config);
54     return FcTrue;
55
56 bail1:
57     FcConfigDestroy (config);
58 bail0:
59     return FcFalse;
60 }
61
62 /*
63  * Locate and parse the configuration file
64  */
65 FcBool
66 FcInitConfig (void)
67 {
68     FcConfig    *config;
69     
70     if (_fcConfig)
71         return FcTrue;
72     
73     config = FcConfigCreate ();
74     if (!config)
75         return FcFalse;
76     
77     if (!FcConfigParseAndLoad (config, 0, FcTrue))
78     {
79         FcConfigDestroy (config);
80         return FcInitFallbackConfig ();
81     }
82     
83     FcConfigSetCurrent (config);
84     return FcTrue;
85 }
86
87 FcBool
88 FcInit (void)
89 {
90     return FcInitConfig () && FcInitFonts ();
91 }
92
93 static struct {
94     char    *name;
95     int     alloc_count;
96     int     alloc_mem;
97     int     free_count;
98     int     free_mem;
99 } FcInUse[FC_MEM_NUM] = {
100     { "charset", 0, 0 },
101     { "charnode", 0 ,0 },
102     { "fontset", 0, 0 },
103     { "fontptr", 0, 0 },
104     { "objectset", 0, 0 },
105     { "objectptr", 0, 0 },
106     { "matrix", 0, 0 },
107     { "pattern", 0, 0 },
108     { "patelt", 0, 0 },
109     { "vallist", 0, 0 },
110     { "substate", 0, 0 },
111     { "string", 0, 0 },
112     { "listbuck", 0, 0 },
113 };
114
115 static int  FcAllocCount, FcAllocMem;
116 static int  FcFreeCount, FcFreeMem;
117
118 static int  FcMemNotice = 1*1024*1024;
119
120 static int  FcAllocNotify, FcFreeNotify;
121
122 void
123 FcMemReport (void)
124 {
125     int i;
126     printf ("Fc Memory Usage:\n");
127     printf ("\t   Which       Alloc           Free           Active\n");
128     printf ("\t           count   bytes   count   bytes   count   bytes\n");
129     for (i = 0; i < FC_MEM_NUM; i++)
130         printf ("\t%8.8s%8d%8d%8d%8d%8d%8d\n",
131                 FcInUse[i].name,
132                 FcInUse[i].alloc_count, FcInUse[i].alloc_mem,
133                 FcInUse[i].free_count, FcInUse[i].free_mem,
134                 FcInUse[i].alloc_count - FcInUse[i].free_count,
135                 FcInUse[i].alloc_mem - FcInUse[i].free_mem);
136     printf ("\t%8.8s%8d%8d%8d%8d%8d%8d\n",
137             "Total",
138             FcAllocCount, FcAllocMem,
139             FcFreeCount, FcFreeMem,
140             FcAllocCount - FcFreeCount,
141             FcAllocMem - FcFreeMem);
142     FcAllocNotify = 0;
143     FcFreeNotify = 0;
144 }
145
146 void
147 FcMemAlloc (int kind, int size)
148 {
149     if (FcDebug() & FC_DBG_MEMORY)
150     {
151         FcInUse[kind].alloc_count++;
152         FcInUse[kind].alloc_mem += size;
153         FcAllocCount++;
154         FcAllocMem += size;
155         FcAllocNotify += size;
156         if (FcAllocNotify > FcMemNotice)
157             FcMemReport ();
158     }
159 }
160
161 void
162 FcMemFree (int kind, int size)
163 {
164     if (FcDebug() & FC_DBG_MEMORY)
165     {
166         FcInUse[kind].free_count++;
167         FcInUse[kind].free_mem += size;
168         FcFreeCount++;
169         FcFreeMem += size;
170         FcFreeNotify += size;
171         if (FcFreeNotify > FcMemNotice)
172             FcMemReport ();
173     }
174 }