2 * Copyright (c) 2012 Vincent Bernat <bernat@luffy.cx>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 enum marshal_subinfo_kind
{
26 #define MARSHAL_INFO_POINTER 1
27 #define MARSHAL_INFO_SUB 2
28 struct marshal_subinfo
{
29 size_t offset
; /* Offset compared to parent structure */
30 enum marshal_subinfo_kind kind
; /* Kind of substructure */
31 struct marshal_info
*mi
;
34 char *name
; /* Name of structure */
35 size_t size
; /* Size of the structure */
36 struct marshal_subinfo pointers
[]; /* Pointer to other structures */
38 /* Special case for strings */
39 extern struct marshal_info marshal_info__string
;
41 /* Declare a new marshal_info struct named after the type we want to
42 marshal. The marshalled type has to be a structure. */
43 #define MARSHAL_BEGIN(type) struct marshal_info marshal_info_##type = \
46 .size = sizeof(struct type), \
48 #define MARSHAL_ADD(_kind, type, subtype, member) \
49 { .offset = offsetof(struct type, member), \
51 .mi = &marshal_info_##subtype },
52 #define MARSHAL_POINTER(...) MARSHAL_ADD(pointer, ##__VA_ARGS__)
53 #define MARSHAL_SUBSTRUCT(...) MARSHAL_ADD(substruct, ##__VA_ARGS__)
54 #define MARSHAL_STR(type, member) MARSHAL_ADD(pointer, type, _string, member)
55 #define MARSHAL_TQE(type, field) \
56 MARSHAL_POINTER(type, type, field.tqe_next) \
57 MARSHAL_POINTER(type, type, field.tqe_prev)
58 #define MARSHAL_TQH(type, subtype) \
59 MARSHAL_POINTER(type, subtype, tqh_first) \
60 MARSHAL_POINTER(type, subtype, tqh_last)
61 #define MARSHAL_SUBTQ(type, subtype, field) \
62 MARSHAL_POINTER(type, subtype, field.tqh_first) \
63 MARSHAL_POINTER(type, subtype, field.tqh_last)
64 #define MARSHAL_END { .mi = NULL } } }
66 #define MARSHAL(type) \
69 #define MARSHAL_TQ(type, subtype) \
71 MARSHAL_TQH(type, subtype) \
75 size_t _marshal_serialize(struct marshal_info
*, void *, void **, int, void *);
76 #define marshal_serialize(type, o, output) _marshal_serialize(&marshal_info_##type, o, output, 0, NULL)
79 size_t _marshal_unserialize(struct marshal_info
*, void *, size_t, void **, void*, int);
80 #define marshal_unserialize(type, o, l, input) \
81 _marshal_unserialize(&marshal_info_##type, o, l, (void **)input, NULL, 0)