bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1 | /* -*- mode: C; c-basic-offset: 3; -*- */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 2 | /* |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 3 | This file is part of drd, a thread error detector. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 4 | |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 5 | Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 6 | |
| 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. |
| 11 | |
| 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. |
| 16 | |
| 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 |
| 20 | 02111-1307, USA. |
| 21 | |
| 22 | The GNU General Public License is contained in the file COPYING. |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #include "drd_vc.h" |
| 27 | #include "pub_tool_basics.h" // Addr, SizeT |
| 28 | #include "pub_tool_libcassert.h" // tl_assert() |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 29 | #include "pub_tool_libcbase.h" // VG_(memcpy) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 30 | #include "pub_tool_libcprint.h" // VG_(printf) |
| 31 | #include "pub_tool_mallocfree.h" // VG_(malloc), VG_(free) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 32 | |
| 33 | |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 34 | /* Local function declarations. */ |
| 35 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 36 | static |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 37 | void DRD_(vc_reserve)(VectorClock* const vc, const unsigned new_capacity); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 38 | |
| 39 | |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 40 | /* Function definitions. */ |
| 41 | |
| 42 | /** |
| 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. |
| 46 | */ |
| 47 | void DRD_(vc_init)(VectorClock* const vc, |
| 48 | const VCElem* const vcelem, |
| 49 | const unsigned size) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 50 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 51 | tl_assert(vc); |
| 52 | vc->size = 0; |
| 53 | vc->capacity = 0; |
| 54 | vc->vc = 0; |
| 55 | DRD_(vc_reserve)(vc, size); |
| 56 | tl_assert(size == 0 || vc->vc != 0); |
| 57 | if (vcelem) |
| 58 | { |
| 59 | VG_(memcpy)(vc->vc, vcelem, size * sizeof(vcelem[0])); |
| 60 | vc->size = size; |
| 61 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 62 | } |
| 63 | |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 64 | /** Reset vc to the empty vector clock. */ |
| 65 | void DRD_(vc_cleanup)(VectorClock* const vc) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 66 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 67 | DRD_(vc_reserve)(vc, 0); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 68 | } |
| 69 | |
bart | c46c232 | 2008-02-24 18:26:46 +0000 | [diff] [blame] | 70 | /** Copy constructor -- initializes *new. */ |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 71 | void DRD_(vc_copy)(VectorClock* const new, const VectorClock* const rhs) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 72 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 73 | DRD_(vc_init)(new, rhs->vc, rhs->size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 74 | } |
| 75 | |
bart | c46c232 | 2008-02-24 18:26:46 +0000 | [diff] [blame] | 76 | /** Assignment operator -- *lhs is already a valid vector clock. */ |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 77 | void DRD_(vc_assign)(VectorClock* const lhs, const VectorClock* const rhs) |
bart | c46c232 | 2008-02-24 18:26:46 +0000 | [diff] [blame] | 78 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 79 | DRD_(vc_cleanup)(lhs); |
| 80 | DRD_(vc_copy)(lhs, rhs); |
bart | c46c232 | 2008-02-24 18:26:46 +0000 | [diff] [blame] | 81 | } |
| 82 | |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 83 | /** Increment the clock of thread 'tid' in vector clock 'vc'. */ |
| 84 | void DRD_(vc_increment)(VectorClock* const vc, DrdThreadId const tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 85 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 86 | unsigned i; |
| 87 | for (i = 0; i < vc->size; i++) |
| 88 | { |
| 89 | if (vc->vc[i].threadid == tid) |
| 90 | { |
| 91 | typeof(vc->vc[i].count) const oldcount = vc->vc[i].count; |
| 92 | vc->vc[i].count++; |
| 93 | // Check for integer overflow. |
| 94 | tl_assert(oldcount < vc->vc[i].count); |
| 95 | return; |
| 96 | } |
| 97 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 98 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 99 | /* |
| 100 | * The specified thread ID does not yet exist in the vector clock |
| 101 | * -- insert it. |
| 102 | */ |
| 103 | { |
| 104 | const VCElem vcelem = { tid, 1 }; |
| 105 | VectorClock vc2; |
| 106 | DRD_(vc_init)(&vc2, &vcelem, 1); |
| 107 | DRD_(vc_combine)(vc, &vc2); |
| 108 | DRD_(vc_cleanup)(&vc2); |
| 109 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 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"). |
| 115 | */ |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 116 | Bool DRD_(vc_ordered)(const VectorClock* const vc1, |
| 117 | const VectorClock* const vc2) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 118 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 119 | return DRD_(vc_lte)(vc1, vc2) || DRD_(vc_lte)(vc2, vc1); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 120 | } |
| 121 | |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 122 | /** Compute elementwise minimum. */ |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 123 | void DRD_(vc_min)(VectorClock* const result, const VectorClock* const rhs) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 124 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 125 | unsigned i; |
| 126 | unsigned j; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 127 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 128 | tl_assert(result); |
| 129 | tl_assert(rhs); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 130 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 131 | DRD_(vc_check)(result); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 132 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 133 | /* Next, combine both vector clocks into one. */ |
| 134 | i = 0; |
| 135 | for (j = 0; j < rhs->size; j++) |
| 136 | { |
| 137 | while (i < result->size && result->vc[i].threadid < rhs->vc[j].threadid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 138 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 139 | /* Thread ID is missing in second vector clock. Clear the count. */ |
| 140 | result->vc[i].count = 0; |
| 141 | i++; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 142 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 143 | if (i >= result->size) |
| 144 | { |
| 145 | break; |
| 146 | } |
| 147 | if (result->vc[i].threadid <= rhs->vc[j].threadid) |
| 148 | { |
| 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) |
| 153 | { |
| 154 | result->vc[i].count = rhs->vc[j].count; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | DRD_(vc_check)(result); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Compute elementwise maximum. |
| 163 | */ |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 164 | void DRD_(vc_combine)(VectorClock* const result, const VectorClock* const rhs) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 165 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 166 | DRD_(vc_combine2)(result, rhs, -1); |
bart | fbbac09 | 2008-04-06 14:57:37 +0000 | [diff] [blame] | 167 | } |
| 168 | |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 169 | /** |
| 170 | * Compute elementwise maximum. |
bart | fbbac09 | 2008-04-06 14:57:37 +0000 | [diff] [blame] | 171 | * |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 172 | * @return True if *result and *rhs are equal, or if *result and *rhs only |
| 173 | * differ in the component with thread ID tid. |
bart | fbbac09 | 2008-04-06 14:57:37 +0000 | [diff] [blame] | 174 | */ |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 175 | Bool DRD_(vc_combine2)(VectorClock* const result, |
| 176 | const VectorClock* const rhs, |
| 177 | const DrdThreadId tid) |
bart | fbbac09 | 2008-04-06 14:57:37 +0000 | [diff] [blame] | 178 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 179 | unsigned i; |
| 180 | unsigned j; |
| 181 | unsigned shared; |
| 182 | unsigned new_size; |
| 183 | Bool almost_equal = True; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 184 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 185 | tl_assert(result); |
| 186 | tl_assert(rhs); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 187 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 188 | // First count the number of shared thread id's. |
| 189 | j = 0; |
| 190 | shared = 0; |
| 191 | for (i = 0; i < result->size; i++) |
| 192 | { |
| 193 | while (j < rhs->size && rhs->vc[j].threadid < result->vc[i].threadid) |
| 194 | j++; |
| 195 | if (j >= rhs->size) |
| 196 | break; |
| 197 | if (result->vc[i].threadid == rhs->vc[j].threadid) |
| 198 | shared++; |
| 199 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 200 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 201 | DRD_(vc_check)(result); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 202 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 203 | new_size = result->size + rhs->size - shared; |
| 204 | if (new_size > result->capacity) |
| 205 | DRD_(vc_reserve)(result, new_size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 206 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 207 | DRD_(vc_check)(result); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 208 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 209 | // Next, combine both vector clocks into one. |
| 210 | i = 0; |
| 211 | for (j = 0; j < rhs->size; j++) |
| 212 | { |
| 213 | /* First of all, skip those clocks in result->vc[] for which there */ |
| 214 | /* is no corresponding clock in rhs->vc[]. */ |
| 215 | while (i < result->size && result->vc[i].threadid < rhs->vc[j].threadid) |
bart | fbbac09 | 2008-04-06 14:57:37 +0000 | [diff] [blame] | 216 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 217 | if (result->vc[i].threadid != tid) |
| 218 | { |
| 219 | almost_equal = False; |
| 220 | } |
| 221 | i++; |
bart | fbbac09 | 2008-04-06 14:57:37 +0000 | [diff] [blame] | 222 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 223 | /* If the end of *result is met, append rhs->vc[j] to *result. */ |
| 224 | if (i >= result->size) |
bart | fbbac09 | 2008-04-06 14:57:37 +0000 | [diff] [blame] | 225 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 226 | result->size++; |
| 227 | result->vc[i] = rhs->vc[j]; |
| 228 | if (result->vc[i].threadid != tid) |
| 229 | { |
| 230 | almost_equal = False; |
| 231 | } |
bart | fbbac09 | 2008-04-06 14:57:37 +0000 | [diff] [blame] | 232 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 233 | /* If clock rhs->vc[j] is not in *result, insert it. */ |
| 234 | else if (result->vc[i].threadid > rhs->vc[j].threadid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 235 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 236 | unsigned k; |
| 237 | for (k = result->size; k > i; k--) |
| 238 | { |
| 239 | result->vc[k] = result->vc[k - 1]; |
| 240 | } |
| 241 | result->size++; |
| 242 | result->vc[i] = rhs->vc[j]; |
| 243 | if (result->vc[i].threadid != tid) |
| 244 | { |
| 245 | almost_equal = False; |
| 246 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 247 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 248 | /* Otherwise, both *result and *rhs have a clock for thread */ |
| 249 | /* result->vc[i].threadid == rhs->vc[j].threadid. Compute the maximum. */ |
| 250 | else |
bart | fbbac09 | 2008-04-06 14:57:37 +0000 | [diff] [blame] | 251 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 252 | tl_assert(result->vc[i].threadid == rhs->vc[j].threadid); |
| 253 | if (result->vc[i].threadid != tid |
| 254 | && rhs->vc[j].count != result->vc[i].count) |
| 255 | { |
| 256 | almost_equal = False; |
| 257 | } |
| 258 | if (rhs->vc[j].count > result->vc[i].count) |
| 259 | { |
| 260 | result->vc[i].count = rhs->vc[j].count; |
| 261 | } |
bart | fbbac09 | 2008-04-06 14:57:37 +0000 | [diff] [blame] | 262 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 263 | } |
| 264 | DRD_(vc_check)(result); |
| 265 | tl_assert(result->size == new_size); |
bart | fbbac09 | 2008-04-06 14:57:37 +0000 | [diff] [blame] | 266 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 267 | return almost_equal; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 268 | } |
| 269 | |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 270 | /** Print the contents of vector clock 'vc'. */ |
| 271 | void DRD_(vc_print)(const VectorClock* const vc) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 272 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 273 | unsigned i; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 274 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 275 | tl_assert(vc); |
| 276 | VG_(printf)("["); |
| 277 | for (i = 0; i < vc->size; i++) |
| 278 | { |
| 279 | tl_assert(vc->vc); |
| 280 | VG_(printf)("%s %d: %d", i > 0 ? "," : "", |
| 281 | vc->vc[i].threadid, vc->vc[i].count); |
| 282 | } |
| 283 | VG_(printf)(" ]"); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 284 | } |
| 285 | |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 286 | /** |
| 287 | * Print the contents of vector clock 'vc' to the character array 'str' that |
| 288 | * has 'size' elements. |
| 289 | */ |
| 290 | void DRD_(vc_snprint)(Char* const str, const Int size, |
| 291 | const VectorClock* const vc) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 292 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 293 | unsigned i; |
| 294 | unsigned j = 1; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 295 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 296 | tl_assert(vc); |
| 297 | VG_(snprintf)(str, size, "["); |
| 298 | for (i = 0; i < vc->size; i++) |
| 299 | { |
| 300 | tl_assert(vc->vc); |
| 301 | for ( ; j <= vc->vc[i].threadid; j++) |
| 302 | { |
| 303 | VG_(snprintf)(str + VG_(strlen)(str), size - VG_(strlen)(str), |
| 304 | "%s %d", |
| 305 | i > 0 ? "," : "", |
| 306 | (j == vc->vc[i].threadid) ? vc->vc[i].count : 0); |
| 307 | } |
| 308 | } |
| 309 | VG_(snprintf)(str + VG_(strlen)(str), size - VG_(strlen)(str), " ]"); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Invariant test. |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 314 | * |
| 315 | * The function below tests whether the following two conditions are |
| 316 | * satisfied: |
| 317 | * - size <= capacity. |
| 318 | * - Vector clock elements are stored in thread ID order. |
| 319 | * |
| 320 | * If one of these conditions is not met, an assertion failure is triggered. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 321 | */ |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 322 | void DRD_(vc_check)(const VectorClock* const vc) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 323 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 324 | unsigned i; |
| 325 | tl_assert(vc->size <= vc->capacity); |
| 326 | for (i = 1; i < vc->size; i++) |
| 327 | { |
| 328 | tl_assert(vc->vc[i-1].threadid < vc->vc[i].threadid); |
| 329 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Change the size of the memory block pointed at by vc->vc. |
| 334 | * Changes capacity, but does not change size. If the size of the memory |
| 335 | * block is increased, the newly allocated memory is not initialized. |
| 336 | */ |
| 337 | static |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 338 | void DRD_(vc_reserve)(VectorClock* const vc, const unsigned new_capacity) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 339 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 340 | tl_assert(vc); |
| 341 | if (new_capacity > vc->capacity) |
| 342 | { |
| 343 | if (vc->vc) |
| 344 | { |
| 345 | vc->vc = VG_(realloc)("drd.vc.vr.1", |
| 346 | vc->vc, new_capacity * sizeof(vc->vc[0])); |
| 347 | } |
| 348 | else if (new_capacity > 0) |
| 349 | { |
| 350 | vc->vc = VG_(malloc)("drd.vc.vr.2", |
| 351 | new_capacity * sizeof(vc->vc[0])); |
| 352 | } |
| 353 | else |
| 354 | { |
| 355 | tl_assert(vc->vc == 0 && new_capacity == 0); |
| 356 | } |
| 357 | vc->capacity = new_capacity; |
| 358 | } |
| 359 | tl_assert(new_capacity == 0 || vc->vc != 0); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 360 | } |
| 361 | |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 362 | #if 0 |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 363 | /** |
| 364 | * Unit test. |
| 365 | */ |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 366 | void DRD_(vc_test)(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 367 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 368 | VectorClock vc1; |
| 369 | VCElem vc1elem[] = { { 3, 7 }, { 5, 8 }, }; |
| 370 | VectorClock vc2; |
| 371 | VCElem vc2elem[] = { { 1, 4 }, { 3, 9 }, }; |
| 372 | VectorClock vc3; |
| 373 | VCElem vc4elem[] = { { 1, 3 }, { 2, 1 }, }; |
| 374 | VectorClock vc4; |
| 375 | VCElem vc5elem[] = { { 1, 4 }, }; |
| 376 | VectorClock vc5; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 377 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 378 | vc_init(&vc1, vc1elem, sizeof(vc1elem)/sizeof(vc1elem[0])); |
| 379 | vc_init(&vc2, vc2elem, sizeof(vc2elem)/sizeof(vc2elem[0])); |
| 380 | vc_init(&vc3, 0, 0); |
| 381 | vc_init(&vc4, vc4elem, sizeof(vc4elem)/sizeof(vc4elem[0])); |
| 382 | vc_init(&vc5, vc5elem, sizeof(vc5elem)/sizeof(vc5elem[0])); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 383 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 384 | vc_combine(&vc3, &vc1); |
| 385 | vc_combine(&vc3, &vc2); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 386 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 387 | VG_(printf)("vc1: "); |
| 388 | vc_print(&vc1); |
| 389 | VG_(printf)("\nvc2: "); |
| 390 | vc_print(&vc2); |
| 391 | VG_(printf)("\nvc3: "); |
| 392 | vc_print(&vc3); |
| 393 | VG_(printf)("\n"); |
| 394 | VG_(printf)("vc_lte(vc1, vc2) = %d, vc_lte(vc1, vc3) = %d," |
| 395 | " vc_lte(vc2, vc3) = %d, vc_lte(", |
| 396 | vc_lte(&vc1, &vc2), vc_lte(&vc1, &vc3), vc_lte(&vc2, &vc3)); |
| 397 | vc_print(&vc4); |
| 398 | VG_(printf)(", "); |
| 399 | vc_print(&vc5); |
| 400 | VG_(printf)(") = %d sw %d\n", vc_lte(&vc4, &vc5), vc_lte(&vc5, &vc4)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 401 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 402 | vc_cleanup(&vc1); |
| 403 | vc_cleanup(&vc2); |
| 404 | vc_cleanup(&vc3); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 405 | } |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 406 | #endif |