From owner-svn-src-head@FreeBSD.ORG Sat May 2 18:10:46 2015 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 047A5959; Sat, 2 May 2015 18:10:46 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id E71261378; Sat, 2 May 2015 18:10:45 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.9/8.14.9) with ESMTP id t42IAjgJ007020; Sat, 2 May 2015 18:10:45 GMT (envelope-from oshogbo@FreeBSD.org) Received: (from oshogbo@localhost) by svn.freebsd.org (8.14.9/8.14.9/Submit) id t42IAjxl007019; Sat, 2 May 2015 18:10:45 GMT (envelope-from oshogbo@FreeBSD.org) Message-Id: <201505021810.t42IAjxl007019@svn.freebsd.org> X-Authentication-Warning: svn.freebsd.org: oshogbo set sender to oshogbo@FreeBSD.org using -f From: Mariusz Zaborski Date: Sat, 2 May 2015 18:10:45 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r282349 - head/sys/kern X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 02 May 2015 18:10:46 -0000 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)