]>
git.wh0rd.org - fontconfig.git/blob - src/fcserialize.c
2 * Copyright © 2006 Keith Packard
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.
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
25 typedef union _FcAlign
{
34 FcAlignSize (intptr_t size
)
36 intptr_t rem
= size
% sizeof (FcAlign
);
38 size
+= sizeof (FcAlign
) - rem
;
43 * Serialization helper object -- allocate space in the
44 * yet-to-be-created linear array for a serialized font set
48 FcSerializeCreate (void)
50 FcSerialize
*serialize
;
52 serialize
= malloc (sizeof (FcSerialize
));
56 serialize
->linear
= NULL
;
57 memset (serialize
->buckets
, '\0', sizeof (serialize
->buckets
));
62 FcSerializeDestroy (FcSerialize
*serialize
)
66 for (bucket
= 0; bucket
< FC_SERIALIZE_HASH_SIZE
; bucket
++)
68 FcSerializeBucket
*buck
, *next
;
70 for (buck
= serialize
->buckets
[bucket
]; buck
; buck
= next
) {
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
84 FcSerializeAlloc (FcSerialize
*serialize
, const void *object
, int size
)
86 uintptr_t bucket
= ((uintptr_t) object
) % FC_SERIALIZE_HASH_SIZE
;
87 FcSerializeBucket
*buck
;
89 for (buck
= serialize
->buckets
[bucket
]; buck
; buck
= buck
->next
)
90 if (buck
->object
== object
)
92 buck
= malloc (sizeof (FcSerializeBucket
));
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
);
104 * Reserve space in the serialization array
107 FcSerializeReserve (FcSerialize
*serialize
, int size
)
109 intptr_t offset
= serialize
->size
;
110 serialize
->size
+= FcAlignSize (size
);
115 * Given an object, return the offset in the serialized array where
116 * the serialized copy of the object is stored
119 FcSerializeOffset (FcSerialize
*serialize
, const void *object
)
121 uintptr_t bucket
= ((uintptr_t) object
) % FC_SERIALIZE_HASH_SIZE
;
122 FcSerializeBucket
*buck
;
124 for (buck
= serialize
->buckets
[bucket
]; buck
; buck
= buck
->next
)
125 if (buck
->object
== object
)
131 * Given a cache and an object, return a pointer to where
132 * the serialized copy of the object is stored
135 FcSerializePtr (FcSerialize
*serialize
, const void *object
)
137 intptr_t offset
= FcSerializeOffset (serialize
, object
);
141 return (void *) ((char *) serialize
->linear
+ offset
);
145 FcStrSerializeAlloc (FcSerialize
*serialize
, const FcChar8
*str
)
147 return FcSerializeAlloc (serialize
, str
, strlen ((const char *) str
) + 1);
151 FcStrSerialize (FcSerialize
*serialize
, const FcChar8
*str
)
153 FcChar8
*str_serialize
= FcSerializePtr (serialize
, str
);
156 strcpy ((char *) str_serialize
, (const char *) str
);
157 return str_serialize
;