annotate old-trunk/lwlib/lw_mem.c @ 372:90de73ba0cac

Created a useful debug framework and adjusted lw_expr_print() to return a "static" dynamic string
author lost@starbug
date Thu, 22 Apr 2010 18:19:06 -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 lw_mem.c
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
3
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
4 Copyright © 2008 William Astle
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
5
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
6 This file is part of LWASM.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
7
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
8 LWASM is free software: you can redistribute it and/or modify it under the
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
11 version.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
12
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
16 more details.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
17
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
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 #include <stdlib.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
23
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
24 void (*lw_mem_error)(size_t s) = NULL;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
25
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
26 void *lw_malloc(size_t s)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
27 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
28 void *r;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
29
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
30 if (s < 1)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
31 s = 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
32 r = malloc(s);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
33 if (!r && lw_mem_error)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
34 (*lw_mem_error)(s);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
35 return r;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
36 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
37
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
38 void *lw_calloc(size_t s)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
39 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
40 void *r;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
41
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
42 if (s < 1)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
43 s = 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
44 r = calloc(s, 1);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
45 if (!r && lw_mem_error)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
46 (*lw_mem_error)(s);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
47 return r;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
48 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
49
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
50 void lw_free(void *p)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
51 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
52 if (p)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
53 free(p);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
54 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
55
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
56 void *lw_realloc(void *p, size_t s)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
57 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
58 void *r;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
59
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
60 if (s < 1)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
61 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
62 free(p);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
63 return NULL;
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 r = realloc(p, s);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
67 if (!r && lw_mem_error)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
68 (*lw_mem_error)(s);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
69 return r;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
70 }