Remove duplicated code using macro template for the nvlist_add_.* functions.

Approved by:	pjd (mentor)
This commit is contained in:
Mariusz Zaborski 2015-05-02 18:10:45 +00:00
parent 00173c094e
commit da20d06f84
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=282349

View File

@ -1141,101 +1141,6 @@ nvlist_add_null(nvlist_t *nvl, const char *name)
}
}
void
nvlist_add_bool(nvlist_t *nvl, const char *name, bool value)
{
nvpair_t *nvp;
if (nvlist_error(nvl) != 0) {
ERRNO_SET(nvlist_error(nvl));
return;
}
nvp = nvpair_create_bool(name, value);
if (nvp == NULL) {
nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
ERRNO_SET(nvl->nvl_error);
} else {
nvlist_move_nvpair(nvl, nvp);
}
}
void
nvlist_add_number(nvlist_t *nvl, const char *name, uint64_t value)
{
nvpair_t *nvp;
if (nvlist_error(nvl) != 0) {
ERRNO_SET(nvlist_error(nvl));
return;
}
nvp = nvpair_create_number(name, value);
if (nvp == NULL) {
nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
ERRNO_SET(nvl->nvl_error);
} else {
nvlist_move_nvpair(nvl, nvp);
}
}
void
nvlist_add_string(nvlist_t *nvl, const char *name, const char *value)
{
nvpair_t *nvp;
if (nvlist_error(nvl) != 0) {
ERRNO_SET(nvlist_error(nvl));
return;
}
nvp = nvpair_create_string(name, value);
if (nvp == NULL) {
nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
ERRNO_SET(nvl->nvl_error);
} else {
nvlist_move_nvpair(nvl, nvp);
}
}
void
nvlist_add_nvlist(nvlist_t *nvl, const char *name, const nvlist_t *value)
{
nvpair_t *nvp;
if (nvlist_error(nvl) != 0) {
ERRNO_SET(nvlist_error(nvl));
return;
}
nvp = nvpair_create_nvlist(name, value);
if (nvp == NULL) {
nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
ERRNO_SET(nvl->nvl_error);
} else {
nvlist_move_nvpair(nvl, nvp);
}
}
#ifndef _KERNEL
void
nvlist_add_descriptor(nvlist_t *nvl, const char *name, int value)
{
nvpair_t *nvp;
if (nvlist_error(nvl) != 0) {
errno = nvlist_error(nvl);
return;
}
nvp = nvpair_create_descriptor(name, value);
if (nvp == NULL)
nvl->nvl_error = errno = (errno != 0 ? errno : ENOMEM);
else
nvlist_move_nvpair(nvl, nvp);
}
#endif
void
nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value,
size_t size)
@ -1256,6 +1161,37 @@ nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value,
}
}
#define NVLIST_ADD(vtype, type) \
void \
nvlist_add_##type(nvlist_t *nvl, const char *name, vtype value) \
{ \
nvpair_t *nvp; \
\
if (nvlist_error(nvl) != 0) { \
ERRNO_SET(nvlist_error(nvl)); \
return; \
} \
\
nvp = nvpair_create_##type(name, value); \
if (nvp == NULL) { \
nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM); \
ERRNO_SET(nvl->nvl_error); \
} else { \
nvlist_move_nvpair(nvl, nvp); \
} \
}
NVLIST_ADD(bool, bool)
NVLIST_ADD(uint64_t, number)
NVLIST_ADD(const char *, string)
NVLIST_ADD(const nvlist_t *, nvlist)
#ifndef _KERNEL
NVLIST_ADD(int, descriptor);
#endif
#undef NVLIST_ADD
void
nvlist_move_nvpair(nvlist_t *nvl, nvpair_t *nvp)
{