File: safemalloc.c

package info (click to toggle)
fvwm 1:2.6.8-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 15,804 kB
  • sloc: ansic: 145,770; xml: 17,093; perl: 7,302; sh: 4,921; makefile: 1,094; yacc: 688; lex: 187; sed: 11
file content (112 lines) | stat: -rw-r--r-- 2,710 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* -*-c-*- */
/* This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see: <http://www.gnu.org/licenses/>
 */

#include "config.h"

#include <stdio.h>
#include "safemalloc.h"

static void alloc_failed(char *c, int length)
{
	fprintf(stderr, "%s of %d bytes failed. Exiting\n", c, length);
	exit(1);
}


/***********************************************************************
 *
 *  Procedure:
 *      safemalloc - mallocs specified space or exits if there's a
 *                   problem
 *
 ***********************************************************************/
char *safemalloc(int length)
{
	char *ptr;

	if(length <= 0)
		length = 1;

	ptr = malloc(length);
	if(ptr == (char *)0)
	{
		/* doesn't return */
		alloc_failed("malloc", length);
	}
	return ptr;
}


/***********************************************************************
 *
 *  Procedure:
 *      safecalloc - callocs specified space or exits if there's a
 *                   problem
 *
 ***********************************************************************/
char *safecalloc(int num, int length)
{
	char *ptr;

	ptr = calloc(num, length);
	if(ptr == (char *)0)
	{
		/* doesn't return */
		alloc_failed("calloc", length);
	}
	return ptr;
}


/***********************************************************************
 *
 *  Procedure:
 *      saferealloc - reallocs specified space or exits if there's a
 *                   problem
 *
 ***********************************************************************/
char *saferealloc(char *src, int length)
{
	char *ptr;

	if (src)
		ptr = realloc((void *)src, length);
	else
		ptr = malloc(length);
	if(ptr == (char *)0)
	{
		/* doesn't return */
		alloc_failed("realloc", length);
	}
	return ptr;
}

/***********************************************************************
 *
 *  Procedure:
 *      safestrdup - duplicates a string or exits if there's a
 *                   problem
 *
 ***********************************************************************/
char *safestrdup(const char *src)
{
	char *cpy = strdup(src);
	if (cpy == (char *)0)
	{
		/* doesn't return */
		alloc_failed("strdup", strlen(src) + (size_t)1);
	}
	return cpy;
}