]> mj.ucw.cz Git - osdd.git/blob - util.c
Added forgotten include of util.h
[osdd.git] / util.c
1 /*
2  *      On-screen Display Daemon -- Utility Functions
3  *
4  *      (c) 2010 Martin Mares <mj@ucw.cz>
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <stdarg.h>
10 #include <sys/time.h>
11
12 #include "osd.h"
13 #include "util.h"
14
15 void __attribute__((noreturn)) __attribute__((format(printf,1,2)))
16 die(char *fmt, ...)
17 {
18   va_list args;
19   va_start(args, fmt);
20   fputs("osdd: ", stderr);
21   vfprintf(stderr, fmt, args);
22   fputc('\n', stderr);
23   exit(1);
24 }
25
26 void *
27 xmalloc(int size)
28 {
29   void *p = malloc(size);
30   if (!p)
31     die("Failed to allocate %d bytes of memory", size);
32   return p;
33 }
34
35 void *
36 xrealloc(void *ptr, int size)
37 {
38   void *p = realloc(ptr, size);
39   if (!p)
40     die("Failed to re-allocate %d bytes of memory", size);
41   return p;
42 }
43
44 timestamp_t
45 get_current_time(void)
46 {
47   struct timeval tv;
48   gettimeofday(&tv, NULL);
49   return (timestamp_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
50 }