private.c revision 23a0898a
1/* $Xorg: private.c,v 1.4 2001/02/09 02:04:04 xorgcvs Exp $ */
2
3/*
4
5Copyright 1991, 1998  The Open Group
6
7Permission to use, copy, modify, distribute, and sell this software and its
8documentation for any purpose is hereby granted without fee, provided that
9the above copyright notice appear in all copies and that both that
10copyright notice and this permission notice appear in supporting
11documentation.
12
13The above copyright notice and this permission notice shall be included in
14all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
19OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23Except as contained in this notice, the name of The Open Group shall not be
24used in advertising or otherwise to promote the sale, use or other dealings
25in this Software without prior written authorization from The Open Group.
26
27*/
28/* $XFree86: xc/lib/font/util/private.c,v 1.8tsi Exp $ */
29
30/*
31 * Author:  Keith Packard, MIT X Consortium
32 */
33
34#ifdef HAVE_CONFIG_H
35#include <config.h>
36#endif
37#include    <X11/fonts/fontmisc.h>
38#include    <X11/fonts/fontstruct.h>
39
40static int _FontPrivateAllocateIndex = 0;
41
42int
43AllocateFontPrivateIndex (void)
44{
45    return _FontPrivateAllocateIndex++;
46}
47
48FontPtr
49CreateFontRec (void)
50{
51    FontPtr pFont;
52    int size;
53
54    size = sizeof(FontRec) + (sizeof(pointer) * _FontPrivateAllocateIndex);
55
56    pFont = (FontPtr)xalloc(size);
57
58    if(pFont) {
59	bzero((char*)pFont, size);
60	pFont->maxPrivate = _FontPrivateAllocateIndex - 1;
61	if(_FontPrivateAllocateIndex)
62	    pFont->devPrivates = (pointer)(&pFont[1]);
63    }
64
65    return pFont;
66}
67
68void
69DestroyFontRec (FontPtr pFont)
70{
71   if (pFont->devPrivates && pFont->devPrivates != (pointer)(&pFont[1]))
72	xfree(pFont->devPrivates);
73   xfree(pFont);
74}
75
76void
77ResetFontPrivateIndex (void)
78{
79    _FontPrivateAllocateIndex = 0;
80}
81
82Bool
83_FontSetNewPrivate (FontPtr pFont, int n, pointer ptr)
84{
85    pointer *new;
86
87    if (n > pFont->maxPrivate) {
88	if (pFont->devPrivates && pFont->devPrivates != (pointer)(&pFont[1])) {
89	    new = (pointer *) xrealloc (pFont->devPrivates, (n + 1) * sizeof (pointer));
90	    if (!new)
91		return FALSE;
92	} else {
93	    new = (pointer *) xalloc ((n + 1) * sizeof (pointer));
94	    if (!new)
95		return FALSE;
96	    if (pFont->devPrivates)
97		memcpy (new, pFont->devPrivates, (pFont->maxPrivate + 1) * sizeof (pointer));
98	}
99	pFont->devPrivates = new;
100	/* zero out new, uninitialized privates */
101	while(++pFont->maxPrivate < n)
102	    pFont->devPrivates[pFont->maxPrivate] = (pointer)0;
103    }
104    pFont->devPrivates[n] = ptr;
105    return TRUE;
106}
107
108