Disable optimizations on one function (was: 'pragma optimize' ...)

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

 



>> I have one more question related to the use of '#pragma GCC optimize'.
>> I have a zeroizer that must execute due to Certification and
>> Accreditation (C&A) requirements. When the zeroizer is removed as a
>> dead store, the compiler folks usually say, "but you asked for
>> optimizations...".
>>
>> If we cannot use '#pragma GCC optimize' to turn off the optimizer for
>> a function, then how do we tell the compiler to *not* remove the code?
>
> It is hard to tell without seeing the code. But see noclone, noinline
> entries at:
> http://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html
>

As a simplified example, imagine I have a function ClearAndFree:

    void ClearAndFree(void* ptr, size_t n)
    {
        if(!ptr) return;

        memset(ptr, 0x00, n);
        free(ptr);
    }

We've seen these type of functions have the memset optimized away.

Many folks try and cast ptr to volatile, but that's an abuse because
GCC considers volatile something for memory mapped hardware. Volatile
should not be used in an attempt to tame the optimizer.

The trick mentioned with noinlne looks promising. Can I use 'asm
("");' to stop the optimizer from removing the call to memset? If so,
does the trick work with GCC 4.2.1 and above?

(GCC 4.2.1 and above is required because OpenBSD still provides it).

Jeff



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux