From 48f326b8af7f33f46f9df5bfd802281056e79afa Mon Sep 17 00:00:00 2001 From: Michael Beck Date: Fri, 3 Mar 2006 12:53:22 +0000 Subject: [PATCH] replaced old panic module by newer error handling module [r7385] --- ir/adt/xmalloc.c | 28 +++++++++++----------------- ir/adt/xmalloc.h | 1 - ir/common/error.c | 32 ++++++++++++++++++++++++++++++++ ir/common/error.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ ir/common/panic.c | 36 ------------------------------------ ir/common/panic.h | 28 ---------------------------- 6 files changed, 89 insertions(+), 82 deletions(-) create mode 100644 ir/common/error.c create mode 100644 ir/common/error.h delete mode 100644 ir/common/panic.c delete mode 100644 ir/common/panic.h diff --git a/ir/adt/xmalloc.c b/ir/adt/xmalloc.c index 7680c4be6..07252a60d 100644 --- a/ir/adt/xmalloc.c +++ b/ir/adt/xmalloc.c @@ -31,11 +31,14 @@ #endif #include "xmalloc.h" -#include "panic.h" +#include "error.h" -void * -xmalloc(size_t size) { - void *res = malloc (size); +static NORETURN xnomem(void) { + panic("out of memory"); +} + +void *xmalloc(size_t size) { + void *res = malloc(size); if (!res) xnomem(); return res; @@ -48,24 +51,15 @@ void *xcalloc(size_t num, size_t size) { return res; } -void * -xrealloc(void *ptr, size_t size) { +void *xrealloc(void *ptr, size_t size) { /* ANSI blesses realloc (0, x) but SunOS chokes on it */ void *res = ptr ? realloc (ptr, size) : malloc (size); - if (!res) xnomem (); + if (!res) xnomem(); return res; } - -char * -xstrdup(const char *str) { +char *xstrdup(const char *str) { size_t len = strlen (str) + 1; - return memcpy ((xmalloc) (len), str, len); -} - - -void -xnomem(void) { - panic("out of memory"); + return memcpy((xmalloc) (len), str, len); } diff --git a/ir/adt/xmalloc.h b/ir/adt/xmalloc.h index 9d4048c1d..040040887 100644 --- a/ir/adt/xmalloc.h +++ b/ir/adt/xmalloc.h @@ -22,7 +22,6 @@ void *xmalloc(size_t size); void *xcalloc(size_t num, size_t size); void *xrealloc(void *ptr, size_t size); char *xstrdup(const char *str); -void xnomem(void); void free(void *ptr); #define xfree(ptr) free(ptr) diff --git a/ir/common/error.c b/ir/common/error.c new file mode 100644 index 000000000..0cc58df50 --- /dev/null +++ b/ir/common/error.c @@ -0,0 +1,32 @@ +/* + * Project: libFIRM + * File name: ir/common/error.c + * Purpose: Error handling for libFirm + * Author: Michael Beck + * Modified by: + * Created: + * CVS-ID: $Id$ + * Copyright: (C) 1998-2006 Universität Karlsruhe + * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE. + */ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include + +#include +#include +#include "error.h" + +NORETURN panic(const char *fmt, ...) +{ + va_list ap; + + fputs("libFirm panic: ", stderr); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + putc('\n', stderr); + exit(EXIT_FAILURE); +} diff --git a/ir/common/error.h b/ir/common/error.h new file mode 100644 index 000000000..c075da240 --- /dev/null +++ b/ir/common/error.h @@ -0,0 +1,46 @@ +/* + * Project: libFIRM + * File name: ir/common/error.h + * Purpose: Error handling for libFirm + * Author: Michael Beck + * Modified by: + * Created: + * CVS-ID: $Id$ + * Copyright: (C) 1998-2006 Universität Karlsruhe + * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE. + */ +#ifndef _ERROR_H_ +#define _ERROR_H_ + +/** + * @file error.h + * + * Error handling for libFirm. + * + * @author Michael Beck + */ + +/* define a NORETURN attribute */ +#ifndef NORETURN +# if defined(__GNUC__) +# if __GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 70) +# define NORETURN void __attribute__ ((noreturn)) +# endif /* __GNUC__ >= 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 70) */ +# endif /* defined(__GNUC__) */ + +# if defined(_MSC_VER) +# define NORETURN void __declspec(noreturn) +# endif /* defined(_MSC_VER) */ + +/* If not set above, use "void" for DOES_NOT_RETURN. */ +# ifndef NORETURN +# define NORETURN void +# endif /* ifndef NORETURN */ +#endif /* ifndef NORETURN */ + +/** + * Prints a panic message to stderr and exits. + */ +NORETURN panic(const char *fmt, ...); + +# endif /*_ERROR_H_ */ diff --git a/ir/common/panic.c b/ir/common/panic.c deleted file mode 100644 index a316e2f79..000000000 --- a/ir/common/panic.c +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Project: libFIRM - * File name: ir/common/panic.c - * Purpose: - * Author: Martin Trapp, Christian Schaefer - * Modified by: - * Created: - * CVS-ID: $Id$ - * Copyright: (c) 1998-2003 Universität Karlsruhe - * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE. - */ - - -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - -# include - -# include -# include -# include "panic.h" - - -void -panic (const char *fmt, ...) -{ - va_list ap; - - fputs ("(panic) ", stderr); - va_start (ap, fmt); - vfprintf (stderr, fmt, ap); - va_end (ap); - putc ('\n', stderr); - exit (1); -} diff --git a/ir/common/panic.h b/ir/common/panic.h deleted file mode 100644 index 63b68d35a..000000000 --- a/ir/common/panic.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Project: libFIRM - * File name: ir/common/panic.h - * Purpose: - * Author: Martin Trapp, Christian Schaefer - * Modified by: - * Created: - * CVS-ID: $Id$ - * Copyright: (c) 1998-2003 Universität Karlsruhe - * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE. - */ - -/** - * @file panic.h - * - * @author Martin Trapp, Christian Schaefer - */ - - -# ifndef _PANIC_H_ -# define _PANIC_H_ - -/** - * Prints a panic message to stderr and exits. - */ -void panic (const char *fmt, ...); - -# endif /*_PANIC_H_ */ -- 2.20.1