GVariantTypeInfo changes
[platform/upstream/glib.git] / glib / gvarianttypeinfo.c
1 /*
2  * Copyright © 2008 Ryan Lortie
3  * Copyright © 2010 Codethink Limited
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Ryan Lortie <desrt@desrt.ca>
21  */
22
23 #include "gvarianttypeinfo.h"
24 #include <glib.h>
25
26 #include "galias.h"
27
28 /* < private >
29  * GVariantTypeInfo:
30  *
31  * This structure contains the necessary information to facilitate the
32  * serialisation and fast deserialisation of a given type of GVariant
33  * value.  A GVariant instance holds a pointer to one of these
34  * structures to provide for efficient operation.
35  *
36  * The GVariantTypeInfo structures for all of the base types, plus the
37  * "variant" type are stored in a read-only static array.
38  *
39  * For container types, a hash table and reference counting is used to
40  * ensure that only one of these structures exists for any given type.
41  * In general, a container GVariantTypeInfo will exist for a given type
42  * only if one or more GVariant instances of that type exist or if
43  * another GVariantTypeInfo has that type as a subtype.  For example, if
44  * a process contains a single GVariant instance with type "(asv)", then
45  * container GVariantTypeInfo structures will exist for "(asv)" and
46  * for "as" (note that "s" and "v" always exist in the static array).
47  *
48  * The trickiest part of GVariantTypeInfo (and in fact, the major reason
49  * for its existance) is the storage of somewhat magical constants that
50  * allow for O(1) lookups of items in tuples.  This is described below.
51  *
52  * 'container_class' is set to 'a' or 'r' if the GVariantTypeInfo is
53  * contained inside of an ArrayInfo or TupleInfo, respectively.  This
54  * allows the storage of the necessary additional information.
55  *
56  * 'fixed_size' is set to the fixed size of the type, if applicable, or
57  * 0 otherwise (since no type has a fixed size of 0).
58  *
59  * 'alignment' is set to one less than the alignment requirement for
60  * this type.  This makes many operations much more convenient.
61  */
62 struct _GVariantTypeInfo
63 {
64   gsize fixed_size;
65   guchar alignment;
66   guchar container_class;
67 };
68
69 /* Container types are reference counted.  They also need to have their
70  * type string stored explicitly since it is not merely a single letter.
71  */
72 typedef struct
73 {
74   GVariantTypeInfo info;
75
76   gchar *type_string;
77   gint ref_count;
78 } ContainerInfo;
79
80 /* For 'array' and 'maybe' types, we store some extra information on the
81  * end of the GVariantTypeInfo struct -- the element type (ie: "s" for
82  * "as").  The container GVariantTypeInfo structure holds a reference to
83  * the element typeinfo.
84  */
85 typedef struct
86 {
87   ContainerInfo container;
88
89   GVariantTypeInfo *element;
90 } ArrayInfo;
91
92 /* For 'tuple' and 'dict entry' types, we store extra information for
93  * each member -- its type and how to find it inside the serialised data
94  * in O(1) time using 4 variables -- 'i', 'a', 'b', and 'c'.  See the
95  * comment on GVariantMemberInfo in gvarianttypeinfo.h.
96  */
97 typedef struct
98 {
99   ContainerInfo container;
100
101   GVariantMemberInfo *members;
102   gsize n_members;
103 } TupleInfo;
104
105
106 /* Hard-code the base types in a constant array */
107 static const GVariantTypeInfo g_variant_type_info_basic_table[24] = {
108 #define fixed_aligned(x)  x, x - 1
109 #define unaligned         0, 0
110 #define aligned(x)        0, x - 1
111   /* 'b' */ { fixed_aligned(1) },   /* boolean */
112   /* 'c' */ { },
113   /* 'd' */ { fixed_aligned(8) },   /* double */
114   /* 'e' */ { },
115   /* 'f' */ { },
116   /* 'g' */ { unaligned        },   /* signature string */
117   /* 'h' */ { fixed_aligned(4) },   /* file handle (int32) */
118   /* 'i' */ { fixed_aligned(4) },   /* int32 */
119   /* 'j' */ { },
120   /* 'k' */ { },
121   /* 'l' */ { },
122   /* 'm' */ { },
123   /* 'n' */ { fixed_aligned(2) },   /* int16 */
124   /* 'o' */ { unaligned        },   /* object path string */
125   /* 'p' */ { },
126   /* 'q' */ { fixed_aligned(2) },   /* uint16 */
127   /* 'r' */ { },
128   /* 's' */ { unaligned        },   /* string */
129   /* 't' */ { fixed_aligned(8) },   /* uint64 */
130   /* 'u' */ { fixed_aligned(4) },   /* uint32 */
131   /* 'v' */ { aligned(8)       },   /* variant */
132   /* 'w' */ { },
133   /* 'x' */ { fixed_aligned(8) },   /* int64 */
134   /* 'y' */ { fixed_aligned(1) },   /* byte */
135 #undef fixed_aligned
136 #undef unaligned
137 #undef aligned
138 };
139
140 /* We need to have type strings to return for the base types.  We store
141  * those in another array.  Since all base type strings are single
142  * characters this is easy.  By not storing pointers to strings into the
143  * GVariantTypeInfo itself, we save a bunch of relocations.
144  */
145 static const char g_variant_type_info_basic_chars[24][2] = {
146   "b", " ", "d", " ", " ", "g", "h", "i", " ", " ", " ", " ",
147   "n", "o", " ", "q", " ", "s", "t", "u", "v", " ", "x", "y"
148 };
149
150 /* sanity checks to make debugging easier */
151 static void
152 g_variant_type_info_check (const GVariantTypeInfo *info,
153                            char                    container_class)
154 {
155   g_assert (!container_class || info->container_class == container_class);
156
157   /* alignment can only be one of these */
158   g_assert (info->alignment == 0 || info->alignment == 1 ||
159             info->alignment == 3 || info->alignment == 7);
160
161   if (info->container_class)
162     {
163       ContainerInfo *container = (ContainerInfo *) info;
164
165       /* extra checks for containers */
166       g_assert_cmpint (container->ref_count, >, 0);
167       g_assert (container->type_string != NULL);
168     }
169   else
170     {
171       gint index;
172
173       /* if not a container, then ensure that it is a valid member of
174        * the basic types table
175        */
176       index = info - g_variant_type_info_basic_table;
177
178       g_assert (G_N_ELEMENTS (g_variant_type_info_basic_table) == 24);
179       g_assert (G_N_ELEMENTS (g_variant_type_info_basic_chars) == 24);
180       g_assert (0 <= index && index < 24);
181       g_assert (g_variant_type_info_basic_chars[index][0] != ' ');
182     }
183 }
184
185 /* < private >
186  * g_variant_type_info_get_type_string:
187  * @info: a #GVariantTypeInfo
188  *
189  * Gets the type string for @info.  The string is nul-terminated.
190  */
191 const gchar *
192 g_variant_type_info_get_type_string (GVariantTypeInfo *info)
193 {
194   g_variant_type_info_check (info, 0);
195
196   if (info->container_class)
197     {
198       ContainerInfo *container = (ContainerInfo *) info;
199
200       /* containers have their type string stored inside them */
201       return container->type_string;
202     }
203   else
204     {
205       gint index;
206
207       /* look up the type string in the base type array.  the call to
208        * g_variant_type_info_check() above already ensured validity.
209        */
210       index = info - g_variant_type_info_basic_table;
211
212       return g_variant_type_info_basic_chars[index];
213     }
214 }
215
216 /* < private >
217  * g_variant_type_info_query:
218  * @info: a #GVariantTypeInfo
219  * @alignment: the location to store the alignment, or %NULL
220  * @fixed_size: the location to store the fixed size, or %NULL
221  *
222  * Queries @info to determine the alignment requirements and fixed size
223  * (if any) of the type.
224  *
225  * @fixed_size, if non-%NULL is set to the fixed size of the type, or 0
226  * to indicate that the type is a variable-sized type.  No type has a
227  * fixed size of 0.
228  *
229  * @alignment, if non-%NULL, is set to one less than the required
230  * alignment of the type.  For example, for a 32bit integer, @alignment
231  * would be set to 3.  This allows you to round an integer up to the
232  * proper alignment by performing the following efficient calculation:
233  *
234  *   offset += ((-offset) & alignment);
235  */
236 void
237 g_variant_type_info_query (GVariantTypeInfo *info,
238                            guint            *alignment,
239                            gsize            *fixed_size)
240 {
241   g_variant_type_info_check (info, 0);
242
243   if (alignment)
244     *alignment = info->alignment;
245
246   if (fixed_size)
247     *fixed_size = info->fixed_size;
248 }
249
250 /* == array == */
251 #define ARRAY_INFO_CLASS 'a'
252 static ArrayInfo *
253 ARRAY_INFO (GVariantTypeInfo *info)
254 {
255   g_variant_type_info_check (info, ARRAY_INFO_CLASS);
256
257   return (ArrayInfo *) info;
258 }
259
260 static void
261 array_info_free (GVariantTypeInfo *info)
262 {
263   ArrayInfo *array_info;
264
265   g_assert (info->container_class == ARRAY_INFO_CLASS);
266   array_info = (ArrayInfo *) info;
267
268   g_variant_type_info_unref (array_info->element);
269   g_slice_free (ArrayInfo, array_info);
270 }
271
272 static ContainerInfo *
273 array_info_new (const GVariantType *type)
274 {
275   ArrayInfo *info;
276
277   info = g_slice_new (ArrayInfo);
278   info->container.info.container_class = ARRAY_INFO_CLASS;
279
280   info->element = g_variant_type_info_get (g_variant_type_element (type));
281   info->container.info.alignment = info->element->alignment;
282   info->container.info.fixed_size = 0;
283
284   return (ContainerInfo *) info;
285 }
286
287 /* < private >
288  * g_variant_type_info_element:
289  * @info: a #GVariantTypeInfo for an array or maybe type
290  *
291  * Returns the element type for the array or maybe type.  A reference is
292  * not added, so the caller must add their own.
293  */
294 GVariantTypeInfo *
295 g_variant_type_info_element (GVariantTypeInfo *info)
296 {
297   return ARRAY_INFO (info)->element;
298 }
299
300 /* < private >
301  * g_variant_type_query_element:
302  * @info: a #GVariantTypeInfo for an array or maybe type
303  * @alignment: the location to store the alignment, or %NULL
304  * @fixed_size: the location to store the fixed size, or %NULL
305  *
306  * Returns the alignment requires and fixed size (if any) for the
307  * element type of the array.  This call is a convenience wrapper around
308  * g_variant_type_info_element() and g_variant_type_info_query().
309  */
310 void
311 g_variant_type_info_query_element (GVariantTypeInfo *info,
312                                    guint            *alignment,
313                                    gsize            *fixed_size)
314 {
315   g_variant_type_info_query (ARRAY_INFO (info)->element,
316                              alignment, fixed_size);
317 }
318
319 /* == tuple == */
320 #define TUPLE_INFO_CLASS 'r'
321 static TupleInfo *
322 TUPLE_INFO (GVariantTypeInfo *info)
323 {
324   g_variant_type_info_check (info, TUPLE_INFO_CLASS);
325
326   return (TupleInfo *) info;
327 }
328
329 static void
330 tuple_info_free (GVariantTypeInfo *info)
331 {
332   TupleInfo *tuple_info;
333   gint i;
334
335   g_assert (info->container_class == TUPLE_INFO_CLASS);
336   tuple_info = (TupleInfo *) info;
337
338   for (i = 0; i < tuple_info->n_members; i++)
339     g_variant_type_info_unref (tuple_info->members[i].type_info);
340
341   g_slice_free1 (sizeof (GVariantMemberInfo) * tuple_info->n_members,
342                  tuple_info->members);
343   g_slice_free (TupleInfo, tuple_info);
344 }
345
346 static void
347 tuple_allocate_members (const GVariantType  *type,
348                         GVariantMemberInfo **members,
349                         gsize               *n_members)
350 {
351   const GVariantType *item_type;
352   gsize i = 0;
353
354   *n_members = g_variant_type_n_items (type);
355   *members = g_slice_alloc (sizeof (GVariantMemberInfo) * *n_members);
356
357   item_type = g_variant_type_first (type);
358   while (item_type)
359     {
360       GVariantMemberInfo *member = &(*members)[i++];
361
362       member->type_info = g_variant_type_info_get (item_type);
363       item_type = g_variant_type_next (item_type);
364
365       if (item_type == NULL)
366         member->ending_type = G_VARIANT_MEMBER_ENDING_LAST;
367       else if (member->type_info->fixed_size)
368         member->ending_type = G_VARIANT_MEMBER_ENDING_FIXED;
369       else
370         member->ending_type = G_VARIANT_MEMBER_ENDING_OFFSET;
371     }
372
373   g_assert (i == *n_members);
374 }
375
376 /* this is g_variant_type_info_query for a given member of the tuple.
377  * before the access is done, it is ensured that the item is within
378  * range and %FALSE is returned if not.
379  */
380 static gboolean
381 tuple_get_item (TupleInfo          *info,
382                 GVariantMemberInfo *item,
383                 gsize              *d,
384                 gsize              *e)
385 {
386   if (&info->members[info->n_members] == item)
387     return FALSE;
388
389   *d = item->type_info->alignment;
390   *e = item->type_info->fixed_size;
391   return TRUE;
392 }
393
394 /* Read the documentation for #GVariantMemberInfo in gvarianttype.h
395  * before attempting to understand this.
396  *
397  * This function adds one set of "magic constant" values (for one item
398  * in the tuple) to the table.
399  *
400  * The algorithm in tuple_generate_table() calculates values of 'a', 'b'
401  * and 'c' for each item, such that the procedure for finding the item
402  * is to start at the end of the previous variable-sized item, add 'a',
403  * then round up to the nearest multiple of 'b', then then add 'c'.
404  * Note that 'b' is stored in the usual "one less than" form.  ie:
405  *
406  *   start = ROUND_UP(prev_end + a, (b + 1)) + c;
407  *
408  * We tweak these values a little to allow for a slightly easier
409  * computation and more compact storage.
410  */
411 static void
412 tuple_table_append (GVariantMemberInfo **items,
413                     gsize                i,
414                     gsize                a,
415                     gsize                b,
416                     gsize                c)
417 {
418   GVariantMemberInfo *item = (*items)++;
419
420   /* We can shift multiples of the alignment size from 'c' into 'a'.
421    * As long as we're shifting whole multiples, it won't affect the
422    * result.  This means that we can take the "aligned" portion off of
423    * 'c' and add it into 'a'.
424    *
425    *  Imagine (for sake of clarity) that ROUND_10 rounds up to the
426    *  nearest 10.  It is clear that:
427    *
428    *   ROUND_10(a) + c == ROUND_10(a + 10*(c / 10)) + (c % 10)
429    *
430    * ie: remove the 10s portion of 'c' and add it onto 'a'.
431    *
432    * To put some numbers on it, imagine we start with a = 34 and c = 27:
433    *
434    *  ROUND_10(34) + 27 = 40 + 27 = 67
435    *
436    * but also, we can split 27 up into 20 and 7 and do this:
437    *
438    *  ROUND_10(34 + 20) + 7 = ROUND_10(54) + 7 = 60 + 7 = 67
439    *                ^^    ^
440    * without affecting the result.  We do that here.
441    *
442    * This reduction in the size of 'c' means that we can store it in a
443    * gchar instead of a gsize.  Due to how the structure is packed, this
444    * ends up saving us 'two pointer sizes' per item in each tuple when
445    * allocating using GSlice.
446    */
447   a += ~b & c;    /* take the "aligned" part of 'c' and add to 'a' */
448   c &= b;         /* chop 'c' to contain only the unaligned part */
449
450
451   /* Finally, we made one last adjustment.  Recall:
452    *
453    *   start = ROUND_UP(prev_end + a, (b + 1)) + c;
454    *
455    * Forgetting the '+ c' for the moment:
456    *
457    *   ROUND_UP(prev_end + a, (b + 1));
458    *
459    * we can do a "round up" operation by adding 1 less than the amount
460    * to round up to, then rounding down.  ie:
461    *
462    *   #define ROUND_UP(x, y)    ROUND_DOWN(x + (y-1), y)
463    *
464    * Of course, for rounding down to a power of two, we can just mask
465    * out the appropriate number of low order bits:
466    *
467    *   #define ROUND_DOWN(x, y)  (x & ~(y - 1))
468    *
469    * Which gives us
470    *
471    *   #define ROUND_UP(x, y)    (x + (y - 1) & ~(y - 1))
472    *
473    * but recall that our alignment value 'b' is already "one less".
474    * This means that to round 'prev_end + a' up to 'b' we can just do:
475    *
476    *   ((prev_end + a) + b) & ~b
477    *
478    * Associativity, and putting the 'c' back on:
479    *
480    *   (prev_end + (a + b)) & ~b + c
481    *
482    * Now, since (a + b) is constant, we can just add 'b' to 'a' now and
483    * store that as the number to add to prev_end.  Then we use ~b as the
484    * number to take a bitwise 'and' with.  Finally, 'c' is added on.
485    *
486    * Note, however, that all the low order bits of the 'aligned' value
487    * are masked out and that all of the high order bits of 'c' have been
488    * "moved" to 'a' (in the previous step).  This means that there are
489    * no overlapping bits in the addition -- so we can do a bitwise 'or'
490    * equivalently.
491    *
492    * This means that we can now compute the start address of a given
493    * item in the tuple using the algorithm given in the documentation
494    * for #GVariantMemberInfo:
495    *
496    *   item_start = ((prev_end + a) & b) | c;
497    */
498
499   item->i = i;
500   item->a = a + b;
501   item->b = ~b;
502   item->c = c;
503 }
504
505 static gsize
506 tuple_align (gsize offset,
507              guint alignment)
508 {
509   return offset + ((-offset) & alignment);
510 }
511
512 /* This function is the heart of the algorithm for calculating 'i', 'a',
513  * 'b' and 'c' for each item in the tuple.
514  *
515  * Imagine we want to find the start of the "i" in the type "(su(qx)ni)".
516  * That's a string followed by a uint32, then a tuple containing a
517  * uint16 and a int64, then an int16, then our "i".  In order to get to
518  * our "i" we:
519  *
520  * Start at the end of the string, align to 4 (for the uint32), add 4.
521  * Align to 8, add 16 (for the tuple).  Align to 2, add 2 (for the
522  * int16).  Then we're there.  It turns out that, given 3 simple rules,
523  * we can flatten this iteration into one addition, one alignment, then
524  * one more addition.
525  *
526  * The loop below plays through each item in the tuple, querying its
527  * alignment and fixed_size into 'd' and 'e', respectively.  At all
528  * times the variables 'a', 'b', and 'c' are maintained such that in
529  * order to get to the current point, you add 'a', align to 'b' then add
530  * 'c'.  'b' is kept in "one less than" form.  For each item, the proper
531  * alignment is applied to find the values of 'a', 'b' and 'c' to get to
532  * the start of that item.  Those values are recorded into the table.
533  * The fixed size of the item (if applicable) is then added on.
534  *
535  * These 3 rules are how 'a', 'b' and 'c' are modified for alignment and
536  * addition of fixed size.  They have been proven correct but are
537  * presented here, without proof:
538  *
539  *  1) in order to "align to 'd'" where 'd' is less than or equal to the
540  *     largest level of alignment seen so far ('b'), you align 'c' to
541  *     'd'.
542  *  2) in order to "align to 'd'" where 'd' is greater than the largest
543  *     level of alignment seen so far, you add 'c' aligned to 'b' to the
544  *     value of 'a', set 'b' to 'd' (ie: increase the 'largest alignment
545  *     seen') and reset 'c' to 0.
546  *  3) in order to "add 'e'", just add 'e' to 'c'.
547  */
548 static void
549 tuple_generate_table (TupleInfo *info)
550 {
551   GVariantMemberInfo *items = info->members;
552   gsize i = -1, a = 0, b = 0, c = 0, d, e;
553
554   /* iterate over each item in the tuple.
555    *   'd' will be the alignment of the item (in one-less form)
556    *   'e' will be the fixed size (or 0 for variable-size items)
557    */
558   while (tuple_get_item (info, items, &d, &e))
559     {
560       /* align to 'd' */
561       if (d <= b)
562         c = tuple_align (c, d);                   /* rule 1 */
563       else
564         a += tuple_align (c, b), b = d, c = 0;    /* rule 2 */
565
566       /* the start of the item is at this point (ie: right after we
567        * have aligned for it).  store this information in the table.
568        */
569       tuple_table_append (&items, i, a, b, c);
570
571       /* "move past" the item by adding in its size. */
572       if (e == 0)
573         /* variable size:
574          *
575          * we'll have an offset stored to mark the end of this item, so
576          * just bump the offset index to give us a new starting point
577          * and reset all the counters.
578          */
579         i++, a = b = c = 0;
580       else
581         /* fixed size */
582         c += e;                                   /* rule 3 */
583     }
584 }
585
586 static void
587 tuple_set_base_info (TupleInfo *info)
588 {
589   GVariantTypeInfo *base = &info->container.info;
590
591   if (info->n_members > 0)
592     {
593       GVariantMemberInfo *m;
594
595       /* the alignment requirement of the tuple is the alignment
596        * requirement of its largest item.
597        */
598       base->alignment = 0;
599       for (m = info->members; m < &info->members[info->n_members]; m++)
600         /* can find the max of a list of "one less than" powers of two
601          * by 'or'ing them
602          */
603         base->alignment |= m->type_info->alignment;
604
605       m--; /* take 'm' back to the last item */
606
607       /* the structure only has a fixed size if no variable-size
608        * offsets are stored and the last item is fixed-sized too (since
609        * an offset is never stored for the last item).
610        */
611       if (m->i == -1 && m->type_info->fixed_size)
612         /* in that case, the fixed size can be found by finding the
613          * start of the last item (in the usual way) and adding its
614          * fixed size.
615          *
616          * if a tuple has a fixed size then it is always a multiple of
617          * the alignment requirement (to make packing into arrays
618          * easier) so we round up to that here.
619          */
620         base->fixed_size =
621           tuple_align (((m->a & m->b) | m->c) + m->type_info->fixed_size,
622                        base->alignment);
623       else
624         /* else, the tuple is not fixed size */
625         base->fixed_size = 0;
626     }
627   else
628     {
629       /* the empty tuple: '()'.
630        *
631        * has a size of 1 and an no alignment requirement.
632        *
633        * It has a size of 1 (not 0) for two practical reasons:
634        *
635        *  1) So we can determine how many of them are in an array
636        *     without dividing by zero or without other tricks.
637        *
638        *  2) Even if we had some trick to know the number of items in
639        *     the array (as GVariant did at one time) this would open a
640        *     potential denial of service attack: an attacker could send
641        *     you an extremely small array (in terms of number of bytes)
642        *     containing trillions of zero-sized items.  If you iterated
643        *     over this array you would effectively infinite-loop your
644        *     program.  By forcing a size of at least one, we bound the
645        *     amount of computation done in response to a message to a
646        *     reasonable function of the size of that message.
647        */
648       base->alignment = 0;
649       base->fixed_size = 1;
650     }
651 }
652
653 static ContainerInfo *
654 tuple_info_new (const GVariantType *type)
655 {
656   TupleInfo *info;
657
658   info = g_slice_new (TupleInfo);
659   info->container.info.container_class = TUPLE_INFO_CLASS;
660
661   tuple_allocate_members (type, &info->members, &info->n_members);
662   tuple_generate_table (info);
663   tuple_set_base_info (info);
664
665   return (ContainerInfo *) info;
666 }
667
668 /* < private >
669  * g_variant_type_info_n_members:
670  * @info: a #GVariantTypeInfo for a tuple or dictionary entry type
671  *
672  * Returns the number of members in a tuple or dictionary entry type.
673  * For a dictionary entry this will always be 2.
674  */
675 gsize
676 g_variant_type_info_n_members (GVariantTypeInfo *info)
677 {
678   return TUPLE_INFO (info)->n_members;
679 }
680
681 /* < private >
682  * g_variant_type_info_member_info:
683  * @info: a #GVariantTypeInfo for a tuple or dictionary entry type
684  * @index: the member to fetch information for
685  *
686  * Returns the #GVariantMemberInfo for a given member.  See
687  * documentation for that structure for why you would want this
688  * information.
689  *
690  * @index must refer to a valid child (ie: strictly less than
691  * g_variant_type_info_n_members() returns).
692  */
693 const GVariantMemberInfo *
694 g_variant_type_info_member_info (GVariantTypeInfo *info,
695                                  gsize             index)
696 {
697   TupleInfo *tuple_info = TUPLE_INFO (info);
698
699   if (index < tuple_info->n_members)
700     return &tuple_info->members[index];
701
702   return NULL;
703 }
704
705 /* == new/ref/unref == */
706 static GStaticRecMutex g_variant_type_info_lock = G_STATIC_REC_MUTEX_INIT;
707 static GHashTable *g_variant_type_info_table;
708
709 /* < private >
710  * g_variant_type_info_get:
711  * @type: a #GVariantType
712  *
713  * Returns a reference to a #GVariantTypeInfo for @type.
714  *
715  * If an info structure already exists for this type, a new reference is
716  * returned.  If not, the required calculations are performed and a new
717  * info structure is returned.
718  *
719  * It is appropriate to call g_variant_type_info_unref() on the return
720  * value.
721  */
722 GVariantTypeInfo *
723 g_variant_type_info_get (const GVariantType *type)
724 {
725   char type_char;
726
727   type_char = g_variant_type_peek_string (type)[0];
728
729   if (type_char == G_VARIANT_TYPE_INFO_CHAR_MAYBE ||
730       type_char == G_VARIANT_TYPE_INFO_CHAR_ARRAY ||
731       type_char == G_VARIANT_TYPE_INFO_CHAR_TUPLE ||
732       type_char == G_VARIANT_TYPE_INFO_CHAR_DICT_ENTRY)
733     {
734       GVariantTypeInfo *info;
735       gchar *type_string;
736
737       if G_UNLIKELY (g_variant_type_info_table == NULL)
738         g_variant_type_info_table = g_hash_table_new (g_str_hash,
739                                                       g_str_equal);
740
741       type_string = g_variant_type_dup_string (type);
742
743       g_static_rec_mutex_lock (&g_variant_type_info_lock);
744       info = g_hash_table_lookup (g_variant_type_info_table, type_string);
745
746       if (info == NULL)
747         {
748           ContainerInfo *container;
749
750           if (type_char == G_VARIANT_TYPE_INFO_CHAR_MAYBE ||
751               type_char == G_VARIANT_TYPE_INFO_CHAR_ARRAY)
752             {
753               container = array_info_new (type);
754             }
755           else /* tuple or dict entry */
756             {
757               container = tuple_info_new (type);
758             }
759
760           info = (GVariantTypeInfo *) container;
761           container->type_string = type_string;
762           container->ref_count = 1;
763
764           g_hash_table_insert (g_variant_type_info_table, type_string, info);
765           type_string = NULL;
766         }
767       else
768         g_variant_type_info_ref (info);
769
770       g_static_rec_mutex_unlock (&g_variant_type_info_lock);
771       g_variant_type_info_check (info, 0);
772       g_free (type_string);
773
774       return info;
775     }
776   else
777     {
778       const GVariantTypeInfo *info;
779       int index;
780
781       index = type_char - 'b';
782       g_assert (G_N_ELEMENTS (g_variant_type_info_basic_table) == 24);
783       g_assert_cmpint (0, <=, index);
784       g_assert_cmpint (index, <, 24);
785
786       info = g_variant_type_info_basic_table + index;
787       g_variant_type_info_check (info, 0);
788
789       return (GVariantTypeInfo *) info;
790     }
791 }
792
793 /* < private >
794  * g_variant_type_info_ref:
795  * @info: a #GVariantTypeInfo
796  *
797  * Adds a reference to @info.
798  */
799 GVariantTypeInfo *
800 g_variant_type_info_ref (GVariantTypeInfo *info)
801 {
802   g_variant_type_info_check (info, 0);
803
804   if (info->container_class)
805     {
806       ContainerInfo *container = (ContainerInfo *) info;
807
808       g_assert_cmpint (container->ref_count, >, 0);
809       g_atomic_int_inc (&container->ref_count);
810     }
811
812   return info;
813 }
814
815 /* < private >
816  * g_variant_type_info_unref:
817  * @info: a #GVariantTypeInfo
818  *
819  * Releases a reference held on @info.  This may result in @info being
820  * freed.
821  */
822 void
823 g_variant_type_info_unref (GVariantTypeInfo *info)
824 {
825   g_variant_type_info_check (info, 0);
826
827   if (info->container_class)
828     {
829       ContainerInfo *container = (ContainerInfo *) info;
830
831       if (g_atomic_int_dec_and_test (&container->ref_count))
832         {
833           g_static_rec_mutex_lock (&g_variant_type_info_lock);
834           g_hash_table_remove (g_variant_type_info_table,
835                                container->type_string);
836           g_static_rec_mutex_unlock (&g_variant_type_info_lock);
837
838           g_free (container->type_string);
839
840           if (info->container_class == ARRAY_INFO_CLASS)
841             array_info_free (info);
842
843           else if (info->container_class == TUPLE_INFO_CLASS)
844             tuple_info_free (info);
845
846           else
847             g_assert_not_reached ();
848         }
849     }
850 }