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