diff options
Diffstat (limited to 'src/io/tpax_io_read_ahead.c')
-rw-r--r-- | src/io/tpax_io_read_ahead.c | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/io/tpax_io_read_ahead.c b/src/io/tpax_io_read_ahead.c new file mode 100644 index 0000000..c92c08c --- /dev/null +++ b/src/io/tpax_io_read_ahead.c @@ -0,0 +1,75 @@ +/**************************************************************/ +/* tpax: a topological pax implementation */ +/* Copyright (C) 2020--2024 SysDeer Technologies, LLC */ +/* Released under GPLv2 and GPLv3; see COPYING.TPAX. */ +/**************************************************************/ + +#include <stdint.h> +#include <unistd.h> +#include <fcntl.h> +#include <errno.h> + +#include <tpax/tpax.h> +#include <tpax/tpax_specs.h> +#include "tpax_driver_impl.h" +#include "tpax_errinfo_impl.h" + +#define TPAX_IO_READ_MAX (2147483647) + +static int tpax_io_range_read_ahead_fileio( + struct tpax_driver_ctx * dctx, + void * buf, + size_t size) +{ + int fdin; + ssize_t nbytes; + off_t cpos; + + fdin = tpax_driver_fdin(dctx); + cpos = tpax_get_driver_cpos(dctx); + + nbytes = pread(fdin,buf,size,cpos); + + while ((nbytes < 0) && (errno = EINTR)) + nbytes = pread(fdin,buf,size,cpos); + + return nbytes; +} + +static int tpax_io_range_read_ahead_mapped( + struct tpax_driver_ctx * dctx, + void * buf, + size_t size) +{ + struct tpax_driver_ctx_impl * ictx; + char * ch; + off_t cpos; + + ictx = tpax_get_driver_ictx(dctx); + cpos = tpax_get_driver_cpos(dctx); + size = (cpos + size <= ictx->mapsize) ? size : ictx->mapsize - cpos; + + ch = ictx->mapaddr; + ch = &ch[cpos]; + + memcpy(buf,ch,size); + + return size; +} + +int tpax_io_range_read_ahead(struct tpax_driver_ctx * dctx, void * buf, size_t size) +{ + size = (size <= TPAX_IO_READ_MAX) ? size : TPAX_IO_READ_MAX; + + if (dctx->cctx->srcflags & TPAX_SOURCE_DATA_FILEIO) { + return tpax_io_range_read_ahead_fileio(dctx,buf,size); + + } else if (dctx->cctx->srcflags & TPAX_SOURCE_DATA_MAPPED) { + return tpax_io_range_read_ahead_mapped(dctx,buf,size); + + } + + return TPAX_CUSTOM_ERROR( + dctx, + TPAX_ERR_FLOW_ERROR); +} |