]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/m2/pge-boot/GSysStorage.c
Update copyright years.
[thirdparty/gcc.git] / gcc / m2 / pge-boot / GSysStorage.c
1 /* do not edit automatically generated by mc from SysStorage. */
2 /* SysStorage.mod provides dynamic allocation for the system components.
3
4 Copyright (C) 2001-2023 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6
7 This file is part of GNU Modula-2.
8
9 GNU Modula-2 is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GNU Modula-2 is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
22
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
27
28 # if !defined (PROC_D)
29 # define PROC_D
30 typedef void (*PROC_t) (void);
31 typedef struct { PROC_t proc; } PROC;
32 # endif
33
34 # if !defined (TRUE)
35 # define TRUE (1==1)
36 # endif
37
38 # if !defined (FALSE)
39 # define FALSE (1==0)
40 # endif
41
42 #include <stddef.h>
43 #include <stdlib.h>
44 #if defined(__cplusplus)
45 # undef NULL
46 # define NULL 0
47 #endif
48 #define _SysStorage_H
49 #define _SysStorage_C
50
51 # include "Glibc.h"
52 # include "GDebug.h"
53 # include "GSYSTEM.h"
54
55 # define enableDeallocation TRUE
56 # define enableZero FALSE
57 # define enableTrace FALSE
58 static unsigned int callno;
59 static unsigned int zero;
60 static unsigned int trace;
61 extern "C" void SysStorage_ALLOCATE (void * *a, unsigned int size);
62 extern "C" void SysStorage_DEALLOCATE (void * *a, unsigned int size);
63
64 /*
65 REALLOCATE - attempts to reallocate storage. The address,
66 a, should either be NIL in which case ALLOCATE
67 is called, or alternatively it should have already
68 been initialized by ALLOCATE. The allocated storage
69 is resized accordingly.
70 */
71
72 extern "C" void SysStorage_REALLOCATE (void * *a, unsigned int size);
73
74 /*
75 REALLOCATE - attempts to reallocate storage. The address,
76 a, should either be NIL in which case ALLOCATE
77 is called, or alternatively it should have already
78 been initialized by ALLOCATE. The allocated storage
79 is resized accordingly.
80 */
81
82 extern "C" unsigned int SysStorage_Available (unsigned int size);
83
84 /*
85 Init - initializes the heap. This does nothing on a GNU/Linux system.
86 But it remains here since it might be used in an embedded system.
87 */
88
89 extern "C" void SysStorage_Init (void);
90
91 extern "C" void SysStorage_ALLOCATE (void * *a, unsigned int size)
92 {
93 (*a) = libc_malloc (static_cast<size_t> (size));
94 if ((*a) == NULL)
95 {
96 Debug_Halt ((const char *) "out of memory error", 19, 50, (const char *) "../../gcc-git-devel-modula2/gcc/m2/gm2-libs/SysStorage.mod", 58);
97 }
98 if (enableTrace && trace)
99 {
100 libc_printf ((const char *) "<DEBUG-CALL> %d SysStorage.ALLOCATE (0x%x, %d bytes)\\n", 54, callno, (*a), size);
101 libc_printf ((const char *) "<MEM-ALLOC> %ld %d\\n", 20, (*a), size);
102 callno += 1;
103 }
104 }
105
106 extern "C" void SysStorage_DEALLOCATE (void * *a, unsigned int size)
107 {
108 if (enableTrace && trace)
109 {
110 libc_printf ((const char *) "<DEBUG-CALL> %d SysStorage.DEALLOCATE (0x%x, %d bytes)\\n", 56, callno, (*a), size);
111 callno += 1;
112 }
113 if (enableZero && zero)
114 {
115 if (enableTrace && trace)
116 {
117 libc_printf ((const char *) " memset (0x%x, 0, %d bytes)\\n", 30, (*a), size);
118 }
119 if ((libc_memset ((*a), 0, static_cast<size_t> (size))) != (*a))
120 {
121 Debug_Halt ((const char *) "memset should have returned the first parameter", 47, 76, (const char *) "../../gcc-git-devel-modula2/gcc/m2/gm2-libs/SysStorage.mod", 58);
122 }
123 }
124 if (enableDeallocation)
125 {
126 if (enableTrace && trace)
127 {
128 libc_printf ((const char *) " free (0x%x) %d bytes\\n", 26, (*a), size);
129 libc_printf ((const char *) "<MEM-FREE> %ld %d\\n", 19, (*a), size);
130 }
131 libc_free ((*a));
132 }
133 (*a) = NULL;
134 }
135
136
137 /*
138 REALLOCATE - attempts to reallocate storage. The address,
139 a, should either be NIL in which case ALLOCATE
140 is called, or alternatively it should have already
141 been initialized by ALLOCATE. The allocated storage
142 is resized accordingly.
143 */
144
145 extern "C" void SysStorage_REALLOCATE (void * *a, unsigned int size)
146 {
147 if ((*a) == NULL)
148 {
149 SysStorage_ALLOCATE (a, size);
150 }
151 else
152 {
153 if (enableTrace && trace)
154 {
155 libc_printf ((const char *) "<DEBUG-CALL> %d SysStorage.REALLOCATE (0x%x, %d bytes)\\n", 56, callno, (*a), size);
156 callno += 1;
157 }
158 if (enableTrace && trace)
159 {
160 libc_printf ((const char *) " realloc (0x%x, %d bytes) -> ", 32, (*a), size);
161 libc_printf ((const char *) "<MEM-FREE> %ld %d\\n", 19, (*a), size);
162 }
163 (*a) = libc_realloc ((*a), static_cast<size_t> (size));
164 if ((*a) == NULL)
165 {
166 Debug_Halt ((const char *) "out of memory error", 19, 119, (const char *) "../../gcc-git-devel-modula2/gcc/m2/gm2-libs/SysStorage.mod", 58);
167 }
168 if (enableTrace && trace)
169 {
170 libc_printf ((const char *) "<MEM-ALLOC> %ld %d\\n", 20, (*a), size);
171 libc_printf ((const char *) " 0x%x %d bytes\\n", 18, (*a), size);
172 }
173 }
174 }
175
176
177 /*
178 REALLOCATE - attempts to reallocate storage. The address,
179 a, should either be NIL in which case ALLOCATE
180 is called, or alternatively it should have already
181 been initialized by ALLOCATE. The allocated storage
182 is resized accordingly.
183 */
184
185 extern "C" unsigned int SysStorage_Available (unsigned int size)
186 {
187 void * a;
188
189 if (enableTrace && trace)
190 {
191 libc_printf ((const char *) "<DEBUG-CALL> %d SysStorage.Available (%d bytes)\\n", 49, callno, size);
192 callno += 1;
193 }
194 a = libc_malloc (static_cast<size_t> (size));
195 if (a == NULL)
196 {
197 if (enableTrace && trace)
198 {
199 libc_printf ((const char *) " no\\n", 7, size);
200 }
201 return FALSE;
202 }
203 else
204 {
205 if (enableTrace && trace)
206 {
207 libc_printf ((const char *) " yes\\n", 8, size);
208 }
209 libc_free (a);
210 return TRUE;
211 }
212 /* static analysis guarentees a RETURN statement will be used before here. */
213 __builtin_unreachable ();
214 }
215
216
217 /*
218 Init - initializes the heap. This does nothing on a GNU/Linux system.
219 But it remains here since it might be used in an embedded system.
220 */
221
222 extern "C" void SysStorage_Init (void)
223 {
224 }
225
226 extern "C" void _M2_SysStorage_init (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
227 {
228 callno = 0;
229 if (enableTrace)
230 {
231 trace = (libc_getenv (const_cast<void*> (reinterpret_cast<const void*>("M2DEBUG_SYSSTORAGE_trace")))) != NULL;
232 }
233 else
234 {
235 trace = FALSE;
236 }
237 if (enableZero)
238 {
239 zero = (libc_getenv (const_cast<void*> (reinterpret_cast<const void*>("M2DEBUG_SYSSTORAGE_zero")))) != NULL;
240 }
241 else
242 {
243 zero = FALSE;
244 }
245 }
246
247 extern "C" void _M2_SysStorage_finish (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
248 {
249 }