17
|
1 /*
|
|
2 util.c
|
|
3 Copyright © 2008 William Astle
|
|
4
|
|
5 This file is part of LWASM.
|
|
6
|
|
7 LWASM is free software: you can redistribute it and/or modify it under the
|
|
8 terms of the GNU General Public License as published by the Free Software
|
|
9 Foundation, either version 3 of the License, or (at your option) any later
|
|
10 version.
|
|
11
|
|
12 This program is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
15 more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License along with
|
|
18 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
19 */
|
|
20
|
|
21 /*
|
|
22 Utility functions
|
|
23 */
|
|
24
|
|
25 #define __util_c_seen__
|
212
|
26 #include <config.h>
|
17
|
27 #include <malloc.h>
|
|
28 #include <stdio.h>
|
|
29 #include <stdlib.h>
|
|
30 #include <string.h>
|
|
31
|
|
32 #include "util.h"
|
|
33
|
|
34 void *lwasm_alloc(int size)
|
|
35 {
|
|
36 void *ptr;
|
|
37
|
|
38 ptr = malloc(size);
|
|
39 if (!ptr)
|
|
40 {
|
|
41 // bail out; memory allocation error
|
|
42 fprintf(stderr, "Memory allocation error\n");
|
|
43 exit(1);
|
|
44 }
|
|
45 return ptr;
|
|
46 }
|
|
47
|
20
|
48 void *lwasm_realloc(void *optr, int size)
|
|
49 {
|
|
50 void *ptr;
|
|
51
|
|
52 if (size == 0)
|
|
53 {
|
|
54 lwasm_free(optr);
|
|
55 return;
|
|
56 }
|
|
57
|
|
58 ptr = realloc(optr, size);
|
|
59 if (!ptr)
|
|
60 {
|
|
61 fprintf(stderr, "lwasm_realloc(): memory allocation error\n");
|
|
62 exit(1);
|
|
63 }
|
|
64 }
|
|
65
|
17
|
66 void lwasm_free(void *ptr)
|
|
67 {
|
|
68 if (ptr)
|
|
69 free(ptr);
|
|
70 }
|
|
71
|
|
72 char *lwasm_strdup(const char *s)
|
|
73 {
|
|
74 char *d;
|
|
75
|
|
76 d = strdup(s);
|
|
77 if (!d)
|
|
78 {
|
|
79 fprintf(stderr, "lwasm_strdup(): memory allocation error\n");
|
|
80 exit(1);
|
|
81 }
|
|
82
|
|
83 return d;
|
|
84 }
|