New utility function xmemdup().
[sliver-openvswitch.git] / lib / util.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  * 
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33
34 #include "util.h"
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 const char *program_name;
41
42 static void
43 out_of_memory(void) 
44 {
45     fatal(0, "virtual memory exhausted");
46 }
47
48 void *
49 xcalloc(size_t count, size_t size) 
50 {
51     void *p = count && size ? calloc(count, size) : malloc(1);
52     if (p == NULL) {
53         out_of_memory();
54     }
55     return p;
56 }
57
58 void *
59 xmalloc(size_t size) 
60 {
61     void *p = malloc(size ? size : 1);
62     if (p == NULL) {
63         out_of_memory();
64     }
65     return p;
66 }
67
68 void *
69 xrealloc(void *p, size_t size) 
70 {
71     p = realloc(p, size ? size : 1);
72     if (p == NULL) {
73         out_of_memory();
74     }
75     return p;
76 }
77
78 void *
79 xmemdup(const void *p_, size_t size)
80 {
81     void *p = xmalloc(size);
82     memcpy(p, p_, size);
83     return p;
84 }
85
86 char *
87 xstrdup(const char *s) 
88 {
89     return xmemdup(s, strlen(s) + 1);
90 }
91
92 char *
93 xasprintf(const char *format, ...)
94 {
95     va_list args;
96     size_t needed;
97     char *s;
98
99     va_start(args, format);
100     needed = vsnprintf(NULL, 0, format, args);
101     va_end(args);
102
103     s = xmalloc(needed + 1);
104
105     va_start(args, format);
106     vsnprintf(s, needed + 1, format, args);
107     va_end(args);
108
109     return s;
110 }
111
112 void fatal(int err_no, const char *format, ...)
113 {
114     va_list args;
115
116     fprintf(stderr, "%s: ", program_name);
117     va_start(args, format);
118     vfprintf(stderr, format, args);
119     va_end(args);
120     if (err_no != 0)
121         fprintf(stderr, " (%s)", strerror(err_no));
122     putc('\n', stderr);
123
124     exit(EXIT_FAILURE);
125 }
126
127 void error(int err_no, const char *format, ...)
128 {
129     va_list args;
130
131     fprintf(stderr, "%s: ", program_name);
132     va_start(args, format);
133     vfprintf(stderr, format, args);
134     va_end(args);
135     if (err_no != 0)
136         fprintf(stderr, " (%s)", strerror(err_no));
137     putc('\n', stderr);
138 }
139
140 void debug(int err_no, const char *format, ...)
141 {
142     va_list args;
143
144     fprintf(stderr, "%s: ", program_name);
145     va_start(args, format);
146     vfprintf(stderr, format, args);
147     va_end(args);
148     if (err_no != 0)
149         fprintf(stderr, " (%s)", strerror(err_no));
150     putc('\n', stderr);
151 }
152
153 /* Sets program_name based on 'argv0'.  Should be called at the beginning of
154  * main(), as "set_program_name(argv[0]);".  */
155 void set_program_name(const char *argv0)
156 {
157     const char *slash = strrchr(argv0, '/');
158     program_name = slash ? slash + 1 : argv0;
159 }
160
161 /* Writes the 'size' bytes in 'buf' to 'stream' as hex bytes arranged 16 per
162  * line.  Numeric offsets are also included, starting at 'ofs' for the first
163  * byte in 'buf'.  If 'ascii' is true then the corresponding ASCII characters
164  * are also rendered alongside. */
165 void
166 hex_dump(FILE *stream, const void *buf_, size_t size,
167          uintptr_t ofs, bool ascii)
168 {
169   const uint8_t *buf = buf_;
170   const size_t per_line = 16; /* Maximum bytes per line. */
171
172   while (size > 0)
173     {
174       size_t start, end, n;
175       size_t i;
176
177       /* Number of bytes on this line. */
178       start = ofs % per_line;
179       end = per_line;
180       if (end - start > size)
181         end = start + size;
182       n = end - start;
183
184       /* Print line. */
185       fprintf(stream, "%08jx  ", (uintmax_t) ROUND_DOWN(ofs, per_line));
186       for (i = 0; i < start; i++)
187         fprintf(stream, "   ");
188       for (; i < end; i++)
189         fprintf(stream, "%02hhx%c",
190                 buf[i - start], i == per_line / 2 - 1? '-' : ' ');
191       if (ascii)
192         {
193           for (; i < per_line; i++)
194             fprintf(stream, "   ");
195           fprintf(stream, "|");
196           for (i = 0; i < start; i++)
197             fprintf(stream, " ");
198           for (; i < end; i++) {
199               int c = buf[i - start];
200               putc(c >= 32 && c < 127 ? c : '.', stream);
201           }
202           for (; i < per_line; i++)
203             fprintf(stream, " ");
204           fprintf(stream, "|");
205         }
206       fprintf(stream, "\n");
207
208       ofs += n;
209       buf += n;
210       size -= n;
211     }
212 }