medfall

A super great game engine
Log | Files | Refs

context_addr_hash.hpp (2052B)


      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_CONTEXT_ADDR_HASH_HPP
     11 #define RL_CONTEXT_ADDR_HASH_HPP
     12 #ifdef _MSC_VER
     13 #   pragma once
     14 #endif
     15 
     16 #include "base.hpp"
     17 
     18 
     19 namespace rl
     20 {
     21 
     22 
     23 struct context_addr_hash_iface
     24 {
     25     virtual size_t      get_addr_hash               (void const* p) = 0;
     26     virtual             ~context_addr_hash_iface    () {} // to calm down g++
     27 };
     28 
     29 
     30 
     31 
     32 template<typename base_t, thread_id_t thread_count>
     33 class context_addr_hash_impl : protected base_t
     34 {
     35 public:
     36     context_addr_hash_impl(thread_id_t thread_count_param, test_params& params)
     37         : base_t(thread_count_param, params)
     38     {
     39     }
     40 
     41     void iteration_begin()
     42     {
     43         base_t::iteration_begin();
     44         hash_map_.clear();
     45         hash_seq_ = 0;
     46     }
     47 
     48 private:
     49     struct entry
     50     {
     51         uintptr_t       ptr_;
     52         size_t          hash_;
     53     };
     54     typedef map<void const*, size_t>::type  hash_map_t;
     55     hash_map_t                              hash_map_;
     56     size_t                                  hash_seq_;
     57 
     58     virtual size_t      get_addr_hash               (void const* p)
     59     {
     60         //!!! accept 'table size' to do 'hash % table_size'
     61         // will give more information for state exploration
     62 
     63         hash_map_t::iterator iter (hash_map_.find(p));
     64         if (iter != hash_map_.end() && iter->first == p)
     65         {
     66             return iter->second;
     67         }
     68         else
     69         {
     70             //!!! distribute hashes more randomly, use rand()
     71             size_t hash = hash_seq_++;
     72             hash_map_.insert(std::make_pair(p, hash));
     73             return hash;
     74         }
     75     }
     76 };
     77 
     78 
     79 }
     80 
     81 #endif