1 /* Filter driver - utility functions */ 2 3 #include "inc.h" 4 #include <sys/mman.h> 5 #include <signal.h> 6 7 static clock_t next_alarm; 8 9 /*===========================================================================* 10 * flt_malloc * 11 *===========================================================================*/ 12 char *flt_malloc(size_t size, char *sbuf, size_t ssize) 13 { 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. 17 */ 18 char *p; 19 20 if (size <= ssize) 21 return sbuf; 22 23 if(!(p = alloc_contig(size, 0, NULL))) 24 panic("out of memory: %d", size); 25 26 return p; 27 } 28 29 /*===========================================================================* 30 * flt_free * 31 *===========================================================================*/ 32 void flt_free(char *buf, size_t size, const char *sbuf) 33 { 34 /* Free a buffer previously allocated with flt_malloc(). 35 */ 36 37 if(buf != sbuf) 38 free_contig(buf, size); 39 } 40 41 /*===========================================================================* 42 * flt_alarm * 43 *===========================================================================*/ 44 clock_t flt_alarm(clock_t dt) 45 { 46 int r; 47 48 if((int) dt < 0) 49 return next_alarm; 50 51 r = sys_setalarm(dt, 0); 52 53 if(r != OK) 54 panic("sys_setalarm failed: %d", r); 55 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 next_alarm = getticks() + dt; 64 } 65 66 return next_alarm; 67 } 68 69