[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: [Minios-devel] [UNIKRAFT PATCH v2 1/5] lib/vfscore: introduce vfscore_uioforeach
Hi Yuri, On 6/3/19 11:04 AM, Yuri Volchkov wrote: This is a template-type of function, to avoid code duplication. Compiler optimizes extra call to the function pointer passed as argument, so this should not have any performance impacts Signed-off-by: Yuri Volchkov <yuri.volchkov@xxxxxxxxx> --- lib/vfscore/include/vfscore/uio.h | 39 +++++++++++++++++++++++++++++++ lib/vfscore/subr_uio.c | 34 +++++++-------------------- 2 files changed, 47 insertions(+), 26 deletions(-) diff --git a/lib/vfscore/include/vfscore/uio.h b/lib/vfscore/include/vfscore/uio.h index c734fa19..f9885b94 100644 --- a/lib/vfscore/include/vfscore/uio.h +++ b/lib/vfscore/include/vfscore/uio.h @@ -57,6 +57,45 @@ struct uio { enum uio_rw uio_rw; /* operation */ };+static inline+int vfscore_uioforeach(int (*f)(void *, void *, size_t *), void *cp, + size_t n, struct uio *uio) Because this function is used several times to streamline code, and because I guess other people might want to use it in the future, I think this might benefit from a short comment explaining what the function does and how to use it for this purpose. Something like this: /* This is a wrapper for functions f with a "memcpy-like" signature "dst, src, * cnt" to be executed over a scatter-gather list provided by a struct uio. f() * might be called multiple times to read from or write to cp up to n bytes of * data (or up to the capacity of the uio scatter-gather buffers). */ What do you think? +{ + int ret = 0; + + UK_ASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE); + + while (n > 0 && uio->uio_resid) { + struct iovec *iov = uio->uio_iov; + size_t cnt = iov->iov_len; + + if (cnt == 0) { + uio->uio_iov++; + uio->uio_iovcnt--; + continue; + } + if (cnt > n) + cnt = n; + + if (uio->uio_rw == UIO_READ) + ret = f(iov->iov_base, cp, &cnt); + else + ret = f(cp, iov->iov_base, &cnt); + + iov->iov_base = (char *)iov->iov_base + cnt; + iov->iov_len -= cnt; + uio->uio_resid -= cnt; + uio->uio_offset += cnt; + cp = (char *)cp + cnt; + n -= cnt; + + if (ret) + break; + } + + return ret; +} + int vfscore_uiomove(void *cp, int n, struct uio *uio);#endif /* !_UIO_H_ */diff --git a/lib/vfscore/subr_uio.c b/lib/vfscore/subr_uio.c index aa176074..e6e1cdf0 100644 --- a/lib/vfscore/subr_uio.c +++ b/lib/vfscore/subr_uio.c @@ -45,34 +45,16 @@ #include <string.h> #include <vfscore/uio.h>+static int __memcpy_wrapper(void *dst, void *src, size_t *cnt)+{ + memcpy(dst, src, *cnt); + return 0; +} + int vfscore_uiomove(void *cp, int n, struct uio *uio) { - UK_ASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE); + int ret = vfscore_uioforeach(__memcpy_wrapper, cp, n, uio);- while (n > 0 && uio->uio_resid) {- struct iovec *iov = uio->uio_iov; - int cnt = iov->iov_len; - if (cnt == 0) { - uio->uio_iov++; - uio->uio_iovcnt--; - continue; - } - if (cnt > n) - cnt = n; - - if (uio->uio_rw == UIO_READ) - memcpy(iov->iov_base, cp, cnt); - else - memcpy(cp, iov->iov_base, cnt); - - iov->iov_base = (char *)iov->iov_base + cnt; - iov->iov_len -= cnt; - uio->uio_resid -= cnt; - uio->uio_offset += cnt; - cp = (char *)cp + cnt; - n -= cnt; - } - - return 0; + return ret; } -- Dr. Florian Schmidt フローリアン・シュミット Research Scientist, Systems and Machine Learning Group NEC Laboratories Europe Kurfürsten-Anlage 36, D-69115 Heidelberg Tel. +49 (0)6221 4342-265 Fax: +49 (0)6221 4342-155 e-mail: florian.schmidt@xxxxxxxxx ============================================================ Registered at Amtsgericht Mannheim, Germany, HRB728558 _______________________________________________ Minios-devel mailing list Minios-devel@xxxxxxxxxxxxxxxxxxxx https://lists.xenproject.org/mailman/listinfo/minios-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |