]>
git.wh0rd.org - fontconfig.git/blob - src/fcserialize.c
5a4b4aff67331b14fc0231fb76aca5c631056a84
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
{
35 FcAlignSize (intptr_t size
)
37 intptr_t rem
= size
% sizeof (FcAlign
);
39 size
+= sizeof (FcAlign
) - rem
;
44 * Serialization helper object -- allocate space in the
45 * yet-to-be-created linear array for a serialized font set
49 FcSerializeCreate (void)
51 FcSerialize
*serialize
;
53 serialize
= malloc (sizeof (FcSerialize
));
57 serialize
->linear
= NULL
;
58 memset (serialize
->buckets
, '\0', sizeof (serialize
->buckets
));
63 FcSerializeDestroy (FcSerialize
*serialize
)
67 for (bucket
= 0; bucket
< FC_SERIALIZE_HASH_SIZE
; bucket
++)
69 FcSerializeBucket
*buck
, *next
;
71 for (buck
= serialize
->buckets
[bucket
]; buck
; buck
= next
) {
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
85 FcSerializeAlloc (FcSerialize
*serialize
, const void *object
, int size
)
87 uintptr_t bucket
= ((uintptr_t) object
) % FC_SERIALIZE_HASH_SIZE
;
88 FcSerializeBucket
*buck
;
90 for (buck
= serialize
->buckets
[bucket
]; buck
; buck
= buck
->next
)
91 if (buck
->object
== object
)
93 buck
= malloc (sizeof (FcSerializeBucket
));
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
);
105 * Reserve space in the serialization array
108 FcSerializeReserve (FcSerialize
*serialize
, int size
)
110 intptr_t offset
= serialize
->size
;
111 serialize
->size
+= FcAlignSize (size
);
116 * Given an object, return the offset in the serialized array where
117 * the serialized copy of the object is stored
120 FcSerializeOffset (FcSerialize
*serialize
, const void *object
)
122 uintptr_t bucket
= ((uintptr_t) object
) % FC_SERIALIZE_HASH_SIZE
;
123 FcSerializeBucket
*buck
;
125 for (buck
= serialize
->buckets
[bucket
]; buck
; buck
= buck
->next
)
126 if (buck
->object
== object
)
132 * Given a cache and an object, return a pointer to where
133 * the serialized copy of the object is stored
136 FcSerializePtr (FcSerialize
*serialize
, const void *object
)
138 intptr_t offset
= FcSerializeOffset (serialize
, object
);
142 return (void *) ((char *) serialize
->linear
+ offset
);
146 FcStrSerializeAlloc (FcSerialize
*serialize
, const FcChar8
*str
)
148 return FcSerializeAlloc (serialize
, str
, strlen ((const char *) str
) + 1);
152 FcStrSerialize (FcSerialize
*serialize
, const FcChar8
*str
)
154 FcChar8
*str_serialize
= FcSerializePtr (serialize
, str
);
157 strcpy ((char *) str_serialize
, (const char *) str
);
158 return str_serialize
;