blob: 7f3b77a1547f45103f338f97e607a6b13b0e30ac [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
sewardj85642922008-01-14 11:54:56 +00004 Copyright (C) 2006-2008 Bart Van Assche
sewardjaf44c822007-11-25 14:01:38 +00005 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
bart33e56c92008-03-24 06:41:30 +000026#ifndef __DRD_BITMAP_H
27#define __DRD_BITMAP_H
sewardjaf44c822007-11-25 14:01:38 +000028
29
30#include "pub_tool_oset.h"
31
32
33/*
34 Bitmap representation. A bitmap is a data structure in which two bits are
35 reserved per 32 bit address: one bit that indicates that the data at the
36 specified address has been read, and one bit that indicates that the data has
37 been written to.
38*/
39
40
41/* Macro definitions. */
42
bartf587d042008-03-15 14:30:20 +000043#define ADDR0_BITS 16
sewardjaf44c822007-11-25 14:01:38 +000044
barta79df6e2008-03-14 17:07:51 +000045#define ADDR0_COUNT ((UWord)1 << ADDR0_BITS)
sewardjaf44c822007-11-25 14:01:38 +000046
47#define ADDR0_MASK (ADDR0_COUNT - 1)
48
bart0268dfa2008-03-11 20:10:21 +000049#define SPLIT_ADDRESS(a) \
50 UWord a##0 = ((a) & ADDR0_MASK); \
sewardjaf44c822007-11-25 14:01:38 +000051 UWord a##1 = ((a) >> ADDR0_BITS);
52
53// Assumption: sizeof(Addr) == sizeof(UWord).
bart0268dfa2008-03-11 20:10:21 +000054#define MAKE_ADDRESS(a1, a0) \
sewardjaf44c822007-11-25 14:01:38 +000055 (Addr)(((UWord)(a1) << (ADDR0_BITS)) | ((UWord)(a0)))
56
57#define BITS_PER_UWORD (8UL*sizeof(UWord))
58#if defined(VGA_x86) || defined(VGA_ppc32)
59#define BITS_PER_BITS_PER_UWORD 5
60#elif defined(VGA_amd64) || defined(VGA_ppc64)
61#define BITS_PER_BITS_PER_UWORD 6
62#else
63#error Unknown platform.
64#endif
65
66#define BITMAP1_UWORD_COUNT (ADDR0_COUNT >> BITS_PER_BITS_PER_UWORD)
67
68/* Highest bits of an address that fit into the same UWord of bm0[]. */
69#define UWORD_MSB(a) ((a) & ~(BITS_PER_UWORD - 1))
70
71/* Lowest bits of an address that fit into the same UWord of bm0[]. */
72#define UWORD_LSB(a) ((a) & (BITS_PER_UWORD - 1))
73
74/* Highest address that fits in the same UWord as a. */
75#define UWORD_HIGHEST_ADDRESS(a) ((a) | (BITS_PER_UWORD - 1))
76
77
bart33e56c92008-03-24 06:41:30 +000078/* Local variables. */
sewardjaf44c822007-11-25 14:01:38 +000079
80static ULong s_bitmap2_creation_count;
81
82
bart0008f5b2008-03-22 17:07:39 +000083
84/*********************************************************************/
85/* Functions for manipulating a struct bitmap1. */
86/*********************************************************************/
87
88
sewardjaf44c822007-11-25 14:01:38 +000089/* Lowest level, corresponding to the lowest ADDR0_BITS of an address. */
90struct bitmap1
91{
92 UWord bm0_r[BITMAP1_UWORD_COUNT];
93 UWord bm0_w[BITMAP1_UWORD_COUNT];
94};
95
96static __inline__ UWord bm0_mask(const Addr a)
97{
barta79df6e2008-03-14 17:07:51 +000098 return ((UWord)1 << UWORD_LSB(a));
sewardjaf44c822007-11-25 14:01:38 +000099}
100
101static __inline__ void bm0_set(UWord* bm0, const Addr a)
102{
103 //tl_assert(a < ADDR0_COUNT);
barta79df6e2008-03-14 17:07:51 +0000104 bm0[a >> BITS_PER_BITS_PER_UWORD] |= (UWord)1 << UWORD_LSB(a);
105}
106
bartf8bc71d2008-03-15 11:42:34 +0000107/** Set all of the addresses in range [ a1 .. a1 + size [ in bitmap bm0. */
108static __inline__ void bm0_set_range(UWord* bm0,
109 const Addr a1, const SizeT size)
barta79df6e2008-03-14 17:07:51 +0000110{
111#if 0
112 tl_assert(a1 < ADDR0_COUNT);
bartf8bc71d2008-03-15 11:42:34 +0000113 tl_assert(size > 0);
114 tl_assert(a1 + size <= ADDR0_COUNT);
115 tl_assert(UWORD_MSB(a1) == UWORD_MSB(a1 + size - 1));
barta79df6e2008-03-14 17:07:51 +0000116#endif
117 bm0[a1 >> BITS_PER_BITS_PER_UWORD]
bartf8bc71d2008-03-15 11:42:34 +0000118 |= (((UWord)1 << size) - 1) << UWORD_LSB(a1);
sewardjaf44c822007-11-25 14:01:38 +0000119}
120
121static __inline__ void bm0_clear(UWord* bm0, const Addr a)
122{
123 //tl_assert(a < ADDR0_COUNT);
barta79df6e2008-03-14 17:07:51 +0000124 bm0[a >> BITS_PER_BITS_PER_UWORD] &= ~((UWord)1 << UWORD_LSB(a));
sewardjaf44c822007-11-25 14:01:38 +0000125}
126
bart5955f332008-03-25 17:19:20 +0000127/** Clear all of the addresses in range [ a1 .. a1 + size [ in bitmap bm0. */
128static __inline__ void bm0_clear_range(UWord* bm0,
129 const Addr a1, const SizeT size)
130{
131#if 0
132 tl_assert(a1 < ADDR0_COUNT);
133 tl_assert(size > 0);
134 tl_assert(a1 + size <= ADDR0_COUNT);
135 tl_assert(UWORD_MSB(a1) == UWORD_MSB(a1 + size - 1));
136#endif
137 bm0[a1 >> BITS_PER_BITS_PER_UWORD]
138 &= ~(((UWord)1 << size) - 1) << UWORD_LSB(a1);
139}
140
sewardjaf44c822007-11-25 14:01:38 +0000141static __inline__ UWord bm0_is_set(const UWord* bm0, const Addr a)
142{
143 //tl_assert(a < ADDR0_COUNT);
barta79df6e2008-03-14 17:07:51 +0000144 return (bm0[a >> BITS_PER_BITS_PER_UWORD] & ((UWord)1 << UWORD_LSB(a)));
sewardjaf44c822007-11-25 14:01:38 +0000145}
146
bartf8bc71d2008-03-15 11:42:34 +0000147/** Return true if any of the bits [ a1 .. a1+size [ are set in bm0. */
barta79df6e2008-03-14 17:07:51 +0000148static __inline__ UWord bm0_is_any_set(const UWord* bm0,
bartf8bc71d2008-03-15 11:42:34 +0000149 const Addr a1, const SizeT size)
barta79df6e2008-03-14 17:07:51 +0000150{
151#if 0
152 tl_assert(a1 < ADDR0_COUNT);
bartf8bc71d2008-03-15 11:42:34 +0000153 tl_assert(size > 0);
154 tl_assert(a1 + size <= ADDR0_COUNT);
155 tl_assert(UWORD_MSB(a1) == UWORD_MSB(a1 + size - 1));
barta79df6e2008-03-14 17:07:51 +0000156#endif
157 return (bm0[a1 >> BITS_PER_BITS_PER_UWORD]
bartf8bc71d2008-03-15 11:42:34 +0000158 & ((((UWord)1 << size) - 1) << UWORD_LSB(a1)));
barta79df6e2008-03-14 17:07:51 +0000159}
sewardjaf44c822007-11-25 14:01:38 +0000160
bartadbdf142008-03-19 17:00:12 +0000161
bart0008f5b2008-03-22 17:07:39 +0000162
163/*********************************************************************/
164/* Functions for manipulating a struct bitmap. */
bartadbdf142008-03-19 17:00:12 +0000165/*********************************************************************/
166
167
bart33e56c92008-03-24 06:41:30 +0000168/* Second level bitmap. */
sewardjaf44c822007-11-25 14:01:38 +0000169struct bitmap2
170{
bart33e56c92008-03-24 06:41:30 +0000171 Addr addr; ///< address >> ADDR0_BITS
bartf647d342008-03-24 19:12:12 +0000172 int refcnt;
sewardjaf44c822007-11-25 14:01:38 +0000173 struct bitmap1 bm1;
174};
175
bartf647d342008-03-24 19:12:12 +0000176/* One node of bitmap::oset. */
177struct bitmap2ref
178{
179 Addr addr; ///< address >> ADDR0_BITS
180 struct bitmap2* bm2;
181};
182
bart33e56c92008-03-24 06:41:30 +0000183struct bm_cache_elem
184{
185 Addr a1;
186 struct bitmap2* bm2;
187};
188
189#define N_CACHE_ELEM 4
190
sewardjaf44c822007-11-25 14:01:38 +0000191/* Complete bitmap. */
192struct bitmap
193{
bart33e56c92008-03-24 06:41:30 +0000194 struct bm_cache_elem cache[N_CACHE_ELEM];
195 OSet* oset;
sewardjaf44c822007-11-25 14:01:38 +0000196};
197
bart33e56c92008-03-24 06:41:30 +0000198
bartf647d342008-03-24 19:12:12 +0000199static struct bitmap2* bm2_new(const UWord a1);
200static struct bitmap2* bm2_make_exclusive(struct bitmap* const bm,
201 struct bitmap2ref* const bm2ref);
202
203
204#if 0
205/** Bitmap invariant check.
206 *
207 * @return 1 if the invariant is satisfied, 0 if not.
208 */
209static __inline__
210int bm_check(const struct bitmap* const bm)
211{
212 struct bitmap2_ref* bm2ref;
213
214 tl_assert(bm);
215
216 return (bm->cache[0].a1 == 0
217 && bm->cache[1].a1 == 0
218 || ((bm2ref = VG_(OSetGen_Lookup)(bm->oset, &bm->last_lookup_a1))
219 && bm2ref->bm2
220 && bm->last_lookup_a1 == bm2ref->bm2->addr
221 && bm2ref->bm2->refcnt >= 1)
222 );
223}
224#endif
225
bart33e56c92008-03-24 06:41:30 +0000226static __inline__
227struct bitmap2* bm_cache_lookup(const struct bitmap* const bm, const UWord a1)
228{
229 tl_assert(bm);
230
231#if N_CACHE_ELEM > 8
232#error Please update the code below.
233#endif
234#if N_CACHE_ELEM >= 1
235 if (a1 == bm->cache[0].a1)
236 return bm->cache[0].bm2;
237#endif
238#if N_CACHE_ELEM >= 2
239 if (a1 == bm->cache[1].a1)
240 return bm->cache[1].bm2;
241#endif
242#if N_CACHE_ELEM >= 3
243 if (a1 == bm->cache[2].a1)
244 return bm->cache[2].bm2;
245#endif
246#if N_CACHE_ELEM >= 4
247 if (a1 == bm->cache[3].a1)
248 return bm->cache[3].bm2;
249#endif
250#if N_CACHE_ELEM >= 5
251 if (a1 == bm->cache[4].a1)
252 return bm->cache[4].bm2;
253#endif
254#if N_CACHE_ELEM >= 6
255 if (a1 == bm->cache[5].a1)
256 return bm->cache[5].bm2;
257#endif
258#if N_CACHE_ELEM >= 7
259 if (a1 == bm->cache[6].a1)
260 return bm->cache[6].bm2;
261#endif
262#if N_CACHE_ELEM >= 8
263 if (a1 == bm->cache[7].a1)
264 return bm->cache[7].bm2;
265#endif
266 return 0;
267}
268
269static __inline__
270void bm_update_cache(struct bitmap* const bm,
271 const UWord a1,
272 struct bitmap2* const bm2)
273{
274 tl_assert(bm);
275
276#if N_CACHE_ELEM > 8
277#error Please update the code below.
278#endif
279#if N_CACHE_ELEM >= 8
280 bm->cache[7] = bm->cache[6];
281#endif
282#if N_CACHE_ELEM >= 7
283 bm->cache[6] = bm->cache[5];
284#endif
285#if N_CACHE_ELEM >= 6
286 bm->cache[5] = bm->cache[4];
287#endif
288#if N_CACHE_ELEM >= 5
289 bm->cache[4] = bm->cache[3];
290#endif
291#if N_CACHE_ELEM >= 4
292 bm->cache[3] = bm->cache[2];
293#endif
294#if N_CACHE_ELEM >= 3
295 bm->cache[2] = bm->cache[1];
296#endif
297#if N_CACHE_ELEM >= 2
298 bm->cache[1] = bm->cache[0];
299#endif
300 bm->cache[0].a1 = a1;
301 bm->cache[0].bm2 = bm2;
302}
303
bartf647d342008-03-24 19:12:12 +0000304/** Look up the address a1 in bitmap bm and return a pointer to a potentially
305 * shared second level bitmap. The bitmap where the returned pointer points
306 * at may not be modified by the caller.
307 *
bart0008f5b2008-03-22 17:07:39 +0000308 * @param a1 client address shifted right by ADDR0_BITS.
309 * @param bm bitmap pointer.
310 */
sewardjaf44c822007-11-25 14:01:38 +0000311static __inline__
bartf647d342008-03-24 19:12:12 +0000312const struct bitmap2* bm2_lookup(const struct bitmap* const bm, const UWord a1)
sewardjaf44c822007-11-25 14:01:38 +0000313{
bartf647d342008-03-24 19:12:12 +0000314 struct bitmap2ref* bm2ref;
315
316 tl_assert(bm);
317 if (a1 == bm->cache[0].a1)
barta79df6e2008-03-14 17:07:51 +0000318 {
bartf647d342008-03-24 19:12:12 +0000319 return bm->cache[0].bm2;
320 }
321 if (a1 == bm->cache[1].a1)
322 {
323 return bm->cache[1].bm2;
324 }
325 bm2ref = VG_(OSetGen_Lookup)(bm->oset, &a1);
326 if (bm2ref)
327 {
328 struct bitmap2* const bm2 = bm2ref->bm2;
329 bm_update_cache(*(struct bitmap**)&bm, a1, bm2);
330 return bm2;
331 }
332 return 0;
333}
334
335/** Look up the address a1 in bitmap bm and return a pointer to a second
336 * level bitmap that is not shared and hence may be modified.
337 *
338 * @param a1 client address shifted right by ADDR0_BITS.
339 * @param bm bitmap pointer.
340 */
341static __inline__
342struct bitmap2*
343bm2_lookup_exclusive(const struct bitmap* const bm, const UWord a1)
344{
345 struct bitmap2ref* bm2ref;
346 struct bitmap2* bm2;
347
348 bm2ref = 0;
349 bm2 = bm_cache_lookup(bm, a1);
350 if (bm2)
351 {
352 if (bm2->refcnt > 1)
bart33e56c92008-03-24 06:41:30 +0000353 {
bartf647d342008-03-24 19:12:12 +0000354 bm2ref = VG_(OSetGen_Lookup)(bm->oset, &a1);
bart33e56c92008-03-24 06:41:30 +0000355 }
barta79df6e2008-03-14 17:07:51 +0000356 }
bartf647d342008-03-24 19:12:12 +0000357 else
358 {
359 bm2ref = VG_(OSetGen_Lookup)(bm->oset, &a1);
360 if (bm2ref)
361 {
362 bm2 = bm2ref->bm2;
363 }
364 else
365 {
366 return 0;
367 }
368 }
369
370 tl_assert(bm2);
371
372 if (bm2->refcnt > 1)
373 {
374 tl_assert(bm2ref);
375 bm2 = bm2_make_exclusive(*(struct bitmap**)&bm, bm2ref);
376 }
377
bart33e56c92008-03-24 06:41:30 +0000378 return bm2;
sewardjaf44c822007-11-25 14:01:38 +0000379}
380
bartf647d342008-03-24 19:12:12 +0000381/** Look up the address a1 in bitmap bm. The returned second level bitmap has
382 * reference count one and hence may be modified.
383 *
384 * @param a1 client address shifted right by ADDR0_BITS.
385 * @param bm bitmap pointer.
386 */
sewardjaf44c822007-11-25 14:01:38 +0000387static __inline__
bart0008f5b2008-03-22 17:07:39 +0000388struct bitmap2* bm2_insert(const struct bitmap* const bm, const UWord a1)
sewardjaf44c822007-11-25 14:01:38 +0000389{
bartf647d342008-03-24 19:12:12 +0000390 struct bitmap2ref* bm2ref;
bart33e56c92008-03-24 06:41:30 +0000391 struct bitmap2* bm2;
392
bartf647d342008-03-24 19:12:12 +0000393 bm2ref = VG_(OSetGen_AllocNode)(bm->oset, sizeof(*bm2ref));
394 bm2ref->addr = a1;
395 bm2 = bm2_new(a1);
396 bm2ref->bm2 = bm2;
bart0008f5b2008-03-22 17:07:39 +0000397 VG_(memset)(&bm2->bm1, 0, sizeof(bm2->bm1));
bartf647d342008-03-24 19:12:12 +0000398 VG_(OSetGen_Insert)(bm->oset, bm2ref);
barta79df6e2008-03-14 17:07:51 +0000399
bart33e56c92008-03-24 06:41:30 +0000400 bm_update_cache(*(struct bitmap**)&bm, a1, bm2);
bartf647d342008-03-24 19:12:12 +0000401
402 return bm2;
403}
404
405/** Insert a new node in bitmap bm that points to the second level bitmap
406 * *bm2. This means that *bm2 becomes shared over two or more bitmaps.
407 */
408static __inline__
409struct bitmap2* bm2_insert_addref(const struct bitmap* const bm,
410 struct bitmap2* const bm2)
411{
412 struct bitmap2ref* bm2ref;
413
414 tl_assert(bm);
415 //tl_assert(VG_(OSetGen_Lookup)(bm->oset, &bm2->addr) == 0);
416 bm2ref = VG_(OSetGen_AllocNode)(bm->oset, sizeof(*bm2ref));
417 bm2ref->addr = bm2->addr;
418 bm2ref->bm2 = bm2;
419 bm2->refcnt++;
420 VG_(OSetGen_Insert)(bm->oset, bm2ref);
barta79df6e2008-03-14 17:07:51 +0000421
bartf647d342008-03-24 19:12:12 +0000422 bm_update_cache(*(struct bitmap**)&bm, bm2->addr, bm2);
423
bart0008f5b2008-03-22 17:07:39 +0000424 return bm2;
sewardjaf44c822007-11-25 14:01:38 +0000425}
426
bart0008f5b2008-03-22 17:07:39 +0000427/** Look up the address a1 in bitmap bm, and insert it if not found.
bartf647d342008-03-24 19:12:12 +0000428 * The returned second level bitmap may not be modified.
bart0008f5b2008-03-22 17:07:39 +0000429 *
430 * @param a1 client address shifted right by ADDR0_BITS.
431 * @param bm bitmap pointer.
432 */
sewardjaf44c822007-11-25 14:01:38 +0000433static __inline__
434struct bitmap2* bm2_lookup_or_insert(const struct bitmap* const bm,
435 const UWord a1)
436{
bartf647d342008-03-24 19:12:12 +0000437 struct bitmap2ref* bm2ref;
bart0008f5b2008-03-22 17:07:39 +0000438 struct bitmap2* bm2;
barta79df6e2008-03-14 17:07:51 +0000439
bartf647d342008-03-24 19:12:12 +0000440 tl_assert(bm);
bart33e56c92008-03-24 06:41:30 +0000441 bm2 = bm_cache_lookup(bm, a1);
bart0008f5b2008-03-22 17:07:39 +0000442 if (bm2 == 0)
barta79df6e2008-03-14 17:07:51 +0000443 {
bartf647d342008-03-24 19:12:12 +0000444 bm2ref = VG_(OSetGen_Lookup)(bm->oset, &a1);
445 if (bm2ref)
446 {
447 bm2 = bm2ref->bm2;
448 }
449 else
bart33e56c92008-03-24 06:41:30 +0000450 {
451 bm2 = bm2_insert(bm, a1);
452 }
453 bm_update_cache(*(struct bitmap**)&bm, a1, bm2);
barta79df6e2008-03-14 17:07:51 +0000454 }
bart0008f5b2008-03-22 17:07:39 +0000455 return bm2;
sewardjaf44c822007-11-25 14:01:38 +0000456}
457
bartf647d342008-03-24 19:12:12 +0000458/** Look up the address a1 in bitmap bm, and insert it if not found.
459 * The returned second level bitmap may be modified.
460 *
461 * @param a1 client address shifted right by ADDR0_BITS.
462 * @param bm bitmap pointer.
463 */
464static __inline__
465struct bitmap2* bm2_lookup_or_insert_exclusive(struct bitmap* const bm,
466 const UWord a1)
467{
468 struct bitmap2* bm2;
469
470 tl_assert(bm);
471 bm2 = (struct bitmap2*)bm2_lookup_or_insert(bm, a1);
472 tl_assert(bm2);
473 if (bm2->refcnt > 1)
474 {
475 struct bitmap2ref* bm2ref;
476 bm2ref = VG_(OSetGen_Lookup)(bm->oset, &a1);
477 bm2 = bm2_make_exclusive(bm, bm2ref);
478 }
479 return bm2;
480}
481
sewardjaf44c822007-11-25 14:01:38 +0000482
bart33e56c92008-03-24 06:41:30 +0000483#endif /* __DRD_BITMAP_H */