- Set a default PCM volume so that something can be heard when driver is
[AROS.git] / compiler / alib / alib_util.c
blob6911d1cf8e34a6c7135e642e03dea398b495a6b8
1 /*
2 Copyright © 1995-2010, The AROS Development Team. All rights reserved.
3 $Id$
5 Internal utility functions.
6 */
8 #include <aros/system.h>
9 #include "alib_intern.h"
11 #ifdef AROS_SLOWSTACKMETHODS
12 #include <intuition/classusr.h>
13 #include <proto/exec.h>
14 /******************************************************************************
16 NAME */
17 Msg GetMsgFromStack (
19 /* SYNOPSIS */
20 IPTR MethodID,
21 va_list args)
23 /* FUNCTION
24 Builds a message structure with the parameters which are passed on
25 the stack. This function is used on machines which have compilers
26 which don't pass the arguments to a varargs function unlike the
27 Amiga ones.
29 INPUTS
30 MethodID - This is the ID of the message
31 args - This has to be initialized by va_start()
32 firstlocal - The address of the first local function of the
33 function which wants to call GetMsgFromStack()
35 RESULT
36 A message which can be passed to any function which expects the
37 structure which is defined for this MethodID or NULL if something
38 failed. This call may fail for different reasons on different
39 systems. On some systems, NULL indicates that there was not enough
40 memory, on others that the MethodID is unknown.
42 NOTES
43 This function fails for structures with more than 32 fields.
45 EXAMPLE
47 BUGS
49 SEE ALSO
50 boopsi.library/NewObject(), boopsi.library/SetAttrs(), boopsi.library/GetAttr(),
51 boopsi.library/DisposeObject(), DoMethodA(),
52 DoSuperMethodA(), "Basic Object-Oriented Programming System for
53 Intuition" and the "boopsi Class Reference" Dokument.
55 INTERNALS
56 HPPA: Allocate a structure which can contain all IPTRs between
57 the first argument of, for example, DoMethod() and its first local
58 variable. This will copy a bit too much memory but in the end, it
59 saves a lot of work, since it's not neccessary to register every
60 structure.
62 HISTORY
63 22.11.96 digulla created
65 ******************************************************************************/
67 ULONG size;
68 Msg msg;
70 size = 33;
72 if ((msg = AllocVec (size * sizeof (IPTR), MEMF_CLEAR)))
74 IPTR * ulptr = (IPTR *)msg;
76 *ulptr ++ = MethodID;
78 while (-- size)
80 *ulptr ++ = va_arg (args, IPTR);
84 return msg;
85 } /* GetMsgFromStack */
87 /******************************************************************************
89 NAME */
90 void FreeMsgFromStack (
92 /* SYNOPSIS */
93 Msg msg)
95 /* FUNCTION
96 Frees the memory occupied by the message which was created by
97 GetMsgFromStack().
99 INPUTS
100 msg - The return value of GetMsgFromStack(). May be NULL.
102 RESULT
103 None.
105 NOTES
107 EXAMPLE
109 BUGS
111 SEE ALSO
112 GetMsgFromStack()
114 HISTORY
115 22.11.96 digulla created
117 ******************************************************************************/
119 if (msg)
120 FreeVec (msg);
121 } /* FreeMsgFromStack */
123 #endif /* AROS_SLOWSTACKMETHODS */
125 #ifdef AROS_SLOWSTACKTAGS
126 #include <stdarg.h>
127 #include <utility/tagitem.h>
128 #include <exec/memory.h>
129 #include <proto/exec.h>
130 /******************************************************************************
132 NAME */
133 struct TagItem * GetTagsFromStack (
135 /* SYNOPSIS */
136 IPTR firstTag,
137 va_list args)
139 /* FUNCTION
140 Builds a tagitem array with the tags on the stack. This function is
141 used on machines which have compilers which don't pass the
142 arguments to a varargs function unlike the Amiga ones.
144 INPUTS
145 firstTag - This is the first tag passed to the function
146 args - This has to be initialized by va_start()
148 RESULT
149 A TagItem array which can be passed to any function which expects
150 such an array or NULL if something failed. This call may fail for
151 different reasons on different systems. On some systems, NULL
152 indicates that there was not enough memory, on others that the
153 MethodID is unknown.
155 NOTES
157 EXAMPLE
159 BUGS
161 SEE ALSO
162 boopsi.library/NewObject(), boopsi.library/SetAttrs(), boopsi.library/GetAttr(),
163 boopsi.library/DisposeObject(), DoMethodA(),
164 DoSuperMethodA(), "Basic Object-Oriented Programming System for
165 Intuition" and the "boopsi Class Reference" Dokument.
167 INTERNALS
168 Allocate a structure which can contain all tags until the first
169 TAG_END. This takes into account TAG_MORE and the like. The code
170 will break if someone makes assumptions about the way taglists
171 are built in memory, ie. if he looks for the next TAG_MORE and
172 then simply skips it instead of following it.
174 HISTORY
175 22.11.96 digulla created
177 ******************************************************************************/
179 struct TagItem * ti;
180 IPTR tag;
181 ULONG size;
182 va_list ap;
184 va_copy(ap, args);
185 tag = firstTag;
187 for (size=0;;size++)
189 if (tag == TAG_END || tag == TAG_MORE)
191 size ++; /* Copy this tag, too */
192 break;
195 switch (tag)
197 case TAG_IGNORE:
198 (void) va_arg(args, IPTR);
199 size --; /* Don't copy this tag */
200 break;
202 case TAG_SKIP: {
203 ULONG skip;
205 skip = va_arg(args, IPTR);
207 while (skip --)
209 (void) va_arg(args, IPTR);
210 (void) va_arg(args, IPTR);
213 break; }
215 default:
216 (void) va_arg(args, IPTR);
219 tag = va_arg (args, IPTR);
222 tag = firstTag;
224 if ((ti = AllocVec (size*sizeof(struct TagItem), MEMF_ANY)))
226 for (size=0;;size++)
228 ti[size].ti_Tag = tag;
230 if (tag == TAG_END)
231 break;
232 else if (tag == TAG_MORE)
234 ti[size].ti_Data = (IPTR) va_arg (ap, struct TagItem *);
235 break;
238 switch (tag)
240 case TAG_IGNORE:
241 (void) va_arg(ap, IPTR);
242 size --; /* Don't copy this tag */
243 break;
245 case TAG_SKIP: {
246 ULONG skip;
248 skip = va_arg(ap, IPTR);
250 while (skip --)
252 (void) va_arg(ap, IPTR);
253 (void) va_arg(ap, IPTR);
256 break; }
258 default:
259 ti[size].ti_Data = va_arg(ap, IPTR);
262 tag = va_arg (ap, IPTR);
265 va_end(ap);
266 return ti;
267 } /* GetTagsFromStack */
269 /******************************************************************************
271 NAME */
272 void FreeTagsFromStack (
274 /* SYNOPSIS */
275 struct TagItem * tags)
277 /* FUNCTION
278 Frees the memory occupied by the tagitems which were created by
279 GetTagsFromStack().
281 INPUTS
282 tags - The return value of GetTagsFromStack(). May be NULL.
284 RESULT
285 None.
287 NOTES
289 EXAMPLE
291 BUGS
293 SEE ALSO
294 GetTagsFromStack()
296 HISTORY
297 22.11.96 digulla created
299 ******************************************************************************/
301 if (tags)
302 FreeVec (tags);
303 } /* FreeTagsFromStack */
305 /******************************************************************************
307 NAME */
308 APTR GetParamsFromStack (
310 /* SYNOPSIS */
311 va_list args)
313 /* FUNCTION
314 Builds an array of parameters which are passed on the stack.
315 This function is used on machines which have compilers which
316 don't pass the arguments to a varargs function unlike the
317 Amiga ones.
319 INPUTS
320 args - This has to be initialized by va_start()
322 RESULT
323 An array which can be passed to any function which expects the
324 structure or NULL if something failed. This call may fail for
325 different reasons on different systems. On some systems, NULL
326 indicates that there was not enough memory.
328 NOTES
329 This function fails for structures with more than 20 fields.
331 EXAMPLE
333 BUGS
335 SEE ALSO
336 CallHook()
338 INTERNALS
339 HPPA: Allocate a structure which can contain all APTRs between the
340 first variadic parameter of, for example, CallHook() and its first
341 local variable. This will copy a bit too much memory but in the end,
342 it saves a lot of work, since it's not neccessary to register every
343 structure.
345 HISTORY
346 25.04.08 sszymczy adapted from GetMsgFromStack()
348 ******************************************************************************/
350 ULONG size;
351 APTR params;
353 size = 21;
355 if ((params = AllocVec (size * sizeof (IPTR), MEMF_CLEAR)))
357 IPTR * ulptr = (IPTR *) params;
359 while (-- size)
361 *ulptr ++ = va_arg (args, IPTR);
365 return params;
366 } /* GetParamsFromStack */
368 /******************************************************************************
370 NAME */
371 void FreeParamsFromStack (
373 /* SYNOPSIS */
374 APTR params)
376 /* FUNCTION
377 Frees the memory occupied by the parameters array which was
378 created by GetParamsFromStack().
380 INPUTS
381 params - The return value of GetParamsFromStack(). May be NULL.
383 RESULT
384 None.
386 NOTES
388 EXAMPLE
390 BUGS
392 SEE ALSO
393 GetParamsFromStack()
395 HISTORY
396 25.04.08 sszymczy adapted from FreeMsgFromStack()
398 ******************************************************************************/
400 if (params)
401 FreeVec (params);
402 } /* FreeParamsFromStack */
404 #endif /* AROS_SLOWSTACKTAGS */