From 03a8f91651ed26c3db97ec5e94834ba6eba54cc2 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Fri, 6 Nov 2026 05:52:23 +0300 Subject: feat(iobuf): add I/O buffer --- include/iobuf.h | 40 +++++++++++++++++ src/iobuf.c | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 include/iobuf.h create mode 100644 src/iobuf.c diff --git a/include/iobuf.h b/include/iobuf.h new file mode 100644 index 0000000..79f5195 --- /dev/null +++ b/include/iobuf.h @@ -0,0 +1,40 @@ +// iobuf.h --- Input/Output buffer -*- C -*- +// +// Copyright (c) 2026 Martin Michalec +// +// SPDX-License-Identifier: MIT +// +// This file is part of COSMOS Codebase. +// +// Commentary: +// +// Code: + +#ifndef INCLUDED_COSMOS_IOBUF_H +#define INCLUDED_COSMOS_IOBUF_H + +#include "ms.h" + +struct iobuf { + u8 *beg; + u8 *rhead; + u8 *whead; + u8 *end; +}; + +struct iobuf iobuf_from (struct ms ms); +usz iobuf_sz (struct iobuf); +struct mv *iobuf_rmv (struct iobuf *); +struct ms *iobuf_wms (struct iobuf *); + +void iobuf_compact (struct iobuf *); + +#ifndef IOBUF_NO_UNISTD + +int iobuf_read (struct iobuf *, int fd); + +#endif // IOBUF_NO_UNISTD + +#endif // INCLUDED_COSMOS_IOBUF_H + +// iobuf.h ends here diff --git a/src/iobuf.c b/src/iobuf.c new file mode 100644 index 0000000..083978b --- /dev/null +++ b/src/iobuf.c @@ -0,0 +1,133 @@ +// iobuf.c --- Input/Output buffer -*- C -*- +// +// Copyright (c) 2026 Martin Michalec +// +// SPDX-License-Identifier: MIT +// +// This file is part of COSMOS Codebase. +// +// Commentary: +// +// Code: + +#include "iobuf.h" +#include "dbg.h" + +#define DBG_ASSERT_IOBUF(iobuf) /* TODO(cmmm): check the invariant */ + +struct iobuf +iobuf_from (struct ms ms) +{ + struct iobuf iobuf = { + .beg = ms.beg, + .rhead = ms.beg, + .whead = ms.beg, + .end = ms.end, + }; + DBG_ASSERT_IOBUF (iobuf); + return iobuf; +} + +struct mv * +iobuf_rmv (struct iobuf *buf) +{ + DBG_ASSERT_NN ( buf); + DBG_ASSERT_IOBUF (*buf); + return (struct mv *) &buf->rhead; +} + +struct ms * +iobuf_wms (struct iobuf *buf) +{ + DBG_ASSERT_NN ( buf); + DBG_ASSERT_IOBUF (*buf); + return (struct ms *) &buf->whead; +} + +usz +iobuf_sz (struct iobuf iobuf) +{ + DBG_ASSERT_IOBUF (iobuf); + return iobuf.end - iobuf.beg; +} + +usz +iobuf_isz (struct iobuf iobuf) +{ + DBG_ASSERT_IOBUF (iobuf); + return iobuf.whead - iobuf.rhead; +} + +usz +iobuf_osz (struct iobuf iobuf) +{ + DBG_ASSERT_IOBUF (iobuf); + return iobuf.end - iobuf.whead; +} + +usz +iobuf_rsz (struct iobuf iobuf) +{ + DBG_ASSERT_IOBUF (iobuf); + return iobuf.rhead - iobuf.beg; +} + +usz +iobuf_wsz (struct iobuf iobuf) +{ + DBG_ASSERT_IOBUF (iobuf); + return iobuf.rhead - iobuf.beg; +} + +usz +iobuf_ospc (struct iobuf iobuf) +{ + DBG_ASSERT_IOBUF (iobuf); + return iobuf.end - iobuf.whead; +} + +#include // TODO(cmmm): remove libc dep +void +iobuf_compact (struct iobuf *iobuf) +{ + DBG_ASSERT_NN ( iobuf); + DBG_ASSERT_IOBUF (*iobuf); + + if (iobuf->rhead == iobuf->beg) + return; + + usz unread = iobuf->whead - iobuf->rhead; + if (unread) + memmove (iobuf->beg, iobuf->rhead, unread); + + iobuf->rhead = (u8 *) iobuf->beg; + iobuf->whead = (u8 *) iobuf->beg + unread; +} + + +#ifndef IOBUF_NO_FEAT_UNISTD + +#include +int iobuf_read (struct iobuf *iobuf, int fd) +{ + DBG_ASSERT_NN ( iobuf); + DBG_ASSERT_IOBUF (*iobuf); + + iobuf_compact (iobuf); + + usz avail = iobuf->end - iobuf->whead; + if (avail == 0) + return 0; + + isz bytes_read = read (fd, iobuf->whead, avail); + if (bytes_read == -1) + return -1; + + iobuf->whead = (u8 *) iobuf->whead + bytes_read; + + return bytes_read; +} + +#endif // IOBUF_NO_FEAT_UNISTD + +// iobuf.c ends here -- cgit v1.3.1