]> git.wh0rd.org - fontconfig.git/blame - src/fcserialize.c
Fix build problems caused by cache rework.
[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;
29 off_t o;
30 FcBool b;
31 void *p;
32} FcAlign;
33
34intptr_t
35FcAlignSize (intptr_t size)
36{
37 intptr_t rem = size % sizeof (FcAlign);
38 if (rem)
39 size += sizeof (FcAlign) - rem;
40 return size;
41}
42
43/*
44 * Serialization helper object -- allocate space in the
45 * yet-to-be-created linear array for a serialized font set
46 */
47
48FcSerialize *
49FcSerializeCreate (void)
50{
51 FcSerialize *serialize;
52
53 serialize = malloc (sizeof (FcSerialize));
54 if (!serialize)
55 return NULL;
56 serialize->size = 0;
57 serialize->linear = NULL;
58 memset (serialize->buckets, '\0', sizeof (serialize->buckets));
59 return serialize;
60}
61
62void
63FcSerializeDestroy (FcSerialize *serialize)
64{
65 uintptr_t bucket;
66
67 for (bucket = 0; bucket < FC_SERIALIZE_HASH_SIZE; bucket++)
68 {
69 FcSerializeBucket *buck, *next;
70
71 for (buck = serialize->buckets[bucket]; buck; buck = next) {
72 next = buck->next;
73 free (buck);
74 }
75 }
76 free (serialize);
77}
78
79/*
80 * Allocate space for an object in the serialized array. Keep track
81 * of where the object is placed and only allocate one copy of each object
82 */
83
84FcBool
85FcSerializeAlloc (FcSerialize *serialize, const void *object, int size)
86{
87 uintptr_t bucket = ((uintptr_t) object) % FC_SERIALIZE_HASH_SIZE;
88 FcSerializeBucket *buck;
89
90 for (buck = serialize->buckets[bucket]; buck; buck = buck->next)
91 if (buck->object == object)
92 return FcTrue;
93 buck = malloc (sizeof (FcSerializeBucket));
94 if (!buck)
95 return FcFalse;
96 buck->object = object;
97 buck->offset = serialize->size;
98 buck->next = serialize->buckets[bucket];
99 serialize->buckets[bucket] = buck;
100 serialize->size += FcAlignSize (size);
101 return FcTrue;
102}
103
104/*
105 * Reserve space in the serialization array
106 */
107intptr_t
108FcSerializeReserve (FcSerialize *serialize, int size)
109{
110 intptr_t offset = serialize->size;
111 serialize->size += FcAlignSize (size);
112 return offset;
113}
114
115/*
116 * Given an object, return the offset in the serialized array where
117 * the serialized copy of the object is stored
118 */
119intptr_t
120FcSerializeOffset (FcSerialize *serialize, const void *object)
121{
122 uintptr_t bucket = ((uintptr_t) object) % FC_SERIALIZE_HASH_SIZE;
123 FcSerializeBucket *buck;
124
125 for (buck = serialize->buckets[bucket]; buck; buck = buck->next)
126 if (buck->object == object)
127 return buck->offset;
128 return 0;
129}
130
131/*
132 * Given a cache and an object, return a pointer to where
133 * the serialized copy of the object is stored
134 */
135void *
136FcSerializePtr (FcSerialize *serialize, const void *object)
137{
138 intptr_t offset = FcSerializeOffset (serialize, object);
139
140 if (!offset)
141 return NULL;
142 return (void *) ((char *) serialize->linear + offset);
143}
144
145FcBool
146FcStrSerializeAlloc (FcSerialize *serialize, const FcChar8 *str)
147{
148 return FcSerializeAlloc (serialize, str, strlen ((const char *) str) + 1);
149}
150
151FcChar8 *
152FcStrSerialize (FcSerialize *serialize, const FcChar8 *str)
153{
154 FcChar8 *str_serialize = FcSerializePtr (serialize, str);
155 if (!str_serialize)
156 return NULL;
157 strcpy ((char *) str_serialize, (const char *) str);
158 return str_serialize;
159}