339
|
1 /*
|
|
2 util.c
|
|
3 Copyright © 2009 William Astle
|
|
4
|
|
5 This file is part of LWAR.
|
|
6
|
|
7 LWAR 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__
|
|
26 #include <config.h>
|
|
27
|
|
28 #include <malloc.h>
|
|
29 #include <stdio.h>
|
|
30 #include <stdlib.h>
|
|
31 #include <string.h>
|
|
32
|
|
33 #include "util.h"
|
|
34
|
|
35 void *lw_malloc(int size)
|
|
36 {
|
|
37 void *ptr;
|
|
38
|
|
39 ptr = malloc(size);
|
|
40 if (!ptr)
|
|
41 {
|
|
42 // bail out; memory allocation error
|
|
43 fprintf(stderr, "lw_malloc(): Memory allocation error\n");
|
|
44 exit(1);
|
|
45 }
|
|
46 return ptr;
|
|
47 }
|
|
48
|
|
49 void *lw_realloc(void *optr, int size)
|
|
50 {
|
|
51 void *ptr;
|
|
52
|
|
53 if (size == 0)
|
|
54 {
|
|
55 lw_free(optr);
|
|
56 return;
|
|
57 }
|
|
58
|
|
59 ptr = realloc(optr, size);
|
|
60 if (!ptr)
|
|
61 {
|
|
62 fprintf(stderr, "lw_realloc(): memory allocation error\n");
|
|
63 exit(1);
|
|
64 }
|
|
65 }
|
|
66
|
|
67 void lw_free(void *ptr)
|
|
68 {
|
|
69 if (ptr)
|
|
70 free(ptr);
|
|
71 }
|
|
72
|
|
73 char *lw_strdup(const char *s)
|
|
74 {
|
|
75 char *d;
|
|
76
|
|
77 if (!s)
|
|
78 return NULL;
|
|
79
|
|
80 d = strdup(s);
|
|
81 if (!d)
|
|
82 {
|
|
83 fprintf(stderr, "lw_strdup(): memory allocation error\n");
|
|
84 exit(1);
|
|
85 }
|
|
86
|
|
87 return d;
|
|
88 }
|