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