]> git.ipfire.org Git - thirdparty/lldpd.git/blame - src/marshal.h
build: reformat code
[thirdparty/lldpd.git] / src / marshal.h
CommitLineData
4b292b55 1/* -*- mode: c; c-file-style: "openbsd" -*- */
db323555
VB
2/*
3 * Copyright (c) 2012 Vincent Bernat <bernat@luffy.cx>
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#ifndef _MARSHAL_H
19#define _MARSHAL_H
20
4b292b55 21#include <stddef.h>
ff3dcc4a 22#include <stdint.h>
4b292b55
VB
23#include <sys/types.h>
24
db323555
VB
25struct marshal_info;
26enum marshal_subinfo_kind {
27 pointer,
28 substruct,
305e061c 29 ignore,
db323555
VB
30};
31#define MARSHAL_INFO_POINTER 1
8b549648 32#define MARSHAL_INFO_SUB 2
db323555 33struct marshal_subinfo {
8b549648
VB
34 size_t offset; /* Offset compared to parent structure */
35 size_t offset2; /* Ancillary offset (for related data) */
db323555 36 enum marshal_subinfo_kind kind; /* Kind of substructure */
8b549648 37 struct marshal_info *mi;
db323555 38};
8b549648
VB
39#define MARSHAL_SUBINFO_NULL \
40 { \
41 .offset = 0, .offset2 = 0, .kind = ignore, .mi = NULL \
42 }
db323555 43struct marshal_info {
8b549648
VB
44 char *name; /* Name of structure */
45 size_t size; /* Size of the structure */
01d467de
VB
46#if defined __GNUC__ && __GNUC__ < 3
47 /* With gcc 2.96, flexible arrays are not supported, even with
48 * -std=gnu99. And with gcc 3.x, zero-sized arrays cannot be statically
49 * initialized (with more than one element). */
50 struct marshal_subinfo pointers[0]; /* Pointer to other structures */
51#else
db323555 52 struct marshal_subinfo pointers[]; /* Pointer to other structures */
01d467de 53#endif
db323555 54};
da781141 55/* Special case for strings */
6bf5e749
VB
56extern struct marshal_info marshal_info_string;
57extern struct marshal_info marshal_info_fstring;
58extern struct marshal_info marshal_info_ignore;
db323555
VB
59
60/* Declare a new marshal_info struct named after the type we want to
61 marshal. The marshalled type has to be a structure. */
6bf5e749
VB
62#define MARSHAL_INFO(type) marshal_info_##type
63#ifdef MARSHAL_EXPORT
8b549648
VB
64# define MARSHAL_HELPER_FUNCTIONS(type, ttype) \
65 ssize_t type##_serialize(ttype *source, void *buffer) \
66 { \
67 return marshal_serialize(type, source, buffer); \
68 } \
69 size_t type##_unserialize(void *buffer, size_t len, ttype **destination) \
70 { \
71 void *p; \
72 size_t rc; \
73 rc = marshal_unserialize(type, buffer, len, &p); \
74 if (rc <= 0) return rc; \
75 *destination = p; \
76 return rc; \
77 }
78# define MARSHAL_BEGIN(type) \
79 struct marshal_info MARSHAL_INFO( \
80 type) = { .name = #type, .size = sizeof(struct type), .pointers = {
81# define MARSHAL_ADD(_kind, type, subtype, member) \
82 { .offset = offsetof(struct type, member), \
83 .offset2 = 0, \
84 .kind = _kind, \
85 .mi = &MARSHAL_INFO(subtype) },
86# define MARSHAL_FSTR(type, member, len) \
87 { .offset = offsetof(struct type, member), \
88 .offset2 = offsetof(struct type, len), \
89 .kind = pointer, \
90 .mi = &marshal_info_fstring },
91# define MARSHAL_END(type) \
92 MARSHAL_SUBINFO_NULL \
93 } \
94 } \
95 ; \
96 MARSHAL_HELPER_FUNCTIONS(type, struct type)
6bf5e749 97#else
8b549648
VB
98# define MARSHAL_HELPER_FUNCTIONS(type, ttype) \
99 ssize_t type##_serialize(ttype *, void *); \
100 size_t type##_unserialize(void *, size_t, ttype **);
101# define MARSHAL_BEGIN(type) extern struct marshal_info MARSHAL_INFO(type);
102# define MARSHAL_ADD(...)
103# define MARSHAL_FSTR(...)
104# define MARSHAL_END(type) MARSHAL_HELPER_FUNCTIONS(type, struct type)
6bf5e749
VB
105#endif
106/* Shortcuts */
107#define MARSHAL_POINTER(...) MARSHAL_ADD(pointer, ##__VA_ARGS__)
108#define MARSHAL_SUBSTRUCT(...) MARSHAL_ADD(substruct, ##__VA_ARGS__)
109#define MARSHAL_STR(type, member) MARSHAL_ADD(pointer, type, string, member)
110#define MARSHAL_IGNORE(type, member) MARSHAL_ADD(ignore, type, ignore, member)
8b549648
VB
111#define MARSHAL_TQE(type, field) \
112 MARSHAL_POINTER(type, type, field.tqe_next) \
113 MARSHAL_IGNORE(type, field.tqe_prev)
6bf5e749
VB
114/* Support for TAILQ list is partial. Access to last and previous
115 elements is not available. Some operations are therefore not
4e90a9e0 116 possible. However, TAILQ_FOREACH is still
6bf5e749 117 available. */
8b549648
VB
118#define MARSHAL_TQH(type, subtype) \
119 MARSHAL_POINTER(type, subtype, tqh_first) \
120 MARSHAL_IGNORE(type, tqh_last)
121#define MARSHAL_SUBTQ(type, subtype, field) \
122 MARSHAL_POINTER(type, subtype, field.tqh_first) \
123 MARSHAL_IGNORE(type, field.tqh_last)
124#define MARSHAL(type) \
125 MARSHAL_BEGIN(type) \
126 MARSHAL_END(type)
127#define MARSHAL_TQ(type, subtype) \
128 MARSHAL_BEGIN(type) \
129 MARSHAL_TQH(type, subtype) \
130 MARSHAL_END(type)
db323555
VB
131
132/* Serialization */
8b549648
VB
133ssize_t marshal_serialize_(struct marshal_info *, void *, void **, int, void *, int)
134 __attribute__((nonnull(1, 2, 3)));
135#define marshal_serialize(type, o, output) \
136 marshal_serialize_(&MARSHAL_INFO(type), o, output, 0, NULL, 0)
db323555
VB
137
138/* Unserialization */
8b549648
VB
139size_t marshal_unserialize_(struct marshal_info *, void *, size_t, void **, void *, int,
140 int) __attribute__((nonnull(1, 2, 4)));
db323555 141#define marshal_unserialize(type, o, l, input) \
8b549648 142 marshal_unserialize_(&MARSHAL_INFO(type), o, l, input, NULL, 0, 0)
db323555 143
8b549648
VB
144#define marshal_repair_tailq(type, head, field) \
145 do { \
146 struct type *__item, *__item_next; \
147 (head)->tqh_last = &(head)->tqh_first; \
148 for (__item = TAILQ_FIRST(head); __item != NULL; __item = __item_next) { \
149 __item_next = TAILQ_NEXT(__item, field); \
150 __item->field.tqe_prev = (head)->tqh_last; \
151 *(head)->tqh_last = __item; \
152 (head)->tqh_last = &__item->field.tqe_next; \
153 } \
154 } while (0)
2e043f72 155
db323555 156#endif