Debian Codegraph @

gcc/m2/gm2-libs-iso/RTdata.mod

raw
1 (* RTdata.mod implements a mechanism whereby devices can attach data.
2
3 Copyright (C) 2008-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
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)
11 any later version.
12
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.
17
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.
21
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/>. *)
26
27 IMPLEMENTATION MODULE RTdata ;
28
29 FROM IOLink IMPORT RAISEdevException ;
30 FROM RTentity IMPORT Group, InitGroup, PutKey, IsIn, DelKey ;
31 FROM Storage IMPORT ALLOCATE, DEALLOCATE ;
32
33 IMPORT IOChan ;
34
35 TYPE
36 key = (allocated, deallocated) ;
37
38 (* each module can register one of these *)
39 ModuleId = POINTER TO RECORD
40 id: CARDINAL ;
41 END ;
42
43 (* each device can contain multiple of the these *)
44 ModuleData = POINTER TO RECORD
45 mid : ModuleId ;
46 data : ADDRESS ;
47 free : FreeProcedure ;
48 right: ModuleData ;
49 END ;
50
51
52 (*
53 MakeModuleId - creates a unique module Id.
54 *)
55
56 PROCEDURE MakeModuleId (VAR m: ModuleId) ;
57 BEGIN
58 INC(lastId) ;
59 NEW(m) ;
60 WITH m^ DO
61 id := lastId
62 END ;
63 PutKey(mids, m, ORD(allocated))
64 END MakeModuleId ;
65
66
67 (*
68 verifyModuleId - verifies that, m, has not been deallocated.
69 *)
70
71 PROCEDURE verifyModuleId (m: ModuleId; d: DeviceTablePtr) ;
72 BEGIN
73 IF NOT IsIn(mids, m)
74 THEN
75 WITH d^ DO
76 RAISEdevException(cid, did, IOChan.hardDeviceError,
77 'RTdata: invalid module id')
78 END
79 END
80 END verifyModuleId ;
81
82
83 (*
84 InitData - adds, datum, to the device, d. The datum
85 is associated with ModuleID, m.
86 *)
87
88 PROCEDURE InitData (d: DeviceTablePtr; m: ModuleId;
89 datum: ADDRESS; f: FreeProcedure) ;
90 VAR
91 md: ModuleData ;
92 BEGIN
93 NEW(md) ;
94 WITH md^ DO
95 mid := m ;
96 data := datum ;
97 free := f ;
98 right := d^.cd
99 END ;
100 d^.cd := md
101 END InitData ;
102
103
104 (*
105 GetData - returns the datum assocated with ModuleId, m.
106 *)
107
108 PROCEDURE GetData (d: DeviceTablePtr; m: ModuleId) : ADDRESS ;
109 VAR
110 md: ModuleData ;
111 BEGIN
112 verifyModuleId(m, d) ;
113 md := d^.cd ;
114 WHILE md#NIL DO
115 IF md^.mid=m
116 THEN
117 RETURN( md^.data )
118 ELSE
119 md := md^.right
120 END
121 END ;
122 RETURN( md )
123 END GetData ;
124
125
126 (*
127 KillData - destroys the datum associated with ModuleId, m,
128 in device, d. It invokes the free procedure
129 given during InitData.
130 *)
131
132 PROCEDURE KillData (d: DeviceTablePtr; m: ModuleId) ;
133 VAR
134 last,
135 md : ModuleData ;
136 BEGIN
137 verifyModuleId(m, d) ;
138 last := NIL ;
139 md := d^.cd ;
140 WHILE md#NIL DO
141 IF md^.mid=m
142 THEN
143 IF last=NIL
144 THEN
145 d^.cd := md^.right
146 ELSE
147 last^.right := md^.right
148 END ;
149 DelKey(mids, m) ;
150 md^.free(md^.data) ;
151 md^.data := NIL ;
152 DISPOSE(md)
153 ELSE
154 last := md ;
155 md := md^.right
156 END
157 END
158 END KillData ;
159
160
161 VAR
162 mids : Group ;
163 lastId: CARDINAL ;
164 BEGIN
165 mids := InitGroup() ;
166 lastId := 0
167 END RTdata.