annotate src/util.c @ 212:bae1e3ecdce1

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