LCOV - code coverage report
Current view: top level - include/util - misc.h (source / functions) Hit Total Coverage
Test: CoherentDB code coverage Lines: 14 14 100.0 %
Date: 2011-02-13 Functions: 9 10 90.0 %

          Line data    Source code
       1             : /*
       2             :  * (C) Copyright 2010 Marek Dopiera
       3             :  * 
       4             :  * This file is part of CoherentDB.
       5             :  * 
       6             :  * CoherentDB is free software: you can redistribute it and/or modify it
       7             :  * under the terms of the GNU General Public License as published by
       8             :  * the Free Software Foundation, either version 3 of the License, or
       9             :  * (at your option) any later version.
      10             :  * 
      11             :  * CoherentDB is distributed in the hope that it will be useful, but
      12             :  * WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
      14             :  * General Public License for more details.
      15             :  * 
      16             :  * You should have received a copy of the GNU General Public
      17             :  * License along with CoherentDB. If not, see
      18             :  * http://www.gnu.org/licenses/.
      19             :  */
      20             : 
      21             : #ifndef MISC_H_2345
      22             : #define MISC_H_2345
      23             : 
      24             : #include <cassert>
      25             : 
      26             : #define stringify(s) stringify_helper(s)
      27             : #define stringify_helper(s) #s
      28             : 
      29             : #ifdef __GNUC__
      30             : #define likely(x) __builtin_expect(!!(x), 1)
      31             : #define unlikely(x) __builtin_expect(!!(x), 0)
      32             : #else
      33             : #define likely(x) (x)
      34             : #define unlikely(x) (x)
      35             : #endif
      36             : 
      37             : #ifdef VALGRIND
      38             : #define VALGRIND_SLOWDOWN 1000
      39             : #else
      40             : #define VALGRIND_SLOWDOWN 1
      41             : #endif
      42             : 
      43             : namespace coherent {
      44             : namespace util {
      45             : 
      46             : template <class T>
      47        2036 : bool is_pow2(T v)
      48             : {
      49       26271 :         while (v != 0)
      50             :         {
      51       26270 :                 if ((v & 1) == 1)
      52        2035 :                         return v == 1;
      53       24235 :                 v = v >> 1;
      54             :         }
      55        2036 :         return false;
      56             : }
      57             : 
      58             : template <class T, class A>
      59        2024 : inline T align_down(T v, A alignment)
      60             : {
      61             :         //can't use d_assert here, because the file is included by asserts.h
      62        2024 :         assert(is_pow2(alignment));
      63        2024 :         return v - (v & (alignment - 1));
      64             : }
      65             : 
      66             : template <class T, class A>
      67        1012 : inline T align_up(T v, A alignment)
      68             : {
      69        1012 :         T res = align_down(v, alignment);
      70        1012 :         return (res < v) ? (res + alignment) : res;
      71             : }
      72             : 
      73             : class virtual_dest
      74         151 : {
      75             : public:
      76         151 :         virtual ~virtual_dest() {}
      77             : };
      78             : 
      79             : } // namespace util
      80             : } // namespace coherent
      81             : 
      82             : #endif /* MISC_H_2345 */

Generated by: LCOV version 1.9