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 | |
sewardj | 9eecbbb | 2010-05-03 21:37:12 +0000 | [diff] [blame] | 5 | Copyright (C) 2006-2010 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 | |
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 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 30 | #include "pub_drd_bitmap.h" |
bart | 82a0c5d | 2009-02-14 14:49:23 +0000 | [diff] [blame] | 31 | #include "pub_tool_basics.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 32 | #include "pub_tool_oset.h" |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 33 | #include "pub_tool_libcbase.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 34 | |
| 35 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 36 | /* Bitmap representation. A bitmap is a data structure in which two bits are |
| 37 | * reserved per 32 bit address: one bit that indicates that the data at the |
| 38 | * specified address has been read, and one bit that indicates that the data |
| 39 | * has been written to. |
| 40 | */ |
| 41 | |
| 42 | /* Client addresses are split into bitfields as follows: |
| 43 | * ------------------------------------------------------ |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 44 | * | Address MSB | Address LSB | Ignored bits | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 45 | * ------------------------------------------------------ |
| 46 | * | Address MSB | UWord MSB | UWord LSB | Ignored bits | |
| 47 | * ------------------------------------------------------ |
| 48 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 49 | |
| 50 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 51 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 52 | /* Address MSB / LSB split. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 53 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 54 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 55 | /** Number of least significant address bits that are ignored. */ |
| 56 | #define ADDR_IGNORED_BITS 0 |
| 57 | #define ADDR_IGNORED_MASK ((1U << ADDR_IGNORED_BITS) - 1U) |
| 58 | #define ADDR_GRANULARITY (1U << ADDR_IGNORED_BITS) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 59 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 60 | /** |
| 61 | * Round argument a up to a multiple of (1 << ADDR_GRANULARITY), and next |
| 62 | * shift it right ADDR_GRANULARITY bits. The expression below is optimized |
| 63 | * for the case where a is a constant. |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 64 | */ |
| 65 | #define SCALED_SIZE(a) \ |
| 66 | (((((a) - 1U) | ADDR_IGNORED_MASK) + 1U) >> ADDR_IGNORED_BITS) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 67 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 68 | /** |
| 69 | * Number of bits assigned to the least significant component of an address. |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 70 | */ |
| 71 | #define ADDR_LSB_BITS 12 |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 72 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 73 | /** |
| 74 | * Mask that has to be applied to an address of type Addr in order to |
| 75 | * compute the least significant part of an address split, after having |
| 76 | * shifted the address bits ADDR_GRANULARITY to the right. |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 77 | */ |
| 78 | #define ADDR_LSB_MASK (((UWord)1 << ADDR_LSB_BITS) - 1U) |
| 79 | |
| 80 | /** Compute least significant bits of an address of type Addr. */ |
| 81 | static __inline__ |
| 82 | UWord address_lsb(const Addr a) |
| 83 | { return (a >> ADDR_IGNORED_BITS) & ADDR_LSB_MASK; } |
| 84 | |
| 85 | /** |
| 86 | * Compute the first address for which address_lsb() is equal to |
| 87 | * address_lsb(a). |
| 88 | */ |
| 89 | static __inline__ |
| 90 | Addr first_address_with_same_lsb(const Addr a) |
| 91 | { |
| 92 | return ((a | ADDR_IGNORED_MASK) ^ ADDR_IGNORED_MASK); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Compute the first address for which address_lsb() is greater than |
| 97 | * address_lsb(a). |
| 98 | */ |
| 99 | static __inline__ |
| 100 | Addr first_address_with_higher_lsb(const Addr a) |
| 101 | { |
| 102 | return ((a | ADDR_IGNORED_MASK) + 1U); |
| 103 | } |
| 104 | |
| 105 | /** Compute most significant bits of an address of type Addr. */ |
| 106 | static __inline__ |
| 107 | UWord address_msb(const Addr a) |
| 108 | { return a >> (ADDR_LSB_BITS + ADDR_IGNORED_BITS); } |
| 109 | |
| 110 | static __inline__ |
| 111 | Addr first_address_with_higher_msb(const Addr a) |
| 112 | { |
| 113 | return ((a | ((ADDR_LSB_MASK << ADDR_IGNORED_BITS) | ADDR_IGNORED_MASK)) |
| 114 | + 1U); |
| 115 | } |
| 116 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 117 | /** |
| 118 | * Convert LSB and MSB back into an address. |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 119 | * |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 120 | * @note It is assumed that sizeof(Addr) == sizeof(UWord). |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 121 | */ |
| 122 | static __inline__ |
| 123 | Addr make_address(const UWord a1, const UWord a0) |
| 124 | { |
| 125 | return ((a1 << (ADDR_LSB_BITS + ADDR_IGNORED_BITS)) |
| 126 | | (a0 << ADDR_IGNORED_BITS)); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 133 | /** Number of bits that fit in a variable of type UWord. */ |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 134 | #define BITS_PER_UWORD (8U * sizeof(UWord)) |
| 135 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 136 | /** Log2 of BITS_PER_UWORD. */ |
sewardj | 4cb6bf7 | 2010-01-01 18:31:41 +0000 | [diff] [blame] | 137 | #if defined(VGA_x86) || defined(VGA_ppc32) || defined(VGA_arm) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 138 | #define BITS_PER_BITS_PER_UWORD 5 |
| 139 | #elif defined(VGA_amd64) || defined(VGA_ppc64) |
| 140 | #define BITS_PER_BITS_PER_UWORD 6 |
| 141 | #else |
| 142 | #error Unknown platform. |
| 143 | #endif |
| 144 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 145 | /** Number of UWord's needed to store one bit per address LSB. */ |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 146 | #define BITMAP1_UWORD_COUNT (1U << (ADDR_LSB_BITS - BITS_PER_BITS_PER_UWORD)) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 147 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 148 | /** |
| 149 | * Mask that has to be applied to an (Addr >> ADDR_IGNORED_BITS) expression |
| 150 | * in order to compute the least significant part of an UWord. |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 151 | */ |
| 152 | #define UWORD_LSB_MASK (((UWord)1 << BITS_PER_BITS_PER_UWORD) - 1) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 153 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 154 | /** |
| 155 | * Compute index into bm0[] array. |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 156 | * |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 157 | * @param a Address shifted right ADDR_IGNORED_BITS bits. |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 158 | */ |
| 159 | static __inline__ |
| 160 | UWord uword_msb(const UWord a) |
| 161 | { |
| 162 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 163 | tl_assert(a < (1U << ADDR_LSB_BITS)); |
| 164 | #endif |
| 165 | return a >> BITS_PER_BITS_PER_UWORD; |
| 166 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 167 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 168 | /** |
| 169 | * Return the least significant bits. |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 170 | * |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 171 | * @param a Address shifted right ADDR_IGNORED_BITS bits. |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 172 | */ |
| 173 | static __inline__ |
| 174 | UWord uword_lsb(const UWord a) |
| 175 | { |
| 176 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 177 | tl_assert(a < (1U << ADDR_LSB_BITS)); |
| 178 | #endif |
| 179 | return a & UWORD_LSB_MASK; |
| 180 | } |
| 181 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 182 | /** |
| 183 | * Compute the highest address lower than a for which |
| 184 | * uword_lsb(address_lsb(a)) == 0. |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 185 | * |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 186 | * @param a Address. |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 187 | */ |
| 188 | static __inline__ |
| 189 | Addr first_address_with_same_uword_lsb(const Addr a) |
| 190 | { |
| 191 | return (a & (~UWORD_LSB_MASK << ADDR_IGNORED_BITS)); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * First address that will go in the UWord past the one 'a' goes in. |
| 196 | * |
| 197 | * @param a Address. |
| 198 | */ |
| 199 | static __inline__ |
| 200 | Addr first_address_with_higher_uword_msb(const Addr a) |
| 201 | { |
| 202 | return ((a | ((UWORD_LSB_MASK << ADDR_IGNORED_BITS) | ADDR_IGNORED_MASK)) |
| 203 | + 1); |
| 204 | } |
| 205 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 206 | |
| 207 | |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 208 | /* Local variables. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 209 | |
| 210 | static ULong s_bitmap2_creation_count; |
| 211 | |
| 212 | |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 213 | |
| 214 | /*********************************************************************/ |
| 215 | /* Functions for manipulating a struct bitmap1. */ |
| 216 | /*********************************************************************/ |
| 217 | |
| 218 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 219 | /* Lowest level, corresponding to the lowest ADDR_LSB_BITS of an address. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 220 | struct bitmap1 |
| 221 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 222 | UWord bm0_r[BITMAP1_UWORD_COUNT]; |
| 223 | UWord bm0_w[BITMAP1_UWORD_COUNT]; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 224 | }; |
| 225 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 226 | static __inline__ UWord bm0_mask(const UWord a) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 227 | { |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 228 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 229 | tl_assert(address_msb(make_address(0, a)) == 0); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 230 | #endif |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 231 | return ((UWord)1 << uword_lsb(a)); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 232 | } |
| 233 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 234 | /** Set the bit corresponding to address a in bitmap bm0. */ |
| 235 | static __inline__ void bm0_set(UWord* bm0, const UWord a) |
| 236 | { |
| 237 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 238 | tl_assert(address_msb(make_address(0, a)) == 0); |
| 239 | #endif |
| 240 | bm0[uword_msb(a)] |= (UWord)1 << uword_lsb(a); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Set the bits corresponding to all of the addresses in range |
| 245 | * [ a << ADDR_IGNORED_BITS .. (a + size) << ADDR_IGNORED_BITS [ |
| 246 | * in bitmap bm0. |
| 247 | */ |
bart | f8bc71d | 2008-03-15 11:42:34 +0000 | [diff] [blame] | 248 | static __inline__ void bm0_set_range(UWord* bm0, |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 249 | const UWord a, const SizeT size) |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 250 | { |
bart | 8b4b2ee | 2008-06-11 13:17:56 +0000 | [diff] [blame] | 251 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 252 | tl_assert(size > 0); |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 253 | tl_assert(address_msb(make_address(0, a)) == 0); |
| 254 | tl_assert(address_msb(make_address(0, a + size - 1)) == 0); |
| 255 | tl_assert(uword_msb(a) == uword_msb(a + size - 1)); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 256 | #endif |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 257 | bm0[uword_msb(a)] |
| 258 | |= (((UWord)1 << size) - 1) << uword_lsb(a); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 259 | } |
| 260 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 261 | /** Clear the bit corresponding to address a in bitmap bm0. */ |
| 262 | static __inline__ void bm0_clear(UWord* bm0, const UWord a) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 263 | { |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 264 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 265 | tl_assert(address_msb(make_address(0, a)) == 0); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 266 | #endif |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 267 | bm0[uword_msb(a)] &= ~((UWord)1 << uword_lsb(a)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 268 | } |
| 269 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 270 | /** |
| 271 | * Clear all of the addresses in range |
| 272 | * [ a << ADDR_IGNORED_BITS .. (a + size) << ADDR_IGNORED_BITS [ |
| 273 | * in bitmap bm0. |
| 274 | */ |
bart | 5955f33 | 2008-03-25 17:19:20 +0000 | [diff] [blame] | 275 | static __inline__ void bm0_clear_range(UWord* bm0, |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 276 | const UWord a, const SizeT size) |
bart | 5955f33 | 2008-03-25 17:19:20 +0000 | [diff] [blame] | 277 | { |
bart | 8b4b2ee | 2008-06-11 13:17:56 +0000 | [diff] [blame] | 278 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 279 | tl_assert(address_msb(make_address(0, a)) == 0); |
| 280 | tl_assert(size == 0 || address_msb(make_address(0, a + size - 1)) == 0); |
| 281 | tl_assert(size == 0 || uword_msb(a) == uword_msb(a + size - 1)); |
bart | 5955f33 | 2008-03-25 17:19:20 +0000 | [diff] [blame] | 282 | #endif |
bart | 8c04673 | 2009-04-25 06:52:01 +0000 | [diff] [blame] | 283 | /* |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 284 | * Note: although the expression below yields a correct result even if |
bart | 8c04673 | 2009-04-25 06:52:01 +0000 | [diff] [blame] | 285 | * size == 0, do not touch bm0[] if size == 0 because this might otherwise |
| 286 | * cause an access of memory just past the end of the bm0[] array. |
| 287 | */ |
| 288 | if (size > 0) |
| 289 | { |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 290 | bm0[uword_msb(a)] |
| 291 | &= ~((((UWord)1 << size) - 1) << uword_lsb(a)); |
bart | 8c04673 | 2009-04-25 06:52:01 +0000 | [diff] [blame] | 292 | } |
bart | 5955f33 | 2008-03-25 17:19:20 +0000 | [diff] [blame] | 293 | } |
| 294 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 295 | /** Test whether the bit corresponding to address a is set in bitmap bm0. */ |
| 296 | static __inline__ UWord bm0_is_set(const UWord* bm0, const UWord a) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 297 | { |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 298 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 299 | tl_assert(address_msb(make_address(0, a)) == 0); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 300 | #endif |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 301 | return (bm0[uword_msb(a)] & ((UWord)1 << uword_lsb(a))); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 302 | } |
| 303 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 304 | /** |
| 305 | * Return true if a bit corresponding to any of the addresses in range |
| 306 | * [ a << ADDR_IGNORED_BITS .. (a + size) << ADDR_IGNORED_BITS [ |
| 307 | * is set in bm0. |
| 308 | */ |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 309 | static __inline__ UWord bm0_is_any_set(const UWord* bm0, |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 310 | const Addr a, const SizeT size) |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 311 | { |
bart | 8b4b2ee | 2008-06-11 13:17:56 +0000 | [diff] [blame] | 312 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 313 | tl_assert(size > 0); |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 314 | tl_assert(address_msb(make_address(0, a)) == 0); |
| 315 | tl_assert(address_msb(make_address(0, a + size - 1)) == 0); |
| 316 | tl_assert(uword_msb(a) == uword_msb(a + size - 1)); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 317 | #endif |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 318 | return (bm0[uword_msb(a)] & ((((UWord)1 << size) - 1) << uword_lsb(a))); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 319 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 320 | |
bart | adbdf14 | 2008-03-19 17:00:12 +0000 | [diff] [blame] | 321 | |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 322 | |
| 323 | /*********************************************************************/ |
| 324 | /* Functions for manipulating a struct bitmap. */ |
bart | adbdf14 | 2008-03-19 17:00:12 +0000 | [diff] [blame] | 325 | /*********************************************************************/ |
| 326 | |
| 327 | |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 328 | /* Second level bitmap. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 329 | struct bitmap2 |
| 330 | { |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 331 | Addr addr; ///< address_msb(...) |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 332 | Bool recalc; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 333 | struct bitmap1 bm1; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 334 | }; |
| 335 | |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 336 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 337 | static void bm2_clear(struct bitmap2* const bm2); |
| 338 | static __inline__ |
| 339 | struct bitmap2* bm2_insert(struct bitmap* const bm, const UWord a1); |
| 340 | |
| 341 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 342 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 343 | /** |
| 344 | * Rotate elements cache[0..n-1] such that the element at position n-1 is |
| 345 | * moved to position 0. This allows to speed up future cache lookups. |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 346 | */ |
| 347 | static __inline__ |
| 348 | void bm_cache_rotate(struct bm_cache_elem cache[], const int n) |
| 349 | { |
bart | 45af5ea | 2008-06-12 06:04:59 +0000 | [diff] [blame] | 350 | #if 0 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 351 | struct bm_cache_elem t; |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 352 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 353 | tl_assert(2 <= n && n <= 8); |
bart | ea2fa4c | 2008-06-10 06:32:49 +0000 | [diff] [blame] | 354 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 355 | t = cache[0]; |
| 356 | if (n > 1) |
| 357 | cache[0] = cache[1]; |
| 358 | if (n > 2) |
| 359 | cache[1] = cache[2]; |
| 360 | if (n > 3) |
| 361 | cache[2] = cache[3]; |
| 362 | if (n > 4) |
| 363 | cache[3] = cache[4]; |
| 364 | if (n > 5) |
| 365 | cache[4] = cache[5]; |
| 366 | if (n > 6) |
| 367 | cache[5] = cache[6]; |
| 368 | if (n > 7) |
| 369 | cache[6] = cache[7]; |
| 370 | cache[n - 1] = t; |
bart | ea2fa4c | 2008-06-10 06:32:49 +0000 | [diff] [blame] | 371 | #endif |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 372 | } |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 373 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 374 | static __inline__ |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 375 | Bool bm_cache_lookup(struct bitmap* const bm, const UWord a1, |
bart | f29205e | 2008-03-25 18:51:06 +0000 | [diff] [blame] | 376 | struct bitmap2** bm2) |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 377 | { |
bart | 8b4b2ee | 2008-06-11 13:17:56 +0000 | [diff] [blame] | 378 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 379 | tl_assert(bm); |
| 380 | tl_assert(bm2); |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 381 | #endif |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 382 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 383 | #if DRD_BITMAP_N_CACHE_ELEM > 8 |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 384 | #error Please update the code below. |
| 385 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 386 | #if DRD_BITMAP_N_CACHE_ELEM >= 1 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 387 | if (a1 == bm->cache[0].a1) |
| 388 | { |
| 389 | *bm2 = bm->cache[0].bm2; |
| 390 | return True; |
| 391 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 392 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 393 | #if DRD_BITMAP_N_CACHE_ELEM >= 2 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 394 | if (a1 == bm->cache[1].a1) |
| 395 | { |
| 396 | *bm2 = bm->cache[1].bm2; |
| 397 | return True; |
| 398 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 399 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 400 | #if DRD_BITMAP_N_CACHE_ELEM >= 3 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 401 | if (a1 == bm->cache[2].a1) |
| 402 | { |
| 403 | *bm2 = bm->cache[2].bm2; |
| 404 | bm_cache_rotate(bm->cache, 3); |
| 405 | return True; |
| 406 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 407 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 408 | #if DRD_BITMAP_N_CACHE_ELEM >= 4 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 409 | if (a1 == bm->cache[3].a1) |
| 410 | { |
| 411 | *bm2 = bm->cache[3].bm2; |
| 412 | bm_cache_rotate(bm->cache, 4); |
| 413 | return True; |
| 414 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 415 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 416 | #if DRD_BITMAP_N_CACHE_ELEM >= 5 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 417 | if (a1 == bm->cache[4].a1) |
| 418 | { |
| 419 | *bm2 = bm->cache[4].bm2; |
| 420 | bm_cache_rotate(bm->cache, 5); |
| 421 | return True; |
| 422 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 423 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 424 | #if DRD_BITMAP_N_CACHE_ELEM >= 6 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 425 | if (a1 == bm->cache[5].a1) |
| 426 | { |
| 427 | *bm2 = bm->cache[5].bm2; |
| 428 | bm_cache_rotate(bm->cache, 6); |
| 429 | return True; |
| 430 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 431 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 432 | #if DRD_BITMAP_N_CACHE_ELEM >= 7 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 433 | if (a1 == bm->cache[6].a1) |
| 434 | { |
| 435 | *bm2 = bm->cache[6].bm2; |
| 436 | bm_cache_rotate(bm->cache, 7); |
| 437 | return True; |
| 438 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 439 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 440 | #if DRD_BITMAP_N_CACHE_ELEM >= 8 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 441 | if (a1 == bm->cache[7].a1) |
| 442 | { |
| 443 | *bm2 = bm->cache[7].bm2; |
| 444 | bm_cache_rotate(bm->cache, 8); |
| 445 | return True; |
| 446 | } |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 447 | #endif |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 448 | *bm2 = 0; |
| 449 | return False; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | static __inline__ |
| 453 | void bm_update_cache(struct bitmap* const bm, |
| 454 | const UWord a1, |
| 455 | struct bitmap2* const bm2) |
| 456 | { |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 457 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 458 | tl_assert(bm); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 459 | #endif |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 460 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 461 | #if DRD_BITMAP_N_CACHE_ELEM > 8 |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 462 | #error Please update the code below. |
| 463 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 464 | #if DRD_BITMAP_N_CACHE_ELEM >= 8 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 465 | bm->cache[7] = bm->cache[6]; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 466 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 467 | #if DRD_BITMAP_N_CACHE_ELEM >= 7 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 468 | bm->cache[6] = bm->cache[5]; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 469 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 470 | #if DRD_BITMAP_N_CACHE_ELEM >= 6 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 471 | bm->cache[5] = bm->cache[4]; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 472 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 473 | #if DRD_BITMAP_N_CACHE_ELEM >= 5 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 474 | bm->cache[4] = bm->cache[3]; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 475 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 476 | #if DRD_BITMAP_N_CACHE_ELEM >= 4 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 477 | bm->cache[3] = bm->cache[2]; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 478 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 479 | #if DRD_BITMAP_N_CACHE_ELEM >= 3 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 480 | bm->cache[2] = bm->cache[1]; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 481 | #endif |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 482 | #if DRD_BITMAP_N_CACHE_ELEM >= 2 |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 483 | bm->cache[1] = bm->cache[0]; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 484 | #endif |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 485 | bm->cache[0].a1 = a1; |
| 486 | bm->cache[0].bm2 = bm2; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 487 | } |
| 488 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 489 | /** |
| 490 | * Look up the address a1 in bitmap bm and return a pointer to a potentially |
| 491 | * shared second level bitmap. The bitmap where the returned pointer points |
| 492 | * at may not be modified by the caller. |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 493 | * |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 494 | * @param a1 client address shifted right by ADDR_LSB_BITS. |
| 495 | * @param bm bitmap pointer. |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 496 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 497 | static __inline__ |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 498 | const struct bitmap2* bm2_lookup(struct bitmap* const bm, const UWord a1) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 499 | { |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 500 | struct bitmap2* bm2; |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 501 | |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 502 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 503 | tl_assert(bm); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 504 | #endif |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 505 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 506 | if (! bm_cache_lookup(bm, a1, &bm2)) |
| 507 | { |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 508 | bm2 = VG_(OSetGen_Lookup)(bm->oset, &a1); |
| 509 | bm_update_cache(bm, a1, bm2); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 510 | } |
| 511 | return bm2; |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 512 | } |
| 513 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 514 | /** |
| 515 | * Look up the address a1 in bitmap bm and return a pointer to a second |
| 516 | * level bitmap that is not shared and hence may be modified. |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 517 | * |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 518 | * @param a1 client address shifted right by ADDR_LSB_BITS. |
| 519 | * @param bm bitmap pointer. |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 520 | */ |
| 521 | static __inline__ |
| 522 | struct bitmap2* |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 523 | bm2_lookup_exclusive(struct bitmap* const bm, const UWord a1) |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 524 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 525 | struct bitmap2* bm2; |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 526 | |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 527 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 528 | tl_assert(bm); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 529 | #endif |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 530 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 531 | if (! bm_cache_lookup(bm, a1, &bm2)) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 532 | { |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 533 | bm2 = VG_(OSetGen_Lookup)(bm->oset, &a1); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 534 | } |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 535 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 536 | return bm2; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 537 | } |
| 538 | |
bart | 6584b69 | 2009-06-21 10:11:15 +0000 | [diff] [blame] | 539 | /** Clear the content of the second-level bitmap. */ |
| 540 | static __inline__ |
| 541 | void bm2_clear(struct bitmap2* const bm2) |
| 542 | { |
| 543 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 544 | tl_assert(bm2); |
| 545 | #endif |
| 546 | VG_(memset)(&bm2->bm1, 0, sizeof(bm2->bm1)); |
| 547 | } |
| 548 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 549 | /** |
| 550 | * Insert an uninitialized second level bitmap for the address a1. |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 551 | * |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 552 | * @param bm bitmap pointer. |
| 553 | * @param a1 client address shifted right by ADDR_LSB_BITS. |
| 554 | * |
| 555 | * @note bitmap2::recalc isn't initialized here on purpose. |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 556 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 557 | static __inline__ |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 558 | struct bitmap2* bm2_insert(struct bitmap* const bm, const UWord a1) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 559 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 560 | struct bitmap2* bm2; |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 561 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 562 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 563 | tl_assert(bm); |
| 564 | #endif |
| 565 | |
| 566 | s_bitmap2_creation_count++; |
| 567 | |
| 568 | bm2 = VG_(OSetGen_AllocNode)(bm->oset, sizeof(*bm2)); |
| 569 | bm2->addr = a1; |
| 570 | VG_(OSetGen_Insert)(bm->oset, bm2); |
| 571 | |
| 572 | bm_update_cache(bm, a1, bm2); |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 573 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 574 | return bm2; |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 575 | } |
| 576 | |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 577 | static __inline__ |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 578 | struct bitmap2* bm2_insert_copy(struct bitmap* const bm, |
| 579 | struct bitmap2* const bm2) |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 580 | { |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 581 | struct bitmap2* bm2_copy; |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 582 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 583 | bm2_copy = bm2_insert(bm, bm2->addr); |
| 584 | VG_(memcpy)(&bm2_copy->bm1, &bm2->bm1, sizeof(bm2->bm1)); |
| 585 | return bm2_copy; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 586 | } |
| 587 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 588 | /** |
| 589 | * Look up the address a1 in bitmap bm, and insert it if not found. |
| 590 | * The returned second level bitmap may not be modified. |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 591 | * |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 592 | * @param bm bitmap pointer. |
| 593 | * @param a1 client address shifted right by ADDR_LSB_BITS. |
bart | 0008f5b | 2008-03-22 17:07:39 +0000 | [diff] [blame] | 594 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 595 | static __inline__ |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 596 | struct bitmap2* bm2_lookup_or_insert(struct bitmap* const bm, const UWord a1) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 597 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 598 | struct bitmap2* bm2; |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 599 | |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 600 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 601 | tl_assert(bm); |
bart | 3c9989f | 2008-06-11 19:17:01 +0000 | [diff] [blame] | 602 | #endif |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 603 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 604 | if (bm_cache_lookup(bm, a1, &bm2)) |
| 605 | { |
| 606 | if (bm2 == 0) |
| 607 | { |
| 608 | bm2 = bm2_insert(bm, a1); |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 609 | bm2_clear(bm2); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | else |
| 613 | { |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 614 | bm2 = VG_(OSetGen_Lookup)(bm->oset, &a1); |
| 615 | if (! bm2) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 616 | { |
| 617 | bm2 = bm2_insert(bm, a1); |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 618 | bm2_clear(bm2); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 619 | } |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 620 | bm_update_cache(bm, a1, bm2); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 621 | } |
| 622 | return bm2; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 623 | } |
| 624 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 625 | /** |
| 626 | * Look up the address a1 in bitmap bm, and insert it if not found. |
| 627 | * The returned second level bitmap may be modified. |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 628 | * |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 629 | * @param a1 client address shifted right by ADDR_LSB_BITS. |
| 630 | * @param bm bitmap pointer. |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 631 | */ |
| 632 | static __inline__ |
| 633 | struct bitmap2* bm2_lookup_or_insert_exclusive(struct bitmap* const bm, |
| 634 | const UWord a1) |
| 635 | { |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 636 | return bm2_lookup_or_insert(bm, a1); |
bart | f647d34 | 2008-03-24 19:12:12 +0000 | [diff] [blame] | 637 | } |
| 638 | |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 639 | static __inline__ |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 640 | void bm2_remove(struct bitmap* const bm, const UWord a1) |
| 641 | { |
| 642 | struct bitmap2* bm2; |
| 643 | |
| 644 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 645 | tl_assert(bm); |
| 646 | #endif |
| 647 | |
| 648 | bm2 = VG_(OSetGen_Remove)(bm->oset, &a1); |
| 649 | VG_(OSetGen_FreeNode)(bm->oset, bm2); |
| 650 | |
| 651 | bm_update_cache(bm, a1, NULL); |
| 652 | } |
| 653 | |
| 654 | static __inline__ |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 655 | void bm_access_aligned_load(struct bitmap* const bm, |
| 656 | const Addr a1, const SizeT size) |
| 657 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 658 | struct bitmap2* bm2; |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 659 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 660 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 661 | tl_assert(bm); |
| 662 | #endif |
| 663 | |
| 664 | bm2 = bm2_lookup_or_insert_exclusive(bm, address_msb(a1)); |
| 665 | bm0_set_range(bm2->bm1.bm0_r, |
| 666 | (a1 >> ADDR_IGNORED_BITS) & ADDR_LSB_MASK, |
| 667 | SCALED_SIZE(size)); |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | static __inline__ |
| 671 | void bm_access_aligned_store(struct bitmap* const bm, |
| 672 | const Addr a1, const SizeT size) |
| 673 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 674 | struct bitmap2* bm2; |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 675 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 676 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 677 | tl_assert(bm); |
| 678 | #endif |
| 679 | |
| 680 | bm2 = bm2_lookup_or_insert_exclusive(bm, address_msb(a1)); |
| 681 | bm0_set_range(bm2->bm1.bm0_w, |
| 682 | (a1 >> ADDR_IGNORED_BITS) & ADDR_LSB_MASK, |
| 683 | SCALED_SIZE(size)); |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | static __inline__ |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 687 | Bool bm_aligned_load_has_conflict_with(struct bitmap* const bm, |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 688 | const Addr a, const SizeT size) |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 689 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 690 | const struct bitmap2* bm2; |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 691 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 692 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 693 | tl_assert(bm); |
| 694 | #endif |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 695 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 696 | bm2 = bm2_lookup(bm, address_msb(a)); |
| 697 | return (bm2 |
| 698 | && bm0_is_any_set(bm2->bm1.bm0_w, |
| 699 | address_lsb(a), |
| 700 | SCALED_SIZE(size))); |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | static __inline__ |
bart | 7e81a17 | 2008-06-09 19:50:51 +0000 | [diff] [blame] | 704 | Bool bm_aligned_store_has_conflict_with(struct bitmap* const bm, |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 705 | const Addr a, const SizeT size) |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 706 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 707 | const struct bitmap2* bm2; |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 708 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 709 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 710 | tl_assert(bm); |
| 711 | #endif |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 712 | |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 713 | bm2 = bm2_lookup(bm, address_msb(a)); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 714 | if (bm2) |
| 715 | { |
bart | 7b706b3 | 2009-05-10 06:55:39 +0000 | [diff] [blame] | 716 | if (bm0_is_any_set(bm2->bm1.bm0_r, address_lsb(a), SCALED_SIZE(size)) |
| 717 | | bm0_is_any_set(bm2->bm1.bm0_w, address_lsb(a), SCALED_SIZE(size))) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 718 | { |
| 719 | return True; |
| 720 | } |
| 721 | } |
| 722 | return False; |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 723 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 724 | |
bart | 33e56c9 | 2008-03-24 06:41:30 +0000 | [diff] [blame] | 725 | #endif /* __DRD_BITMAP_H */ |