summaryrefslogtreecommitdiff
path: root/libhw_generic/include/libhw/generic/io.h
blob: 19edbe6d7ef6c0393b10f3cb1dea28e73f532744 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* libhw/generic/io.h - Device-independent I/O definitions
 *
 * Copyright (C) 2024-2025  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

#ifndef _LIBHW_GENERIC_IO_H_
#define _LIBHW_GENERIC_IO_H_

#include <stddef.h>    /* for size_t */
#include <stdint.h>    /* for uintptr_t */

#include <libmisc/error.h>
#include <libmisc/obj.h>

/* structs ********************************************************************/

/* iovec ===========================================================*/

#if __unix__
#include <sys/uio.h>
#else
struct iovec {
	void    *iov_base;
	size_t   iov_len;
};
#endif
typedef struct iovec iovec;
DECLARE_ERROR_OR(iovec);
DECLARE_ERROR_AND(iovec);

/* duplex_iovec ====================================================*/

#define IOVEC_DISCARD ((void*)(~((uintptr_t)0)))

struct duplex_iovec {
	/**
	 * NULL is a valid pointer value in iov_read_to and
	 * iov_write_from.  To skip a read or write, use the special
	 * value IOVEC_DISCARD.
	 */
	void    *iov_read_to;
	void    *iov_write_from;
	size_t   iov_len;
};
typedef struct duplex_iovec duplex_iovec;
DECLARE_ERROR_OR(duplex_iovec);
DECLARE_ERROR_AND(duplex_iovec);

/* iovec utilities ************************************************************/

/* If byte_max_cnt == 0, then there is no maximum.  */

/* slice iovec lists */
int  io_slice_cnt       (                          const struct        iovec *src, int src_cnt, size_t byte_start, size_t byte_max_cnt);
void io_slice           (struct        iovec *dst, const struct        iovec *src, int src_cnt, size_t byte_start, size_t byte_max_cnt);
int  io_duplex_slice_cnt(                          const struct duplex_iovec *src, int src_cnt, size_t byte_start, size_t byte_max_cnt);
void io_duplex_slice    (struct duplex_iovec *dst, const struct duplex_iovec *src, int src_cnt, size_t byte_start, size_t byte_max_cnt);

/* convert iovec lists */
void io_rd_to_duplex(struct duplex_iovec *dst, const struct iovec *src, int iovcnt);
void io_wr_to_duplex(struct duplex_iovec *dst, const struct iovec *src, int iovcnt);

/* slice and convert in one go */
void io_slice_rd_to_duplex(struct duplex_iovec *dst, const struct iovec *src, int src_cnt, size_t byte_start, size_t byte_max_cnt);
void io_slice_wr_to_duplex(struct duplex_iovec *dst, const struct iovec *src, int src_cnt, size_t byte_start, size_t byte_max_cnt);

/* basic interfaces ***********************************************************/

/*
 * Conventions:
 *
 *  - Naming:
 *     + The "v"[ec] suffix means a sequence of iovecs, instead of a
 *       simple buf+len.
 *
 *  - Errors:
 *     + Short *reads* are *not* errors (and so return size_t *or*
 *       error).
 *     + Short *writes* *are* errors (and so return size_t *and*
 *       (possibly-null) error).
 */

/* read ============================================================*/

/**
 * Return bytes-read on success.  A short read is *not* an error
 * (unlike `write` methods).
 *
 * It is invalid to call readv when the sum length of iovecs is 0.
 */
#define io_reader_LO_IFACE \
	LO_FUNC(size_t_or_error, readv, const struct iovec *iov, int iovcnt)
LO_INTERFACE(io_reader);
#define io_readv(r, iov, iovcnt) LO_CALL(r, readv, iov, iovcnt)
#define io_read(r, buf, count) LO_CALL(r, readv, &((struct iovec){.iov_base = buf, .iov_len = count}), 1)

/* write ===========================================================*/

/**
 * Return bytes-written.  A short write *is* an error (unlike `read`
 * methods).
 *
 * Writes are *not* guaranteed to be atomic, so if you have concurrent
 * writers then you should arrange for a mutex to protect the writer.
 *
 * It is invalid to call writev when the sum length of iovecs is 0.
 */
#define io_writer_LO_IFACE \
	LO_FUNC(size_t_and_error, writev, const struct iovec *iov, int iovcnt)
LO_INTERFACE(io_writer);
#define io_writev(w, iov, iovcnt) LO_CALL(w, writev, iov, iovcnt)
#define io_write(w, buf, count) LO_CALL(w, writev, &((struct iovec){.iov_base = buf, .iov_len = count}), 1)

/* readwrite =======================================================*/

/**
 * A short read/write *is* an error (like `write` methods and unlike
 * `read` methods).
 *
 * Reads/writes are *not* guaranteed to be atomic, so if you have
 * concurrent readers/writers then you should arrange for a mutex to
 * protect the duplex_readwriter.
 *
 * It is invalid to call readwritev when the sum length of iovecs is 0.
 */
#define io_duplex_readwriter_LO_IFACE \
	LO_FUNC(size_t_and_error, readwritev, const struct duplex_iovec *iov, int iovcnt)
LO_INTERFACE(io_duplex_readwriter);
#define io_readwritev(rw, iov, iovcnt) \
	LO_CALL(rw, readwritev, iov, iovcnt)

/* close ===========================================================*/

#define io_closer_LO_IFACE \
	LO_FUNC(error, close)
LO_INTERFACE(io_closer);
#define io_close(c) LO_CALL(c, close)

#define io_bidi_closer_LO_IFACE \
	LO_NEST(io_closer) \
	LO_FUNC(error, close_read) \
	LO_FUNC(error, close_write)
LO_INTERFACE(io_bidi_closer);
#define io_close_read(c) LO_CALL(c, close_read)
#define io_close_write(c) LO_CALL(c, close_write)

/* aggregate interfaces *******************************************************/

#define io_readwriter_LO_IFACE \
	LO_NEST(io_reader) \
	LO_NEST(io_writer)
LO_INTERFACE(io_readwriter);

#endif /* _LIBHW_GENERIC_IO_H_ */