… | |
… | |
39 | #endif |
39 | #endif |
40 | } |
40 | } |
41 | |
41 | |
42 | =item ecb_noinline |
42 | =item ecb_noinline |
43 | |
43 | |
44 | Prevent a function from being inlined - it might be optimsied away, but |
44 | Prevent a function from being inlined - it might be optimised away, but |
45 | not inlined into other functions. This is useful if you know your function |
45 | not inlined into other functions. This is useful if you know your function |
46 | is rarely called and large enough for inlining not to be helpful. |
46 | is rarely called and large enough for inlining not to be helpful. |
47 | |
47 | |
48 | =item ecb_noreturn |
48 | =item ecb_noreturn |
49 | |
49 | |
… | |
… | |
114 | |
114 | |
115 | However, by using C<ecb_likely>, you tell the compiler that the condition |
115 | However, by using C<ecb_likely>, you tell the compiler that the condition |
116 | is likely to be true (and for C<ecb_unlikel>, that it is unlikely to be |
116 | is likely to be true (and for C<ecb_unlikel>, that it is unlikely to be |
117 | true). |
117 | true). |
118 | |
118 | |
119 | For example, when you check for a 0-ptr and expect this to be a rare, |
119 | For example, when you check for a null pointer and expect this to be a |
120 | exceptional, case, then use C<ecb_unlikely>: |
120 | rare, exceptional, case, then use C<ecb_unlikely>: |
121 | |
121 | |
122 | void my_free (void *ptr) |
122 | void my_free (void *ptr) |
123 | { |
123 | { |
124 | if (ecb_unlikely (ptr == 0)) |
124 | if (ecb_unlikely (ptr == 0)) |
125 | return; |
125 | return; |
… | |
… | |
129 | tell the compiler what the hot path through a function is can increase |
129 | tell the compiler what the hot path through a function is can increase |
130 | performance considerably. |
130 | performance considerably. |
131 | |
131 | |
132 | A very good example is in a function that reserves more space for some |
132 | A very good example is in a function that reserves more space for some |
133 | memory block (for example, inside an implementation of a string stream) - |
133 | memory block (for example, inside an implementation of a string stream) - |
134 | eahc time something is added, you have to check for a buffer overrun, but |
134 | each time something is added, you have to check for a buffer overrun, but |
135 | you expect that most checks will turn out to be false: |
135 | you expect that most checks will turn out to be false: |
136 | |
136 | |
137 | /* make sure we have "size" extra room in our buffer */ |
137 | /* make sure we have "size" extra room in our buffer */ |
138 | ecb_inline void |
138 | ecb_inline void |
139 | reserve (int size) |
139 | reserve (int size) |
… | |
… | |
173 | call will never be executed. |
173 | call will never be executed. |
174 | |
174 | |
175 | =item bool ecb_unreachable () |
175 | =item bool ecb_unreachable () |
176 | |
176 | |
177 | This function does nothing itself, except tell the compiler that it will |
177 | This function does nothing itself, except tell the compiler that it will |
178 | never be executed. Apart from supressing a warning in some cases, this |
178 | never be executed. Apart from suppressing a warning in some cases, this |
179 | function can be used to implement C<ecb_assume> or similar functions. |
179 | function can be used to implement C<ecb_assume> or similar functions. |
180 | |
180 | |
181 | =item bool ecb_prefetch (addr, rw, locality) [MACRO] |
181 | =item bool ecb_prefetch (addr, rw, locality) [MACRO] |
182 | |
182 | |
183 | Tells the compiler to try to prefetch memory at the given C<addr>ess |
183 | Tells the compiler to try to prefetch memory at the given C<addr>ess |
… | |
… | |
187 | something... in between. The memory pointed to by the address does not |
187 | something... in between. The memory pointed to by the address does not |
188 | need to be accessible (it could be a null pointer for example), but C<rw> |
188 | need to be accessible (it could be a null pointer for example), but C<rw> |
189 | and C<locality> must be compile-time constants. |
189 | and C<locality> must be compile-time constants. |
190 | |
190 | |
191 | An obvious way to use this is to prefetch some data far away, in a big |
191 | An obvious way to use this is to prefetch some data far away, in a big |
192 | array you loop over. This prefethces memory some 128 array elements later, |
192 | array you loop over. This prefetches memory some 128 array elements later, |
193 | in the hope that it will be ready when the CPU arrives at that location. |
193 | in the hope that it will be ready when the CPU arrives at that location. |
194 | |
194 | |
195 | int sum = 0; |
195 | int sum = 0; |
196 | |
196 | |
197 | for (i = 0; i < N; ++i) |
197 | for (i = 0; i < N; ++i) |