sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | This file is part of drd, a data race detector. |
| 3 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2008 Bart Van Assche |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 5 | bart.vanassche@gmail.com |
| 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 | |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 26 | #ifndef __DRD_BITMAP_H |
| 27 | #define __DRD_BITMAP_H |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 28 | |
| 29 | |
| 30 | #include "pub_tool_oset.h" |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 31 | #include "pub_tool_libcbase.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 32 | |
| 33 | |
| 34 | /* |
| 35 | Bitmap representation. A bitmap is a data structure in which two bits are |
| 36 | reserved per 32 bit address: one bit that indicates that the data at the |
| 37 | specified address has been read, and one bit that indicates that the data has |
| 38 | been written to. |
| 39 | */ |
| 40 | |
| 41 | |
| 42 | /* Macro definitions. */ |
| 43 | |
bart | 8b4b2ee | 2008-06-11 13:17:56 +0000 | [diff] [blame] | 44 | #define ADDR0_BITS 14 |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 45 | |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 46 | #define ADDR0_COUNT ((UWord)1 << ADDR0_BITS) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 47 | |
| 48 | #define ADDR0_MASK (ADDR0_COUNT - 1) |
| 49 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 50 | #define SPLIT_ADDRESS(a) \ |
| 51 | UWord a##0 = ((a) & ADDR0_MASK); \ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 52 | UWord a##1 = ((a) >> ADDR0_BITS); |
| 53 | |
| 54 | // Assumption: sizeof(Addr) == sizeof(UWord). |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 55 | #define MAKE_ADDRESS(a1, a0) \ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 56 | (Addr)(((UWord)(a1) << (ADDR0_BITS)) | ((UWord)(a0))) |
| 57 | |
| 58 | #define BITS_PER_UWORD (8UL*sizeof(UWord)) |
| 59 | #if defined(VGA_x86) || defined(VGA_ppc32) |
| 60 | #define BITS_PER_BITS_PER_UWORD 5 |
| 61 | #elif defined(VGA_amd64) || defined(VGA_ppc64) |
| 62 | #define BITS_PER_BITS_PER_UWORD 6 |
| 63 | #else |
| 64 | #error Unknown platform. |
| 65 | #endif |
| 66 | |
| 67 | #define BITMAP1_UWORD_COUNT (ADDR0_COUNT >> BITS_PER_BITS_PER_UWORD) |
| 68 | |
| 69 | /* Highest bits of an address that fit into the same UWord of bm0[]. */ |
| 70 | #define UWORD_MSB(a) ((a) & ~(BITS_PER_UWORD - 1)) |
| 71 | |
| 72 | /* Lowest bits of an address that fit into the same UWord of bm0[]. */ |
| 73 | #define UWORD_LSB(a) ((a) & (BITS_PER_UWORD - 1)) |
| 74 | |
| 75 | /* Highest address that fits in the same UWord as a. */ |
| 76 | #define UWORD_HIGHEST_ADDRESS(a) ((a) | (BITS_PER_UWORD - 1)) |
| 77 | |
| 78 | |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 79 | /* Local variables. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 80 | |
| 81 | static ULong s_bitmap2_creation_count; |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 82 | static ULong s_bitmap2_node_creation_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 83 | |
| 84 | |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 85 | |
| 86 | /*********************************************************************/ |
| 87 | /* Functions for manipulating a struct bitmap1. */ |
| 88 | /*********************************************************************/ |
| 89 | |
| 90 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 91 | /* Lowest level, corresponding to the lowest ADDR0_BITS of an address. */ |
| 92 | struct bitmap1 |
| 93 | { |
| 94 | UWord bm0_r[BITMAP1_UWORD_COUNT]; |
| 95 | UWord bm0_w[BITMAP1_UWORD_COUNT]; |
| 96 | }; |
| 97 | |
| 98 | static __inline__ UWord bm0_mask(const Addr a) |
| 99 | { |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 100 | return ((UWord)1 << UWORD_LSB(a)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static __inline__ void bm0_set(UWord* bm0, const Addr a) |
| 104 | { |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 105 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 106 | tl_assert(a < ADDR0_COUNT); |
| 107 | #endif |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 108 | bm0[a >> BITS_PER_BITS_PER_UWORD] |= (UWord)1 << UWORD_LSB(a); |
| 109 | } |
| 110 | |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 111 | /** Set all of the addresses in range [ a1 .. a1 + size [ in bitmap bm0. */ |
| 112 | static __inline__ void bm0_set_range(UWord* bm0, |
| 113 | const Addr a1, const SizeT size) |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 114 | { |
bart | 8b4b2ee | 2008-06-11 13:17:56 +0000 | [diff] [blame] | 115 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 116 | tl_assert(a1 < ADDR0_COUNT); |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 117 | tl_assert(size > 0); |
| 118 | tl_assert(a1 + size <= ADDR0_COUNT); |
| 119 | tl_assert(UWORD_MSB(a1) == UWORD_MSB(a1 + size - 1)); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 120 | #endif |
| 121 | bm0[a1 >> BITS_PER_BITS_PER_UWORD] |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 122 | |= (((UWord)1 << size) - 1) << UWORD_LSB(a1); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static __inline__ void bm0_clear(UWord* bm0, const Addr a) |
| 126 | { |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 127 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 128 | tl_assert(a < ADDR0_COUNT); |
| 129 | #endif |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 130 | bm0[a >> BITS_PER_BITS_PER_UWORD] &= ~((UWord)1 << UWORD_LSB(a)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 131 | } |
| 132 | |
bart | 5955f33 | 2008-03-25 17:19:20 +0000 | [diff] [blame] | 133 | /** Clear all of the addresses in range [ a1 .. a1 + size [ in bitmap bm0. */ |
| 134 | static __inline__ void bm0_clear_range(UWord* bm0, |
| 135 | const Addr a1, const SizeT size) |
| 136 | { |
bart | 8b4b2ee | 2008-06-11 13:17:56 +0000 | [diff] [blame] | 137 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | 5955f33 | 2008-03-25 17:19:20 +0000 | [diff] [blame] | 138 | tl_assert(a1 < ADDR0_COUNT); |
| 139 | tl_assert(size > 0); |
| 140 | tl_assert(a1 + size <= ADDR0_COUNT); |
| 141 | tl_assert(UWORD_MSB(a1) == UWORD_MSB(a1 + size - 1)); |
| 142 | #endif |
| 143 | bm0[a1 >> BITS_PER_BITS_PER_UWORD] |
| 144 | &= ~(((UWord)1 << size) - 1) << UWORD_LSB(a1); |
| 145 | } |
| 146 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 147 | static __inline__ UWord bm0_is_set(const UWord* bm0, const Addr a) |
| 148 | { |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 149 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 150 | tl_assert(a < ADDR0_COUNT); |
| 151 | #endif |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 152 | return (bm0[a >> BITS_PER_BITS_PER_UWORD] & ((UWord)1 << UWORD_LSB(a))); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 153 | } |
| 154 | |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 155 | /** Return true if any of the bits [ a1 .. a1+size [ are set in bm0. */ |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 156 | static __inline__ UWord bm0_is_any_set(const UWord* bm0, |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 157 | const Addr a1, const SizeT size) |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 158 | { |
bart | 8b4b2ee | 2008-06-11 13:17:56 +0000 | [diff] [blame] | 159 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 160 | tl_assert(a1 < ADDR0_COUNT); |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 161 | tl_assert(size > 0); |
| 162 | tl_assert(a1 + size <= ADDR0_COUNT); |
| 163 | tl_assert(UWORD_MSB(a1) == UWORD_MSB(a1 + size - 1)); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 164 | #endif |
| 165 | return (bm0[a1 >> BITS_PER_BITS_PER_UWORD] |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 166 | & ((((UWord)1 << size) - 1) << UWORD_LSB(a1))); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 167 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 168 | |
bart | adbdf14 | 2008-03-19 17:00:12 +0000 | [diff] [blame] | 169 | |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 170 | |
| 171 | /*********************************************************************/ |
| 172 | /* Functions for manipulating a struct bitmap. */ |
bart | adbdf14 | 2008-03-19 17:00:12 +0000 | [diff] [blame] | 173 | /*********************************************************************/ |
| 174 | |
| 175 | |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 176 | /* Second level bitmap. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 177 | struct bitmap2 |
| 178 | { |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 179 | Addr addr; ///< address >> ADDR0_BITS |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 180 | int refcnt; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 181 | struct bitmap1 bm1; |
| 182 | }; |
| 183 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 184 | /* One node of bitmap::oset. */ |
| 185 | struct bitmap2ref |
| 186 | { |
| 187 | Addr addr; ///< address >> ADDR0_BITS |
| 188 | struct bitmap2* bm2; |
| 189 | }; |
| 190 | |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 191 | struct bm_cache_elem |
| 192 | { |
| 193 | Addr a1; |
| 194 | struct bitmap2* bm2; |
| 195 | }; |
| 196 | |
| 197 | #define N_CACHE_ELEM 4 |
| 198 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 199 | /* Complete bitmap. */ |
| 200 | struct bitmap |
| 201 | { |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 202 | struct bm_cache_elem cache[N_CACHE_ELEM]; |
| 203 | OSet* oset; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 204 | }; |
| 205 | |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 206 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 207 | static struct bitmap2* bm2_new(const UWord a1); |
| 208 | static struct bitmap2* bm2_make_exclusive(struct bitmap* const bm, |
| 209 | struct bitmap2ref* const bm2ref); |
| 210 | |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 211 | /** Rotate elements cache[0..n-1] such that the element at position n-1 is |
| 212 | * moved to position 0. This allows to speed up future cache lookups. |
| 213 | */ |
| 214 | static __inline__ |
| 215 | void bm_cache_rotate(struct bm_cache_elem cache[], const int n) |
| 216 | { |
bart | 45af5ea | 2008-06-12 06:04:59 +0000 | [diff] [blame] | 217 | #if 0 |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 218 | struct bm_cache_elem t; |
| 219 | |
bart | ea2fa4c | 2008-06-10 06:32:49 +0000 | [diff] [blame] | 220 | tl_assert(2 <= n && n <= 8); |
bart | ea2fa4c | 2008-06-10 06:32:49 +0000 | [diff] [blame] | 221 | |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 222 | t = cache[0]; |
| 223 | if (n > 1) |
| 224 | cache[0] = cache[1]; |
| 225 | if (n > 2) |
| 226 | cache[1] = cache[2]; |
| 227 | if (n > 3) |
| 228 | cache[2] = cache[3]; |
| 229 | if (n > 4) |
| 230 | cache[3] = cache[4]; |
| 231 | if (n > 5) |
| 232 | cache[4] = cache[5]; |
| 233 | if (n > 6) |
| 234 | cache[5] = cache[6]; |
| 235 | if (n > 7) |
| 236 | cache[6] = cache[7]; |
| 237 | cache[n - 1] = t; |
bart | ea2fa4c | 2008-06-10 06:32:49 +0000 | [diff] [blame] | 238 | #endif |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 239 | } |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 240 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 241 | static __inline__ |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 242 | Bool bm_cache_lookup(struct bitmap* const bm, const UWord a1, |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 243 | struct bitmap2** bm2) |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 244 | { |
bart | 8b4b2ee | 2008-06-11 13:17:56 +0000 | [diff] [blame] | 245 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 246 | tl_assert(bm); |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 247 | tl_assert(bm2); |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 248 | #endif |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 249 | |
| 250 | #if N_CACHE_ELEM > 8 |
| 251 | #error Please update the code below. |
| 252 | #endif |
| 253 | #if N_CACHE_ELEM >= 1 |
| 254 | if (a1 == bm->cache[0].a1) |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 255 | { |
| 256 | *bm2 = bm->cache[0].bm2; |
| 257 | return True; |
| 258 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 259 | #endif |
| 260 | #if N_CACHE_ELEM >= 2 |
| 261 | if (a1 == bm->cache[1].a1) |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 262 | { |
| 263 | *bm2 = bm->cache[1].bm2; |
| 264 | return True; |
| 265 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 266 | #endif |
| 267 | #if N_CACHE_ELEM >= 3 |
| 268 | if (a1 == bm->cache[2].a1) |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 269 | { |
| 270 | *bm2 = bm->cache[2].bm2; |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 271 | bm_cache_rotate(bm->cache, 3); |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 272 | return True; |
| 273 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 274 | #endif |
| 275 | #if N_CACHE_ELEM >= 4 |
| 276 | if (a1 == bm->cache[3].a1) |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 277 | { |
| 278 | *bm2 = bm->cache[3].bm2; |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 279 | bm_cache_rotate(bm->cache, 4); |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 280 | return True; |
| 281 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 282 | #endif |
| 283 | #if N_CACHE_ELEM >= 5 |
| 284 | if (a1 == bm->cache[4].a1) |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 285 | { |
| 286 | *bm2 = bm->cache[4].bm2; |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 287 | bm_cache_rotate(bm->cache, 5); |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 288 | return True; |
| 289 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 290 | #endif |
| 291 | #if N_CACHE_ELEM >= 6 |
| 292 | if (a1 == bm->cache[5].a1) |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 293 | { |
| 294 | *bm2 = bm->cache[5].bm2; |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 295 | bm_cache_rotate(bm->cache, 6); |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 296 | return True; |
| 297 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 298 | #endif |
| 299 | #if N_CACHE_ELEM >= 7 |
| 300 | if (a1 == bm->cache[6].a1) |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 301 | { |
| 302 | *bm2 = bm->cache[6].bm2; |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 303 | bm_cache_rotate(bm->cache, 7); |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 304 | return True; |
| 305 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 306 | #endif |
| 307 | #if N_CACHE_ELEM >= 8 |
| 308 | if (a1 == bm->cache[7].a1) |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 309 | { |
| 310 | *bm2 = bm->cache[7].bm2; |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 311 | bm_cache_rotate(bm->cache, 8); |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 312 | return True; |
| 313 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 314 | #endif |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 315 | *bm2 = 0; |
| 316 | return False; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | static __inline__ |
| 320 | void bm_update_cache(struct bitmap* const bm, |
| 321 | const UWord a1, |
| 322 | struct bitmap2* const bm2) |
| 323 | { |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 324 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 325 | tl_assert(bm); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 326 | #endif |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 327 | |
| 328 | #if N_CACHE_ELEM > 8 |
| 329 | #error Please update the code below. |
| 330 | #endif |
| 331 | #if N_CACHE_ELEM >= 8 |
| 332 | bm->cache[7] = bm->cache[6]; |
| 333 | #endif |
| 334 | #if N_CACHE_ELEM >= 7 |
| 335 | bm->cache[6] = bm->cache[5]; |
| 336 | #endif |
| 337 | #if N_CACHE_ELEM >= 6 |
| 338 | bm->cache[5] = bm->cache[4]; |
| 339 | #endif |
| 340 | #if N_CACHE_ELEM >= 5 |
| 341 | bm->cache[4] = bm->cache[3]; |
| 342 | #endif |
| 343 | #if N_CACHE_ELEM >= 4 |
| 344 | bm->cache[3] = bm->cache[2]; |
| 345 | #endif |
| 346 | #if N_CACHE_ELEM >= 3 |
| 347 | bm->cache[2] = bm->cache[1]; |
| 348 | #endif |
| 349 | #if N_CACHE_ELEM >= 2 |
| 350 | bm->cache[1] = bm->cache[0]; |
| 351 | #endif |
| 352 | bm->cache[0].a1 = a1; |
| 353 | bm->cache[0].bm2 = bm2; |
| 354 | } |
| 355 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 356 | /** Look up the address a1 in bitmap bm and return a pointer to a potentially |
| 357 | * shared second level bitmap. The bitmap where the returned pointer points |
| 358 | * at may not be modified by the caller. |
| 359 | * |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 360 | * @param a1 client address shifted right by ADDR0_BITS. |
| 361 | * @param bm bitmap pointer. |
| 362 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 363 | static __inline__ |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 364 | const struct bitmap2* bm2_lookup(struct bitmap* const bm, const UWord a1) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 365 | { |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 366 | struct bitmap2* bm2; |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 367 | struct bitmap2ref* bm2ref; |
| 368 | |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 369 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 370 | tl_assert(bm); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 371 | #endif |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 372 | if (! bm_cache_lookup(bm, a1, &bm2)) |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 373 | { |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 374 | bm2ref = VG_(OSetGen_Lookup)(bm->oset, &a1); |
| 375 | if (bm2ref) |
| 376 | { |
| 377 | bm2 = bm2ref->bm2; |
| 378 | } |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 379 | bm_update_cache(*(struct bitmap**)&bm, a1, bm2); |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 380 | } |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 381 | return bm2; |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /** Look up the address a1 in bitmap bm and return a pointer to a second |
| 385 | * level bitmap that is not shared and hence may be modified. |
| 386 | * |
| 387 | * @param a1 client address shifted right by ADDR0_BITS. |
| 388 | * @param bm bitmap pointer. |
| 389 | */ |
| 390 | static __inline__ |
| 391 | struct bitmap2* |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 392 | bm2_lookup_exclusive(struct bitmap* const bm, const UWord a1) |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 393 | { |
| 394 | struct bitmap2ref* bm2ref; |
| 395 | struct bitmap2* bm2; |
| 396 | |
| 397 | bm2ref = 0; |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 398 | if (bm_cache_lookup(bm, a1, &bm2)) |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 399 | { |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 400 | if (bm2 == 0) |
| 401 | return 0; |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 402 | if (bm2->refcnt > 1) |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 403 | { |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 404 | bm2ref = VG_(OSetGen_Lookup)(bm->oset, &a1); |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 405 | } |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 406 | } |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 407 | else |
| 408 | { |
| 409 | bm2ref = VG_(OSetGen_Lookup)(bm->oset, &a1); |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 410 | if (bm2ref == 0) |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 411 | return 0; |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 412 | bm2 = bm2ref->bm2; |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 413 | } |
| 414 | |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 415 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 416 | tl_assert(bm2); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 417 | #endif |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 418 | |
| 419 | if (bm2->refcnt > 1) |
| 420 | { |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 421 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 422 | tl_assert(bm2ref); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 423 | #endif |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 424 | bm2 = bm2_make_exclusive(*(struct bitmap**)&bm, bm2ref); |
| 425 | } |
| 426 | |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 427 | return bm2; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 428 | } |
| 429 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 430 | /** Look up the address a1 in bitmap bm. The returned second level bitmap has |
| 431 | * reference count one and hence may be modified. |
| 432 | * |
| 433 | * @param a1 client address shifted right by ADDR0_BITS. |
| 434 | * @param bm bitmap pointer. |
| 435 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 436 | static __inline__ |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 437 | struct bitmap2* bm2_insert(struct bitmap* const bm, const UWord a1) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 438 | { |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 439 | struct bitmap2ref* bm2ref; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 440 | struct bitmap2* bm2; |
| 441 | |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 442 | s_bitmap2_node_creation_count++; |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 443 | bm2ref = VG_(OSetGen_AllocNode)(bm->oset, sizeof(*bm2ref)); |
| 444 | bm2ref->addr = a1; |
| 445 | bm2 = bm2_new(a1); |
| 446 | bm2ref->bm2 = bm2; |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 447 | VG_(memset)(&bm2->bm1, 0, sizeof(bm2->bm1)); |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 448 | VG_(OSetGen_Insert)(bm->oset, bm2ref); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 449 | |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 450 | bm_update_cache(*(struct bitmap**)&bm, a1, bm2); |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 451 | |
| 452 | return bm2; |
| 453 | } |
| 454 | |
| 455 | /** Insert a new node in bitmap bm that points to the second level bitmap |
| 456 | * *bm2. This means that *bm2 becomes shared over two or more bitmaps. |
| 457 | */ |
| 458 | static __inline__ |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 459 | struct bitmap2* bm2_insert_addref(struct bitmap* const bm, |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 460 | struct bitmap2* const bm2) |
| 461 | { |
| 462 | struct bitmap2ref* bm2ref; |
| 463 | |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 464 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 465 | tl_assert(bm); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 466 | tl_assert(VG_(OSetGen_Lookup)(bm->oset, &bm2->addr) == 0); |
| 467 | #endif |
bart | 588d90f | 2008-04-06 13:05:58 +0000 | [diff] [blame] | 468 | |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 469 | s_bitmap2_node_creation_count++; |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 470 | bm2ref = VG_(OSetGen_AllocNode)(bm->oset, sizeof(*bm2ref)); |
| 471 | bm2ref->addr = bm2->addr; |
| 472 | bm2ref->bm2 = bm2; |
| 473 | bm2->refcnt++; |
| 474 | VG_(OSetGen_Insert)(bm->oset, bm2ref); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 475 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 476 | bm_update_cache(*(struct bitmap**)&bm, bm2->addr, bm2); |
| 477 | |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 478 | return bm2; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 479 | } |
| 480 | |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 481 | /** Look up the address a1 in bitmap bm, and insert it if not found. |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 482 | * The returned second level bitmap may not be modified. |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 483 | * |
| 484 | * @param a1 client address shifted right by ADDR0_BITS. |
| 485 | * @param bm bitmap pointer. |
| 486 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 487 | static __inline__ |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 488 | struct bitmap2* bm2_lookup_or_insert(struct bitmap* const bm, const UWord a1) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 489 | { |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 490 | struct bitmap2ref* bm2ref; |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 491 | struct bitmap2* bm2; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 492 | |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 493 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 494 | tl_assert(bm); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 495 | #endif |
bart | 567614d | 2008-03-25 19:16:20 +0000 | [diff] [blame] | 496 | if (bm_cache_lookup(bm, a1, &bm2)) |
| 497 | { |
| 498 | if (bm2 == 0) |
| 499 | { |
| 500 | bm2 = bm2_insert(bm, a1); |
| 501 | } |
| 502 | } |
| 503 | else |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 504 | { |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 505 | bm2ref = VG_(OSetGen_Lookup)(bm->oset, &a1); |
| 506 | if (bm2ref) |
| 507 | { |
| 508 | bm2 = bm2ref->bm2; |
| 509 | } |
| 510 | else |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 511 | { |
| 512 | bm2 = bm2_insert(bm, a1); |
| 513 | } |
| 514 | bm_update_cache(*(struct bitmap**)&bm, a1, bm2); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 515 | } |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 516 | return bm2; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 517 | } |
| 518 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 519 | /** Look up the address a1 in bitmap bm, and insert it if not found. |
| 520 | * The returned second level bitmap may be modified. |
| 521 | * |
| 522 | * @param a1 client address shifted right by ADDR0_BITS. |
| 523 | * @param bm bitmap pointer. |
| 524 | */ |
| 525 | static __inline__ |
| 526 | struct bitmap2* bm2_lookup_or_insert_exclusive(struct bitmap* const bm, |
| 527 | const UWord a1) |
| 528 | { |
| 529 | struct bitmap2* bm2; |
| 530 | |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 531 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 532 | tl_assert(bm); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 533 | #endif |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 534 | bm2 = (struct bitmap2*)bm2_lookup_or_insert(bm, a1); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 535 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 536 | tl_assert(bm2); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 537 | #endif |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 538 | if (bm2->refcnt > 1) |
| 539 | { |
| 540 | struct bitmap2ref* bm2ref; |
| 541 | bm2ref = VG_(OSetGen_Lookup)(bm->oset, &a1); |
| 542 | bm2 = bm2_make_exclusive(bm, bm2ref); |
| 543 | } |
| 544 | return bm2; |
| 545 | } |
| 546 | |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 547 | static __inline__ |
| 548 | void bm_access_aligned_load(struct bitmap* const bm, |
| 549 | const Addr a1, const SizeT size) |
| 550 | { |
| 551 | struct bitmap2* bm2; |
| 552 | |
| 553 | bm2 = bm2_lookup_or_insert_exclusive(bm, a1 >> ADDR0_BITS); |
| 554 | bm0_set_range(bm2->bm1.bm0_r, a1 & ADDR0_MASK, size); |
| 555 | } |
| 556 | |
| 557 | static __inline__ |
| 558 | void bm_access_aligned_store(struct bitmap* const bm, |
| 559 | const Addr a1, const SizeT size) |
| 560 | { |
| 561 | struct bitmap2* bm2; |
| 562 | |
| 563 | bm2 = bm2_lookup_or_insert_exclusive(bm, a1 >> ADDR0_BITS); |
| 564 | bm0_set_range(bm2->bm1.bm0_w, a1 & ADDR0_MASK, size); |
| 565 | } |
| 566 | |
| 567 | static __inline__ |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 568 | Bool bm_aligned_load_has_conflict_with(struct bitmap* const bm, |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 569 | const Addr a1, const SizeT size) |
| 570 | { |
| 571 | const struct bitmap2* bm2; |
| 572 | |
| 573 | bm2 = bm2_lookup(bm, a1 >> ADDR0_BITS); |
| 574 | |
| 575 | return (bm2 && bm0_is_any_set(bm2->bm1.bm0_w, a1 & ADDR0_MASK, size)); |
| 576 | } |
| 577 | |
| 578 | static __inline__ |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 579 | Bool bm_aligned_store_has_conflict_with(struct bitmap* const bm, |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 580 | const Addr a1, const SizeT size) |
| 581 | { |
| 582 | const struct bitmap2* bm2; |
| 583 | |
| 584 | bm2 = bm2_lookup(bm, a1 >> ADDR0_BITS); |
| 585 | |
| 586 | if (bm2) |
| 587 | { |
| 588 | if (bm0_is_any_set(bm2->bm1.bm0_r, a1 & ADDR0_MASK, size) |
| 589 | | bm0_is_any_set(bm2->bm1.bm0_w, a1 & ADDR0_MASK, size)) |
| 590 | { |
| 591 | return True; |
| 592 | } |
| 593 | } |
| 594 | return False; |
| 595 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 596 | |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 597 | #endif /* __DRD_BITMAP_H */ |