tlds
Transactional Operations for Linked Data Structures
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Utilities.h
Go to the documentation of this file.
1 /**
2  * Copyright (C) 2011
3  * University of Rochester Department of Computer Science
4  * and
5  * Lehigh University Department of Computer Science and Engineering
6  *
7  * License: Modified BSD
8  * Please see the file LICENSE.RSTM for licensing information
9  */
10 
11 #ifndef STM_ITM2STM_UTILITIES_H
12 #define STM_ITM2STM_UTILITIES_H
13 
14 #include <stdint.h>
15 
16 namespace itm2stm {
17 // -----------------------------------------------------------------------------
18 // We need to mask the offset bits from an address. This is the least
19 // significant N=log_2(sizeof(void*)) bits, which can be computed by just taking
20 // sizeof(void*) - 1 and treating its unsigned representation as a mask. Then we
21 // do a bitwise & and pick out the offset bits.
22 // -----------------------------------------------------------------------------
23 inline size_t
24 offset_of(const void* const address) {
25  const uintptr_t MASK = static_cast<uintptr_t>(sizeof(void*) - 1);
26  const uintptr_t offset = reinterpret_cast<uintptr_t>(address) & MASK;
27  return static_cast<size_t>(offset);
28 }
29 
30 // -----------------------------------------------------------------------------
31 // We always do word-aligned accesses into the TM ABI (with an appropriate mask
32 // as necessary). This drops the word-unaligned least significant bits from the
33 // address, if there are any. We get the least significant bits using the same
34 // technique as offset_of.
35 // -----------------------------------------------------------------------------
36 inline void**
37 base_of(const void* const address) {
38  const uintptr_t MASK = ~static_cast<uintptr_t>(sizeof(void*) - 1);
39  const uintptr_t base = reinterpret_cast<uintptr_t>(address) & MASK;
40  return reinterpret_cast<void**>(base);
41 }
42 
43 inline void
44 add_bytes(const void*& address, size_t bytes) {
45  const uint8_t* temp = reinterpret_cast<const uint8_t*>(address) + bytes;
46  address = reinterpret_cast<const void*>(temp);
47 }
48 
49 inline void
50 add_bytes(void*& address, size_t bytes) {
51  uint8_t* temp = reinterpret_cast<uint8_t*>(address) + bytes;
52  address = reinterpret_cast<void*>(temp);
53 }
54 
55 // -----------------------------------------------------------------------------
56 // Whenever we need to perform a transactional load or store we need a
57 // mask that has 0xFF in all of the bytes that we are intersted in. This
58 // computes a mask given an [i, j) range, where 0 <= i < j <= sizeof(void*).
59 //
60 // NB: When the parameters are compile-time constants we expect this to become a
61 // simple constant in the binary when compiled with optimizations.
62 // -----------------------------------------------------------------------------
63 inline uintptr_t
64 make_mask(size_t i, size_t j) {
65  // assert(0 <= i && i < j && j <= sizeof(void*) && "range is incorrect")
66  uintptr_t mask = ~(uintptr_t)0;
67  mask = mask >> (8 * (sizeof(void*) - j)); // shift 0s to the top
68  mask = mask << (8 * i); // shift 0s into the bottom
69  return mask;
70 }
71 }
72 
73 #endif // STM_ITM2STM_UTILITIES_H
uintptr_t make_mask(size_t i, size_t j)
Definition: Utilities.h:64
void ** base_of(const void *const address)
Definition: Utilities.h:37
size_t offset_of(const void *const address)
Definition: Utilities.h:24
void add_bytes(const void *&address, size_t bytes)
Definition: Utilities.h:44