]>
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 serialize
->cs_freezer
= 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
) {
76 if (serialize
->cs_freezer
)
77 FcCharSetFreezerDestroy (serialize
->cs_freezer
);
82 * Allocate space for an object in the serialized array. Keep track
83 * of where the object is placed and only allocate one copy of each object
87 FcSerializeAlloc (FcSerialize
*serialize
, const void *object
, int size
)
89 uintptr_t bucket
= ((uintptr_t) object
) % FC_SERIALIZE_HASH_SIZE
;
90 FcSerializeBucket
*buck
;
92 for (buck
= serialize
->buckets
[bucket
]; buck
; buck
= buck
->next
)
93 if (buck
->object
== object
)
95 buck
= malloc (sizeof (FcSerializeBucket
));
98 buck
->object
= object
;
99 buck
->offset
= serialize
->size
;
100 buck
->next
= serialize
->buckets
[bucket
];
101 serialize
->buckets
[bucket
] = buck
;
102 serialize
->size
+= FcAlignSize (size
);
107 * Reserve space in the serialization array
110 FcSerializeReserve (FcSerialize
*serialize
, int size
)
112 intptr_t offset
= serialize
->size
;
113 serialize
->size
+= FcAlignSize (size
);
118 * Given an object, return the offset in the serialized array where
119 * the serialized copy of the object is stored
122 FcSerializeOffset (FcSerialize
*serialize
, const void *object
)
124 uintptr_t bucket
= ((uintptr_t) object
) % FC_SERIALIZE_HASH_SIZE
;
125 FcSerializeBucket
*buck
;
127 for (buck
= serialize
->buckets
[bucket
]; buck
; buck
= buck
->next
)
128 if (buck
->object
== object
)
134 * Given a cache and an object, return a pointer to where
135 * the serialized copy of the object is stored
138 FcSerializePtr (FcSerialize
*serialize
, const void *object
)
140 intptr_t offset
= FcSerializeOffset (serialize
, object
);
144 return (void *) ((char *) serialize
->linear
+ offset
);
148 FcStrSerializeAlloc (FcSerialize
*serialize
, const FcChar8
*str
)
150 return FcSerializeAlloc (serialize
, str
, strlen ((const char *) str
) + 1);
154 FcStrSerialize (FcSerialize
*serialize
, const FcChar8
*str
)
156 FcChar8
*str_serialize
= FcSerializePtr (serialize
, str
);
159 strcpy ((char *) str_serialize
, (const char *) str
);
160 return str_serialize
;
162 #define __fcserialize__
163 #include "fcaliastail.h"
164 #undef __fcserialize__