Make FcCacheIsMmapSafe() threadsafe
[platform/upstream/fontconfig.git] / src / fcobjs.c
1 /*
2  * fontconfig/src/fclist.c
3  *
4  * Copyright © 2000 Keith Packard
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 the author(s) not be used in
11  * advertising or publicity pertaining to distribution of the software without
12  * specific, written prior permission.  The authors make 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  * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18  * EVENT SHALL THE AUTHOR(S) 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 "fcint.h"
26
27 #include "fcobjshash.h"
28
29 #include <string.h>
30
31 static int next_id = FC_MAX_BASE_OBJECT + 1;
32 struct FcObjectOtherTypeInfo {
33     struct FcObjectOtherTypeInfo *next;
34     FcObjectType object;
35     int id;
36 } *other_types;
37
38 static FcObjectType *
39 _FcObjectLookupOtherTypeByName (const char *str, FcObject *id)
40 {
41     struct FcObjectOtherTypeInfo *ots, *ot;
42
43     /* XXX MT-unsafe */
44     ots = other_types;
45
46     for (ot = ots; ot; ot = ot->next)
47         if (0 == strcmp (ot->object.object, str))
48             break;
49
50     if (!ot)
51     {
52         ot = malloc (sizeof (*ot));
53         if (!ot)
54             return NULL;
55
56         ot->object.object = strdup (str);
57         ot->object.type = -1;
58         ot->id = next_id++; /* MT_unsafe */
59         ot->next = ot;
60
61         other_types = ot;
62     }
63
64     if (id)
65       *id = ot->id;
66
67     return &ot->object;
68 }
69
70 FcObject
71 FcObjectLookupBuiltinIdByName (const char *str)
72 {
73     const struct FcObjectTypeInfo *o = FcObjectTypeLookup (str, strlen (str));
74     FcObject id;
75     if (o)
76         return o->id;
77
78     return 0;
79 }
80
81 FcObject
82 FcObjectLookupIdByName (const char *str)
83 {
84     const struct FcObjectTypeInfo *o = FcObjectTypeLookup (str, strlen (str));
85     FcObject id;
86     if (o)
87         return o->id;
88
89     if (_FcObjectLookupOtherTypeByName (str, &id))
90         return id;
91
92     return 0;
93 }
94
95 const char *
96 FcObjectLookupOtherNameById (FcObject id)
97 {
98     /* XXX MT-unsafe */
99     struct FcObjectOtherTypeInfo *ot;
100
101     for (ot = other_types; ot; ot = ot->next)
102         if (ot->id == id)
103             return ot->object.object;
104
105     return NULL;
106 }
107
108 const FcObjectType *
109 FcObjectLookupOtherTypeByName (const char *str)
110 {
111     return _FcObjectLookupOtherTypeByName (str, NULL);
112 }
113
114 FcPrivate const FcObjectType *
115 FcObjectLookupOtherTypeById (FcObject id)
116 {
117     /* XXX MT-unsafe */
118     struct FcObjectOtherTypeInfo *ot;
119
120     for (ot = other_types; ot; ot = ot->next)
121         if (ot->id == id)
122             return &ot->object;
123
124     return NULL;
125 }
126
127
128 #define __fcobjs__
129 #include "fcaliastail.h"
130 #undef __fcobjs__