]> git.wh0rd.org - fontconfig.git/blame - src/fcserialize.c
Remove stale architecture signatures.
[fontconfig.git] / src / fcserialize.c
CommitLineData
e3096d90
KP
1/*
2 * Copyright © 2006 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 copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS 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 PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#include "fcint.h"
24
25typedef union _FcAlign {
26 double d;
27 int i;
28 intptr_t ip;
e3096d90
KP
29 FcBool b;
30 void *p;
31} FcAlign;
32
33intptr_t
34FcAlignSize (intptr_t size)
35{
36 intptr_t rem = size % sizeof (FcAlign);
37 if (rem)
38 size += sizeof (FcAlign) - rem;
39 return size;
40}
41
42/*
43 * Serialization helper object -- allocate space in the
44 * yet-to-be-created linear array for a serialized font set
45 */
46
47FcSerialize *
48FcSerializeCreate (void)
49{
50 FcSerialize *serialize;
51
52 serialize = malloc (sizeof (FcSerialize));
53 if (!serialize)
54 return NULL;
55 serialize->size = 0;
56 serialize->linear = NULL;
57 memset (serialize->buckets, '\0', sizeof (serialize->buckets));
58 return serialize;
59}
60
61void
62FcSerializeDestroy (FcSerialize *serialize)
63{
64 uintptr_t bucket;
65
66 for (bucket = 0; bucket < FC_SERIALIZE_HASH_SIZE; bucket++)
67 {
68 FcSerializeBucket *buck, *next;
69
70 for (buck = serialize->buckets[bucket]; buck; buck = next) {
71 next = buck->next;
72 free (buck);
73 }
74 }
75 free (serialize);
76}
77
78/*
79 * Allocate space for an object in the serialized array. Keep track
80 * of where the object is placed and only allocate one copy of each object
81 */
82
83FcBool
84FcSerializeAlloc (FcSerialize *serialize, const void *object, int size)
85{
86 uintptr_t bucket = ((uintptr_t) object) % FC_SERIALIZE_HASH_SIZE;
87 FcSerializeBucket *buck;
88
89 for (buck = serialize->buckets[bucket]; buck; buck = buck->next)
90 if (buck->object == object)
91 return FcTrue;
92 buck = malloc (sizeof (FcSerializeBucket));
93 if (!buck)
94 return FcFalse;
95 buck->object = object;
96 buck->offset = serialize->size;
97 buck->next = serialize->buckets[bucket];
98 serialize->buckets[bucket] = buck;
99 serialize->size += FcAlignSize (size);
100 return FcTrue;
101}
102
103/*
104 * Reserve space in the serialization array
105 */
106intptr_t
107FcSerializeReserve (FcSerialize *serialize, int size)
108{
109 intptr_t offset = serialize->size;
110 serialize->size += FcAlignSize (size);
111 return offset;
112}
113
114/*
115 * Given an object, return the offset in the serialized array where
116 * the serialized copy of the object is stored
117 */
118intptr_t
119FcSerializeOffset (FcSerialize *serialize, const void *object)
120{
121 uintptr_t bucket = ((uintptr_t) object) % FC_SERIALIZE_HASH_SIZE;
122 FcSerializeBucket *buck;
123
124 for (buck = serialize->buckets[bucket]; buck; buck = buck->next)
125 if (buck->object == object)
126 return buck->offset;
127 return 0;
128}
129
130/*
131 * Given a cache and an object, return a pointer to where
132 * the serialized copy of the object is stored
133 */
134void *
135FcSerializePtr (FcSerialize *serialize, const void *object)
136{
137 intptr_t offset = FcSerializeOffset (serialize, object);
138
139 if (!offset)
140 return NULL;
141 return (void *) ((char *) serialize->linear + offset);
142}
143
144FcBool
145FcStrSerializeAlloc (FcSerialize *serialize, const FcChar8 *str)
146{
147 return FcSerializeAlloc (serialize, str, strlen ((const char *) str) + 1);
148}
149
150FcChar8 *
151FcStrSerialize (FcSerialize *serialize, const FcChar8 *str)
152{
153 FcChar8 *str_serialize = FcSerializePtr (serialize, str);
154 if (!str_serialize)
155 return NULL;
156 strcpy ((char *) str_serialize, (const char *) str);
157 return str_serialize;
158}