comparison src/ui/bmi/mmiBlkResources.c @ 3:67bfe9f274f6

src/ui: import of src/ui3 from Magnetite
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 16 Oct 2020 06:33:10 +0000
parents
children c0052fe355d3
comparison
equal deleted inserted replaced
2:3a14ee9a9843 3:67bfe9f274f6
1 /*******************************************************************************
2
3 CONDAT (UK)
4
5 ********************************************************************************
6
7 This software product is the property of Condat (UK) Ltd and may not be
8 disclosed to any third party without the express permission of the owner.
9
10 ********************************************************************************
11
12 $Project name: Basic MMI
13 $Project code: BMI (6349)
14 $Module: PhoneBook
15 $File: MmiBlkResources.c
16 $Revision: 1.0
17
18 $Author: Condat(UK)
19 $Date: 25/10/00
20
21 ********************************************************************************
22
23 Description:
24
25 This modules provides, in conjunction with the MmiBlkManager module,
26 the resource management facilities for the MMI.
27
28 ********************************************************************************
29
30 $History: MmiBlkResources.c
31
32 25/10/00 Original Condat(UK) BMI version.
33
34 $End
35
36 *******************************************************************************/
37
38
39 /*******************************************************************************
40
41 Include Files
42
43 *******************************************************************************/
44
45 #define ENTITY_MFW
46
47 /* includes */
48 #include <string.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51
52 #if defined (NEW_FRAME)
53
54 #include "typedefs.h"
55 #include "vsi.h"
56 #include "pei.h"
57 #include "custom.h"
58 #include "gsm.h"
59
60 #else
61
62 #include "STDDEFS.H"
63 #include "custom.h"
64 #include "gsm.h"
65 #include "vsi.h"
66
67 #endif
68 // #include <malloc.h>
69
70 #include "MmiBlkResources.h"
71
72 /*******************************************************************************
73
74 Local Structures
75
76 *******************************************************************************/
77
78 /* Define the control structures used to implement the block resource
79 manager
80 */
81 #define RESOURCE_MANAGER_KEY 0x00BABE02L
82 #define RESOURCE_SEARCH_LIMIT 4
83
84 typedef struct _tBlkEntry_
85 {
86 tBlkId BlkId;
87 tBlkHandle BlkHandle;
88 } tBlkEntry, *pBlkEntry;
89
90 typedef struct _tBlkControl_
91 {
92 long int BlkKey;
93 tBlkHandle BlkBase;
94 int BlkLength;
95 pBlkEntry BlkStartPtr;
96 tBlkHandle BlkMinAddress;
97 tBlkHandle BlkMaxAddress;
98 } tBlkControl, *pBlkControl;
99
100
101 /*******************************************************************************
102
103 Private Routines
104
105 *******************************************************************************/
106
107 /*******************************************************************************
108
109 $Function: ValidBlockResource
110
111 $Description: Verifies that the resource indicated by the handle is a valid
112 resource.
113
114 $Returns: 0 If invalid, non-zero if valid.
115
116 $Arguments: BlkRsrc, handle of resource
117
118 *******************************************************************************/
119
120
121 static int ValidBlockResource( tBlkHandle BlkRsrc )
122 {
123 pBlkControl BlkControl = (pBlkControl) BlkRsrc;
124
125 /* Check for non-NULL handle
126 */
127 if ( BlkControl == NULL )
128 return 0;
129
130 /* Check key has been set up correctly
131 */
132 return ( BlkControl->BlkKey == RESOURCE_MANAGER_KEY );
133 }
134
135
136 /*******************************************************************************
137
138 Public Routines
139
140 *******************************************************************************/
141
142 /*******************************************************************************
143
144 $Function: mmibr_Initialise
145
146 $Description:
147
148 The initialisation routine must be called as part of the startup
149 phase of the system, it will allocate working space for the block
150 handler if required, and will initialise any structures required
151 to maintain the corect operation of the functions. This routine
152 must be called prior to invocation of any other block resource
153 function
154
155 $Returns: Handle to a resource block, NULL if unsuccessful
156
157 $Arguments: BlkBase, Base address of the block of data relating
158 to the resource
159 NumEntries, number of entries associated with the resource
160
161 *******************************************************************************/
162
163 tBlkHandle mmibr_Initialise( tBlkHandle BlkBase, int NumEntries )
164 {
165 pBlkControl Blk;
166 int i;
167
168 /* As part of the initialisation process, we need to allocate a
169 block of memory in which to store the control information
170 associated with this block resource manager
171 */
172 if ( ( Blk = (pBlkControl) ALLOC_MEMORY( sizeof(tBlkControl) ) ) != NULL )
173 {
174 /* Store the things we know straight off
175 */
176 Blk->BlkKey = RESOURCE_MANAGER_KEY;
177 Blk->BlkBase = BlkBase;
178 Blk->BlkLength = NumEntries;
179 Blk->BlkStartPtr = (pBlkEntry) BlkBase;
180
181 /* In order to detect memory allocations, we scan the list
182 of known entries, storing the maximum and minimum addresses
183 in the list. This scan allows us to detect when allocated
184 memory is being returned, since the address will be outside
185 the contiguous memory block we are managing.
186 */
187 Blk->BlkMinAddress = Blk->BlkMaxAddress = BlkBase;
188 for ( i = 0; i < NumEntries; i++ )
189 {
190 if ( Blk->BlkMaxAddress < Blk->BlkStartPtr[i].BlkHandle )
191 Blk->BlkMaxAddress = Blk->BlkStartPtr[i].BlkHandle;
192 }
193 }
194
195 return Blk;
196 }
197
198 /*******************************************************************************
199
200 $Function: mmibr_ShutDown
201
202 $Description:
203
204 The shutdown function can be called to free any allocations set up
205 by the Initialise routine. In a running system this is unlikely to
206 be called unless a catastrophic error has occurred and the system
207 needs to be restarted.
208
209 $Returns: none.
210
211 $Arguments: Pointer to a block resource handle (ie ptr to ptr)
212
213 *******************************************************************************/
214
215 void mmibr_ShutDown( tBlkHandle *BlkRsrc )
216 {
217 /* Only allow the resource manager to be shutdown if the provided
218 handle is consistent
219 */
220 if ( ValidBlockResource( *BlkRsrc ) )
221 {
222 free( *BlkRsrc );
223 *BlkRsrc = NULL;
224 }
225 }
226
227 /*******************************************************************************
228
229 $Function: mmibr_Fetch
230
231 $Description:
232
233 We will adopt a mechanism where each block of data being provided
234 will need to be returned to the block resource manager when it
235 is no longer being used, this will provide an orthogonal approach
236 when dealing with data coming from either ROM or dynamically
237 allocated memory.
238
239 $Returns: pointer to resource, NULL if unsuccessful
240
241 $Arguments: BlkRsrc, resource handle created by mmibr_Initialise
242 Id, identifier of the resource to be returned
243
244 *******************************************************************************/
245
246 tBlkHandle mmibr_Fetch( tBlkHandle BlkRsrc, tBlkId Id )
247 {
248 int Start, End, Search;
249 tBlkId CurrentId;
250 tBlkHandle SearchPtr;
251
252 /* convert the handle and verify it's valid
253 */
254 pBlkControl BlkControl = (pBlkControl) BlkRsrc;
255 if ( ! ValidBlockResource( BlkRsrc ) )
256 return NULL;
257
258 /* When locating a specific entry, we need to search the list of
259 ids for one matching the input value. Since the Ids will be
260 organised as an ordered list, very important that bit, we can
261 perform a simple binary search to locate the items.
262 */
263 Start = 0;
264 End = BlkControl->BlkLength - 1;
265 SearchPtr = NULL;
266 do
267 {
268 /* grab the entry midway between the current start and end
269 */
270 Search = Start + ((End - Start) >> 1);
271 CurrentId = BlkControl->BlkStartPtr[Search].BlkId;
272
273 /* Binary chop the search space
274 */
275 if ( CurrentId == Id )
276 {
277 /* Found a match, grab handle and terminate search
278 */
279 SearchPtr = BlkControl->BlkStartPtr[Search].BlkHandle;
280 Start = End;
281 }
282 else if ( CurrentId > Id )
283 {
284 /* Not got a match, but it's not in the top half so move
285 the End pointer down
286 */
287 End = Search;
288 }
289 else
290 {
291 /* Not got a match, but it's not in the bottom half so
292 move the Start pointer up
293 */
294 Start = Search;
295 }
296
297 /* when we get down to the last three or four entries, just
298 search linearly to solve it, this is generally quicker for
299 a small number of entries than continuing the binary chop
300 */
301 if ( ( End - Start ) < RESOURCE_SEARCH_LIMIT )
302 {
303 /* search quickly through the possibles
304 */
305 for ( Search = Start; Search <= End; Search++ )
306 if ( Id == BlkControl->BlkStartPtr[Search].BlkId )
307 SearchPtr = BlkControl->BlkStartPtr[Search].BlkHandle;
308
309 /* And terminate the binary chop
310 */
311 Start = End;
312 }
313
314 } while ( Start != End );
315
316 return SearchPtr;
317 }
318
319 /*******************************************************************************
320
321 End of File
322
323 *******************************************************************************/
324