Note that there are some explanatory texts on larger screens.

plurals
  1. PODetect usage of macro? (errno)
    primarykey
    data
    text
    <p>This is very specific, and a bit difficult to explain, and quite likely impossible, but here goes. </p> <p>I want to implement <strong>&lt;errno.h&gt;</strong>. (My hobby project is implementing a Standard <strong>C</strong> library.)</p> <p>The naïve way to go about it is:</p> <pre><code>// in &lt;errno.h&gt; extern int errno; // in some .c file int errno = 0; </code></pre> <p>This works. But it has one disadvantage: If a math library function is called, it <em>always</em> has to query the FPU status after execution to set <code>errno</code> as appropriate. This stalls the FPU in math-heavy applications.</p> <p>The <strong>C</strong> standard endorses lazy evaluation of <code>errno</code> by making it a macro instead:</p> <pre><code>int * __errno(); #define errno *__errno() </code></pre> <p>This way, <code>errno</code> is only "set" when its value is actually requested:</p> <pre><code>// "__errno()" returns the location of the current errno value, // the "errno" macro turns it into a value. x = errno; </code></pre> <p>Given some logic in the rest of the library, the FPU status needs only be queried if the last library function called was actually one <em>using</em> the FPU, <em>and the value of <code>errno</code> is actually requested</em>.</p> <p>So far, everything is fine. But it's the other way around that is giving me headaches:</p> <pre><code>errno = 0; </code></pre> <p>The value of <code>errno</code> is not requested at all. <em>But <code>__errno()</code> does not know that, and will query the FPU status if the last library function called was using the FPU.</em></p> <p>Right now I don't see a way to avoid that (i.e., have the <code>errno</code> macro or the <code>__errno()</code> function somehow work differently depending on whether they are used to the left or the right of the assignment operator), and am almost content to accept this.</p> <p>But perhaps any of you SO'ers has a brilliant idea?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload