medfall

A super great game engine
Log | Files | Refs

signature.hpp (1780B)


      1 /*  Relacy Race Detector
      2  *  Copyright (c) 2008-2013, Dmitry S. Vyukov
      3  *  All rights reserved.
      4  *  This software is provided AS-IS with no warranty, either express or implied.
      5  *  This software is distributed under a license and may not be copied,
      6  *  modified or distributed except as expressly authorized under the
      7  *  terms of the license contained in the file LICENSE in this distribution.
      8  */
      9 
     10 #ifndef RL_SIGNATURE_HPP
     11 #define RL_SIGNATURE_HPP
     12 #ifdef _MSC_VER
     13 #   pragma once
     14 #endif
     15 
     16 #include "base.hpp"
     17 #include "test_result.hpp"
     18 #include "context_base.hpp"
     19 
     20 
     21 namespace rl
     22 {
     23 
     24 
     25 template<unsigned magic>
     26 class signature
     27 {
     28 public:
     29     signature()
     30         : magic_(magic)
     31     {
     32     }
     33 
     34     signature(signature const&)
     35         : magic_(magic)
     36     {
     37     }
     38 
     39     ~signature()
     40     {
     41         check(RL_INFO);
     42         magic_ = 0;
     43     }
     44 
     45     void check(debug_info_param info) const
     46     {
     47         if (
     48             ((uintptr_t)this <= (uintptr_t)-1 - 4096) && 
     49             ((uintptr_t)this >= 4096) &&
     50             ((uintptr_t)this % sizeof(unsigned) == 0) && (magic == magic_))
     51         {
     52             return;
     53         }
     54         else
     55         {
     56             fail(info);
     57         }
     58     }
     59 
     60 private:
     61     unsigned magic_;
     62 
     63     struct fault_event
     64     {
     65         void const* addr_;
     66         void output(std::ostream& s) const
     67         {
     68             s << "<" << std::hex << addr_ << std::dec << ">"
     69                 << " access to freed memory";
     70         }
     71     };
     72 
     73     RL_NOINLINE void fail(debug_info_param info) const
     74     {
     75         context& c = ctx();
     76         RL_HIST(fault_event) {this} RL_HIST_END();
     77         rl::ctx().fail_test("access to freed memory", test_result_access_to_freed_memory, info);
     78     }
     79 };
     80 
     81 
     82 }
     83 
     84 #endif