3.1.7 branch.
[minix.git] / drivers / filter / util.c
blobaf3135e3747852d9af18be1ca397c23a5415564f
1 /* Filter driver - utility functions */
3 #include "inc.h"
4 #include <sys/mman.h>
5 #include <signal.h>
7 static clock_t next_alarm;
9 /*===========================================================================*
10 * flt_malloc *
11 *===========================================================================*/
12 char *flt_malloc(size_t size, char *sbuf, size_t ssize)
14 /* Allocate a buffer for 'size' bytes. If 'size' is equal to or less
15 * than 'ssize', return the static buffer 'sbuf', otherwise, use
16 * malloc() to allocate memory dynamically.
18 char *p;
20 if (size <= ssize)
21 return sbuf;
23 if(!(p = alloc_contig(size, 0, NULL)))
24 panic("out of memory: %d", size);
26 return p;
29 /*===========================================================================*
30 * flt_free *
31 *===========================================================================*/
32 void flt_free(char *buf, size_t size, const char *sbuf)
34 /* Free a buffer previously allocated with flt_malloc().
37 if(buf != sbuf)
38 free_contig(buf, size);
41 /*===========================================================================*
42 * print64 *
43 *===========================================================================*/
44 char *print64(u64_t p)
46 #define NB 10
47 static int n = 0;
48 static char buf[NB][100];
49 u32_t lo = ex64lo(p), hi = ex64hi(p);
50 n = (n+1) % NB;
51 if(!hi) sprintf(buf[n], "%lx", lo);
52 else sprintf(buf[n], "%lx%08lx", hi, lo);
53 return buf[n];
56 /*===========================================================================*
57 * flt_alarm *
58 *===========================================================================*/
59 clock_t flt_alarm(clock_t dt)
61 int r;
63 if(dt < 0)
64 return next_alarm;
66 r = sys_setalarm(dt, 0);
68 if(r != OK)
69 panic("sys_setalarm failed: %d", r);
71 if(dt == 0) {
72 if(!next_alarm)
73 panic("clearing unset alarm: %d", r);
74 next_alarm = 0;
75 } else {
76 if(next_alarm)
77 panic("overwriting alarm: %d", r);
78 if ((r = getuptime(&next_alarm)) != OK)
79 panic("getuptime failed: %d", r);
80 next_alarm += dt;
83 return next_alarm;
86 /*===========================================================================*
87 * got_alarm *
88 *===========================================================================*/
89 static void got_alarm(int sig)
91 /* Do nothing. */