1 /* -*- mode: C; c-basic-offset: 3; indent-tabs-mode: nil; -*- */
3 This file is part of drd, a thread error detector.
5 Copyright (C) 2006-2011 Bart Van Assche <bvanassche@acm.org>.
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 The GNU General Public License is contained in the file COPYING.
27 #include "pub_tool_basics.h" // Addr, SizeT
28 #include "pub_tool_libcassert.h" // tl_assert()
29 #include "pub_tool_libcbase.h" // VG_(memcpy)
30 #include "pub_tool_libcprint.h" // VG_(printf)
31 #include "pub_tool_mallocfree.h" // VG_(malloc), VG_(free)
34 /* Local function declarations. */
37 void DRD_(vc_reserve)(VectorClock* const vc, const unsigned new_capacity);
40 /* Function definitions. */
43 * Initialize the memory 'vc' points at as a vector clock with size 'size'.
44 * If the pointer 'vcelem' is not null, it is assumed to be an array with
45 * 'size' elements and it becomes the initial value of the vector clock.
47 void DRD_(vc_init)(VectorClock* const vc,
48 const VCElem* const vcelem,
55 DRD_(vc_reserve)(vc, size);
56 tl_assert(size == 0 || vc->vc != 0);
59 VG_(memcpy)(vc->vc, vcelem, size * sizeof(vcelem[0]));
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 *new. */
71 void DRD_(vc_copy)(VectorClock* const new, const VectorClock* const rhs)
73 DRD_(vc_init)(new, 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 char* 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 %d: %d", 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)
299 tl_assert(vc->size <= vc->capacity);
300 for (i = 1; i < vc->size; i++)
302 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);