fwcutter/make: Avoid _DEFAULT_SOURCE warning
[b43-tools.git] / disassembler / util.c
1 /*
2  *   Copyright (C) 2006  Michael Buesch <m@bues.ch>
3  *
4  *   This program is free software; you can redistribute it and/or modify
5  *   it under the terms of the GNU General Public License version 2
6  *   as published by the Free Software Foundation.
7  *
8  *   This program is distributed in the hope that it will be useful,
9  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *   GNU General Public License for more details.
12  */
13
14 #include "util.h"
15
16 #include <stdio.h>
17 #include <string.h>
18
19
20 void dump(const char *data,
21           size_t size,
22           const char *description)
23 {
24         size_t i;
25         char c;
26
27         fprintf(stderr, "Data dump (%s, %zd bytes):",
28                 description, size);
29         for (i = 0; i < size; i++) {
30                 c = data[i];
31                 if (i % 8 == 0) {
32                         fprintf(stderr, "\n0x%08zx:  0x%02x, ",
33                                 i, c & 0xff);
34                 } else
35                         fprintf(stderr, "0x%02x, ", c & 0xff);
36         }
37         fprintf(stderr, "\n");
38 }
39
40 void * xmalloc(size_t size)
41 {
42         void *p;
43
44         p = malloc(size);
45         if (!p) {
46                 fprintf(stderr, "Out of memory\n");
47                 exit(1);
48         }
49         memset(p, 0, size);
50
51         return p;
52 }
53
54 char * xstrdup(const char *str)
55 {
56         char *c;
57
58         c = strdup(str);
59         if (!c) {
60                 fprintf(stderr, "Out of memory\n");
61                 exit(1);
62         }
63
64         return c;
65 }
66
67 void * xrealloc(void *in, size_t size)
68 {
69         void *out;
70
71         out = realloc(in, size);
72         if (!out) {
73                 fprintf(stderr, "Out of memory\n");
74                 exit(1);
75         }
76
77         return out;
78 }