2 This file is part of drd, a thread error detector.
4 Copyright (C) 2006-2020 Bart Van Assche <bvanassche@acm.org>.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
19 The GNU General Public License is contained in the file COPYING.
24 #include "pub_tool_basics.h" // Addr, SizeT
25 #include "pub_tool_libcassert.h" // tl_assert()
26 #include "pub_tool_libcbase.h" // VG_(memcpy)
27 #include "pub_tool_libcprint.h" // VG_(printf)
28 #include "pub_tool_mallocfree.h" // VG_(malloc), VG_(free)
31 /* Local function declarations. */
34 void DRD_(vc_reserve
)(VectorClock
* const vc
, const unsigned new_capacity
);
37 /* Function definitions. */
40 * Initialize the memory 'vc' points at as a vector clock with size 'size'.
41 * If the pointer 'vcelem' is not null, it is assumed to be an array with
42 * 'size' elements and it becomes the initial value of the vector clock.
44 void DRD_(vc_init
)(VectorClock
* const vc
,
45 const VCElem
* const vcelem
,
52 DRD_(vc_reserve
)(vc
, size
);
53 tl_assert(size
== 0 || vc
->vc
!= 0);
56 VG_(memcpy
)(vc
->vc
, vcelem
, size
* sizeof(vcelem
[0]));
59 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
64 /** Reset vc to the empty vector clock. */
65 void DRD_(vc_cleanup
)(VectorClock
* const vc
)
67 DRD_(vc_reserve
)(vc
, 0);
70 /** Copy constructor -- initializes *obj. */
71 void DRD_(vc_copy
)(VectorClock
* const obj
, const VectorClock
* const rhs
)
73 DRD_(vc_init
)(obj
, rhs
->vc
, rhs
->size
);
76 /** Assignment operator -- *lhs is already a valid vector clock. */
77 void DRD_(vc_assign
)(VectorClock
* const lhs
, const VectorClock
* const rhs
)
79 DRD_(vc_cleanup
)(lhs
);
80 DRD_(vc_copy
)(lhs
, rhs
);
83 /** Increment the clock of thread 'tid' in vector clock 'vc'. */
84 void DRD_(vc_increment
)(VectorClock
* const vc
, DrdThreadId
const tid
)
87 for (i
= 0; i
< vc
->size
; i
++)
89 if (vc
->vc
[i
].threadid
== tid
)
91 typeof(vc
->vc
[i
].count
) const oldcount
= vc
->vc
[i
].count
;
93 // Check for integer overflow.
94 tl_assert(oldcount
< vc
->vc
[i
].count
);
100 * The specified thread ID does not yet exist in the vector clock
104 const VCElem vcelem
= { tid
, 1 };
106 DRD_(vc_init
)(&vc2
, &vcelem
, 1);
107 DRD_(vc_combine
)(vc
, &vc2
);
108 DRD_(vc_cleanup
)(&vc2
);
113 * @return True if vector clocks vc1 and vc2 are ordered, and false otherwise.
114 * Order is as imposed by thread synchronization actions ("happens before").
116 Bool
DRD_(vc_ordered
)(const VectorClock
* const vc1
,
117 const VectorClock
* const vc2
)
119 return DRD_(vc_lte
)(vc1
, vc2
) || DRD_(vc_lte
)(vc2
, vc1
);
122 /** Compute elementwise minimum. */
123 void DRD_(vc_min
)(VectorClock
* const result
, const VectorClock
* const rhs
)
131 DRD_(vc_check
)(result
);
133 /* Next, combine both vector clocks into one. */
135 for (j
= 0; j
< rhs
->size
; j
++)
137 while (i
< result
->size
&& result
->vc
[i
].threadid
< rhs
->vc
[j
].threadid
)
139 /* Thread ID is missing in second vector clock. Clear the count. */
140 result
->vc
[i
].count
= 0;
143 if (i
>= result
->size
)
147 if (result
->vc
[i
].threadid
<= rhs
->vc
[j
].threadid
)
149 /* The thread ID is present in both vector clocks. Compute the */
150 /* minimum of vc[i].count and vc[j].count. */
151 tl_assert(result
->vc
[i
].threadid
== rhs
->vc
[j
].threadid
);
152 if (rhs
->vc
[j
].count
< result
->vc
[i
].count
)
154 result
->vc
[i
].count
= rhs
->vc
[j
].count
;
158 DRD_(vc_check
)(result
);
162 * Compute elementwise maximum.
164 void DRD_(vc_combine
)(VectorClock
* const result
, const VectorClock
* const rhs
)
174 // First count the number of shared thread id's.
177 for (i
= 0; i
< result
->size
; i
++)
179 while (j
< rhs
->size
&& rhs
->vc
[j
].threadid
< result
->vc
[i
].threadid
)
183 if (result
->vc
[i
].threadid
== rhs
->vc
[j
].threadid
)
187 DRD_(vc_check
)(result
);
189 new_size
= result
->size
+ rhs
->size
- shared
;
190 if (new_size
> result
->capacity
)
191 DRD_(vc_reserve
)(result
, new_size
);
193 DRD_(vc_check
)(result
);
195 // Next, combine both vector clocks into one.
197 for (j
= 0; j
< rhs
->size
; j
++)
199 /* First of all, skip those clocks in result->vc[] for which there */
200 /* is no corresponding clock in rhs->vc[]. */
201 while (i
< result
->size
&& result
->vc
[i
].threadid
< rhs
->vc
[j
].threadid
)
205 /* If the end of *result is met, append rhs->vc[j] to *result. */
206 if (i
>= result
->size
)
209 result
->vc
[i
] = rhs
->vc
[j
];
211 /* If clock rhs->vc[j] is not in *result, insert it. */
212 else if (result
->vc
[i
].threadid
> rhs
->vc
[j
].threadid
)
215 for (k
= result
->size
; k
> i
; k
--)
217 result
->vc
[k
] = result
->vc
[k
- 1];
220 result
->vc
[i
] = rhs
->vc
[j
];
222 /* Otherwise, both *result and *rhs have a clock for thread */
223 /* result->vc[i].threadid == rhs->vc[j].threadid. Compute the maximum. */
226 tl_assert(result
->vc
[i
].threadid
== rhs
->vc
[j
].threadid
);
227 if (rhs
->vc
[j
].count
> result
->vc
[i
].count
)
229 result
->vc
[i
].count
= rhs
->vc
[j
].count
;
233 DRD_(vc_check
)(result
);
234 tl_assert(result
->size
== new_size
);
237 /** Print the contents of vector clock 'vc'. */
238 void DRD_(vc_print
)(const VectorClock
* const vc
)
242 if ((str
= DRD_(vc_aprint
)(vc
)) != NULL
)
244 VG_(printf
)("%s", str
);
250 * Print the contents of vector clock 'vc' to a newly allocated string.
251 * The caller must call VG_(free)() on the return value of this function.
253 HChar
* DRD_(vc_aprint
)(const VectorClock
* const vc
)
263 str
= VG_(realloc
)("drd.vc.aprint.1", str
, reserved
);
266 size
+= VG_(snprintf
)(str
, reserved
, "[");
267 for (i
= 0; i
< vc
->size
; i
++)
270 if (VG_(strlen
)(str
) + 32 > reserved
)
273 str
= VG_(realloc
)("drd.vc.aprint.2", str
, reserved
);
277 size
+= VG_(snprintf
)(str
+ size
, reserved
- size
,
278 "%s %u: %u", i
> 0 ? "," : "",
279 vc
->vc
[i
].threadid
, vc
->vc
[i
].count
);
281 size
+= VG_(snprintf
)(str
+ size
, reserved
- size
, " ]");
289 * The function below tests whether the following two conditions are
291 * - size <= capacity.
292 * - Vector clock elements are stored in thread ID order.
294 * If one of these conditions is not met, an assertion failure is triggered.
296 void DRD_(vc_check
)(const VectorClock
* const vc
)
300 tl_assert(vc
->size
<= vc
->capacity
);
302 for (i
= 1; i
< vc
->size
; i
++)
303 tl_assert(vc
->vc
[i
-1].threadid
< vc
->vc
[i
].threadid
);
307 * Change the size of the memory block pointed at by vc->vc.
308 * Changes capacity, but does not change size. If the size of the memory
309 * block is increased, the newly allocated memory is not initialized.
312 void DRD_(vc_reserve
)(VectorClock
* const vc
, const unsigned new_capacity
)
315 tl_assert(vc
->capacity
> VC_PREALLOCATED
317 || vc
->vc
== vc
->preallocated
);
319 if (new_capacity
> vc
->capacity
)
321 if (vc
->vc
&& vc
->capacity
> VC_PREALLOCATED
)
324 && vc
->vc
!= vc
->preallocated
325 && vc
->capacity
> VC_PREALLOCATED
);
326 vc
->vc
= VG_(realloc
)("drd.vc.vr.1",
327 vc
->vc
, new_capacity
* sizeof(vc
->vc
[0]));
329 else if (vc
->vc
&& new_capacity
> VC_PREALLOCATED
)
331 tl_assert((vc
->vc
== 0 || vc
->vc
== vc
->preallocated
)
332 && new_capacity
> VC_PREALLOCATED
333 && vc
->capacity
<= VC_PREALLOCATED
);
334 vc
->vc
= VG_(malloc
)("drd.vc.vr.2",
335 new_capacity
* sizeof(vc
->vc
[0]));
336 VG_(memcpy
)(vc
->vc
, vc
->preallocated
,
337 vc
->capacity
* sizeof(vc
->vc
[0]));
341 tl_assert(vc
->vc
== vc
->preallocated
342 && new_capacity
<= VC_PREALLOCATED
343 && vc
->capacity
<= VC_PREALLOCATED
);
345 else if (new_capacity
> VC_PREALLOCATED
)
347 tl_assert(vc
->vc
== 0
348 && new_capacity
> VC_PREALLOCATED
349 && vc
->capacity
== 0);
350 vc
->vc
= VG_(malloc
)("drd.vc.vr.3",
351 new_capacity
* sizeof(vc
->vc
[0]));
355 tl_assert(vc
->vc
== 0
356 && new_capacity
<= VC_PREALLOCATED
357 && vc
->capacity
== 0);
358 vc
->vc
= vc
->preallocated
;
360 vc
->capacity
= new_capacity
;
362 else if (new_capacity
== 0 && vc
->vc
)
364 if (vc
->capacity
> VC_PREALLOCATED
)
370 tl_assert(new_capacity
== 0 || vc
->vc
!= 0);
371 tl_assert(vc
->capacity
> VC_PREALLOCATED
373 || vc
->vc
== vc
->preallocated
);