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