]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/m2/gm2-libs/SysStorage.mod
Update copyright years.
[thirdparty/gcc.git] / gcc / m2 / gm2-libs / SysStorage.mod
CommitLineData
1eee94d3
GM
1(* SysStorage.mod provides dynamic allocation for the system components.
2
83ffe9cd 3Copyright (C) 2001-2023 Free Software Foundation, Inc.
1eee94d3
GM
4Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
5
6This file is part of GNU Modula-2.
7
8GNU Modula-2 is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3, or (at your option)
11any later version.
12
13GNU Modula-2 is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16General Public License for more details.
17
18Under Section 7 of GPL version 3, you are granted additional
19permissions described in the GCC Runtime Library Exception, version
203.1, as published by the Free Software Foundation.
21
22You should have received a copy of the GNU General Public License and
23a copy of the GCC Runtime Library Exception along with this program;
24see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25<http://www.gnu.org/licenses/>. *)
26
27IMPLEMENTATION MODULE SysStorage ;
28
29FROM libc IMPORT malloc, free, realloc, memset, getenv, printf ;
30FROM Debug IMPORT Halt ;
31FROM SYSTEM IMPORT ADR ;
32
33
34CONST
35 enableDeallocation = TRUE ;
36 enableZero = FALSE ;
37 enableTrace = FALSE ;
38
39VAR
40 callno: CARDINAL ;
41 zero,
42 trace : BOOLEAN ;
43
44
45PROCEDURE ALLOCATE (VAR a: ADDRESS ; size: CARDINAL) ;
46BEGIN
47 a := malloc (size) ;
48 IF a = NIL
49 THEN
50 Halt ('out of memory error', __LINE__, __FILE__)
51 END ;
52 IF enableTrace AND trace
53 THEN
54 printf ("<DEBUG-CALL> %d SysStorage.ALLOCATE (0x%x, %d bytes)\n", callno, a, size) ;
55 printf ("<MEM-ALLOC> %ld %d\n", a, size);
56 INC (callno)
57 END
58END ALLOCATE ;
59
60
61PROCEDURE DEALLOCATE (VAR a: ADDRESS; size: CARDINAL);
62BEGIN
63 IF enableTrace AND trace
64 THEN
65 printf ("<DEBUG-CALL> %d SysStorage.DEALLOCATE (0x%x, %d bytes)\n", callno, a, size) ;
66 INC (callno)
67 END ;
68 IF enableZero AND zero
69 THEN
70 IF enableTrace AND trace
71 THEN
72 printf (" memset (0x%x, 0, %d bytes)\n", a, size)
73 END ;
74 IF memset (a, 0, size) # a
75 THEN
76 Halt ('memset should have returned the first parameter', __LINE__, __FILE__)
77 END
78 END ;
79 IF enableDeallocation
80 THEN
81 IF enableTrace AND trace
82 THEN
83 printf (" free (0x%x) %d bytes\n", a, size) ;
84 printf ("<MEM-FREE> %ld %d\n", a, size);
85 END ;
86 free (a)
87 END ;
88 a := NIL
89END DEALLOCATE ;
90
91
92(*
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.
98*)
99
100PROCEDURE REALLOCATE (VAR a: ADDRESS; size: CARDINAL) ;
101BEGIN
102 IF a = NIL
103 THEN
104 ALLOCATE (a, size)
105 ELSE
106 IF enableTrace AND trace
107 THEN
108 printf ("<DEBUG-CALL> %d SysStorage.REALLOCATE (0x%x, %d bytes)\n", callno, a, size) ;
109 INC (callno)
110 END ;
111 IF enableTrace AND trace
112 THEN
113 printf (" realloc (0x%x, %d bytes) -> ", a, size) ;
114 printf ("<MEM-FREE> %ld %d\n", a, size)
115 END ;
116 a := realloc (a, size) ;
117 IF a = NIL
118 THEN
119 Halt ('out of memory error', __LINE__, __FILE__)
120 END ;
121 IF enableTrace AND trace
122 THEN
123 printf ("<MEM-ALLOC> %ld %d\n", a, size) ;
124 printf (" 0x%x %d bytes\n", a, size)
125 END
126 END
127END REALLOCATE ;
128
129
130PROCEDURE Available (size: CARDINAL) : BOOLEAN;
131VAR
132 a: ADDRESS ;
133BEGIN
134 IF enableTrace AND trace
135 THEN
136 printf ("<DEBUG-CALL> %d SysStorage.Available (%d bytes)\n", callno, size) ;
137 INC (callno)
138 END ;
139 a := malloc (size) ;
140 IF a = NIL
141 THEN
142 IF enableTrace AND trace
143 THEN
144 printf (" no\n", size)
145 END ;
146 RETURN FALSE
147 ELSE
148 IF enableTrace AND trace
149 THEN
150 printf (" yes\n", size)
151 END ;
152 free (a) ;
153 RETURN TRUE
154 END
155END Available ;
156
157
158(*
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.
161*)
162
163PROCEDURE Init ;
164END Init ;
165
166
167BEGIN
168 callno := 0 ;
169 IF enableTrace
170 THEN
171 trace := getenv (ADR ("M2DEBUG_SYSSTORAGE_trace")) # NIL
172 ELSE
173 trace := FALSE
174 END ;
175 IF enableZero
176 THEN
177 zero := getenv (ADR ("M2DEBUG_SYSSTORAGE_zero")) # NIL
178 ELSE
179 zero := FALSE
180 END
181END SysStorage.