dec21140A ethernet driver for virtualpc, contributed by nicolas tittley.
[minix.git] / drivers / filter / util.c
blob7f476de39f53524d25370bb4cef607a0840c2c46
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 p = mmap(NULL, size, PROT_READ | PROT_WRITE,
24 MAP_PREALLOC | MAP_CONTIG | MAP_ANON, -1, 0);
25 if (p == MAP_FAILED)
26 panic(__FILE__, "out of memory", size);
28 return p;
31 /*===========================================================================*
32 * flt_free *
33 *===========================================================================*/
34 void flt_free(char *buf, size_t size, char *sbuf)
36 /* Free a buffer previously allocated with flt_malloc().
39 if(buf != sbuf)
40 munmap(buf, size);
43 /*===========================================================================*
44 * print64 *
45 *===========================================================================*/
46 char *print64(u64_t p)
48 #define NB 10
49 static int n = 0;
50 static char buf[NB][100];
51 u32_t lo = ex64lo(p), hi = ex64hi(p);
52 n = (n+1) % NB;
53 if(!hi) sprintf(buf[n], "%lx", lo);
54 else sprintf(buf[n], "%lx%08lx", hi, lo);
55 return buf[n];
58 /*===========================================================================*
59 * flt_alarm *
60 *===========================================================================*/
61 clock_t flt_alarm(clock_t dt)
63 int r;
65 if(dt < 0)
66 return next_alarm;
68 r = sys_setalarm(dt, 0);
70 if(r != OK)
71 panic(__FILE__, "sys_setalarm failed", r);
73 if(dt == 0) {
74 if(!next_alarm)
75 panic(__FILE__, "clearing unset alarm", r);
76 next_alarm = 0;
77 } else {
78 if(next_alarm)
79 panic(__FILE__, "overwriting alarm", r);
80 if ((r = getuptime(&next_alarm)) != OK)
81 panic(__FILE__, "getuptime failed", r);
82 next_alarm += dt;
85 return next_alarm;
88 /*===========================================================================*
89 * got_alarm *
90 *===========================================================================*/
91 static void got_alarm(int sig)
93 /* Do nothing. */
96 /*===========================================================================*
97 * flt_sleep *
98 *===========================================================================*/
99 void flt_sleep(int secs)
101 /* Sleep for the given number of seconds. Don't use sleep(), as that
102 * will end up calling select() to VFS. This implementation could be
103 * improved.
106 signal(SIGALRM, got_alarm);
107 alarm(secs);
109 pause();