diff options
author | William Joye <wjoye@cfa.harvard.edu> | 2016-10-17 15:27:21 (GMT) |
---|---|---|
committer | William Joye <wjoye@cfa.harvard.edu> | 2016-10-17 15:27:21 (GMT) |
commit | 912e82088edadbdbf95d594f93ddc9dd99a305f8 (patch) | |
tree | 9153dcd3bcf256fb26ebdcbfd8a1a7d9132430f1 /xpa/xalloc.c | |
parent | fecf4a80a5080aa65e7c2d717f96e86ad04ca46c (diff) | |
parent | d604b7940b14efb191a38ef22c3a38fa3adba4d0 (diff) | |
download | blt-912e82088edadbdbf95d594f93ddc9dd99a305f8.zip blt-912e82088edadbdbf95d594f93ddc9dd99a305f8.tar.gz blt-912e82088edadbdbf95d594f93ddc9dd99a305f8.tar.bz2 |
Merge commit 'd604b7940b14efb191a38ef22c3a38fa3adba4d0' as 'xpa'
Diffstat (limited to 'xpa/xalloc.c')
-rw-r--r-- | xpa/xalloc.c | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/xpa/xalloc.c b/xpa/xalloc.c new file mode 100644 index 0000000..18234db --- /dev/null +++ b/xpa/xalloc.c @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2004-2009 Smithsonian Astrophysical Observatory + */ + +/* + * + * xalloc -- safe memory allocation with error checking + * + */ + +/* this module is compiled within a funtools filter and must not require + the header files */ +#ifdef FILTER_PTYPE +#define ANSI_FUNC 1 +#else +#include <xalloc.h> +#endif + +#define XALLOC_ERROR "ERROR: can't allocate memory (xalloc)\n" + +#if XALLOC_SETJMP + +static jmp_buf *xalloc_envptr=NULL; + +#ifdef ANSI_FUNC +void xalloc_savejmp(jmp_buf *env) +#else +void xalloc_savejmp(env) + jmp_buf *env; +#endif +{ + xalloc_envptr = env; +} +#endif + + +#ifdef ANSI_FUNC +static void _xalloc_error(void) +#else +static void _xalloc_error() +#endif +{ + write(1, XALLOC_ERROR, strlen(XALLOC_ERROR)); +#if XALLOC_SETJMP + if( xalloc_envptr ) + longjmp(*xalloc_envptr, XALLOC_SETJMP); + else +#endif + exit(1); +} + +#ifdef ANSI_FUNC +void *xmalloc(size_t n) +#else +void *xmalloc(n) + size_t n; +#endif +{ + void *p; + + if( !(p = (void *)malloc(n)) ) + _xalloc_error(); + return p; +} + +#ifdef ANSI_FUNC +void *xcalloc (size_t n, size_t s) +#else +void *xcalloc (n, s) + size_t n, s; +#endif +{ + void *p; + + if( !(p = (void *)calloc(n, s)) ) + _xalloc_error(); + return p; +} + +#ifdef ANSI_FUNC +void *xrealloc (void *p, size_t n) +#else +void *xrealloc (p, n) + void *p; + size_t n; +#endif +{ + if( !p ) + return xmalloc(n); + if( !(p = (void *)realloc(p, n)) ) + _xalloc_error(); + return p; +} + +#ifdef ANSI_FUNC +void xfree (void *p) +#else +void xfree (p) + void *p; +#endif +{ + if( p ) + free(p); +} + +#ifdef ANSI_FUNC +char *xstrdup (char *s) +#else +char *xstrdup (s) + char *s; +#endif +{ + if( s ) + return((char *)strcpy((char *)xmalloc((size_t)strlen(s)+1), s)); + else + return NULL; +} |