| 1 | /* |
| 2 | * Copyright (C) 2007 Moritz Grimm <[email protected]> |
| 3 | * |
| 4 | * Permission to use, copy, modify, and distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
| 17 | #ifdef HAVE_CONFIG_H |
| 18 | # include "config.h" |
| 19 | #endif |
| 20 | |
| 21 | #include "compat.h" |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "log.h" |
| 27 | #include "xalloc.h" |
| 28 | |
| 29 | void * |
| 30 | xmalloc_c(size_t size, const char *file, unsigned int line) |
| 31 | { |
| 32 | void *ret = malloc(size); |
| 33 | |
| 34 | if (NULL == ret) { |
| 35 | log_alert("%s[%u]: cannot allocate %zu bytes", |
| 36 | file, line, size); |
| 37 | exit(1); |
| 38 | } |
| 39 | |
| 40 | return (ret); |
| 41 | } |
| 42 | |
| 43 | void * |
| 44 | xcalloc_c(size_t nmemb, size_t size, const char *file, unsigned int line) |
| 45 | { |
| 46 | void *ret = calloc(nmemb, size); |
| 47 | |
| 48 | if (NULL == ret) { |
| 49 | log_alert("%s[%u]: cannot allocate %zu * %zu bytes", |
| 50 | file, line, nmemb, size); |
| 51 | exit(1); |
| 52 | } |
| 53 | |
| 54 | return (ret); |
| 55 | } |
| 56 | |
| 57 | void * |
| 58 | xreallocarray_c(void *ptr, size_t nmemb, size_t size, const char *file, |
| 59 | unsigned int line) |
| 60 | { |
| 61 | void *ret = reallocarray(ptr, nmemb, size); |
| 62 | |
| 63 | if (NULL == ret) { |
| 64 | log_alert("%s[%u]: cannot allocate %zu * %zu bytes", |
| 65 | file, line, nmemb, size); |
| 66 | exit(1); |
| 67 | } |
| 68 | |
| 69 | return (ret); |
| 70 | } |
| 71 | |
| 72 | char * |
| 73 | xstrdup_c(const char *str, const char *file, unsigned int line) |
| 74 | { |
| 75 | char *ret = strdup(str); |
| 76 | |
| 77 | if (NULL == ret) { |
| 78 | log_alert("%s[%u]: cannot allocate %zu bytes", |
| 79 | file, line, strlen(str) + 1); |
| 80 | exit(1); |
| 81 | } |
| 82 | |
| 83 | return (ret); |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | xfree_c(void *ptr, const char *file, unsigned int line) |
| 88 | { |
| 89 | (void)file; |
| 90 | (void)line; |
| 91 | free(ptr); |
| 92 | } |