libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / m2 / mc-boot / GSysStorage.cc
blob1ef723074d464b81470ed87fd97a0ab18ba9e2de
1 /* do not edit automatically generated by mc from SysStorage. */
2 /* SysStorage.mod provides dynamic allocation for the system components.
4 Copyright (C) 2001-2024 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
7 This file is part of GNU Modula-2.
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.
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.
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.
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/>. */
28 #include "config.h"
29 #include "system.h"
30 #include <stdbool.h>
31 # if !defined (PROC_D)
32 # define PROC_D
33 typedef void (*PROC_t) (void);
34 typedef struct { PROC_t proc; } PROC;
35 # endif
37 # if !defined (TRUE)
38 # define TRUE (1==1)
39 # endif
41 # if !defined (FALSE)
42 # define FALSE (1==0)
43 # endif
45 #if defined(__cplusplus)
46 # undef NULL
47 # define NULL 0
48 #endif
49 #define _SysStorage_C
51 #include "GSysStorage.h"
52 # include "Glibc.h"
53 # include "GDebug.h"
54 # include "GSYSTEM.h"
56 # define enableDeallocation true
57 # define enableZero true
58 # define enableTrace false
59 static unsigned int callno;
60 static bool zero;
61 static bool trace;
62 extern "C" void SysStorage_ALLOCATE (void * *a, unsigned int size);
63 extern "C" void SysStorage_DEALLOCATE (void * *a, unsigned int size);
66 REALLOCATE - attempts to reallocate storage. The address,
67 a, should either be NIL in which case ALLOCATE
68 is called, or alternatively it should have already
69 been initialized by ALLOCATE. The allocated storage
70 is resized accordingly.
73 extern "C" void SysStorage_REALLOCATE (void * *a, unsigned int size);
76 REALLOCATE - attempts to reallocate storage. The address,
77 a, should either be NIL in which case ALLOCATE
78 is called, or alternatively it should have already
79 been initialized by ALLOCATE. The allocated storage
80 is resized accordingly.
83 extern "C" bool SysStorage_Available (unsigned int size);
86 Init - initializes the heap. This does nothing on a GNU/Linux system.
87 But it remains here since it might be used in an embedded system.
90 extern "C" void SysStorage_Init (void);
92 extern "C" void SysStorage_ALLOCATE (void * *a, unsigned int size)
94 (*a) = libc_malloc (static_cast<size_t> (size));
95 if ((*a) == NULL)
97 Debug_Halt ((const char *) "out of memory error", 19, (const char *) "../../gcc/m2/gm2-libs/SysStorage.mod", 36, (const char *) "ALLOCATE", 8, 51);
99 if (enableTrace && trace)
101 libc_printf ((const char *) "<DEBUG-CALL> %d SysStorage.ALLOCATE (0x%x, %d bytes)\\n", 54, callno, (*a), size);
102 libc_printf ((const char *) "<MEM-ALLOC> %ld %d\\n", 20, (*a), size);
103 callno += 1;
107 extern "C" void SysStorage_DEALLOCATE (void * *a, unsigned int size)
109 if (enableTrace && trace)
111 libc_printf ((const char *) "<DEBUG-CALL> %d SysStorage.DEALLOCATE (0x%x, %d bytes)\\n", 56, callno, (*a), size);
112 callno += 1;
114 if (enableZero && zero)
116 if (enableTrace && trace)
118 libc_printf ((const char *) " memset (0x%x, 0, %d bytes)\\n", 30, (*a), size);
120 if ((libc_memset ((*a), 0, static_cast<size_t> (size))) != (*a))
122 Debug_Halt ((const char *) "memset should have returned the first parameter", 47, (const char *) "../../gcc/m2/gm2-libs/SysStorage.mod", 36, (const char *) "DEALLOCATE", 10, 78);
125 if (enableDeallocation)
127 if (enableTrace && trace)
129 libc_printf ((const char *) " free (0x%x) %d bytes\\n", 26, (*a), size);
130 libc_printf ((const char *) "<MEM-FREE> %ld %d\\n", 19, (*a), size);
132 libc_free ((*a));
134 (*a) = NULL;
139 REALLOCATE - attempts to reallocate storage. The address,
140 a, should either be NIL in which case ALLOCATE
141 is called, or alternatively it should have already
142 been initialized by ALLOCATE. The allocated storage
143 is resized accordingly.
146 extern "C" void SysStorage_REALLOCATE (void * *a, unsigned int size)
148 if ((*a) == NULL)
150 SysStorage_ALLOCATE (a, size);
152 else
154 if (enableTrace && trace)
156 libc_printf ((const char *) "<DEBUG-CALL> %d SysStorage.REALLOCATE (0x%x, %d bytes)\\n", 56, callno, (*a), size);
157 callno += 1;
159 if (enableTrace && trace)
161 libc_printf ((const char *) " realloc (0x%x, %d bytes) -> ", 32, (*a), size);
162 libc_printf ((const char *) "<MEM-FREE> %ld %d\\n", 19, (*a), size);
164 (*a) = libc_realloc ((*a), static_cast<size_t> (size));
165 if ((*a) == NULL)
167 Debug_Halt ((const char *) "out of memory error", 19, (const char *) "../../gcc/m2/gm2-libs/SysStorage.mod", 36, (const char *) "REALLOCATE", 10, 122);
169 if (enableTrace && trace)
171 libc_printf ((const char *) "<MEM-ALLOC> %ld %d\\n", 20, (*a), size);
172 libc_printf ((const char *) " 0x%x %d bytes\\n", 18, (*a), size);
179 REALLOCATE - attempts to reallocate storage. The address,
180 a, should either be NIL in which case ALLOCATE
181 is called, or alternatively it should have already
182 been initialized by ALLOCATE. The allocated storage
183 is resized accordingly.
186 extern "C" bool SysStorage_Available (unsigned int size)
188 void * a;
190 if (enableTrace && trace)
192 libc_printf ((const char *) "<DEBUG-CALL> %d SysStorage.Available (%d bytes)\\n", 49, callno, size);
193 callno += 1;
195 a = libc_malloc (static_cast<size_t> (size));
196 if (a == NULL)
198 if (enableTrace && trace)
200 libc_printf ((const char *) " no\\n", 7, size);
202 return false;
204 else
206 if (enableTrace && trace)
208 libc_printf ((const char *) " yes\\n", 8, size);
210 libc_free (a);
211 return true;
213 /* static analysis guarentees a RETURN statement will be used before here. */
214 __builtin_unreachable ();
219 Init - initializes the heap. This does nothing on a GNU/Linux system.
220 But it remains here since it might be used in an embedded system.
223 extern "C" void SysStorage_Init (void)
227 extern "C" void _M2_SysStorage_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
229 callno = 0;
230 if (enableTrace)
232 trace = (libc_getenv (const_cast<void*> (static_cast<const void*>("M2DEBUG_SYSSTORAGE_trace")))) != NULL;
234 else
236 trace = false;
238 if (enableZero)
240 zero = (libc_getenv (const_cast<void*> (static_cast<const void*>("M2DEBUG_SYSSTORAGE_zero")))) != NULL;
242 else
244 zero = false;
248 extern "C" void _M2_SysStorage_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])