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)
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/>. */
31 # if !defined (PROC_D)
33 typedef void (*PROC_t
) (void);
34 typedef struct { PROC_t proc
; } PROC
;
45 #if defined(__cplusplus)
51 #include "GSysStorage.h"
56 # define enableDeallocation true
57 # define enableZero true
58 # define enableTrace false
59 static unsigned int callno
;
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
));
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
);
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
);
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
);
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
)
150 SysStorage_ALLOCATE (a
, size
);
154 if (enableTrace
&& trace
)
156 libc_printf ((const char *) "<DEBUG-CALL> %d SysStorage.REALLOCATE (0x%x, %d bytes)\\n", 56, callno
, (*a
), size
);
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
));
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
)
190 if (enableTrace
&& trace
)
192 libc_printf ((const char *) "<DEBUG-CALL> %d SysStorage.Available (%d bytes)\\n", 49, callno
, size
);
195 a
= libc_malloc (static_cast<size_t> (size
));
198 if (enableTrace
&& trace
)
200 libc_printf ((const char *) " no\\n", 7, size
);
206 if (enableTrace
&& trace
)
208 libc_printf ((const char *) " yes\\n", 8, size
);
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
[])
232 trace
= (libc_getenv (const_cast<void*> (static_cast<const void*>("M2DEBUG_SYSSTORAGE_trace")))) != NULL
;
240 zero
= (libc_getenv (const_cast<void*> (static_cast<const void*>("M2DEBUG_SYSSTORAGE_zero")))) != NULL
;
248 extern "C" void _M2_SysStorage_fini (__attribute__((unused
)) int argc
, __attribute__((unused
)) char *argv
[], __attribute__((unused
)) char *envp
[])