mirror of
https://github.com/samba-team/samba.git
synced 2024-12-22 13:34:15 +03:00
lib/util: inline iov_{buflen,buf,advance}()
The main reason for this change was the use of iov_advance() in the next commits in source3/smbd/smb2_server.c And the function calls to iov_advance() showed up in profiling with callgrind. While there iov_buf() and iov_buflen() are moved as well, as they are also used there. Signed-off-by: Stefan Metzmacher <metze@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
This commit is contained in:
parent
76f9a41fa6
commit
a9c53b9eff
@ -22,76 +22,6 @@
|
||||
#include "iov_buf.h"
|
||||
#include <talloc.h>
|
||||
|
||||
ssize_t iov_buflen(const struct iovec *iov, int iovcnt)
|
||||
{
|
||||
return iov_buf(iov, iovcnt, NULL, 0);
|
||||
}
|
||||
|
||||
ssize_t iov_buf(const struct iovec *iov, int iovcnt,
|
||||
uint8_t *buf, size_t buflen)
|
||||
{
|
||||
size_t needed = 0;
|
||||
uint8_t *p = buf;
|
||||
int i;
|
||||
|
||||
for (i=0; i<iovcnt; i++) {
|
||||
size_t thislen = iov[i].iov_len;
|
||||
size_t tmp;
|
||||
|
||||
tmp = needed + thislen;
|
||||
|
||||
if (tmp < needed) {
|
||||
/* wrap */
|
||||
return -1;
|
||||
}
|
||||
needed = tmp;
|
||||
|
||||
if ((p != NULL) && needed <= buflen && thislen > 0) {
|
||||
memcpy(p, iov[i].iov_base, thislen);
|
||||
p += thislen;
|
||||
}
|
||||
}
|
||||
|
||||
return needed;
|
||||
}
|
||||
|
||||
bool iov_advance(struct iovec **iov, int *iovcnt, size_t n)
|
||||
{
|
||||
struct iovec *v = *iov;
|
||||
int cnt = *iovcnt;
|
||||
|
||||
while (n > 0) {
|
||||
if (cnt == 0) {
|
||||
return false;
|
||||
}
|
||||
if (n < v->iov_len) {
|
||||
v->iov_base = (char *)v->iov_base + n;
|
||||
v->iov_len -= n;
|
||||
break;
|
||||
}
|
||||
n -= v->iov_len;
|
||||
v += 1;
|
||||
cnt -= 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Skip 0-length iovec's
|
||||
*
|
||||
* There might be empty buffers at the end of iov. Next time we do a
|
||||
* readv/writev based on this iov would give 0 transferred bytes, also
|
||||
* known as EPIPE. So we need to be careful discarding them.
|
||||
*/
|
||||
|
||||
while ((cnt > 0) && (v->iov_len == 0)) {
|
||||
v += 1;
|
||||
cnt -= 1;
|
||||
}
|
||||
|
||||
*iov = v;
|
||||
*iovcnt = cnt;
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *iov_concat(TALLOC_CTX *mem_ctx, const struct iovec *iov, int count)
|
||||
{
|
||||
ssize_t buflen;
|
||||
|
@ -24,10 +24,79 @@
|
||||
#include <talloc.h>
|
||||
#include "system/filesys.h"
|
||||
|
||||
ssize_t iov_buflen(const struct iovec *iov, int iovlen);
|
||||
static inline
|
||||
ssize_t iov_buf(const struct iovec *iov, int iovcnt,
|
||||
uint8_t *buf, size_t buflen);
|
||||
bool iov_advance(struct iovec **iov, int *iovcnt, size_t n);
|
||||
uint8_t *buf, size_t buflen)
|
||||
{
|
||||
size_t needed = 0;
|
||||
uint8_t *p = buf;
|
||||
int i;
|
||||
|
||||
for (i=0; i<iovcnt; i++) {
|
||||
size_t thislen = iov[i].iov_len;
|
||||
size_t tmp;
|
||||
|
||||
tmp = needed + thislen;
|
||||
|
||||
if (tmp < needed) {
|
||||
/* wrap */
|
||||
return -1;
|
||||
}
|
||||
needed = tmp;
|
||||
|
||||
if ((p != NULL) && needed <= buflen && thislen > 0) {
|
||||
memcpy(p, iov[i].iov_base, thislen);
|
||||
p += thislen;
|
||||
}
|
||||
}
|
||||
|
||||
return needed;
|
||||
}
|
||||
|
||||
static inline
|
||||
ssize_t iov_buflen(const struct iovec *iov, int iovcnt)
|
||||
{
|
||||
return iov_buf(iov, iovcnt, NULL, 0);
|
||||
}
|
||||
|
||||
static inline
|
||||
bool iov_advance(struct iovec **iov, int *iovcnt, size_t n)
|
||||
{
|
||||
struct iovec *v = *iov;
|
||||
int cnt = *iovcnt;
|
||||
|
||||
while (n > 0) {
|
||||
if (cnt == 0) {
|
||||
return false;
|
||||
}
|
||||
if (n < v->iov_len) {
|
||||
v->iov_base = (char *)v->iov_base + n;
|
||||
v->iov_len -= n;
|
||||
break;
|
||||
}
|
||||
n -= v->iov_len;
|
||||
v += 1;
|
||||
cnt -= 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Skip 0-length iovec's
|
||||
*
|
||||
* There might be empty buffers at the end of iov. Next time we do a
|
||||
* readv/writev based on this iov would give 0 transferred bytes, also
|
||||
* known as EPIPE. So we need to be careful discarding them.
|
||||
*/
|
||||
|
||||
while ((cnt > 0) && (v->iov_len == 0)) {
|
||||
v += 1;
|
||||
cnt -= 1;
|
||||
}
|
||||
|
||||
*iov = v;
|
||||
*iovcnt = cnt;
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *iov_concat(TALLOC_CTX *mem_ctx, const struct iovec *iov, int count);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user