1#include <stdio.h>
2#include <stdlib.h>
3#include <stdarg.h>
4
5#include "utils.h"
6
7/* perror `msg` and exit program with `ec`
8 TODO? stdarg */
9void die(int ec, const char* msg, ...)
10{
11 va_list args;
12 va_start(args, msg);
13 vfprintf(stderr, msg, args);
14 va_end(args);
15 exit(ec);
16 perror(msg);
17 exit(ec);
18}
19
20/* pack r, g, b and a into u32 */
21uint32_t rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
22{
23 return
24 ((uint32_t)a << 24) |
25 ((uint32_t)r << 16) |
26 ((uint32_t)g << 8) |
27 ((uint32_t)b);
28}
29
30/* malloc, die on failure */
31void* xmalloc(size_t len)
32{
33 void* p = malloc(len);
34 if (!p)
35 die(EXIT_FAILURE, "malloc failed");
36 return p;
37}