replicode
model_base.h
Go to the documentation of this file.
1 // model_base.h
2 //
3 // Author: Eric Nivel
4 //
5 // BSD license:
6 // Copyright (c) 2010, Eric Nivel
7 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are met:
10 //
11 // - Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // - Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 // - Neither the name of Eric Nivel nor the
17 // names of their contributors may be used to endorse or promote products
18 // derived from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
24 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef model_base_h
32 #define model_base_h
33 
34 #include "factory.h"
35 
36 
37 namespace r_exec {
38 
39 class _Mem;
40 
47 class ModelBase {
48  friend class _Mem;
49 private:
51 
52  std::mutex m_mdlMutex;
53 
55 
56  class MEntry {
57  private:
58  static bool Match(Code *lhs, Code *rhs);
60  static uint64_t _ComputeHashCode(_Fact *component);
61  public:
62  static uint64_t ComputeHashCode(Code *mdl, bool packed);
63 
64  MEntry();
65  MEntry(Code *mdl, bool packed);
66 
71 
72  bool match(const MEntry &e) const;
73 
74  class Hash {
75  public:
76  size_t operator()(MEntry e) const {
77  return e.hash_code;
78  }
79  };
80 
81  class Equal {
82  public:
83  bool operator()(const MEntry lhs, const MEntry rhs) const {
84  return lhs.match(rhs);
85  }
86  };
87  };
88 
89  typedef std::unordered_set<MEntry, typename MEntry::Hash, typename MEntry::Equal> MdlSet;
90 
92  MdlSet black_list;
93 
95  MdlSet white_list;
96 
98  void set_thz(uint64_t thz) {
99  this->thz = thz;
100  }
101 
103  void trim_objects();
104 
105  ModelBase();
106 public:
107  static ModelBase *Get();
108 
111  void load(Code *mdl);
112  void get_models(r_code::list<P<Code> > &models); // white_list first, black_list next.
113 
115  Code *check_existence(Code *mdl);
116 
119  void check_existence(Code *m0, Code *m1, Code *&_m0, Code *&_m1);
120 
123  void register_mdl_failure(Code *mdl);
124 
127  void register_mdl_timeout(Code *mdl);
128 };
129 }
130 
131 
132 #endif
static uint64_t ComputeHashCode(Code *mdl, bool packed)
Definition: model_base.cpp:73
Definition: factory.h:98
uint64_t touch_time
last time the mdl was successfully compared to.
Definition: model_base.h:69
ModelBase()
Definition: model_base.cpp:158
MdlSet black_list
mdls are already packed when inserted (they come from the white list).
Definition: model_base.h:92
std::unordered_set< MEntry, typename MEntry::Hash, typename MEntry::Equal > MdlSet
Definition: model_base.h:89
void register_mdl_failure(Code *mdl)
Definition: model_base.cpp:274
void load(Code *mdl)
Definition: model_base.cpp:176
Code * check_existence(Code *mdl)
caveat: mdl is unpacked; return (a) NULL if the model is in the black list, (b) a model in the white...
Definition: model_base.cpp:195
Definition: _context.cpp:34
std::mutex m_mdlMutex
Definition: model_base.h:52
P< Code > mdl
Definition: model_base.h:67
MEntry()
Definition: model_base.cpp:116
void get_models(r_code::list< P< Code > > &models)
Definition: model_base.cpp:185
uint64_t hash_code
Definition: model_base.h:70
Definition: mem.h:60
void trim_objects()
called by _Mem::GC().
Definition: model_base.cpp:163
void register_mdl_timeout(Code *mdl)
Definition: model_base.cpp:282
Definition: model_base.h:56
size_t operator()(MEntry e) const
Definition: model_base.h:76
static bool Match(Code *lhs, Code *rhs)
Definition: model_base.cpp:93
static ModelBase * Get()
Definition: model_base.cpp:153
Definition: list.h:42
Definition: object.h:172
void set_thz(uint64_t thz)
called by _Mem::start(); set to secondary_thz.
Definition: model_base.h:98
Definition: model_base.h:47
static ModelBase * Singleton
Definition: model_base.h:50
bool operator()(const MEntry lhs, const MEntry rhs) const
Definition: model_base.h:83
uint64_t thz
Definition: model_base.h:54
bool match(const MEntry &e) const
Definition: model_base.cpp:122
Definition: model_base.h:81
MdlSet white_list
mdls are packed just before insertion.
Definition: model_base.h:95
Definition: model_base.h:74
static uint64_t _ComputeHashCode(_Fact *component)
use for lhs/rhs.
Definition: model_base.cpp:37