Changeset 5266


Ignore:
Timestamp:
May 2, 2008, 11:30:37 AM (16 years ago)
Author:
jack
Message:

added xstrdup

Location:
anuga_work/development/anugavis/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • anuga_work/development/anugavis/src/xmalloc.c

    r5261 r5266  
    66#  include <stdlib.h>
    77#endif
     8#ifdef HAVE_STRING_H
     9#  include <string.h>
     10#endif
    811#include "error.h"
    912#include "xmalloc.h"
    1013
    11 void *xmalloc(size_t size, const char *func){
     14void *xmalloc(size_t size, const char *message){
    1215  void *mem;
    1316  if((mem = malloc(size)) == NULL)
    14     AnugaVis_SetError("Failed to malloc %d bytes in %s", size, func);
     17    AnugaVis_SetError("%s: Failed to malloc %d bytes", message, size);
    1518  return mem;
    1619}
     20
     21#ifndef HAVE_STRDUP
     22char *strdup(const char *s){
     23  char *str;
     24  if((str = malloc(strlen(s) + 1)) != NULL)
     25    strcpy(str, s);
     26  return str;
     27}
     28#endif
     29
     30char *xstrdup(const char *s, const char *message){
     31  char *str;
     32  if((str = strdup(s)) == NULL)
     33    AnugaVis_SetError("%s: Failed to strdup \"%s\"", message, s);
     34  return str;
     35}
  • anuga_work/development/anugavis/src/xmalloc.h

    r5261 r5266  
    22#define XMALLOC_H
    33
    4 /* Attempt a malloc. If it fails, set an error message and return NULL.
     4/* For a given func, xfunc behaves like func but sets an error message
     5 * using AnugaVis_SetError().
    56 */
    6 extern void *xmalloc(size_t size, const char *func);
     7extern void *xmalloc(size_t size, const char *message);
     8extern char *xstrdup(const char *s, const char *message);
    79
    810#endif
Note: See TracChangeset for help on using the changeset viewer.