| 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-2025 Free Software Foundation, Inc. |
| 5 |
Contributed by Gaius Mulley <[email protected]>. |
| 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 |
#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 |
| 36 |
|
| 37 |
# if !defined (TRUE) |
| 38 |
# define TRUE (1==1) |
| 39 |
# endif |
| 40 |
|
| 41 |
# if !defined (FALSE) |
| 42 |
# define FALSE (1==0) |
| 43 |
# endif |
| 44 |
|
| 45 |
#if defined(__cplusplus) |
| 46 |
# undef NULL |
| 47 |
# define NULL 0 |
| 48 |
#endif |
| 49 |
#define _SysStorage_C |
| 50 |
|
| 51 |
#include "GSysStorage.h" |
| 52 |
# include "Glibc.h" |
| 53 |
# include "GDebug.h" |
| 54 |
# include "GSYSTEM.h" |
| 55 |
|
| 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); |
| 64 |
|
| 65 |
/* |
| 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. |
| 71 |
*/ |
| 72 |
|
| 73 |
extern "C" void SysStorage_REALLOCATE (void * *a, unsigned int size); |
| 74 |
|
| 75 |
/* |
| 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. |
| 81 |
*/ |
| 82 |
|
| 83 |
extern "C" bool SysStorage_Available (unsigned int size); |
| 84 |
|
| 85 |
/* |
| 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. |
| 88 |
*/ |
| 89 |
|
| 90 |
extern "C" void SysStorage_Init (void); |
| 91 |
|
| 92 |
extern "C" void SysStorage_ALLOCATE (void * *a, unsigned int size) |
| 93 |
{ |
| 94 |
(*a) = libc_malloc (static_cast<size_t> (size)); |
| 95 |
if ((*a) == NULL) |
| 96 |
{ |
| 97 |
Debug_Halt ((const char *) "out of memory error", 19, (const char *) "../../gcc/m2/gm2-libs/SysStorage.mod", 36, (const char *) "ALLOCATE", 8, 51); |
| 98 |
} |
| 99 |
if (enableTrace && trace) |
| 100 |
{ |
| 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; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
extern "C" void SysStorage_DEALLOCATE (void * *a, unsigned int size) |
| 108 |
{ |
| 109 |
if (enableTrace && trace) |
| 110 |
{ |
| 111 |
libc_printf ((const char *) "<DEBUG-CALL> %d SysStorage.DEALLOCATE (0x%x, %d bytes)\\n", 56, callno, (*a), size); |
| 112 |
callno += 1; |
| 113 |
} |
| 114 |
if (enableZero && zero) |
| 115 |
{ |
| 116 |
if (enableTrace && trace) |
| 117 |
{ |
| 118 |
libc_printf ((const char *) " memset (0x%x, 0, %d bytes)\\n", 30, (*a), size); |
| 119 |
} |
| 120 |
if ((libc_memset ((*a), 0, static_cast<size_t> (size))) != (*a)) |
| 121 |
{ |
| 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); |
| 123 |
} |
| 124 |
} |
| 125 |
if (enableDeallocation) |
| 126 |
{ |
| 127 |
if (enableTrace && trace) |
| 128 |
{ |
| 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); |
| 131 |
} |
| 132 |
libc_free ((*a)); |
| 133 |
} |
| 134 |
(*a) = NULL; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
/* |
| 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. |
| 144 |
*/ |
| 145 |
|
| 146 |
extern "C" void SysStorage_REALLOCATE (void * *a, unsigned int size) |
| 147 |
{ |
| 148 |
if ((*a) == NULL) |
| 149 |
{ |
| 150 |
SysStorage_ALLOCATE (a, size); |
| 151 |
} |
| 152 |
else |
| 153 |
{ |
| 154 |
if (enableTrace && trace) |
| 155 |
{ |
| 156 |
libc_printf ((const char *) "<DEBUG-CALL> %d SysStorage.REALLOCATE (0x%x, %d bytes)\\n", 56, callno, (*a), size); |
| 157 |
callno += 1; |
| 158 |
} |
| 159 |
if (enableTrace && trace) |
| 160 |
{ |
| 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); |
| 163 |
} |
| 164 |
(*a) = libc_realloc ((*a), static_cast<size_t> (size)); |
| 165 |
if ((*a) == NULL) |
| 166 |
{ |
| 167 |
Debug_Halt ((const char *) "out of memory error", 19, (const char *) "../../gcc/m2/gm2-libs/SysStorage.mod", 36, (const char *) "REALLOCATE", 10, 122); |
| 168 |
} |
| 169 |
if (enableTrace && trace) |
| 170 |
{ |
| 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); |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
/* |
| 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. |
| 184 |
*/ |
| 185 |
|
| 186 |
extern "C" bool SysStorage_Available (unsigned int size) |
| 187 |
{ |
| 188 |
void * a; |
| 189 |
|
| 190 |
if (enableTrace && trace) |
| 191 |
{ |
| 192 |
libc_printf ((const char *) "<DEBUG-CALL> %d SysStorage.Available (%d bytes)\\n", 49, callno, size); |
| 193 |
callno += 1; |
| 194 |
} |
| 195 |
a = libc_malloc (static_cast<size_t> (size)); |
| 196 |
if (a == NULL) |
| 197 |
{ |
| 198 |
if (enableTrace && trace) |
| 199 |
{ |
| 200 |
libc_printf ((const char *) " no\\n", 7, size); |
| 201 |
} |
| 202 |
return false; |
| 203 |
} |
| 204 |
else |
| 205 |
{ |
| 206 |
if (enableTrace && trace) |
| 207 |
{ |
| 208 |
libc_printf ((const char *) " yes\\n", 8, size); |
| 209 |
} |
| 210 |
libc_free (a); |
| 211 |
return true; |
| 212 |
} |
| 213 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 214 |
__builtin_unreachable (); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
/* |
| 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. |
| 221 |
*/ |
| 222 |
|
| 223 |
extern "C" void SysStorage_Init (void) |
| 224 |
{ |
| 225 |
} |
| 226 |
|
| 227 |
extern "C" void _M2_SysStorage_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[]) |
| 228 |
{ |
| 229 |
callno = 0; |
| 230 |
if (enableTrace) |
| 231 |
{ |
| 232 |
trace = (libc_getenv (const_cast<void*> (static_cast<const void*>("M2DEBUG_SYSSTORAGE_trace")))) != NULL; |
| 233 |
} |
| 234 |
else |
| 235 |
{ |
| 236 |
trace = false; |
| 237 |
} |
| 238 |
if (enableZero) |
| 239 |
{ |
| 240 |
zero = (libc_getenv (const_cast<void*> (static_cast<const void*>("M2DEBUG_SYSSTORAGE_zero")))) != NULL; |
| 241 |
} |
| 242 |
else |
| 243 |
{ |
| 244 |
zero = false; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
extern "C" void _M2_SysStorage_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[]) |
| 249 |
{ |
| 250 |
} |