A buffer variable wasn't dereferenced in two similar comparisons in ResList
[AROS.git] / compiler / alib / alib_util.c
blob88569820468af75f417b1ff494b9ddebc3cd4817
1 /*
2 Copyright © 1995-2013, 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 intuition.library/NewObjectA(), intuition.library/SetAttrsA(), intuition.library/GetAttr(),
51 intuition.library/DisposeObject(), DoMethodA(),
52 DoSuperMethodA(), "Basic Object-Oriented Programming System for
53 Intuition" and the "boopsi Class Reference" documentation.
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 ******************************************************************************/
64 ULONG size;
65 Msg msg;
67 size = 33;
69 if ((msg = AllocVec (size * sizeof (IPTR), MEMF_CLEAR)))
71 IPTR * ulptr = (IPTR *)msg;
73 *ulptr ++ = MethodID;
75 while (-- size)
77 *ulptr ++ = va_arg (args, IPTR);
81 return msg;
82 } /* GetMsgFromStack */
84 /******************************************************************************
86 NAME */
87 void FreeMsgFromStack (
89 /* SYNOPSIS */
90 Msg msg)
92 /* FUNCTION
93 Frees the memory occupied by the message which was created by
94 GetMsgFromStack().
96 INPUTS
97 msg - The return value of GetMsgFromStack(). May be NULL.
99 RESULT
100 None.
102 NOTES
104 EXAMPLE
106 BUGS
108 SEE ALSO
109 GetMsgFromStack()
111 ******************************************************************************/
113 FreeVec(msg);
114 } /* FreeMsgFromStack */
116 #endif /* AROS_SLOWSTACKMETHODS */
118 #ifdef AROS_SLOWSTACKTAGS
119 #include <stdarg.h>
120 #include <utility/tagitem.h>
121 #include <exec/memory.h>
122 #include <proto/exec.h>
123 /******************************************************************************
125 NAME */
126 struct TagItem * GetTagsFromStack (
128 /* SYNOPSIS */
129 IPTR firstTag,
130 va_list args)
132 /* FUNCTION
133 Builds a tagitem array with the tags on the stack. This function is
134 used on machines which have compilers which don't pass the
135 arguments to a varargs function unlike the Amiga ones.
137 INPUTS
138 firstTag - This is the first tag passed to the function
139 args - This has to be initialized by va_start()
141 RESULT
142 A TagItem array which can be passed to any function which expects
143 such an array or NULL if something failed. This call may fail for
144 different reasons on different systems. On some systems, NULL
145 indicates that there was not enough memory, on others that the
146 MethodID is unknown.
148 NOTES
150 EXAMPLE
152 BUGS
154 SEE ALSO
155 intuition.library/NewObjectA(), intuition.library/SetAttrsA(), intuition.library/GetAttr(),
156 intuition.library/DisposeObject(), DoMethodA(),
157 DoSuperMethodA(), "Basic Object-Oriented Programming System for
158 Intuition" and the "boopsi Class Reference" documentation.
160 INTERNALS
161 Allocate a structure which can contain all tags until the first
162 TAG_END. This takes into account TAG_MORE and the like. The code
163 will break if someone makes assumptions about the way taglists
164 are built in memory, ie. if he looks for the next TAG_MORE and
165 then simply skips it instead of following it.
167 ******************************************************************************/
169 struct TagItem * ti;
170 IPTR tag;
171 ULONG size;
172 va_list ap;
174 va_copy(ap, args);
175 tag = firstTag;
177 for (size=0;;size++)
179 if (tag == TAG_END || tag == TAG_MORE)
181 size ++; /* Copy this tag, too */
182 break;
185 switch (tag)
187 case TAG_IGNORE:
188 (void) va_arg(args, IPTR);
189 size --; /* Don't copy this tag */
190 break;
192 case TAG_SKIP: {
193 ULONG skip;
195 skip = va_arg(args, IPTR);
197 while (skip --)
199 (void) va_arg(args, IPTR);
200 (void) va_arg(args, IPTR);
203 break; }
205 default:
206 (void) va_arg(args, IPTR);
209 tag = va_arg (args, IPTR);
212 tag = firstTag;
214 if ((ti = AllocVec (size*sizeof(struct TagItem), MEMF_ANY)))
216 for (size=0;;size++)
218 ti[size].ti_Tag = tag;
220 if (tag == TAG_END)
221 break;
222 else if (tag == TAG_MORE)
224 ti[size].ti_Data = (IPTR) va_arg (ap, struct TagItem *);
225 break;
228 switch (tag)
230 case TAG_IGNORE:
231 (void) va_arg(ap, IPTR);
232 size --; /* Don't copy this tag */
233 break;
235 case TAG_SKIP: {
236 ULONG skip;
238 skip = va_arg(ap, IPTR);
240 while (skip --)
242 (void) va_arg(ap, IPTR);
243 (void) va_arg(ap, IPTR);
246 break; }
248 default:
249 ti[size].ti_Data = va_arg(ap, IPTR);
252 tag = va_arg (ap, IPTR);
255 va_end(ap);
256 return ti;
257 } /* GetTagsFromStack */
259 /******************************************************************************
261 NAME */
262 void FreeTagsFromStack (
264 /* SYNOPSIS */
265 struct TagItem * tags)
267 /* FUNCTION
268 Frees the memory occupied by the tagitems which were created by
269 GetTagsFromStack().
271 INPUTS
272 tags - The return value of GetTagsFromStack(). May be NULL.
274 RESULT
275 None.
277 NOTES
279 EXAMPLE
281 BUGS
283 SEE ALSO
284 GetTagsFromStack()
286 ******************************************************************************/
288 FreeVec(tags);
289 } /* FreeTagsFromStack */
291 /******************************************************************************
293 NAME */
294 APTR GetParamsFromStack (
296 /* SYNOPSIS */
297 va_list args)
299 /* FUNCTION
300 Builds an array of parameters which are passed on the stack.
301 This function is used on machines which have compilers which
302 don't pass the arguments to a varargs function unlike the
303 Amiga ones.
305 INPUTS
306 args - This has to be initialized by va_start()
308 RESULT
309 An array which can be passed to any function which expects the
310 structure or NULL if something failed. This call may fail for
311 different reasons on different systems. On some systems, NULL
312 indicates that there was not enough memory.
314 NOTES
315 This function fails for structures with more than 20 fields.
317 EXAMPLE
319 BUGS
321 SEE ALSO
322 CallHook()
324 INTERNALS
325 HPPA: Allocate a structure which can contain all APTRs between the
326 first variadic parameter of, for example, CallHook() and its first
327 local variable. This will copy a bit too much memory but in the end,
328 it saves a lot of work, since it's not neccessary to register every
329 structure.
331 ******************************************************************************/
333 ULONG size;
334 APTR params;
336 size = 21;
338 if ((params = AllocVec (size * sizeof (IPTR), MEMF_CLEAR)))
340 IPTR * ulptr = (IPTR *) params;
342 while (-- size)
344 *ulptr ++ = va_arg (args, IPTR);
348 return params;
349 } /* GetParamsFromStack */
351 /******************************************************************************
353 NAME */
354 void FreeParamsFromStack (
356 /* SYNOPSIS */
357 APTR params)
359 /* FUNCTION
360 Frees the memory occupied by the parameters array which was
361 created by GetParamsFromStack().
363 INPUTS
364 params - The return value of GetParamsFromStack(). May be NULL.
366 RESULT
367 None.
369 NOTES
371 EXAMPLE
373 BUGS
375 SEE ALSO
376 GetParamsFromStack()
378 ******************************************************************************/
380 FreeVec(params);
381 } /* FreeParamsFromStack */
383 #endif /* AROS_SLOWSTACKTAGS */