bug-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: GNU attributes


From: Guus Leeuw jr.
Subject: Re: GNU attributes
Date: Tue, 25 Mar 2003 17:57:31 +0100
User-agent: KMail/1.5

Derek,

Taking a bit from the cluebook from gcc3.0.2, an optimizer may only reduce the 
number calls to the function, iff
a) the parameter values are guaranteed the same (i.e. static or non changing 
variable);
b) the global variable values used by the pure function are guaranteed to be 
the same from call to call;

If the optimizer decides a) and b) hold, you should be safe, even when 
malloc()ing. It will only compute the result once, a re-use it. But then, 
your semantics must re-use the firstly returned value as well (i.e. no calls 
to free() in between for the return value!).

If your semantics from within the function malloc() for a global variable, the 
optimizer should be wise enough to understand that the operating environment 
of the pure function changes from call to call. Hence b) doesn't hold, and it 
is not safe to 'call the function fewer times than the program says'.

If, yet again, your function malloc()s and free()s for a global variable the 
operating environment does not change, and it is safe to 'call the function 
fewer times than the program says'.

That's how I read it... So I guess it depends on the semantics of the function 
*harhar*

from the GCC Manual 
(http://www.sunsite.ualberta.ca/Documentation/Gnu/gcc-3.0.2/html_node/gcc_93.html)

pure 
Many functions have no effects except the return value and their return value 
depends only on the parameters and/or global variables. Such a function can 
be subject to common subexpression elimination and loop optimization just as 
an arithmetic operator would be. These functions should be declared with the 
attribute pure. For example, 
int square (int) __attribute__ ((pure));
says that the hypothetical function square is safe to call fewer times than 
the program says. 
Some of common examples of pure functions are strlen or memcmp. Interesting 
non-pure functions are functions with infinite loops or those depending on 
volatile memory or other system resource, that may change between two 
consecutive calls (such as feof in a multithreading environment). 
The attribute pure is not implemented in GCC versions earlier than 2.96. 

Cheers,
Guus

On Tuesday 25 March 2003 16:52, Derek Robert Price wrote:
> Anyone know whether a function that allocates memory can be considered
> "pure", in the GNU attribute sense of the term?
>
> Derek
>
> --
>                 *8^)
>
> Email: derek@ximbiot.com
>
> Get CVS support at <http://ximbiot.com>!





reply via email to

[Prev in Thread] Current Thread [Next in Thread]