summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/birdlib.h52
-rw-r--r--lib/printf.c37
-rw-r--r--lib/string.h4
3 files changed, 86 insertions, 7 deletions
diff --git a/lib/birdlib.h b/lib/birdlib.h
index 479f3d5c..2d6849e1 100644
--- a/lib/birdlib.h
+++ b/lib/birdlib.h
@@ -10,6 +10,7 @@
#define _BIRD_BIRDLIB_H_
#include "timer.h"
+#include "alloca.h"
/* Ugly structure offset handling macros */
@@ -19,12 +20,12 @@
/* Utility macros */
-#ifdef PARSER
#define _MIN(a,b) (((a)<(b))?(a):(b))
#define _MAX(a,b) (((a)>(b))?(a):(b))
-#else
-#define MIN(a,b) (((a)<(b))?(a):(b))
-#define MAX(a,b) (((a)>(b))?(a):(b))
+
+#ifndef PARSER
+#define MIN(a,b) _MIN(a,b)
+#define MAX(a,b) _MAX(a,b)
#endif
#define ABS(a) ((a)>=0 ? (a) : -(a))
@@ -34,24 +35,61 @@
#define NULL ((void *) 0)
#endif
+
/* Macros for gcc attributes */
#define NORET __attribute__((noreturn))
#define UNUSED __attribute__((unused))
+
+/* Microsecond time */
+
+typedef s64 btime;
+
+#define _S *1000000
+#define _MS *1000
+#define _US *1
+#define TO_S /1000000
+#define TO_MS /1000
+#define TO_US /1
+
+#ifndef PARSER
+#define S _S
+#define MS _MS
+#define US _US
+#endif
+
+
/* Logging and dying */
+typedef struct buffer {
+ byte *start;
+ byte *pos;
+ byte *end;
+} buffer;
+
+#define STACK_BUFFER_INIT(buf,size) \
+ do { \
+ buf.start = alloca(size); \
+ buf.pos = buf.start; \
+ buf.end = buf.start + size; \
+ } while(0)
+
+#define LOG_BUFFER_INIT(buf) \
+ STACK_BUFFER_INIT(buf, LOG_BUFFER_SIZE)
+
+#define LOG_BUFFER_SIZE 1024
+
+
struct rate_limit {
bird_clock_t timestamp;
int count;
};
#define log log_msg
-void log_reset(void);
-void log_commit(int class);
+void log_commit(int class, buffer *buf);
void log_msg(char *msg, ...);
void log_rl(struct rate_limit *rl, char *msg, ...);
-void logn(char *msg, ...);
void die(char *msg, ...) NORET;
void bug(char *msg, ...) NORET;
diff --git a/lib/printf.c b/lib/printf.c
index 14af1062..d8600b61 100644
--- a/lib/printf.c
+++ b/lib/printf.c
@@ -410,3 +410,40 @@ int bsnprintf(char * buf, int size, const char *fmt, ...)
va_end(args);
return i;
}
+
+int
+buffer_vprint(buffer *buf, const char *fmt, va_list args)
+{
+ int i = bvsnprintf((char *) buf->pos, buf->end - buf->pos, fmt, args);
+ buf->pos = (i >= 0) ? (buf->pos + i) : buf->end;
+ return i;
+}
+
+int
+buffer_print(buffer *buf, const char *fmt, ...)
+{
+ va_list args;
+ int i;
+
+ va_start(args, fmt);
+ i=bvsnprintf((char *) buf->pos, buf->end - buf->pos, fmt, args);
+ va_end(args);
+
+ buf->pos = (i >= 0) ? (buf->pos + i) : buf->end;
+ return i;
+}
+
+void
+buffer_puts(buffer *buf, const char *str)
+{
+ byte *bp = buf->pos;
+ byte *be = buf->end;
+
+ while (bp < be && *str)
+ *bp++ = *str++;
+
+ if (bp < be)
+ *bp = 0;
+
+ buf->pos = bp;
+}
diff --git a/lib/string.h b/lib/string.h
index 14eaa360..2c477294 100644
--- a/lib/string.h
+++ b/lib/string.h
@@ -17,6 +17,10 @@ int bvsprintf(char *str, const char *fmt, va_list args);
int bsnprintf(char *str, int size, const char *fmt, ...);
int bvsnprintf(char *str, int size, const char *fmt, va_list args);
+int buffer_vprint(buffer *buf, const char *fmt, va_list args);
+int buffer_print(buffer *buf, const char *fmt, ...);
+void buffer_puts(buffer *buf, const char *str);
+
int patmatch(byte *pat, byte *str);
#endif