fcobjs.c revision 953daeba
1c9710b42Smrg/*
2c9710b42Smrg * fontconfig/src/fclist.c
3c9710b42Smrg *
4c9710b42Smrg * Copyright © 2000 Keith Packard
5c9710b42Smrg *
6c9710b42Smrg * Permission to use, copy, modify, distribute, and sell this software and its
7c9710b42Smrg * documentation for any purpose is hereby granted without fee, provided that
8c9710b42Smrg * the above copyright notice appear in all copies and that both that
9c9710b42Smrg * copyright notice and this permission notice appear in supporting
10c9710b42Smrg * documentation, and that the name of the author(s) not be used in
11c9710b42Smrg * advertising or publicity pertaining to distribution of the software without
12c9710b42Smrg * specific, written prior permission.  The authors make no
13c9710b42Smrg * representations about the suitability of this software for any purpose.  It
14c9710b42Smrg * is provided "as is" without express or implied warranty.
15c9710b42Smrg *
16c9710b42Smrg * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17c9710b42Smrg * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18c9710b42Smrg * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19c9710b42Smrg * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20c9710b42Smrg * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21c9710b42Smrg * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22c9710b42Smrg * PERFORMANCE OF THIS SOFTWARE.
23c9710b42Smrg */
24c9710b42Smrg
25c9710b42Smrg#include "fcint.h"
26c9710b42Smrg
27c9710b42Smrgstatic unsigned int
28953daebaSmrgFcObjectTypeHash (register const char *str, register FC_GPERF_SIZE_T len);
29c9710b42Smrg
30c9710b42Smrgstatic const struct FcObjectTypeInfo *
31953daebaSmrgFcObjectTypeLookup (register const char *str, register FC_GPERF_SIZE_T len);
32c9710b42Smrg
33c9710b42Smrg#include "fcobjshash.h"
34c9710b42Smrg
35c9710b42Smrg#include <string.h>
36c9710b42Smrg
37c9710b42Smrg/* The 1000 is to leave some room for future added internal objects, such
38c9710b42Smrg * that caches from newer fontconfig can still be used with older fontconfig
39c9710b42Smrg * without getting confused. */
406fc018e4Smrgstatic fc_atomic_int_t next_id = FC_MAX_BASE_OBJECT + FC_EXT_OBJ_INDEX;
41c9710b42Smrgstruct FcObjectOtherTypeInfo {
42c9710b42Smrg    struct FcObjectOtherTypeInfo *next;
43c9710b42Smrg    FcObjectType object;
44c9710b42Smrg    FcObject id;
45c9710b42Smrg} *other_types;
46c9710b42Smrg
47953daebaSmrgvoid
48953daebaSmrgFcObjectFini (void)
49953daebaSmrg{
50953daebaSmrg    struct FcObjectOtherTypeInfo *ots, *ot;
51953daebaSmrg
52953daebaSmrgretry:
53953daebaSmrg    ots = fc_atomic_ptr_get (&other_types);
54953daebaSmrg    if (!ots)
55953daebaSmrg	return;
56953daebaSmrg    if (!fc_atomic_ptr_cmpexch (&other_types, ots, NULL))
57953daebaSmrg	goto retry;
58953daebaSmrg
59953daebaSmrg    while (ots)
60953daebaSmrg    {
61953daebaSmrg	ot = ots->next;
62953daebaSmrg	if (ots->object.object)
63953daebaSmrg	    free (ots->object.object);
64953daebaSmrg	free (ots);
65953daebaSmrg	ots = ot;
66953daebaSmrg    }
67953daebaSmrg}
68953daebaSmrg
69c9710b42Smrgstatic FcObjectType *
70c9710b42Smrg_FcObjectLookupOtherTypeByName (const char *str, FcObject *id)
71c9710b42Smrg{
72c9710b42Smrg    struct FcObjectOtherTypeInfo *ots, *ot;
73c9710b42Smrg
74c9710b42Smrgretry:
75c9710b42Smrg    ots = fc_atomic_ptr_get (&other_types);
76c9710b42Smrg
77c9710b42Smrg    for (ot = ots; ot; ot = ot->next)
78c9710b42Smrg	if (0 == strcmp (ot->object.object, str))
79c9710b42Smrg	    break;
80c9710b42Smrg
81c9710b42Smrg    if (!ot)
82c9710b42Smrg    {
83c9710b42Smrg	ot = malloc (sizeof (*ot));
84c9710b42Smrg	if (!ot)
85c9710b42Smrg	    return NULL;
86c9710b42Smrg
87953daebaSmrg	ot->object.object = (char *) FcStrdup (str);
886fc018e4Smrg	ot->object.type = FcTypeUnknown;
89c9710b42Smrg	ot->id = fc_atomic_int_add (next_id, +1);
90953daebaSmrg	if (ot->id < (FC_MAX_BASE_OBJECT + FC_EXT_OBJ_INDEX))
91953daebaSmrg	{
92953daebaSmrg	    fprintf (stderr, "Fontconfig error: No object ID to assign\n");
93953daebaSmrg	    abort ();
94953daebaSmrg	}
95c9710b42Smrg	ot->next = ots;
96c9710b42Smrg
97c9710b42Smrg	if (!fc_atomic_ptr_cmpexch (&other_types, ots, ot)) {
98953daebaSmrg	    if (ot->object.object)
99953daebaSmrg		free (ot->object.object);
100c9710b42Smrg	    free (ot);
101c9710b42Smrg	    goto retry;
102c9710b42Smrg	}
103c9710b42Smrg    }
104c9710b42Smrg
105c9710b42Smrg    if (id)
106c9710b42Smrg      *id = ot->id;
107c9710b42Smrg
108c9710b42Smrg    return &ot->object;
109c9710b42Smrg}
110c9710b42Smrg
111c9710b42SmrgFcObject
112c9710b42SmrgFcObjectLookupBuiltinIdByName (const char *str)
113c9710b42Smrg{
114c9710b42Smrg    const struct FcObjectTypeInfo *o = FcObjectTypeLookup (str, strlen (str));
115c9710b42Smrg
116c9710b42Smrg    if (o)
117c9710b42Smrg	return o->id;
118c9710b42Smrg
119c9710b42Smrg    return 0;
120c9710b42Smrg}
121c9710b42Smrg
122c9710b42SmrgFcObject
123c9710b42SmrgFcObjectLookupIdByName (const char *str)
124c9710b42Smrg{
125c9710b42Smrg    const struct FcObjectTypeInfo *o = FcObjectTypeLookup (str, strlen (str));
126c9710b42Smrg    FcObject id;
127c9710b42Smrg    if (o)
128c9710b42Smrg	return o->id;
129c9710b42Smrg
130c9710b42Smrg    if (_FcObjectLookupOtherTypeByName (str, &id))
131c9710b42Smrg	return id;
132c9710b42Smrg
133c9710b42Smrg    return 0;
134c9710b42Smrg}
135c9710b42Smrg
136c9710b42Smrgconst char *
137c9710b42SmrgFcObjectLookupOtherNameById (FcObject id)
138c9710b42Smrg{
139c9710b42Smrg    struct FcObjectOtherTypeInfo *ot;
140c9710b42Smrg
141c9710b42Smrg    for (ot = fc_atomic_ptr_get (&other_types); ot; ot = ot->next)
142c9710b42Smrg	if (ot->id == id)
143c9710b42Smrg	    return ot->object.object;
144c9710b42Smrg
145c9710b42Smrg    return NULL;
146c9710b42Smrg}
147c9710b42Smrg
148c9710b42Smrgconst FcObjectType *
149c9710b42SmrgFcObjectLookupOtherTypeByName (const char *str)
150c9710b42Smrg{
151c9710b42Smrg    return _FcObjectLookupOtherTypeByName (str, NULL);
152c9710b42Smrg}
153c9710b42Smrg
154c9710b42SmrgFcPrivate const FcObjectType *
155c9710b42SmrgFcObjectLookupOtherTypeById (FcObject id)
156c9710b42Smrg{
157c9710b42Smrg    struct FcObjectOtherTypeInfo *ot;
158c9710b42Smrg
159c9710b42Smrg    for (ot = fc_atomic_ptr_get (&other_types); ot; ot = ot->next)
160c9710b42Smrg	if (ot->id == id)
161c9710b42Smrg	    return &ot->object;
162c9710b42Smrg
163c9710b42Smrg    return NULL;
164c9710b42Smrg}
165c9710b42Smrg
166c9710b42Smrg
167c9710b42Smrg#include "fcaliastail.h"
168c9710b42Smrg#undef __fcobjs__
169