void *xcalloc(size_t num, size_t size) { void *ptr = calloc(num, size); if (!ptr) memfatal("calloc", num*size); return ptr; }
void *xmalloc(size_t size) { void *ptr = malloc(size); if (!ptr) memfatal("malloc", size); return ptr; }
char * xstrdup (const char *s) { #ifndef HAVE_STRDUP int l = strlen (s); char *s1 = malloc (l + 1); if (!s1) memfatal ("strdup"); memcpy (s1, s, l + 1); return s1; #else /* HAVE_STRDUP */ char *s1 = strdup (s); if (!s1) memfatal ("strdup"); return s1; #endif /* HAVE_STRDUP */ }
STATIC_IF_DEBUG void * xmalloc_real (size_t size) { void *ptr = malloc (size); if (!ptr) memfatal ("malloc"); return ptr; }
/* xmalloc, xrealloc and xstrdup exit the program if there is not enough memory. xstrdup also implements strdup on systems that do not have it. */ void * xmalloc (size_t size) { void *res; res = malloc (size); if (!res) memfatal ("malloc"); return res; }
STATIC_IF_DEBUG char * xstrdup_real (const char *s) { char *copy; #ifndef HAVE_STRDUP int l = strlen (s); copy = malloc (l + 1); if (!copy) memfatal ("strdup"); memcpy (copy, s, l + 1); #else /* HAVE_STRDUP */ copy = strdup (s); if (!copy) memfatal ("strdup"); #endif /* HAVE_STRDUP */ return copy; }
STATIC_IF_DEBUG void* checking_calloc(size_t num, size_t size) { void *ptr = calloc(num, size); if (!ptr) { memfatal(__FUNCTION__, "calloc", size); } return ptr; }
STATIC_IF_DEBUG void* checking_malloc(size_t size) { void *ptr = malloc(size); if (NULL == ptr) { memfatal(__FUNCTION__, "malloc", size); } return ptr; }
char * xstrdup(const char *s) { char *copy; copy = strdup(s); if (!copy) memfatal("strdup", 1 + strlen(s)); return copy; }
void *xmalloc0(size_t size) { /* Using calloc can be faster than malloc+memset because some calloc implementations know when they're dealing with zeroed-out memory from the system and can avoid unnecessary memset. */ void *ptr = calloc(1, size); if (!ptr) memfatal("calloc", size); return ptr; }
STATIC_IF_DEBUG char* checking_strdup(const char *s) { char *copy; copy = strdup(s); if (NULL == copy) { memfatal(__FUNCTION__, "strdup", 1 + strlen(s)); } return copy; }
void inittrap(void) { register int i; memfatal(); trap.caught = newof(0, int, sig_info.sigmax + 1, 0); for (i = 0; i < elementsof(signals); i++) if (signal(signals[i], interrupt) == SIG_IGN) signal(signals[i], SIG_IGN); }
void * xrealloc (void *obj, size_t size) { void *res; /* Not all Un*xes have the feature of realloc() that calling it with a NULL-pointer is the same as malloc(), but it is easy to simulate. */ if (obj) res = realloc (obj, size); else res = malloc (size); if (!res) memfatal ("realloc"); return res; }
STATIC_IF_DEBUG void * xrealloc_real (void *ptr, size_t newsize) { void *newptr; /* Not all Un*xes have the feature of realloc() that calling it with a NULL-pointer is the same as malloc(), but it is easy to simulate. */ if (ptr) newptr = realloc (ptr, newsize); else newptr = malloc (newsize); if (!newptr) memfatal ("realloc"); return newptr; }