sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- An expandable array implementation. m_xarray.h ---*/ |
| 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
| 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
| 9 | |
sewardj | 03f8d3f | 2012-08-05 15:46:46 +0000 | [diff] [blame] | 10 | Copyright (C) 2007-2012 OpenWorks LLP |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 11 | info@open-works.co.uk |
| 12 | |
| 13 | This program is free software; you can redistribute it and/or |
| 14 | modify it under the terms of the GNU General Public License as |
| 15 | published by the Free Software Foundation; either version 2 of the |
| 16 | License, or (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but |
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 26 | 02111-1307, USA. |
| 27 | |
| 28 | The GNU General Public License is contained in the file COPYING. |
| 29 | */ |
| 30 | |
| 31 | #include "pub_core_basics.h" |
| 32 | #include "pub_core_libcbase.h" |
| 33 | #include "pub_core_libcassert.h" |
| 34 | #include "pub_core_libcprint.h" |
| 35 | #include "pub_core_xarray.h" /* self */ |
| 36 | |
| 37 | |
sewardj | 7df1515 | 2007-02-25 15:04:40 +0000 | [diff] [blame] | 38 | /* See pub_tool_xarray.h for details of what this is all about. */ |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 39 | |
| 40 | struct _XArray { |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 41 | void* (*alloc) ( HChar*, SizeT ); /* alloc fn (nofail) */ |
| 42 | HChar* cc; /* cost centre for alloc */ |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 43 | void (*free) ( void* ); /* free fn */ |
njn | b4cfa2f | 2007-03-28 01:27:05 +0000 | [diff] [blame] | 44 | Int (*cmpFn) ( void*, void* ); /* cmp fn (may be NULL) */ |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 45 | Word elemSzB; /* element size in bytes */ |
| 46 | void* arr; /* pointer to elements */ |
| 47 | Word usedsizeE; /* # used elements in arr */ |
| 48 | Word totsizeE; /* max size of arr, in elements */ |
| 49 | Bool sorted; /* is it sorted? */ |
| 50 | }; |
| 51 | |
| 52 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 53 | XArray* VG_(newXA) ( void*(*alloc_fn)(HChar*,SizeT), |
| 54 | HChar* cc, |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 55 | void(*free_fn)(void*), |
| 56 | Word elemSzB ) |
| 57 | { |
| 58 | struct _XArray* xa; |
| 59 | /* Implementation relies on Word being signed and (possibly) |
| 60 | on SizeT being unsigned. */ |
| 61 | vg_assert( sizeof(Word) == sizeof(void*) ); |
| 62 | vg_assert( ((Word)(-1)) < ((Word)(0)) ); |
| 63 | vg_assert( ((SizeT)(-1)) > ((SizeT)(0)) ); |
| 64 | /* check user-supplied info .. */ |
| 65 | vg_assert(alloc_fn); |
| 66 | vg_assert(free_fn); |
| 67 | vg_assert(elemSzB > 0); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 68 | xa = alloc_fn( cc, sizeof(struct _XArray) ); |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 69 | vg_assert(xa); |
| 70 | xa->alloc = alloc_fn; |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 71 | xa->cc = cc; |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 72 | xa->free = free_fn; |
| 73 | xa->cmpFn = NULL; |
| 74 | xa->elemSzB = elemSzB; |
| 75 | xa->usedsizeE = 0; |
| 76 | xa->totsizeE = 0; |
| 77 | xa->sorted = False; |
| 78 | xa->arr = NULL; |
| 79 | return xa; |
| 80 | } |
| 81 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 82 | XArray* VG_(cloneXA)( HChar* cc, XArray* xao ) |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 83 | { |
| 84 | struct _XArray* xa = (struct _XArray*)xao; |
| 85 | struct _XArray* nyu; |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 86 | HChar* nyu_cc; |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 87 | vg_assert(xa); |
| 88 | vg_assert(xa->alloc); |
| 89 | vg_assert(xa->free); |
| 90 | vg_assert(xa->elemSzB >= 1); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 91 | nyu_cc = cc ? cc : xa->cc; |
| 92 | nyu = xa->alloc( nyu_cc, sizeof(struct _XArray) ); |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 93 | if (!nyu) |
| 94 | return NULL; |
| 95 | /* Copy everything verbatim ... */ |
| 96 | *nyu = *xa; |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 97 | nyu->cc = nyu_cc; |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 98 | /* ... except we have to clone the contents-array */ |
| 99 | if (nyu->arr) { |
sewardj | 7db115a | 2008-08-22 23:16:06 +0000 | [diff] [blame] | 100 | /* Restrict the total size of the new array to its current |
| 101 | actual size. That means we don't waste space copying the |
| 102 | unused tail of the original. The tradeoff is that it |
| 103 | guarantees we will have to resize the child if even one more |
| 104 | element is later added to it, unfortunately. */ |
| 105 | nyu->totsizeE = nyu->usedsizeE; |
| 106 | /* and allocate .. */ |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 107 | nyu->arr = nyu->alloc( nyu->cc, nyu->totsizeE * nyu->elemSzB ); |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 108 | if (!nyu->arr) { |
| 109 | nyu->free(nyu); |
| 110 | return NULL; |
| 111 | } |
| 112 | VG_(memcpy)( nyu->arr, xa->arr, nyu->totsizeE * nyu->elemSzB ); |
| 113 | } |
| 114 | /* We're done! */ |
| 115 | return nyu; |
| 116 | } |
| 117 | |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 118 | void VG_(deleteXA) ( XArray* xao ) |
| 119 | { |
| 120 | struct _XArray* xa = (struct _XArray*)xao; |
| 121 | vg_assert(xa); |
| 122 | vg_assert(xa->free); |
sewardj | e783ceb | 2007-08-28 05:19:16 +0000 | [diff] [blame] | 123 | if (xa->arr) |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 124 | xa->free(xa->arr); |
| 125 | xa->free(xa); |
| 126 | } |
| 127 | |
njn | b4cfa2f | 2007-03-28 01:27:05 +0000 | [diff] [blame] | 128 | void VG_(setCmpFnXA) ( XArray* xao, Int (*compar)(void*,void*) ) |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 129 | { |
| 130 | struct _XArray* xa = (struct _XArray*)xao; |
| 131 | vg_assert(xa); |
| 132 | vg_assert(compar); |
| 133 | xa->cmpFn = compar; |
| 134 | xa->sorted = False; |
| 135 | } |
| 136 | |
| 137 | inline void* VG_(indexXA) ( XArray* xao, Word n ) |
| 138 | { |
| 139 | struct _XArray* xa = (struct _XArray*)xao; |
| 140 | vg_assert(xa); |
| 141 | vg_assert(n >= 0); |
| 142 | vg_assert(n < xa->usedsizeE); |
| 143 | return ((char*)xa->arr) + n * xa->elemSzB; |
| 144 | } |
| 145 | |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 146 | static inline void ensureSpaceXA ( struct _XArray* xa ) |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 147 | { |
sewardj | aa5a1ea | 2011-02-27 23:53:32 +0000 | [diff] [blame] | 148 | if (xa->usedsizeE == xa->totsizeE) { |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 149 | void* tmp; |
| 150 | Word newsz; |
| 151 | if (xa->totsizeE == 0) |
| 152 | vg_assert(!xa->arr); |
| 153 | if (xa->totsizeE > 0) |
| 154 | vg_assert(xa->arr); |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 155 | if (xa->totsizeE == 0) { |
| 156 | /* No point in having tiny (eg) 2-byte allocations for the |
| 157 | element array, since all allocs are rounded up to 8 anyway. |
| 158 | Hence increase the initial array size for tiny elements in |
| 159 | an attempt to avoid reallocations of size 2, 4, 8 if the |
| 160 | array does start to fill up. */ |
sewardj | aa5a1ea | 2011-02-27 23:53:32 +0000 | [diff] [blame] | 161 | if (xa->elemSzB == 1) newsz = 8; |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 162 | else if (xa->elemSzB == 2) newsz = 4; |
| 163 | else newsz = 2; |
| 164 | } else { |
sewardj | f98e1c0 | 2008-10-25 16:22:41 +0000 | [diff] [blame] | 165 | newsz = 2 + (3 * xa->totsizeE) / 2; /* 2 * xa->totsizeE; */ |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 166 | } |
sewardj | 7db115a | 2008-08-22 23:16:06 +0000 | [diff] [blame] | 167 | if (0 && xa->totsizeE >= 10000) |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 168 | VG_(printf)("addToXA: increasing from %ld to %ld\n", |
| 169 | xa->totsizeE, newsz); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 170 | tmp = xa->alloc(xa->cc, newsz * xa->elemSzB); |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 171 | vg_assert(tmp); |
| 172 | if (xa->usedsizeE > 0) |
| 173 | VG_(memcpy)(tmp, xa->arr, xa->usedsizeE * xa->elemSzB); |
| 174 | if (xa->arr) |
| 175 | xa->free(xa->arr); |
| 176 | xa->arr = tmp; |
| 177 | xa->totsizeE = newsz; |
| 178 | } |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 179 | } |
| 180 | |
sewardj | bee43c1 | 2008-08-19 08:57:49 +0000 | [diff] [blame] | 181 | Word VG_(addToXA) ( XArray* xao, void* elem ) |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 182 | { |
| 183 | struct _XArray* xa = (struct _XArray*)xao; |
| 184 | vg_assert(xa); |
| 185 | vg_assert(elem); |
| 186 | vg_assert(xa->totsizeE >= 0); |
| 187 | vg_assert(xa->usedsizeE >= 0 && xa->usedsizeE <= xa->totsizeE); |
| 188 | ensureSpaceXA( xa ); |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 189 | vg_assert(xa->usedsizeE < xa->totsizeE); |
| 190 | vg_assert(xa->arr); |
| 191 | VG_(memcpy)( ((UChar*)xa->arr) + xa->usedsizeE * xa->elemSzB, |
| 192 | elem, xa->elemSzB ); |
| 193 | xa->usedsizeE++; |
| 194 | xa->sorted = False; |
sewardj | 0f63148 | 2007-02-27 16:40:53 +0000 | [diff] [blame] | 195 | return xa->usedsizeE-1; |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 196 | } |
| 197 | |
sewardj | bee43c1 | 2008-08-19 08:57:49 +0000 | [diff] [blame] | 198 | Word VG_(addBytesToXA) ( XArray* xao, void* bytesV, Word nbytes ) |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 199 | { |
sewardj | bee43c1 | 2008-08-19 08:57:49 +0000 | [diff] [blame] | 200 | Word r, i; |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 201 | struct _XArray* xa = (struct _XArray*)xao; |
| 202 | vg_assert(xa); |
| 203 | vg_assert(xa->elemSzB == 1); |
| 204 | vg_assert(nbytes >= 0); |
| 205 | vg_assert(xa->totsizeE >= 0); |
| 206 | vg_assert(xa->usedsizeE >= 0 && xa->usedsizeE <= xa->totsizeE); |
| 207 | r = xa->usedsizeE; |
| 208 | for (i = 0; i < nbytes; i++) { |
| 209 | ensureSpaceXA( xa ); |
| 210 | vg_assert(xa->usedsizeE < xa->totsizeE); |
| 211 | vg_assert(xa->arr); |
| 212 | * (((UChar*)xa->arr) + xa->usedsizeE) = ((UChar*)bytesV)[i]; |
| 213 | xa->usedsizeE++; |
| 214 | } |
| 215 | xa->sorted = False; |
| 216 | return r; |
| 217 | } |
| 218 | |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 219 | void VG_(sortXA) ( XArray* xao ) |
| 220 | { |
| 221 | struct _XArray* xa = (struct _XArray*)xao; |
| 222 | vg_assert(xa); |
| 223 | vg_assert(xa->cmpFn); |
njn | b4cfa2f | 2007-03-28 01:27:05 +0000 | [diff] [blame] | 224 | VG_(ssort)( xa->arr, xa->usedsizeE, xa->elemSzB, xa->cmpFn ); |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 225 | xa->sorted = True; |
| 226 | } |
| 227 | |
sewardj | e2253bf | 2009-07-24 08:11:39 +0000 | [diff] [blame] | 228 | Bool VG_(lookupXA_UNSAFE) ( XArray* xao, void* key, |
| 229 | /*OUT*/Word* first, /*OUT*/Word* last, |
| 230 | Int(*cmpFn)(void*,void*) ) |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 231 | { |
| 232 | Word lo, mid, hi, cres; |
| 233 | void* midv; |
| 234 | struct _XArray* xa = (struct _XArray*)xao; |
| 235 | vg_assert(xa); |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 236 | lo = 0; |
| 237 | hi = xa->usedsizeE-1; |
| 238 | while (True) { |
| 239 | /* current unsearched space is from lo to hi, inclusive. */ |
| 240 | if (lo > hi) return False; /* not found */ |
| 241 | mid = (lo + hi) / 2; |
| 242 | midv = VG_(indexXA)( xa, mid ); |
sewardj | e2253bf | 2009-07-24 08:11:39 +0000 | [diff] [blame] | 243 | cres = cmpFn( key, midv ); |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 244 | if (cres < 0) { hi = mid-1; continue; } |
| 245 | if (cres > 0) { lo = mid+1; continue; } |
| 246 | /* Found it, at mid. See how far we can expand this. */ |
sewardj | e2253bf | 2009-07-24 08:11:39 +0000 | [diff] [blame] | 247 | vg_assert(cmpFn( key, VG_(indexXA)(xa, lo) ) >= 0); |
| 248 | vg_assert(cmpFn( key, VG_(indexXA)(xa, hi) ) <= 0); |
sewardj | ffce815 | 2011-06-24 10:09:41 +0000 | [diff] [blame] | 249 | if (first) { |
| 250 | *first = mid; |
| 251 | while (*first > 0 |
| 252 | && 0 == cmpFn( key, VG_(indexXA)(xa, (*first)-1))) { |
| 253 | (*first)--; |
| 254 | } |
| 255 | } |
| 256 | if (last) { |
| 257 | *last = mid; |
| 258 | while (*last < xa->usedsizeE-1 |
| 259 | && 0 == cmpFn( key, VG_(indexXA)(xa, (*last)+1))) { |
| 260 | (*last)++; |
| 261 | } |
| 262 | } |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 263 | return True; |
| 264 | } |
| 265 | } |
| 266 | |
sewardj | e2253bf | 2009-07-24 08:11:39 +0000 | [diff] [blame] | 267 | Bool VG_(lookupXA) ( XArray* xao, void* key, |
| 268 | /*OUT*/Word* first, /*OUT*/Word* last ) |
| 269 | { |
| 270 | struct _XArray* xa = (struct _XArray*)xao; |
| 271 | vg_assert(xa); |
| 272 | vg_assert(xa->cmpFn); |
| 273 | vg_assert(xa->sorted); |
| 274 | return VG_(lookupXA_UNSAFE)(xao, key, first, last, xa->cmpFn); |
| 275 | } |
| 276 | |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 277 | Word VG_(sizeXA) ( XArray* xao ) |
| 278 | { |
| 279 | struct _XArray* xa = (struct _XArray*)xao; |
| 280 | vg_assert(xa); |
| 281 | return xa->usedsizeE; |
| 282 | } |
| 283 | |
| 284 | void VG_(dropTailXA) ( XArray* xao, Word n ) |
| 285 | { |
| 286 | struct _XArray* xa = (struct _XArray*)xao; |
| 287 | vg_assert(xa); |
| 288 | vg_assert(n >= 0); |
| 289 | vg_assert(n <= xa->usedsizeE); |
| 290 | xa->usedsizeE -= n; |
| 291 | } |
| 292 | |
sewardj | e2253bf | 2009-07-24 08:11:39 +0000 | [diff] [blame] | 293 | void VG_(dropHeadXA) ( XArray* xao, Word n ) |
| 294 | { |
| 295 | struct _XArray* xa = (struct _XArray*)xao; |
| 296 | vg_assert(xa); |
| 297 | vg_assert(n >= 0); |
| 298 | vg_assert(n <= xa->usedsizeE); |
| 299 | if (n == 0) { |
| 300 | return; |
| 301 | } |
| 302 | if (n == xa->usedsizeE) { |
| 303 | xa->usedsizeE = 0; |
| 304 | return; |
| 305 | } |
| 306 | vg_assert(n > 0); |
| 307 | vg_assert(xa->usedsizeE - n > 0); |
| 308 | VG_(memcpy)( (char*)xa->arr, |
| 309 | ((char*)xa->arr) + n * xa->elemSzB, |
| 310 | (xa->usedsizeE - n) * xa->elemSzB ); |
| 311 | xa->usedsizeE -= n; |
| 312 | } |
| 313 | |
sewardj | 291849f | 2012-04-20 23:58:55 +0000 | [diff] [blame] | 314 | void VG_(removeIndexXA)( XArray* xao, Word n ) |
| 315 | { |
| 316 | struct _XArray* xa = (struct _XArray*)xao; |
| 317 | vg_assert(xa); |
| 318 | vg_assert(n >= 0); |
| 319 | vg_assert(n < xa->usedsizeE); |
| 320 | if (n+1 < xa->usedsizeE) { |
| 321 | VG_(memmove)( ((char*)xa->arr) + (n+0) * xa->elemSzB, |
| 322 | ((char*)xa->arr) + (n+1) * xa->elemSzB, |
| 323 | (xa->usedsizeE - n - 1) * xa->elemSzB ); |
| 324 | } |
| 325 | xa->usedsizeE--; |
| 326 | } |
| 327 | |
sewardj | 8f8fa17 | 2010-05-05 09:23:41 +0000 | [diff] [blame] | 328 | void VG_(getContentsXA_UNSAFE)( XArray* xao, |
| 329 | /*OUT*/void** ctsP, |
| 330 | /*OUT*/Word* usedP ) |
| 331 | { |
| 332 | struct _XArray* xa = (struct _XArray*)xao; |
| 333 | vg_assert(xa); |
| 334 | *ctsP = (void*)xa->arr; |
| 335 | *usedP = xa->usedsizeE; |
| 336 | } |
| 337 | |
sewardj | 588adef | 2009-08-15 22:41:51 +0000 | [diff] [blame] | 338 | /* --------- Printeffery --------- */ |
| 339 | |
| 340 | static void add_char_to_XA ( HChar c, void* opaque ) |
| 341 | { |
| 342 | XArray* dst = (XArray*)opaque; |
| 343 | (void) VG_(addBytesToXA)( dst, &c, 1 ); |
| 344 | } |
| 345 | |
| 346 | void VG_(xaprintf)( XArray* dst, const HChar* format, ... ) |
| 347 | { |
| 348 | va_list vargs; |
| 349 | va_start(vargs, format); |
| 350 | VG_(vcbprintf)( add_char_to_XA, (void*)dst, format, vargs ); |
| 351 | va_end(vargs); |
| 352 | } |
| 353 | |
sewardj | d864eb9 | 2007-02-25 11:51:13 +0000 | [diff] [blame] | 354 | |
| 355 | /*--------------------------------------------------------------------*/ |
| 356 | /*--- end m_xarray.c ---*/ |
| 357 | /*--------------------------------------------------------------------*/ |