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