Annotation of docbook2mdoc/xmalloc.c, Revision 1.1
1.1 ! schwarze 1: /* $Id$ */
! 2: /*
! 3: * Copyright (c) 2019 Ingo Schwarze <schwarze@openbsd.org>
! 4: *
! 5: * Permission to use, copy, modify, and distribute this software for any
! 6: * purpose with or without fee is hereby granted, provided that the above
! 7: * copyright notice and this permission notice appear in all copies.
! 8: *
! 9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
! 10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
! 11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
! 12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
! 13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
! 14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
! 15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
! 16: */
! 17: #include <sys/types.h>
! 18:
! 19: #include <stdarg.h>
! 20: #include <stdio.h>
! 21: #include <stdlib.h>
! 22: #include <string.h>
! 23:
! 24: #include "xmalloc.h"
! 25:
! 26: /*
! 27: * The implementation of the exiting allocator functions.
! 28: */
! 29:
! 30: void *
! 31: xcalloc(size_t nmemb, size_t size)
! 32: {
! 33: void *p;
! 34:
! 35: if ((p = calloc(nmemb, size)) == NULL) {
! 36: perror(NULL);
! 37: exit(6);
! 38: }
! 39: return p;
! 40: }
! 41:
! 42: void *
! 43: xrealloc(void *p, size_t size)
! 44: {
! 45: if ((p = realloc(p, size)) == NULL) {
! 46: perror(NULL);
! 47: exit(6);
! 48: }
! 49: return p;
! 50: }
! 51:
! 52: void *
! 53: xreallocarray(void *p, size_t nmemb, size_t size)
! 54: {
! 55: if ((p = reallocarray(p, nmemb, size)) == NULL) {
! 56: perror(NULL);
! 57: exit(6);
! 58: }
! 59: return p;
! 60: }
! 61:
! 62: char *
! 63: xstrdup(const char *in)
! 64: {
! 65: char *out;
! 66:
! 67: if ((out = strdup(in)) == NULL) {
! 68: perror(NULL);
! 69: exit(6);
! 70: }
! 71: return out;
! 72: }
! 73:
! 74: char *
! 75: xstrndup(const char *in, size_t maxlen)
! 76: {
! 77: char *out;
! 78:
! 79: if ((out = strndup(in, maxlen)) == NULL) {
! 80: perror(NULL);
! 81: exit(6);
! 82: }
! 83: return out;
! 84: }
! 85:
! 86: int
! 87: xasprintf(char **dest, const char *fmt, ...)
! 88: {
! 89: va_list ap;
! 90: int ret;
! 91:
! 92: va_start(ap, fmt);
! 93: ret = vasprintf(dest, fmt, ap);
! 94: va_end(ap);
! 95:
! 96: if (ret == -1) {
! 97: perror(NULL);
! 98: exit(6);
! 99: }
! 100: return ret;
! 101: }
CVSweb