Lines Matching defs:nw

1047 snl_init_writer(struct snl_state *ss, struct snl_writer *nw)
1049 nw->size = SNL_WRITER_BUFFER_SIZE;
1050 nw->base = (char *)snl_allocz(ss, nw->size);
1051 if (nw->base == NULL) {
1052 nw->error = true;
1053 nw->size = 0;
1056 nw->offset = 0;
1057 nw->hdr = NULL;
1058 nw->error = false;
1059 nw->ss = ss;
1063 snl_realloc_msg_buffer(struct snl_writer *nw, size_t sz)
1065 uint32_t new_size = nw->size * 2;
1067 while (new_size < nw->size + sz)
1070 if (nw->error)
1073 if (snl_allocz(nw->ss, new_size) == NULL) {
1074 nw->error = true;
1077 nw->size = new_size;
1079 void *new_base = nw->ss->lb->base;
1080 if (new_base != nw->base) {
1081 memcpy(new_base, nw->base, nw->offset);
1082 if (nw->hdr != NULL) {
1083 int hdr_off = (char *)(nw->hdr) - nw->base;
1085 nw->hdr = (struct nlmsghdr *)
1088 nw->base = (char *)new_base;
1095 snl_reserve_msg_data_raw(struct snl_writer *nw, size_t sz)
1099 if (__predict_false(nw->offset + sz > nw->size)) {
1100 if (!snl_realloc_msg_buffer(nw, sz))
1104 void *data_ptr = &nw->base[nw->offset];
1105 nw->offset += sz;
1113 _snl_reserve_msg_attr(struct snl_writer *nw, uint16_t nla_type, uint16_t sz)
1117 struct nlattr *nla = snl_reserve_msg_data(nw, sz, struct nlattr);
1128 snl_add_msg_attr(struct snl_writer *nw, int attr_type, int attr_len, const void *data)
1132 if (__predict_false(nw->offset + required_len > nw->size)) {
1133 if (!snl_realloc_msg_buffer(nw, required_len))
1137 struct nlattr *nla = (struct nlattr *)(void *)(&nw->base[nw->offset]);
1148 nw->offset += required_len;
1153 snl_add_msg_attr_raw(struct snl_writer *nw, const struct nlattr *nla_src)
1159 return (snl_add_msg_attr(nw, nla_src->nla_type, attr_len, (const void *)(nla_src + 1)));
1163 snl_add_msg_attr_bool(struct snl_writer *nw, int attrtype, bool value)
1165 return (snl_add_msg_attr(nw, attrtype, sizeof(bool), &value));
1169 snl_add_msg_attr_u8(struct snl_writer *nw, int attrtype, uint8_t value)
1171 return (snl_add_msg_attr(nw, attrtype, sizeof(uint8_t), &value));
1175 snl_add_msg_attr_u16(struct snl_writer *nw, int attrtype, uint16_t value)
1177 return (snl_add_msg_attr(nw, attrtype, sizeof(uint16_t), &value));
1181 snl_add_msg_attr_u32(struct snl_writer *nw, int attrtype, uint32_t value)
1183 return (snl_add_msg_attr(nw, attrtype, sizeof(uint32_t), &value));
1187 snl_add_msg_attr_u64(struct snl_writer *nw, int attrtype, uint64_t value)
1189 return (snl_add_msg_attr(nw, attrtype, sizeof(uint64_t), &value));
1193 snl_add_msg_attr_s8(struct snl_writer *nw, int attrtype, int8_t value)
1195 return (snl_add_msg_attr(nw, attrtype, sizeof(int8_t), &value));
1199 snl_add_msg_attr_s16(struct snl_writer *nw, int attrtype, int16_t value)
1201 return (snl_add_msg_attr(nw, attrtype, sizeof(int16_t), &value));
1205 snl_add_msg_attr_s32(struct snl_writer *nw, int attrtype, int32_t value)
1207 return (snl_add_msg_attr(nw, attrtype, sizeof(int32_t), &value));
1211 snl_add_msg_attr_s64(struct snl_writer *nw, int attrtype, int64_t value)
1213 return (snl_add_msg_attr(nw, attrtype, sizeof(int64_t), &value));
1217 snl_add_msg_attr_flag(struct snl_writer *nw, int attrtype)
1219 return (snl_add_msg_attr(nw, attrtype, 0, NULL));
1223 snl_add_msg_attr_string(struct snl_writer *nw, int attrtype, const char *str)
1225 return (snl_add_msg_attr(nw, attrtype, strlen(str) + 1, str));
1230 snl_get_msg_offset(const struct snl_writer *nw)
1232 return (nw->offset - ((char *)nw->hdr - nw->base));
1236 _snl_restore_msg_offset(const struct snl_writer *nw, int off)
1238 return ((void *)((char *)nw->hdr + off));
1243 snl_add_msg_attr_nested(struct snl_writer *nw, int attrtype)
1245 int off = snl_get_msg_offset(nw);
1246 struct nlattr *nla = snl_reserve_msg_data(nw, sizeof(struct nlattr), struct nlattr);
1254 snl_end_attr_nested(const struct snl_writer *nw, int off)
1256 if (!nw->error) {
1257 struct nlattr *nla = snl_restore_msg_offset(nw, off, struct nlattr);
1258 nla->nla_len = NETLINK_ALIGN(snl_get_msg_offset(nw) - off);
1263 snl_create_msg_request(struct snl_writer *nw, int nlmsg_type)
1265 assert(nw->hdr == NULL);
1267 struct nlmsghdr *hdr = snl_reserve_msg_object(nw, struct nlmsghdr);
1270 nw->hdr = hdr;
1276 snl_abort_msg(struct snl_writer *nw)
1278 if (nw->hdr != NULL) {
1279 int offset = (char *)(&nw->base[nw->offset]) - (char *)(nw->hdr);
1281 nw->offset -= offset;
1282 nw->hdr = NULL;
1287 snl_finalize_msg(struct snl_writer *nw)
1289 if (nw->error)
1290 snl_abort_msg(nw);
1291 if (nw->hdr != NULL) {
1292 struct nlmsghdr *hdr = nw->hdr;
1294 int offset = (char *)(&nw->base[nw->offset]) - (char *)(nw->hdr);
1296 hdr->nlmsg_seq = snl_get_seq(nw->ss);
1297 nw->hdr = NULL;
1305 snl_send_msgs(struct snl_writer *nw)
1307 int offset = nw->offset;
1309 assert(nw->hdr == NULL);
1310 nw->offset = 0;
1312 return (snl_send(nw->ss, nw->base, offset));