Spdk/lib/util/pipe.c
paul luse a6dbe3721e update Intel copyright notices
per Intel policy to include file commit date using git cmd
below.  The policy does not apply to non-Intel (C) notices.

git log --follow -C90% --format=%ad --date default <file> | tail -1

and then pull just the 4 digit year from the result.

Intel copyrights were not added to files where Intel either had
no contribution ot the contribution lacked substance (ie license
header updates, formatting changes, etc).  Contribution date used
"--follow -C95%" to get the most accurate date.

Note that several files in this patch didn't end the license/(c)
block with a blank comment line so these were added as the vast
majority of files do have this last blank line.  Simply there for
consistency.

Signed-off-by: paul luse <paul.e.luse@intel.com>
Change-Id: Id5b7ce4f658fe87132f14139ead58d6e285c04d4
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/15192
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Community-CI: Mellanox Build Bot
2022-11-10 08:28:53 +00:00

219 lines
3.7 KiB
C

/* SPDX-License-Identifier: BSD-3-Clause
* Copyright (C) 2019 Intel Corporation.
* All rights reserved.
*/
#include "spdk/pipe.h"
#include "spdk/util.h"
struct spdk_pipe {
uint8_t *buf;
uint32_t sz;
uint32_t write;
uint32_t read;
};
struct spdk_pipe *
spdk_pipe_create(void *buf, uint32_t sz)
{
struct spdk_pipe *pipe;
pipe = calloc(1, sizeof(*pipe));
if (pipe == NULL) {
return NULL;
}
pipe->buf = buf;
pipe->sz = sz;
return pipe;
}
void
spdk_pipe_destroy(struct spdk_pipe *pipe)
{
free(pipe);
}
int
spdk_pipe_writer_get_buffer(struct spdk_pipe *pipe, uint32_t requested_sz, struct iovec *iovs)
{
uint32_t sz;
uint32_t read;
uint32_t write;
read = pipe->read;
write = pipe->write;
if (read <= write) {
requested_sz = spdk_min(requested_sz, ((read + pipe->sz) - write - 1));
sz = spdk_min(requested_sz, pipe->sz - write);
iovs[0].iov_base = (sz == 0) ? NULL : (pipe->buf + write);
iovs[0].iov_len = sz;
requested_sz -= sz;
if (requested_sz > 0) {
sz = spdk_min(requested_sz, read);
iovs[1].iov_base = (sz == 0) ? NULL : pipe->buf;
iovs[1].iov_len = sz;
} else {
iovs[1].iov_base = NULL;
iovs[1].iov_len = 0;
}
} else {
sz = spdk_min(requested_sz, read - write - 1);
iovs[0].iov_base = (sz == 0) ? NULL : (pipe->buf + write);
iovs[0].iov_len = sz;
iovs[1].iov_base = NULL;
iovs[1].iov_len = 0;
}
return iovs[0].iov_len + iovs[1].iov_len;
}
int
spdk_pipe_writer_advance(struct spdk_pipe *pipe, uint32_t requested_sz)
{
uint32_t sz;
uint32_t read;
uint32_t write;
read = pipe->read;
write = pipe->write;
if (requested_sz > pipe->sz - 1) {
return -EINVAL;
}
if (read <= write) {
if (requested_sz > (read + pipe->sz) - write) {
return -EINVAL;
}
sz = spdk_min(requested_sz, pipe->sz - write);
write += sz;
if (write > pipe->sz - 1) {
write = 0;
}
requested_sz -= sz;
if (requested_sz > 0) {
if (requested_sz >= read) {
return -EINVAL;
}
write = requested_sz;
}
} else {
if (requested_sz > (read - write - 1)) {
return -EINVAL;
}
write += requested_sz;
}
pipe->write = write;
return 0;
}
uint32_t
spdk_pipe_reader_bytes_available(struct spdk_pipe *pipe)
{
uint32_t read;
uint32_t write;
read = pipe->read;
write = pipe->write;
if (read <= write) {
return write - read;
}
return (write + pipe->sz) - read;
}
int
spdk_pipe_reader_get_buffer(struct spdk_pipe *pipe, uint32_t requested_sz, struct iovec *iovs)
{
uint32_t sz;
uint32_t read;
uint32_t write;
read = pipe->read;
write = pipe->write;
if (read <= write) {
sz = spdk_min(requested_sz, write - read);
iovs[0].iov_base = (sz == 0) ? NULL : (pipe->buf + read);
iovs[0].iov_len = sz;
iovs[1].iov_base = NULL;
iovs[1].iov_len = 0;
} else {
sz = spdk_min(requested_sz, pipe->sz - read);
iovs[0].iov_base = (sz == 0) ? NULL : (pipe->buf + read);
iovs[0].iov_len = sz;
requested_sz -= sz;
if (requested_sz > 0) {
sz = spdk_min(requested_sz, write);
iovs[1].iov_base = (sz == 0) ? NULL : pipe->buf;
iovs[1].iov_len = sz;
} else {
iovs[1].iov_base = NULL;
iovs[1].iov_len = 0;
}
}
return iovs[0].iov_len + iovs[1].iov_len;
}
int
spdk_pipe_reader_advance(struct spdk_pipe *pipe, uint32_t requested_sz)
{
uint32_t sz;
uint32_t read;
uint32_t write;
read = pipe->read;
write = pipe->write;
if (read <= write) {
if (requested_sz > (write - read)) {
return -EINVAL;
}
read += requested_sz;
} else {
sz = spdk_min(requested_sz, pipe->sz - read);
read += sz;
if (read > pipe->sz - 1) {
read = 0;
}
requested_sz -= sz;
if (requested_sz > 0) {
if (requested_sz > write) {
return -EINVAL;
}
read = requested_sz;
}
}
pipe->read = read;
return 0;
}