]> mj.ucw.cz Git - osdd.git/blob - util.c
util.c: Added xrealloc()
[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
14 void __attribute__((noreturn)) __attribute__((format(printf,1,2)))
15 die(char *fmt, ...)
16 {
17   va_list args;
18   va_start(args, fmt);
19   fputs("osdd: ", stderr);
20   vfprintf(stderr, fmt, args);
21   fputc('\n', stderr);
22   exit(1);
23 }
24
25 void *
26 xmalloc(int size)
27 {
28   void *p = malloc(size);
29   if (!p)
30     die("Failed to allocate %d bytes of memory", size);
31   return p;
32 }
33
34 void *
35 xrealloc(void *ptr, int size)
36 {
37   void *p = realloc(ptr, size);
38   if (!p)
39     die("Failed to re-allocate %d bytes of memory", size);
40   return p;
41 }
42
43 timestamp_t
44 get_current_time(void)
45 {
46   struct timeval tv;
47   gettimeofday(&tv, NULL);
48   return (timestamp_t) tv.tv_sec * 1000 + tv.tv_usec / 1000;
49 }