vm, kernel, top: report memory usage of vm, kernel
[minix.git] / servers / procfs / buf.c
blobee2ef7990b91bbb37905e251fd33c341df5fdadf
1 /* ProcFS - buf.c - by Alen Stojanov and David van Moolenbroek */
3 #include "inc.h"
4 #include <stdarg.h>
6 #define BUF_SIZE 4096
8 static char buf[BUF_SIZE + 1];
9 static size_t off, left, used;
10 static off_t skip;
12 /*===========================================================================*
13 * buf_init *
14 *===========================================================================*/
15 void buf_init(off_t start, size_t len)
17 /* Initialize the buffer for fresh use. The first 'start' bytes of the
18 * produced output are to be skipped. After that, up to a total of
19 * 'len' bytes are requested.
22 skip = start;
23 left = MIN(len, BUF_SIZE);
24 off = 0;
25 used = 0;
28 /*===========================================================================*
29 * buf_printf *
30 *===========================================================================*/
31 void buf_printf(char *fmt, ...)
33 /* Add formatted text to the end of the buffer.
35 va_list args;
36 ssize_t len, max;
38 if (left == 0)
39 return;
41 /* There is no way to estimate how much space the result will take, so
42 * we need to produce the string even when skipping part of the start.
43 * If part of the result is to be skipped, do not memcpy; instead, save
44 * the offset of where the result starts within the buffer.
46 * The null terminating character is not part of the result, so room
47 * must be given for it to be stored after completely filling up the
48 * requested part of the buffer.
50 max = MIN(skip + left, BUF_SIZE);
52 va_start(args, fmt);
53 len = vsnprintf(&buf[off + used], max + 1, fmt, args);
54 va_end(args);
56 if (skip > 0) {
57 assert(off == 0);
58 assert(used == 0);
60 if (skip >= len) {
61 skip -= len;
63 return;
66 off = skip;
67 if (left > BUF_SIZE - off)
68 left = BUF_SIZE - off;
69 len -= off;
70 skip = 0;
73 assert(skip == 0);
74 assert(len >= 0);
75 assert((long) left >= 0);
77 if (len > (ssize_t) left)
78 len = left;
80 used += len;
81 left -= len;
84 /*===========================================================================*
85 * buf_append *
86 *===========================================================================*/
87 void buf_append(char *data, size_t len)
89 /* Add arbitrary data to the end of the buffer.
92 if (left == 0)
93 return;
95 if (skip > 0) {
96 if (skip >= (ssize_t) len) {
97 skip -= len;
99 return;
102 data += skip;
103 len -= skip;
104 skip = 0;
107 if (len > left)
108 len = left;
110 memcpy(&buf[off + used], data, len);
112 used += len;
113 left -= len;
116 /*===========================================================================*
117 * buf_get *
118 *===========================================================================*/
119 size_t buf_get(char **ptr)
121 /* Return the buffer's starting address and the length of the used
122 * part, not counting the trailing null character for the latter.
125 *ptr = &buf[off];
127 return used;