annotate old-trunk/lwasm/old/util.c @ 348:11a95c6414b4

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