]> git.ipfire.org Git - thirdparty/chrony.git/blob - memory.c
cmdmon: save NTS cookies and server keys on dump command
[thirdparty/chrony.git] / memory.c
1 /*
2 chronyd/chronyc - Programs for keeping computer clocks accurate.
3
4 **********************************************************************
5 * Copyright (C) Miroslav Lichvar 2014, 2017
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 **********************************************************************
21
22 =======================================================================
23
24 Utility functions for memory allocation.
25
26 */
27
28 #include "config.h"
29
30 #include "logging.h"
31 #include "memory.h"
32
33 void *
34 Malloc(size_t size)
35 {
36 void *r;
37
38 r = malloc(size);
39 if (!r && size)
40 LOG_FATAL("Could not allocate memory");
41
42 return r;
43 }
44
45 void *
46 Realloc(void *ptr, size_t size)
47 {
48 void *r;
49
50 r = realloc(ptr, size);
51 if (!r && size)
52 LOG_FATAL("Could not allocate memory");
53
54 return r;
55 }
56
57 static size_t
58 get_array_size(size_t nmemb, size_t size)
59 {
60 size_t array_size;
61
62 array_size = nmemb * size;
63
64 /* Check for overflow */
65 if (nmemb > 0 && array_size / nmemb != size)
66 LOG_FATAL("Could not allocate memory");
67
68 return array_size;
69 }
70
71 void *
72 Malloc2(size_t nmemb, size_t size)
73 {
74 return Malloc(get_array_size(nmemb, size));
75 }
76
77 void *
78 Realloc2(void *ptr, size_t nmemb, size_t size)
79 {
80 return Realloc(ptr, get_array_size(nmemb, size));
81 }
82
83 char *
84 Strdup(const char *s)
85 {
86 void *r;
87
88 r = strdup(s);
89 if (!r)
90 LOG_FATAL("Could not allocate memory");
91
92 return r;
93 }