replace malloc/calloc/free with snapshot_{malloc/calloc/free}
[c11tester.git] / clockvector.cc
1 #include <algorithm>
2 #include <cstring>
3 #include <stdlib.h>
4
5 #include "model.h"
6 #include "action.h"
7 #include "clockvector.h"
8 #include "common.h"
9
10 /**
11  * Constructs a new ClockVector, given a parent ClockVector and a first
12  * ModelAction. This constructor can assign appropriate default settings if no
13  * parent and/or action is supplied.
14  * @param parent is the previous ClockVector to inherit (i.e., clock from the
15  * same thread or the parent that created this thread)
16  * @param act is an action with which to update the ClockVector
17  */
18 ClockVector::ClockVector(ClockVector *parent, ModelAction *act)
19 {
20         num_threads = model->get_num_threads();
21         clock = (modelclock_t *)snapshot_calloc(num_threads, sizeof(int));
22         if (parent)
23                 std::memcpy(clock, parent->clock, parent->num_threads * sizeof(modelclock_t));
24
25         if (act)
26                 clock[id_to_int(act->get_tid())] = act->get_seq_number();
27 }
28
29 /** @brief Destructor */
30 ClockVector::~ClockVector()
31 {
32         snapshot_free(clock);
33 }
34
35 /**
36  * Merge a clock vector into this vector, using a pairwise comparison. The
37  * resulting vector length will be the maximum length of the two being merged.
38  * @param cv is the ClockVector being merged into this vector.
39  */
40 void ClockVector::merge(const ClockVector *cv)
41 {
42         modelclock_t *clk = clock;
43         bool resize = false;
44
45         ASSERT(cv != NULL);
46
47         if (cv->num_threads > num_threads) {
48                 resize = true;
49                 clk = (modelclock_t *)snapshot_malloc(cv->num_threads * sizeof(modelclock_t));
50         }
51
52         /* Element-wise maximum */
53         for (int i = 0; i < num_threads; i++)
54                 clk[i] = std::max(clock[i], cv->clock[i]);
55
56         if (resize) {
57                 for (int i = num_threads; i < cv->num_threads; i++)
58                         clk[i] = cv->clock[i];
59                 num_threads = cv->num_threads;
60                 snapshot_free(clock);
61         }
62         clock = clk;
63 }
64
65 /**
66  * Check whether this vector's thread has synchronized with another action's
67  * thread. This effectively checks the happens-before relation (or actually,
68  * happens after), but it's easier to compare two ModelAction events directly,
69  * using ModelAction::happens_before.
70  *
71  * @see ModelAction::happens_before
72  *
73  * @return true if this ClockVector's thread has synchronized with act's
74  * thread, false otherwise. That is, this function returns:
75  * <BR><CODE>act <= cv[act->tid]</CODE>
76  */
77 bool ClockVector::synchronized_since(const ModelAction *act) const
78 {
79         int i = id_to_int(act->get_tid());
80
81         if (i < num_threads)
82                 return act->get_seq_number() <= clock[i];
83         return false;
84 }
85
86 bool ClockVector::has_synchronized_with(const ClockVector *cv) const
87 {
88         ASSERT(cv);
89         if (cv->num_threads > num_threads)
90                 return false;
91         for (int i = 0; i < cv->num_threads; i++)
92                 if (cv->clock[i] > clock[i])
93                         return false;
94         return true;
95 }
96
97 /** Gets the clock corresponding to a given thread id from the clock vector. */
98 modelclock_t ClockVector::getClock(thread_id_t thread) {
99         int threadid = id_to_int(thread);
100
101         if (threadid < num_threads)
102                 return clock[threadid];
103         else
104                 return 0;
105 }
106
107 /** @brief Formats and prints this ClockVector's data. */
108 void ClockVector::print() const
109 {
110         int i;
111         printf("CV: (");
112         for (i = 0; i < num_threads; i++)
113                 printf("%2u%s", clock[i], (i == num_threads - 1) ? ")\n" : ", ");
114 }