Line data Source code
1 : /**
2 : * @file gensvm_memory.c
3 : * @author G.J.J. van den Burg
4 : * @date 2016-05-01
5 : * @brief Utility functions for memory allocation
6 : *
7 : * @copyright
8 : Copyright 2016, G.J.J. van den Burg.
9 :
10 : This file is part of GenSVM.
11 :
12 : GenSVM is free software: you can redistribute it and/or modify
13 : it under the terms of the GNU General Public License as published by
14 : the Free Software Foundation, either version 3 of the License, or
15 : (at your option) any later version.
16 :
17 : GenSVM is distributed in the hope that it will be useful,
18 : but WITHOUT ANY WARRANTY; without even the implied warranty of
19 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 : GNU General Public License for more details.
21 :
22 : You should have received a copy of the GNU General Public License
23 : along with GenSVM. If not, see <http://www.gnu.org/licenses/>.
24 :
25 : */
26 :
27 : #include "gensvm_globals.h" // imports gensvm_memory.h
28 :
29 : /**
30 : * @brief Wrapper for calloc() which warns when allocation fails
31 : *
32 : * @details
33 : * This is a wrapper function around calloc from <stdlib.h>. It tries to
34 : * allocate the requested memory and checks if the memory was correctly
35 : * allocated. If not, an error is printed using err(), which describes the
36 : * file and linenumber and size failed to allocate. After this, the program
37 : * exits. See also the defines in gensvm_memory.h.
38 : *
39 : * @note
40 : * This function should not be used directly. Calloc() should be used.
41 : *
42 : * @param[in] file filename used for error printing
43 : * @param[in] line line number used for error printing
44 : * @param[in] size the size to allocate
45 : * @param[in] typesize the size of the type to allocate
46 : * @return the pointer to the memory allocated
47 : */
48 1086 : void *mycalloc(const char *file, int line, unsigned long size,
49 : size_t typesize)
50 : {
51 1086 : void *ptr = calloc(size, typesize);
52 :
53 1086 : if (!ptr) {
54 : // LCOV_EXCL_START
55 : fprintf(stderr, "[GenSVM Error]: Couldn't allocate memory: "
56 : "%lu bytes (%s:%d)\n", size, file, line);
57 : exit(EXIT_FAILURE);
58 : // LCOV_EXCL_STOP
59 : }
60 1086 : return ptr;
61 : }
62 :
63 : /**
64 : * @brief Wrapper for malloc() which warns when allocation fails
65 : *
66 : * @details
67 : * This is a wrapper function around malloc from <stdlib.h>. It tries to
68 : * allocate the requested memory and checks if the memory was correctly
69 : * allocated. If not, an error is printed using err(), which describes the
70 : * file and linenumber and size failed to allocate. After this, the program
71 : * exits. See also the defines in gensvm_memory.h.
72 : *
73 : * @note
74 : * This function should not be used directly. Malloc() should be used.
75 : *
76 : * @param[in] file filename used for error printing
77 : * @param[in] line line number used for error printing
78 : * @param[in] size the size to allocate
79 : * @return the pointer to the memory allocated
80 : */
81 333 : void *mymalloc(const char *file, int line, unsigned long size)
82 : {
83 333 : void *ptr = malloc(size);
84 333 : if (!ptr) {
85 : // LCOV_EXCL_START
86 : fprintf(stderr, "[GenSVM Error]: Couldn't allocate memory: "
87 : "%lu bytes (%s:%d)\n", size, file, line);
88 : exit(EXIT_FAILURE);
89 : // LCOV_EXCL_STOP
90 : }
91 333 : return ptr;
92 : }
93 :
94 : /**
95 : * @brief Wrapper for realloc() which warns when allocation fails
96 : *
97 : * @details
98 : * This is a wrapper function around realloc from <stdlib.h>. It tries to
99 : * reallocate the requested memory and checks if the memory was correctly
100 : * reallocated. If not, an error is printed using err(), which describes the
101 : * file and linenumber and size failed to reallocate. After this, the program
102 : * exits. See also the defines in gensvm_memory.h.
103 : *
104 : * @note
105 : * This function should not be used directly. Realloc() should be used.
106 : *
107 : * @param[in] file filename used for error printing
108 : * @param[in] line line number used for error printing
109 : * @param[in] size the size to allocate
110 : * @param[in] var the pointer to the memory to reallocate
111 : * @return the pointer to the memory reallocated
112 : */
113 20 : void *myrealloc(const char *file, int line, unsigned long size, void *var)
114 : {
115 20 : void *ptr = realloc(var, size);
116 20 : if (!ptr) {
117 : // LCOV_EXCL_START
118 : fprintf(stderr, "[GenSVM Error]: Couldn't reallocate memory: "
119 : "%lu bytes (%s:%d)\n", size, file, line);
120 : exit(EXIT_FAILURE);
121 : // LCOV_EXCL_STOP
122 : }
123 20 : return ptr;
124 : }
|