ahci: link FIS receive mode to memory allocation
[minix.git] / drivers / filter / util.c
blobf573198744817ddfb14d4c02975d5a1d187876f3
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 * flt_alarm *
43 *===========================================================================*/
44 clock_t flt_alarm(clock_t dt)
46 int r;
48 if(dt < 0)
49 return next_alarm;
51 r = sys_setalarm(dt, 0);
53 if(r != OK)
54 panic("sys_setalarm failed: %d", r);
56 if(dt == 0) {
57 if(!next_alarm)
58 panic("clearing unset alarm: %d", r);
59 next_alarm = 0;
60 } else {
61 if(next_alarm)
62 panic("overwriting alarm: %d", r);
63 if ((r = getuptime(&next_alarm)) != OK)
64 panic("getuptime failed: %d", r);
65 next_alarm += dt;
68 return next_alarm;