Tervel  1.0.0
A collection of wait-free containers and algorithms.
array_array.h
Go to the documentation of this file.
1 /*
2 The MIT License (MIT)
3 
4 Copyright (c) 2015 University of Central Florida's Computer Software Engineering
5 Scalable & Secure Systems (CSE - S3) Lab
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24 */
25 #ifndef __TERVEL_CONTAINERS_WF_VECTOR_VECTOR_ARRAY_ARRAY_H
26 #define __TERVEL_CONTAINERS_WF_VECTOR_VECTOR_ARRAY_ARRAY_H
27 
28 #include <tervel/util/util.h>
29 
31 
32 namespace tervel {
33 namespace containers {
34 namespace wf {
35 namespace vector {
36 
37 template<typename T>
38 class ArrayArray : public VectorArray<T> {
39  typedef std::atomic<T> ArrayElement;
40  typedef ArrayElement * ArraySegment;
51  public:
52  ArrayArray(size_t capacity, T default_value = nullptr)
53  : default_value_(default_value) {
54  if (capacity < 2) {
55  capacity = 2;
56  }
58  offset_ = std::pow(2, offset_pow_);
59 
62  }
63 
65  for (size_t i = 0; i < k_max_array_segments_; i++) {
66  delete [] array_of_arrays[i];
67  }
68  }
69 
76  ArraySegment add_segment(const size_t pos) {
77  ArraySegment cur_seg = array_of_arrays[pos].load();
78 
79  if (cur_seg == nullptr) {
80  size_t seg_cap = 0x1 << (offset_pow_ + pos);
81  ArraySegment new_seg = allocate_array_segment(seg_cap);
82 
83  if (array_of_arrays[pos].compare_exchange_strong(cur_seg, new_seg)) {
84  current_capacity_.fetch_add(seg_cap);
85  return new_seg;
86  } else {
87  delete [] new_seg;
88  return cur_seg;
89  }
90  } // if cur_seg == nullprt
91  return cur_seg;
92  }; // add_segment
93 
94 
101  ArrayElement * get_spot(const size_t raw_pos, const bool no_add = false) {
102  if (raw_pos < offset_) {
103  ArraySegment seg = array_of_arrays[0].load();
104  return &(seg[raw_pos]);
105  } else {
106  static const int nobits = (sizeof(unsigned int) << 3) - 1;
107  size_t pos = raw_pos + offset_;
108  size_t num = nobits - __builtin_clz(pos);
109 
110  size_t elem_pos = pos ^ (1 << num);
111  size_t seg_num = num - offset_pow_;
112 
113  ArraySegment seg = array_of_arrays[seg_num].load();
114  if (seg == nullptr && !no_add) {
115  seg = add_segment(seg_num);
116  assert(seg != NULL);
117  }
118 
119  if (seg != nullptr) {
120  return &(seg[elem_pos]);
121  } else {
122  return nullptr;
123  }
124  } // else it is first array
125  } // get_pos
126 
127  ArrayElement *allocate_array_segment(const size_t capacity) {
128  ArrayElement * temp = new ArrayElement[capacity];
129  for (uint64_t i = 0; i < capacity; i++) {
130  temp[i] = default_value_;
131  }
132  return temp;
133  }
134 
135  size_t capacity() {
136  return current_capacity_.load();
137  }
138 
139  private:
140  const T default_value_ {nullptr};
141  static const size_t k_max_array_segments_ {64};
142 
143  std::atomic<ArraySegment> array_of_arrays[k_max_array_segments_];
145 
146  std::atomic<size_t> current_capacity_;
147 }; // class Vector Array
148 } // namespace vector
149 } // namespace wf
150 } // namespace containers
151 } // namespace tervel
152 
153 #endif // __TERVEL_CONTAINERS_WF_VECTOR_VECTOR_
std::atomic< ArraySegment > array_of_arrays[k_max_array_segments_]
Definition: array_array.h:143
std::atomic< T > ArrayElement
Definition: array_array.h:39
ArrayArray(size_t capacity, T default_value=nullptr)
This class contains code related to managing elements stored in the vector It stores elements on a se...
Definition: array_array.h:52
~ArrayArray()
Definition: array_array.h:64
const T default_value_
Definition: array_array.h:140
TODO(steven):
Definition: mcas.h:36
ArrayElement * ArraySegment
Definition: array_array.h:40
size_t offset_pow_
Definition: array_array.h:144
std::atomic< size_t > current_capacity_
Definition: array_array.h:146
int round_to_next_power_of_two(uint64_t value)
Returns the next power of two.
Definition: wf_hash_map_no_delete.h:44
size_t capacity()
Definition: array_array.h:135
Definition: vector_array.h:38
ArrayElement * allocate_array_segment(const size_t capacity)
Definition: array_array.h:127
Definition: array_array.h:38
size_t offset_
Definition: array_array.h:144
ArrayElement * get_spot(const size_t raw_pos, const bool no_add=false)
This function returns the address of the specified position.
Definition: array_array.h:101
ArraySegment add_segment(const size_t pos)
This function adds an array segment to the arrays used to hold additional elements.
Definition: array_array.h:76
static const size_t k_max_array_segments_
Definition: array_array.h:141