1/*
2 * Copyright © 2000 Keith Packard
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of Keith Packard not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission.  Keith Packard makes no
11 * representations about the suitability of this software for any purpose.  It
12 * is provided "as is" without express or implied warranty.
13 *
14 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
21 */
22
23#include "xftint.h"
24
25static Bool _XftConfigInitialized;
26
27_X_EXPORT Bool
28XftInit (_Xconst char *config _X_UNUSED)
29{
30    if (_XftConfigInitialized)
31	return True;
32    _XftConfigInitialized = True;
33    if (!FcInit ())
34	return False;
35    return True;
36}
37
38_X_EXPORT int
39XftGetVersion (void)
40{
41    return XftVersion;
42}
43
44static struct {
45    const char *name;
46    int	    alloc_count;
47    size_t  alloc_mem;
48    int	    free_count;
49    size_t  free_mem;
50} XftInUse[XFT_MEM_NUM] = {
51    { "XftDraw",   0, 0, 0, 0 },	/* XFT_MEM_DRAW */
52    { "XftFont",   0, 0, 0, 0 },	/* XFT_MEM_FONT */
53    { "XftFtFile", 0, 0, 0, 0 },	/* XFT_MEM_FILE */
54    { "XftGlyph",  0, 0, 0, 0 },	/* XFT_MEM_GLYPH */
55};
56
57static int  XftAllocCount;
58static size_t XftAllocMem;
59
60static int  XftFreeCount;
61static size_t XftFreeMem;
62
63static const size_t  XftMemNotice = 1*1024*1024;
64
65static size_t  XftAllocNotify, XftFreeNotify;
66
67_X_HIDDEN void
68XftMemReport (void)
69{
70    int	i;
71    printf ("Xft Memory Usage:\n");
72    printf ("\t    Which       Alloc           Free\n");
73    printf ("\t            count   bytes   count   bytes\n");
74    for (i = 0; i < XFT_MEM_NUM; i++)
75	printf ("\t%9.9s%8d%8lu%8d%8lu\n",
76		XftInUse[i].name,
77		XftInUse[i].alloc_count, (unsigned long) XftInUse[i].alloc_mem,
78		XftInUse[i].free_count, (unsigned long) XftInUse[i].free_mem);
79    printf ("\t%9.9s%8d%8lu%8d%8lu\n",
80	    "Total",
81	    XftAllocCount, (unsigned long) XftAllocMem,
82	    XftFreeCount, (unsigned long) XftFreeMem);
83    XftAllocNotify = 0;
84    XftFreeNotify = 0;
85}
86
87_X_HIDDEN void
88XftMemAlloc (int kind, size_t size)
89{
90    if (XftDebug() & XFT_DBG_MEMORY)
91    {
92	XftInUse[kind].alloc_count++;
93	XftInUse[kind].alloc_mem += size;
94	XftAllocCount++;
95	XftAllocMem += size;
96	XftAllocNotify += size;
97	if (XftAllocNotify > XftMemNotice)
98	    XftMemReport ();
99    }
100}
101
102_X_HIDDEN void
103XftMemFree (int kind, size_t size)
104{
105    if (XftDebug() & XFT_DBG_MEMORY)
106    {
107	XftInUse[kind].free_count++;
108	XftInUse[kind].free_mem += size;
109	XftFreeCount++;
110	XftFreeMem += size;
111	XftFreeNotify += size;
112	if (XftFreeNotify > XftMemNotice)
113	    XftMemReport ();
114    }
115}
116