Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
EventHandler.cxx
Go to the documentation of this file.
1
36
37namespace openlcb
38{
39
40EventRegistry::EventRegistry()
41{
42}
43
44EventRegistry::~EventRegistry()
45{
46}
47
48// static
49unsigned EventRegistry::align_mask(EventId *event, unsigned size)
50{
51 // example: size = 140. highest bit set is bit 7
52 // example2: size = 256. highest bit set is bit 8, but it is power-of-two.
53 HASSERT(event);
54 if (size <= 1)
55 {
56 return 0;
57 }
58 // should be 7 in both examples
59 unsigned log2 = sizeof(size) * 8 - __builtin_clz(size - 1) - 1;
60 uint64_t new_event, rounded_range;
61 do
62 {
63 ++log2; // 8 in both examples
64 rounded_range = 1ULL << log2; // 256
65 new_event = *event & (~(rounded_range - 1));
66 // we have to be careful for overflowing uint64 in new_event =
67 // rounded_range
68 } while (log2 < 64 &&
69 ((new_event + (rounded_range - 1)) < (*event + (size - 1))));
70 if (log2 >= 64)
71 {
72 new_event = 0;
73 log2 = 64;
74 }
75 // The rounding is successful.
76 *event = new_event;
77 return log2;
78}
79
80} /* namespace openlcb */
static unsigned align_mask(EventId *event, unsigned size)
Computes the alignment mask for registering an event range.
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138