summaryrefslogtreecommitdiffstats
path: root/sys/kern
diff options
context:
space:
mode:
authoroshogbo <oshogbo@FreeBSD.org>2015-05-02 18:10:45 +0000
committeroshogbo <oshogbo@FreeBSD.org>2015-05-02 18:10:45 +0000
commit2cc8650d9b4f57202c86ad2e5f068fe65530c29f (patch)
tree6625154641aef2326d363da2014c2670d98c4d82 /sys/kern
parentd04625185d1c7db2c2b13a174fc578db514d6ef0 (diff)
downloadFreeBSD-src-2cc8650d9b4f57202c86ad2e5f068fe65530c29f.zip
FreeBSD-src-2cc8650d9b4f57202c86ad2e5f068fe65530c29f.tar.gz
Remove duplicated code using macro template for the nvlist_add_.* functions.
Approved by: pjd (mentor)
Diffstat (limited to 'sys/kern')
-rw-r--r--sys/kern/subr_nvlist.c118
1 files changed, 27 insertions, 91 deletions
diff --git a/sys/kern/subr_nvlist.c b/sys/kern/subr_nvlist.c
index 4528a35..e7e1438 100644
--- a/sys/kern/subr_nvlist.c
+++ b/sys/kern/subr_nvlist.c
@@ -1142,45 +1142,8 @@ 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)
+nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value,
+ size_t size)
{
nvpair_t *nvp;
@@ -1189,7 +1152,7 @@ nvlist_add_string(nvlist_t *nvl, const char *name, const char *value)
return;
}
- nvp = nvpair_create_string(name, value);
+ nvp = nvpair_create_binary(name, value, size);
if (nvp == NULL) {
nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
ERRNO_SET(nvl->nvl_error);
@@ -1198,63 +1161,36 @@ nvlist_add_string(nvlist_t *nvl, const char *name, const char *value)
}
}
-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);
- }
+#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
-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);
-}
+NVLIST_ADD(int, descriptor);
#endif
-void
-nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value,
- size_t size)
-{
- nvpair_t *nvp;
-
- if (nvlist_error(nvl) != 0) {
- ERRNO_SET(nvlist_error(nvl));
- return;
- }
-
- nvp = nvpair_create_binary(name, value, size);
- if (nvp == NULL) {
- nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
- ERRNO_SET(nvl->nvl_error);
- } else {
- nvlist_move_nvpair(nvl, nvp);
- }
-}
+#undef NVLIST_ADD
void
nvlist_move_nvpair(nvlist_t *nvl, nvpair_t *nvp)
OpenPOWER on IntegriCloud