From 3cfb71c1860c4b6eab08dba1253a0b1de5d64826 Mon Sep 17 00:00:00 2001 From: Mariusz Zaborski Date: Wed, 29 Apr 2015 22:15:02 +0000 Subject: [PATCH] Remove recursion from descriptor-related functions. Approved by: pjd (mentor) --- sys/kern/subr_nvlist.c | 75 +++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/sys/kern/subr_nvlist.c b/sys/kern/subr_nvlist.c index ba15035f14bd..11265c4bf151 100644 --- a/sys/kern/subr_nvlist.c +++ b/sys/kern/subr_nvlist.c @@ -533,27 +533,30 @@ out: #ifndef _KERNEL static int * -nvlist_xdescriptors(const nvlist_t *nvl, int *descs, int level) +nvlist_xdescriptors(const nvlist_t *nvl, int *descs) { - const nvpair_t *nvp; + nvpair_t *nvp; + const char *name; + int type; NVLIST_ASSERT(nvl); PJDLOG_ASSERT(nvl->nvl_error == 0); - PJDLOG_ASSERT(level < 3); - for (nvp = nvlist_first_nvpair(nvl); nvp != NULL; - nvp = nvlist_next_nvpair(nvl, nvp)) { - switch (nvpair_type(nvp)) { - case NV_TYPE_DESCRIPTOR: - *descs = nvpair_get_descriptor(nvp); - descs++; - break; - case NV_TYPE_NVLIST: - descs = nvlist_xdescriptors(nvpair_get_nvlist(nvp), - descs, level + 1); - break; + nvp = NULL; + do { + while ((name = nvlist_next(nvl, &type, (void**)&nvp)) != NULL) { + switch (type) { + case NV_TYPE_DESCRIPTOR: + *descs = nvpair_get_descriptor(nvp); + descs++; + break; + case NV_TYPE_NVLIST: + nvl = nvpair_get_nvlist(nvp); + nvp = NULL; + break; + } } - } + } while ((nvl = nvlist_get_parent(nvl, (void**)&nvp)) != NULL); return (descs); } @@ -571,7 +574,7 @@ nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp) if (fds == NULL) return (NULL); if (nitems > 0) - nvlist_xdescriptors(nvl, fds, 0); + nvlist_xdescriptors(nvl, fds); fds[nitems] = -1; if (nitemsp != NULL) *nitemsp = nitems; @@ -579,30 +582,33 @@ nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp) } #endif -static size_t -nvlist_xndescriptors(const nvlist_t *nvl, int level) +size_t +nvlist_ndescriptors(const nvlist_t *nvl) { #ifndef _KERNEL - const nvpair_t *nvp; + nvpair_t *nvp; + const char *name; size_t ndescs; + int type; NVLIST_ASSERT(nvl); PJDLOG_ASSERT(nvl->nvl_error == 0); - PJDLOG_ASSERT(level < 3); ndescs = 0; - for (nvp = nvlist_first_nvpair(nvl); nvp != NULL; - nvp = nvlist_next_nvpair(nvl, nvp)) { - switch (nvpair_type(nvp)) { - case NV_TYPE_DESCRIPTOR: - ndescs++; - break; - case NV_TYPE_NVLIST: - ndescs += nvlist_xndescriptors(nvpair_get_nvlist(nvp), - level + 1); - break; + nvp = NULL; + do { + while ((name = nvlist_next(nvl, &type, (void**)&nvp)) != NULL) { + switch (type) { + case NV_TYPE_DESCRIPTOR: + ndescs++; + break; + case NV_TYPE_NVLIST: + nvl = nvpair_get_nvlist(nvp); + nvp = NULL; + break; + } } - } + } while ((nvl = nvlist_get_parent(nvl, (void**)&nvp)) != NULL); return (ndescs); #else @@ -610,13 +616,6 @@ nvlist_xndescriptors(const nvlist_t *nvl, int level) #endif } -size_t -nvlist_ndescriptors(const nvlist_t *nvl) -{ - - return (nvlist_xndescriptors(nvl, 0)); -} - static unsigned char * nvlist_pack_header(const nvlist_t *nvl, unsigned char *ptr, size_t *leftp) {