Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

scbuf.c

00001 #ifndef _SCBUF_C_
00002 #define _SCBUF_C_
00003 
00004 /* -*- linux-c -*- */
00005 /** @file scbuf.c
00006  * @addtogroup scbuf Scratch Buffer
00007  * Scratch Buffer Functions.
00008  * The scratch buffer is for collecting output before storing in a map,
00009  * printing, etc. This is a per-cpu static buffer.  It is necessary because 
00010  * of the limited stack space available in the kernel.
00011  * @{
00012  */
00013 
00014 /** Maximum size of buffer, not including terminating NULL */
00015 #define STP_BUF_LEN 8191
00016 
00017 /** Scratch buffer for printing, building strings, etc */
00018 char _stp_scbuf[STP_BUF_LEN+1];
00019 static int _stp_scbuf_len = STP_BUF_LEN;
00020 
00021 /** Sprint into the scratch buffer.
00022  * Like printf, except output goes into  #_stp_scbuf,
00023  * which will contain the null-terminated output.
00024  * Safe because overflowing #_stp_scbuf is not allowed.
00025  * Size is limited by length of scratch buffer, STP_BUF_LEN.
00026  *
00027  * @param fmt A printf-style format string followed by a 
00028  * variable number of args.
00029  * @sa _stp_scbuf_clear
00030  */
00031 
00032 void _stp_sprint (const char *fmt, ...)
00033 {
00034   int num;
00035   va_list args;
00036   char *buf = _stp_scbuf + STP_BUF_LEN - _stp_scbuf_len;
00037   va_start(args, fmt);
00038   num = vscnprintf(buf, _stp_scbuf_len, fmt, args);
00039   va_end(args);
00040   if (num > 0)
00041     _stp_scbuf_len -= num;
00042 }
00043 
00044 void _stp_sprint_str (const char *str)
00045 {
00046   char *buf = _stp_scbuf + STP_BUF_LEN - _stp_scbuf_len;
00047   int num = strlen (str);
00048   if (num > _stp_scbuf_len)
00049     num = _stp_scbuf_len;
00050   strncpy (buf, str, num);
00051   _stp_scbuf_len -= num;
00052 }
00053 
00054 /** Clear the scratch buffer.
00055  * Output from _stp_sprint() will accumulate in the buffer
00056  * until this is called.
00057  */
00058 
00059 void _stp_scbuf_clear (void)
00060 {
00061   _stp_scbuf_len = STP_BUF_LEN;
00062   _stp_scbuf[0] = 0;
00063 }
00064 
00065 static char *_stp_scbuf_cur (void)
00066 {
00067   return _stp_scbuf + STP_BUF_LEN - _stp_scbuf_len;
00068 }
00069 
00070 /** @} */
00071 #endif /* _SCBUF_C_ */