Author: oshogbo
Date: Sat May  2 18:10:45 2015
New Revision: 282349
URL: https://svnweb.freebsd.org/changeset/base/282349

Log:
  Remove duplicated code using macro template for the nvlist_add_.* functions.
  
  Approved by:  pjd (mentor)

Modified:
  head/sys/kern/subr_nvlist.c

Modified: head/sys/kern/subr_nvlist.c
==============================================================================
--- head/sys/kern/subr_nvlist.c Sat May  2 18:07:47 2015        (r282348)
+++ head/sys/kern/subr_nvlist.c Sat May  2 18:10:45 2015        (r282349)
@@ -1142,45 +1142,8 @@ nvlist_add_null(nvlist_t *nvl, const cha
 }
 
 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 c
                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 c
        }
 }
 
-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)
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to