]>
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
26 FcAlignSize (intptr_t size
)
28 intptr_t rem
= size
% sizeof (FcAlign
);
30 size
+= sizeof (FcAlign
) - rem
;
35 * Serialization helper object -- allocate space in the
36 * yet-to-be-created linear array for a serialized font set
40 FcSerializeCreate (void)
42 FcSerialize
*serialize
;
44 serialize
= malloc (sizeof (FcSerialize
));
48 serialize
->linear
= NULL
;
49 serialize
->cs_freezer
= NULL
;
50 memset (serialize
->buckets
, '\0', sizeof (serialize
->buckets
));
55 FcSerializeDestroy (FcSerialize
*serialize
)
59 for (bucket
= 0; bucket
< FC_SERIALIZE_HASH_SIZE
; bucket
++)
61 FcSerializeBucket
*buck
, *next
;
63 for (buck
= serialize
->buckets
[bucket
]; buck
; buck
= next
) {
68 if (serialize
->cs_freezer
)
69 FcCharSetFreezerDestroy (serialize
->cs_freezer
);
74 * Allocate space for an object in the serialized array. Keep track
75 * of where the object is placed and only allocate one copy of each object
79 FcSerializeAlloc (FcSerialize
*serialize
, const void *object
, int size
)
81 uintptr_t bucket
= ((uintptr_t) object
) % FC_SERIALIZE_HASH_SIZE
;
82 FcSerializeBucket
*buck
;
84 for (buck
= serialize
->buckets
[bucket
]; buck
; buck
= buck
->next
)
85 if (buck
->object
== object
)
87 buck
= malloc (sizeof (FcSerializeBucket
));
90 buck
->object
= object
;
91 buck
->offset
= serialize
->size
;
92 buck
->next
= serialize
->buckets
[bucket
];
93 serialize
->buckets
[bucket
] = buck
;
94 serialize
->size
+= FcAlignSize (size
);
99 * Reserve space in the serialization array
102 FcSerializeReserve (FcSerialize
*serialize
, int size
)
104 intptr_t offset
= serialize
->size
;
105 serialize
->size
+= FcAlignSize (size
);
110 * Given an object, return the offset in the serialized array where
111 * the serialized copy of the object is stored
114 FcSerializeOffset (FcSerialize
*serialize
, const void *object
)
116 uintptr_t bucket
= ((uintptr_t) object
) % FC_SERIALIZE_HASH_SIZE
;
117 FcSerializeBucket
*buck
;
119 for (buck
= serialize
->buckets
[bucket
]; buck
; buck
= buck
->next
)
120 if (buck
->object
== object
)
126 * Given a cache and an object, return a pointer to where
127 * the serialized copy of the object is stored
130 FcSerializePtr (FcSerialize
*serialize
, const void *object
)
132 intptr_t offset
= FcSerializeOffset (serialize
, object
);
136 return (void *) ((char *) serialize
->linear
+ offset
);
140 FcStrSerializeAlloc (FcSerialize
*serialize
, const FcChar8
*str
)
142 return FcSerializeAlloc (serialize
, str
, strlen ((const char *) str
) + 1);
146 FcStrSerialize (FcSerialize
*serialize
, const FcChar8
*str
)
148 FcChar8
*str_serialize
= FcSerializePtr (serialize
, str
);
151 strcpy ((char *) str_serialize
, (const char *) str
);
152 return str_serialize
;
154 #define __fcserialize__
155 #include "fcaliastail.h"
156 #undef __fcserialize__