2 * Use vmsplice to fill some user memory into a pipe. vmsplice writes
3 * to stdout, so that must be a pipe.
12 #include <sys/types.h>
19 #define ALIGN_MASK (65535) /* 64k-1, should just be PAGE_SIZE - 1 */
20 #define ALIGN(buf) (void *) (((unsigned long) (buf) + ALIGN_MASK) & ~ALIGN_MASK)
22 #define ALIGN_MASK (0)
23 #define ALIGN(buf) (buf)
26 int do_vmsplice(int fd, void *buffer, int len)
28 struct pollfd pfd = { .fd = fd, .events = POLLOUT, };
33 * in a real app you'd be more clever with poll of course,
34 * here we are basically just blocking on output room and
35 * not using the free time for anything interesting.
37 if (poll(&pfd, 1, -1) < 0)
40 written = vmsplice(fd, buffer, min(SPLICE_SIZE, len), 0);
43 return error("vmsplice");
52 int main(int argc, char *argv[])
54 unsigned char *buffer;
59 if (fstat(STDOUT_FILENO, &sb) < 0)
61 if (!S_ISFIFO(sb.st_mode)) {
62 fprintf(stderr, "stdout must be a pipe\n");
66 ret = fcntl(STDOUT_FILENO, F_GETPSZ);
68 return error("F_GETPSZ");
70 page_size = sysconf(_SC_PAGESIZE);
72 return error("_SC_PAGESIZE");
74 fprintf(stderr, "Pipe size: %d pages / %ld bytes\n", ret, ret * page_size);
76 buffer = ALIGN(malloc(2 * SPLICE_SIZE + ALIGN_MASK));
77 for (i = 0; i < 2 * SPLICE_SIZE; i++)
78 buffer[i] = (i & 0xff);
82 * vmsplice the first half of the buffer into the pipe
84 if (do_vmsplice(STDOUT_FILENO, buffer, SPLICE_SIZE))
88 * first half is now in pipe, but we don't quite know when
93 * vmsplice second half
95 if (do_vmsplice(STDOUT_FILENO, buffer + SPLICE_SIZE, SPLICE_SIZE))
99 * We still don't know when we can reuse the second half of
100 * the buffer, but we do now know that all parts of the first
101 * half have been consumed from the pipe - so we can reuse that.