1 /*
2 * Wrappers around standard functions that use the heap.
3 *
4 * Copyright (c) 2015 Riverbank Computing Limited <info@riverbankcomputing.com>
5 *
6 * This file is part of SIP.
7 *
8 * This copy of SIP is licensed for use under the terms of the SIP License
9 * Agreement. See the file LICENSE for more details.
10 *
11 * This copy of SIP may also used under the terms of the GNU General Public
12 * License v2 or v3 as published by the Free Software Foundation which can be
13 * found in the files LICENSE-GPL2 and LICENSE-GPL3 included in this package.
14 *
15 * SIP is supplied WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 */
18
19
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <string.h>
23 #include <sys/types.h>
24
25 #include "sip.h"
26
27
28 static void nomem(void);
29
30
31 /*
32 * Wrap malloc() and handle any errors.
33 */
sipMalloc(size_t n)34 void *sipMalloc(size_t n)
35 {
36 void *h;
37
38 if ((h = malloc(n)) == NULL)
39 nomem();
40
41 memset(h, 0, n);
42
43 return h;
44 }
45
46
47 /*
48 * Wrap calloc() and handle any errors.
49 */
sipCalloc(size_t nr,size_t n)50 void *sipCalloc(size_t nr, size_t n)
51 {
52 void *h;
53
54 if ((h = calloc(nr, n)) == NULL)
55 nomem();
56
57 return h;
58 }
59
60
61 /*
62 * Wrap strdup() and handle any errors.
63 */
sipStrdup(const char * s)64 char *sipStrdup(const char *s)
65 {
66 char *h;
67
68 if ((h = strdup(s)) == NULL)
69 nomem();
70
71 return h;
72 }
73
74
75 /*
76 * Return a string on the heap which is the concatenation of all the arguments.
77 */
concat(const char * s,...)78 char *concat(const char *s, ...)
79 {
80 const char *sp;
81 char *new;
82 size_t len;
83 va_list ap;
84
85 /* Find the length of the final string. */
86
87 len = 1;
88 va_start(ap,s);
89
90 for (sp = s; sp != NULL; sp = va_arg(ap, const char *))
91 len += strlen(sp);
92
93 va_end(ap);
94
95 /* Create the new string. */
96
97 new = sipMalloc(len);
98 *new = '\0';
99
100 va_start(ap,s);
101
102 for (sp = s; sp != NULL; sp = va_arg(ap, const char *))
103 strcat(new,sp);
104
105 va_end(ap);
106
107 return new;
108 }
109
110
111 /*
112 * Append a string to another that is on the heap.
113 */
114
append(char ** s,const char * new)115 void append(char **s, const char *new)
116 {
117 if ((*s = realloc(*s,strlen(*s) + strlen(new) + 1)) == NULL)
118 nomem();
119
120 strcat(*s,new);
121 }
122
123
124 /*
125 * Display a standard error message when the heap is exhausted.
126 */
127
nomem(void)128 static void nomem(void)
129 {
130 fatal("Unable to allocate memory on the heap\n");
131 }
132