1/* 2 * mem.c - memory allocation checkers 3 * Copyright (c) Clemens Ladisch <clemens@ladisch.de> 4 * 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19#include "aconfig.h" 20#include <stdio.h> 21#include <stdlib.h> 22#include <stdarg.h> 23#include <string.h> 24#include <errno.h> 25#include "die.h" 26#include "mem.h" 27 28static void check(void *p) 29{ 30 if (!p) 31 fatal_error("out of memory"); 32} 33 34void *ccalloc(size_t n, size_t size) 35{ 36 void *mem = calloc(n, size); 37 if (n && size) 38 check(mem); 39 return mem; 40} 41 42void *crealloc(void *ptr, size_t new_size) 43{ 44 ptr = realloc(ptr, new_size); 45 if (new_size) 46 check(ptr); 47 return ptr; 48} 49 50char *cstrdup(const char *s) 51{ 52 char *str = strdup(s); 53 check(str); 54 return str; 55} 56 57char *casprintf(const char *fmt, ...) 58{ 59 va_list ap; 60 char *str; 61 62 va_start(ap, fmt); 63 if (vasprintf(&str, fmt, ap) < 0) 64 check(NULL); 65 va_end(ap); 66 return str; 67} 68