1 (* SysStorage.mod provides dynamic allocation for the system components.
3 Copyright (C) 2001-2023 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. *)
27 IMPLEMENTATION MODULE SysStorage ;
29 FROM libc IMPORT malloc, free, realloc, memset, getenv, printf ;
30 FROM Debug IMPORT Halt ;
31 FROM SYSTEM IMPORT ADR ;
35 enableDeallocation = TRUE ;
45 PROCEDURE ALLOCATE (VAR a: ADDRESS ; size: CARDINAL) ;
50 Halt ('out of memory error', __LINE__, __FILE__)
52 IF enableTrace AND trace
54 printf ("<DEBUG-CALL> %d SysStorage.ALLOCATE (0x%x, %d bytes)\n", callno, a, size) ;
55 printf ("<MEM-ALLOC> %ld %d\n", a, size);
61 PROCEDURE DEALLOCATE (VAR a: ADDRESS; size: CARDINAL);
63 IF enableTrace AND trace
65 printf ("<DEBUG-CALL> %d SysStorage.DEALLOCATE (0x%x, %d bytes)\n", callno, a, size) ;
68 IF enableZero AND zero
70 IF enableTrace AND trace
72 printf (" memset (0x%x, 0, %d bytes)\n", a, size)
74 IF memset (a, 0, size) # a
76 Halt ('memset should have returned the first parameter', __LINE__, __FILE__)
81 IF enableTrace AND trace
83 printf (" free (0x%x) %d bytes\n", a, size) ;
84 printf ("<MEM-FREE> %ld %d\n", a, size);
93 REALLOCATE - attempts to reallocate storage. The address,
94 a, should either be NIL in which case ALLOCATE
95 is called, or alternatively it should have already
96 been initialized by ALLOCATE. The allocated storage
97 is resized accordingly.
100 PROCEDURE REALLOCATE (VAR a: ADDRESS; size: CARDINAL) ;
106 IF enableTrace AND trace
108 printf ("<DEBUG-CALL> %d SysStorage.REALLOCATE (0x%x, %d bytes)\n", callno, a, size) ;
111 IF enableTrace AND trace
113 printf (" realloc (0x%x, %d bytes) -> ", a, size) ;
114 printf ("<MEM-FREE> %ld %d\n", a, size)
116 a := realloc (a, size) ;
119 Halt ('out of memory error', __LINE__, __FILE__)
121 IF enableTrace AND trace
123 printf ("<MEM-ALLOC> %ld %d\n", a, size) ;
124 printf (" 0x%x %d bytes\n", a, size)
130 PROCEDURE Available (size: CARDINAL) : BOOLEAN;
134 IF enableTrace AND trace
136 printf ("<DEBUG-CALL> %d SysStorage.Available (%d bytes)\n", callno, size) ;
142 IF enableTrace AND trace
144 printf (" no\n", size)
148 IF enableTrace AND trace
150 printf (" yes\n", size)
159 Init - initializes the heap. This does nothing on a GNU/Linux system.
160 But it remains here since it might be used in an embedded system.
171 trace := getenv (ADR ("M2DEBUG_SYSSTORAGE_trace")) # NIL
177 zero := getenv (ADR ("M2DEBUG_SYSSTORAGE_zero")) # NIL