aboutsummaryrefslogtreecommitdiff
path: root/src/gensvm_memory.c
blob: 3c149fec900408f42614a8e7a51652ae2b3db638 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
/**
 * @file gensvm_memory.c
 * @author G.J.J. van den Burg
 * @date 2016-05-01
 * @brief Utility functions for memory allocation
 *
 * @copyright
 Copyright 2016, G.J.J. van den Burg.

 This file is part of GenSVM.

 GenSVM 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 3 of the License, or
 (at your option) any later version.

 GenSVM 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 GenSVM. If not, see <http://www.gnu.org/licenses/>.

 */

#include "gensvm_globals.h" // imports gensvm_memory.h

/**
 * @brief Wrapper for calloc() which warns when allocation fails
 *
 * @details
 * This is a wrapper function around calloc from <stdlib.h>. It tries to
 * allocate the requested memory and checks if the memory was correctly
 * allocated. If not, an error is printed using err(), which describes the
 * file and linenumber and size failed to allocate. After this, the program
 * exits. See also the defines in gensvm_memory.h.
 *
 * @note
 * This function should not be used directly. Calloc() should be used.
 *
 * @param[in] 	file 		filename used for error printing
 * @param[in] 	line 		line number used for error printing
 * @param[in] 	size 		the size to allocate
 * @param[in] 	typesize 	the size of the type to allocate
 * @return 			the pointer to the memory allocated
 */
void *mycalloc(const char *file, int line, unsigned long size,
	       	size_t typesize)
{
	void *ptr = calloc(size, typesize);

	if (!ptr) {
		// LCOV_EXCL_START
		fprintf(stderr, "[GenSVM Error]: Couldn't allocate memory: "
				"%lu bytes (%s:%d)\n", size, file, line);
		exit(EXIT_FAILURE);
		// LCOV_EXCL_STOP
	}
	return ptr;
}

/**
 * @brief Wrapper for malloc() which warns when allocation fails
 *
 * @details
 * This is a wrapper function around malloc from <stdlib.h>. It tries to
 * allocate the requested memory and checks if the memory was correctly
 * allocated. If not, an error is printed using err(), which describes the
 * file and linenumber and size failed to allocate. After this, the program
 * exits. See also the defines in gensvm_memory.h.
 *
 * @note
 * This function should not be used directly. Malloc() should be used.
 *
 * @param[in] 	file 		filename used for error printing
 * @param[in] 	line 		line number used for error printing
 * @param[in] 	size 		the size to allocate
 * @return 			the pointer to the memory allocated
 */
void *mymalloc(const char *file, int line, unsigned long size)
{
	void *ptr = malloc(size);
	if (!ptr) {
		// LCOV_EXCL_START
		fprintf(stderr, "[GenSVM Error]: Couldn't allocate memory: "
				"%lu bytes (%s:%d)\n", size, file, line);
		exit(EXIT_FAILURE);
		// LCOV_EXCL_STOP
	}
	return ptr;
}

/**
 * @brief Wrapper for realloc() which warns when allocation fails
 *
 * @details
 * This is a wrapper function around realloc from <stdlib.h>. It tries to
 * reallocate the requested memory and checks if the memory was correctly
 * reallocated. If not, an error is printed using err(), which describes the
 * file and linenumber and size failed to reallocate. After this, the program
 * exits. See also the defines in gensvm_memory.h.
 *
 * @note
 * This function should not be used directly. Realloc() should be used.
 *
 * @param[in] 	file 		filename used for error printing
 * @param[in] 	line 		line number used for error printing
 * @param[in] 	size 		the size to allocate
 * @param[in] 	var 		the pointer to the memory to reallocate
 * @return 			the pointer to the memory reallocated
 */
void *myrealloc(const char *file, int line, unsigned long size, void *var)
{
	void *ptr = realloc(var, size);
	if (!ptr) {
		// LCOV_EXCL_START
		fprintf(stderr, "[GenSVM Error]: Couldn't reallocate memory: "
				"%lu bytes (%s:%d)\n", size, file, line);
		exit(EXIT_FAILURE);
		// LCOV_EXCL_STOP
	}
	return ptr;
}