aewl

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