diff options
author | William Joye <wjoye@cfa.harvard.edu> | 2017-10-26 16:45:12 (GMT) |
---|---|---|
committer | William Joye <wjoye@cfa.harvard.edu> | 2017-10-26 16:45:12 (GMT) |
commit | 6054f36bb658916bd231b9002efb94444e2871c8 (patch) | |
tree | a815500ec9bf0e100f4075a825213605c382def0 /xalloc.c | |
download | blt-6054f36bb658916bd231b9002efb94444e2871c8.zip blt-6054f36bb658916bd231b9002efb94444e2871c8.tar.gz blt-6054f36bb658916bd231b9002efb94444e2871c8.tar.bz2 |
Squashed 'xpa/' content from commit d6a54301
git-subtree-dir: xpa
git-subtree-split: d6a543018b46c5a6c47b5ff91e0e131549595ca6
Diffstat (limited to 'xalloc.c')
-rw-r--r-- | xalloc.c | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/xalloc.c b/xalloc.c new file mode 100644 index 0000000..18234db --- /dev/null +++ b/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; +} |