Initial revision
[ssr] / StraySrc / Glass / !Glass / c / indir
1 /*
2 * indir.c
3 *
4 * Control of memory allocation
5 *
6 * © 1994-1998 Straylight
7 */
8
9 /*----- Licensing note ----------------------------------------------------*
10 *
11 * This file is part of Straylight's Glass.
12 *
13 * Glass is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
16 * any later version.
17 *
18 * Glass is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with Glass. If not, write to the Free Software Foundation,
25 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 /*
31 * ANSI standard headers
32 */
33
34 #include <stdlib.h>
35
36 /*
37 * Steel headers
38 */
39
40 #define _STDAPP
41 #include "steel/Steel.h"
42
43 #include "steel/buttons.h"
44 #include "steel/bbc.h"
45
46
47 extern void flex_dump(const char *p);
48
49 /*
50 * Glass headers
51 */
52
53 #include "gIcons.h"
54
55 #include "glass.h"
56 #include "heap.h"
57 #include "indir.h"
58
59 /*----- Static variables --------------------------------------------------*/
60
61 static heap_infostr indir__info;
62 static BOOL indir__outOfDate;
63 static dbox indir__dbox;
64
65 /*----- Support routines --------------------------------------------------*/
66
67 /*
68 * void indir__update(void *handle)
69 *
70 * Use
71 * Updates the heap info window if necessary
72 */
73
74 static void indir__update(void *handle)
75 {
76 unused(handle);
77 if (indir__dbox)
78 {
79 indir__info=heap_info();
80 dbox_setfield(indir__dbox,
81 glass_HEAPSIZE,
82 "%s",
83 utils_cvtSize(indir__info.size));
84 dbox_setfield(indir__dbox,
85 glass_FREESIZE,
86 "%s",
87 utils_cvtSize(indir__info.free));
88 dbox_setfield(indir__dbox,
89 glass_LARGEST,
90 "%s",
91 utils_cvtSize(indir__info.largest));
92 buttons_updateSlider(indir__dbox,
93 glass_HEAPSLIDE,
94 indir__info.size,
95 indir__info.size-indir__info.free,
96 11,
97 FALSE);
98 }
99 if (indir__outOfDate)
100 {
101 win_removeIdleClaimer(indir__update,0);
102 indir__outOfDate=FALSE;
103 }
104 }
105
106 /*----- Event handlers ----------------------------------------------------*/
107
108 /*
109 * void indir__dboxRedraw(wimp_redrawstr *r,void *handle)
110 *
111 * Use
112 * Redraws the dbox used to show heap information
113 *
114 * Parameters
115 * wimp_redrawstr *r == information about the redraw
116 * void *handle == a pointer (ignored)
117 */
118
119 static void indir__dboxRedraw(wimp_redrawstr *r,void *handle)
120 {
121 unused(r);
122 unused(handle);
123 buttons_redrawSlider(indir__dbox,glass_HEAPSLIDE,indir__info.size,
124 indir__info.size-indir__info.free,11,FALSE);
125 }
126
127 /*
128 * BOOL indir__dboxRaw(dbox d,wimp_eventstr *e,void *handle)
129 *
130 * Use
131 * Handles raw events (just redraw requests for the moment) for the heap
132 * info dialogue.
133 *
134 * Parameters
135 * dbox d == the dbox handle
136 * wimp_eventstr *e == the event that occurred
137 * void *handle == a pointer (ignored)
138 *
139 * Returns
140 * TRUE if the event has been dealt with
141 */
142
143 static BOOL indir__dboxRaw(dbox d,wimp_eventstr *e,void *handle)
144 {
145 BOOL handled=FALSE;
146 unused(d);
147 unused(handle);
148 switch (e->e)
149 {
150 case wimp_EREDRAW:
151 wimpt_redraw(indir__dboxRedraw,0);
152 handled=TRUE;
153 break;
154 }
155 return (handled);
156 }
157
158 /*
159 * void indir__dboxHandler(dbox d,dbox_field f,void *handle)
160 *
161 * Use
162 * Handles events for the heap info dialogue box.
163 *
164 * Parameters
165 * dbox d == the dbox handle
166 * dbox_field f == the event that occurred
167 * void *handle == a pointer (ignored)
168 */
169
170 static void indir__dboxHandler(dbox d,dbox_field f,void *handle)
171 {
172 unused(handle);
173 switch (f)
174 {
175 case dbox_CLOSE:
176 dbox_delete(d);
177 win_remove_idle_claimer(indir__update,0);
178 indir__dbox=0;
179 break;
180 #ifdef _dll_NODLL
181 case 9:
182 dbox_clickicon(d,f);
183 dbox_unclick();
184 bbc_vdu(26);
185 bbc_vdu(4);
186 bbc_vdu(12);
187 bbc_vdu(14);
188 flex_dump("Glass flex dump");
189 bbc_get();
190 bbc_vdu(15);
191 bbc_vdu(5);
192 wimpt_forceredraw();
193 break;
194 #endif
195 case dbox_HELP:
196 help_startHelp();
197 help_addLine(msgs_lookup("idhHPINF"));
198 help_endHelp();
199 break;
200 }
201 }
202
203 /*----- External routines -------------------------------------------------*/
204
205 /*
206 * void indir_init(void)
207 *
208 * Use
209 * Jumps in to ensure that indir gets the first flex block, so it doesn't
210 * move.
211 */
212
213 void indir_init(void)
214 {
215 heap_init();
216 }
217
218 /*
219 * void *indir_alloc(int size)
220 *
221 * Use
222 * Allocate memory from heap.
223 *
224 * Parameters
225 * int size == the number of bytes the caller wants
226 */
227
228 void *indir_alloc(size_t size)
229 {
230 if (!indir__outOfDate)
231 {
232 indir__outOfDate=TRUE;
233 win_addIdleClaimer(indir__update,win_DONTCARE,0);
234 }
235 return (heap_alloc(size));
236 }
237
238 /*
239 * void indir_free(void *p)
240 *
241 * Use
242 * Reclaims the memory from the block pointed to by p. Some simple checks
243 * are used to ensure that p is valid.
244 *
245 * Parameters
246 * void *p == pointer to a block to free.
247 */
248
249 void indir_free(void *p)
250 {
251 if (!indir__outOfDate)
252 {
253 indir__outOfDate=TRUE;
254 win_addIdleClaimer(indir__update,win_DONTCARE,0);
255 }
256 heap_free(p);
257 }
258
259 /*
260 * void *indir_realloc(void *p,int newsize)
261 *
262 * Use
263 * Resizes a heap block.
264 *
265 * Parameters
266 * void *p == pointer to block to resize
267 * int newsize == the new size to make it
268 *
269 * Returns
270 * Pointer to the block (may have moved) or 0 (failure, block didn't move)
271 */
272
273 void *indir_realloc(void *p,int newsize)
274 {
275 if (!indir__outOfDate)
276 {
277 indir__outOfDate=TRUE;
278 win_addIdleClaimer(indir__update,win_DONTCARE,0);
279 }
280 return (heap_realloc(p,newsize));
281 }
282
283 /*
284 * void indir_heapInfo(void)
285 *
286 * Use
287 * Displays a dialogue box showing current heap stats
288 */
289
290 void indir_heapInfo(void)
291 {
292 if (!indir__dbox)
293 {
294 indir__dbox=dbox_create("heapInfo");
295 indir__outOfDate=FALSE;
296 indir__update(0);
297 #ifndef _dll_NODLL
298 dbox_shadeicon(indir__dbox,9,TRUE);
299 #endif
300 dbox_eventHandler(indir__dbox,indir__dboxHandler,0);
301 dbox_rawEventHandler(indir__dbox,indir__dboxRaw,0);
302 }
303 dbox_display(indir__dbox,dbox_STATIC_LASTPOS);
304 }