aewl

view util.c @ 3:e969f3575b7a

several new changes, made gridmenu working
author Anselm R. Garbe <garbeam@wmii.de>
date Mon, 10 Jul 2006 19:46:24 +0200
parents a79188fe4a40
children e5018cae273f
line source
1 /*
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
4 */
6 #include <stdarg.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
11 void
12 error(char *errstr, ...) {
13 va_list ap;
14 va_start(ap, errstr);
15 vfprintf(stderr, errstr, ap);
16 va_end(ap);
17 exit(1);
18 }
20 static void
21 bad_malloc(unsigned int size)
22 {
23 fprintf(stderr, "fatal: could not malloc() %d bytes\n",
24 (int) size);
25 exit(1);
26 }
28 void *
29 emallocz(unsigned int size)
30 {
31 void *res = calloc(1, size);
32 if(!res)
33 bad_malloc(size);
34 return res;
35 }
37 void *
38 emalloc(unsigned int size)
39 {
40 void *res = malloc(size);
41 if(!res)
42 bad_malloc(size);
43 return res;
44 }
46 void *
47 erealloc(void *ptr, unsigned int size)
48 {
49 void *res = realloc(ptr, size);
50 if(!res)
51 bad_malloc(size);
52 return res;
53 }
55 char *
56 estrdup(const char *str)
57 {
58 void *res = strdup(str);
59 if(!res)
60 bad_malloc(strlen(str));
61 return res;
62 }
64 void
65 failed_assert(char *a, char *file, int line)
66 {
67 fprintf(stderr, "Assertion \"%s\" failed at %s:%d\n", a, file, line);
68 abort();
69 }
71 void
72 swap(void **p1, void **p2)
73 {
74 void *tmp = *p1;
75 *p1 = *p2;
76 *p2 = tmp;
77 }