Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / lib / perf / lib.c
blob696fb0ea67c6e31e35b3bd9ab8055f121051f707
1 // SPDX-License-Identifier: GPL-2.0
2 #include <unistd.h>
3 #include <stdbool.h>
4 #include <errno.h>
5 #include <linux/kernel.h>
6 #include <internal/lib.h>
8 unsigned int page_size;
10 static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
12 void *buf_start = buf;
13 size_t left = n;
15 while (left) {
16 /* buf must be treated as const if !is_read. */
17 ssize_t ret = is_read ? read(fd, buf, left) :
18 write(fd, buf, left);
20 if (ret < 0 && errno == EINTR)
21 continue;
22 if (ret <= 0)
23 return ret;
25 left -= ret;
26 buf += ret;
29 BUG_ON((size_t)(buf - buf_start) != n);
30 return n;
34 * Read exactly 'n' bytes or return an error.
36 ssize_t readn(int fd, void *buf, size_t n)
38 return ion(true, fd, buf, n);
41 ssize_t preadn(int fd, void *buf, size_t n, off_t offs)
43 size_t left = n;
45 while (left) {
46 ssize_t ret = pread(fd, buf, left, offs);
48 if (ret < 0 && errno == EINTR)
49 continue;
50 if (ret <= 0)
51 return ret;
53 left -= ret;
54 buf += ret;
55 offs += ret;
58 return n;
62 * Write exactly 'n' bytes or return an error.
64 ssize_t writen(int fd, const void *buf, size_t n)
66 /* ion does not modify buf. */
67 return ion(false, fd, (void *)buf, n);