Mercurial > hg-old > index.cgi
annotate lwlink/util.c @ 146:6c0a30278982
Made development version of LWASM be 2.1, not 3.0, because the next release will be an incremental feature release
author | lost |
---|---|
date | Thu, 29 Jan 2009 06:14:04 +0000 |
parents | 106c2fe3c9d9 |
children | bae1e3ecdce1 |
rev | line source |
---|---|
114
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
1 /* |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
2 util.c |
118 | 3 Copyright © 2009 William Astle |
114
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
4 |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
5 This file is part of LWLINK. |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
6 |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
7 LWLINK is free software: you can redistribute it and/or modify it under the |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
8 terms of the GNU General Public License as published by the Free Software |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
9 Foundation, either version 3 of the License, or (at your option) any later |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
10 version. |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
11 |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
12 This program is distributed in the hope that it will be useful, but WITHOUT |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
15 more details. |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
16 |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
17 You should have received a copy of the GNU General Public License along with |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
18 this program. If not, see <http://www.gnu.org/licenses/>. |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
19 */ |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
20 |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
21 /* |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
22 Utility functions |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
23 */ |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
24 |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
25 #define __util_c_seen__ |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
26 |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
27 #include <malloc.h> |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
28 #include <stdio.h> |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
29 #include <stdlib.h> |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
30 #include <string.h> |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
31 |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
32 #include "util.h" |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
33 |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
34 void *lw_malloc(int size) |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
35 { |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
36 void *ptr; |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
37 |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
38 ptr = malloc(size); |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
39 if (!ptr) |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
40 { |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
41 // bail out; memory allocation error |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
42 fprintf(stderr, "lw_malloc(): Memory allocation error\n"); |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
43 exit(1); |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
44 } |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
45 return ptr; |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
46 } |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
47 |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
48 void *lw_realloc(void *optr, int size) |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
49 { |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
50 void *ptr; |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
51 |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
52 if (size == 0) |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
53 { |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
54 lw_free(optr); |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
55 return; |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
56 } |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
57 |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
58 ptr = realloc(optr, size); |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
59 if (!ptr) |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
60 { |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
61 fprintf(stderr, "lw_realloc(): memory allocation error\n"); |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
62 exit(1); |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
63 } |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
64 } |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
65 |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
66 void lw_free(void *ptr) |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
67 { |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
68 if (ptr) |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
69 free(ptr); |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
70 } |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
71 |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
72 char *lw_strdup(const char *s) |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
73 { |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
74 char *d; |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
75 |
116 | 76 if (!s) |
77 return NULL; | |
78 | |
114
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
79 d = strdup(s); |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
80 if (!d) |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
81 { |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
82 fprintf(stderr, "lw_strdup(): memory allocation error\n"); |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
83 exit(1); |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
84 } |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
85 |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
86 return d; |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
diff
changeset
|
87 } |