aewl

diff 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 diff
     1.1 --- a/util.c	Mon Jul 10 18:35:39 2006 +0200
     1.2 +++ b/util.c	Mon Jul 10 19:46:24 2006 +0200
     1.3 @@ -6,6 +6,7 @@
     1.4  #include <stdarg.h>
     1.5  #include <stdio.h>
     1.6  #include <stdlib.h>
     1.7 +#include <string.h>
     1.8  
     1.9  void
    1.10  error(char *errstr, ...) {
    1.11 @@ -16,3 +17,61 @@
    1.12  	exit(1);
    1.13  }
    1.14  
    1.15 +static void
    1.16 +bad_malloc(unsigned int size)
    1.17 +{
    1.18 +	fprintf(stderr, "fatal: could not malloc() %d bytes\n",
    1.19 +			(int) size);
    1.20 +	exit(1);
    1.21 +}
    1.22 +
    1.23 +void *
    1.24 +emallocz(unsigned int size)
    1.25 +{
    1.26 +	void *res = calloc(1, size);
    1.27 +	if(!res)
    1.28 +		bad_malloc(size);
    1.29 +	return res;
    1.30 +}
    1.31 +
    1.32 +void *
    1.33 +emalloc(unsigned int size)
    1.34 +{
    1.35 +	void *res = malloc(size);
    1.36 +	if(!res)
    1.37 +		bad_malloc(size);
    1.38 +	return res;
    1.39 +}
    1.40 +
    1.41 +void *
    1.42 +erealloc(void *ptr, unsigned int size)
    1.43 +{
    1.44 +	void *res = realloc(ptr, size);
    1.45 +	if(!res)
    1.46 +		bad_malloc(size);
    1.47 +	return res;
    1.48 +}
    1.49 +
    1.50 +char *
    1.51 +estrdup(const char *str)
    1.52 +{
    1.53 +	void *res = strdup(str);
    1.54 +	if(!res)
    1.55 +		bad_malloc(strlen(str));
    1.56 +	return res;
    1.57 +}
    1.58 +
    1.59 +void
    1.60 +failed_assert(char *a, char *file, int line)
    1.61 +{
    1.62 +	fprintf(stderr, "Assertion \"%s\" failed at %s:%d\n", a, file, line);
    1.63 +	abort();
    1.64 +}
    1.65 +
    1.66 +void
    1.67 +swap(void **p1, void **p2)
    1.68 +{
    1.69 +	void *tmp = *p1;
    1.70 +	*p1 = *p2;
    1.71 +	*p2 = tmp;
    1.72 +}