diff options
author | hunt <hunt> | 2005-03-29 18:07:58 +0000 |
---|---|---|
committer | hunt <hunt> | 2005-03-29 18:07:58 +0000 |
commit | e32551b18f4560056d2d482f5e1505b1b98fa82a (patch) | |
tree | 4e9e07a9b46a4fd5dea27732571cbb04c0ef5dee /runtime/scbuf.c | |
parent | 13b35bb112459702e7371ecc89d7deb789818a86 (diff) | |
download | systemtap-steved-e32551b18f4560056d2d482f5e1505b1b98fa82a.tar.gz systemtap-steved-e32551b18f4560056d2d482f5e1505b1b98fa82a.tar.xz systemtap-steved-e32551b18f4560056d2d482f5e1505b1b98fa82a.zip |
*** empty log message ***
Diffstat (limited to 'runtime/scbuf.c')
-rw-r--r-- | runtime/scbuf.c | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/runtime/scbuf.c b/runtime/scbuf.c new file mode 100644 index 00000000..15458bee --- /dev/null +++ b/runtime/scbuf.c @@ -0,0 +1,71 @@ +#ifndef _SCBUF_C_ +#define _SCBUF_C_ + +/* -*- linux-c -*- */ +/** @file scbuf.c + * @addtogroup scbuf Scratch Buffer + * Scratch Buffer Functions. + * The scratch buffer is for collecting output before storing in a map, + * printing, etc. This is a per-cpu static buffer. It is necessary because + * of the limited stack space available in the kernel. + * @{ + */ + +/** Maximum size of buffer, not including terminating NULL */ +#define STP_BUF_LEN 8191 + +/** Scratch buffer for printing, building strings, etc */ +char _stp_scbuf[STP_BUF_LEN+1]; +static int _stp_scbuf_len = STP_BUF_LEN; + +/** Sprint into the scratch buffer. + * Like printf, except output goes into #_stp_scbuf, + * which will contain the null-terminated output. + * Safe because overflowing #_stp_scbuf is not allowed. + * Size is limited by length of scratch buffer, STP_BUF_LEN. + * + * @param fmt A printf-style format string followed by a + * variable number of args. + * @sa _stp_scbuf_clear + */ + +void _stp_sprint (const char *fmt, ...) +{ + int num; + va_list args; + char *buf = _stp_scbuf + STP_BUF_LEN - _stp_scbuf_len; + va_start(args, fmt); + num = vscnprintf(buf, _stp_scbuf_len, fmt, args); + va_end(args); + if (num > 0) + _stp_scbuf_len -= num; +} + +void _stp_sprint_str (const char *str) +{ + char *buf = _stp_scbuf + STP_BUF_LEN - _stp_scbuf_len; + int num = strlen (str); + if (num > _stp_scbuf_len) + num = _stp_scbuf_len; + strncpy (buf, str, num); + _stp_scbuf_len -= num; +} + +/** Clear the scratch buffer. + * Output from _stp_sprint() will accumulate in the buffer + * until this is called. + */ + +void _stp_scbuf_clear (void) +{ + _stp_scbuf_len = STP_BUF_LEN; + _stp_scbuf[0] = 0; +} + +static char *_stp_scbuf_cur (void) +{ + return _stp_scbuf + STP_BUF_LEN - _stp_scbuf_len; +} + +/** @} */ +#endif /* _SCBUF_C_ */ |