1 /*
2     Copyright (c) 2005-2022 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #ifndef __TBB__flow_graph_item_buffer_impl_H
18 #define __TBB__flow_graph_item_buffer_impl_H
19 
20 #ifndef __TBB_flow_graph_H
21 #error Do not #include this internal file directly; use public TBB headers instead.
22 #endif
23 
24 #include "_aligned_space.h"
25 
26 // in namespace tbb::flow::interfaceX (included in _flow_graph_node_impl.h)
27 
28 //! Expandable buffer of items.  The possible operations are push, pop,
29 //* tests for empty and so forth.  No mutual exclusion is built in.
30 //* objects are constructed into and explicitly-destroyed.  get_my_item gives
31 // a read-only reference to the item in the buffer.  set_my_item may be called
32 // with either an empty or occupied slot.
33 
34 template <typename T, typename A=cache_aligned_allocator<T> >
35 class item_buffer {
36 public:
37     typedef T item_type;
38     enum buffer_item_state { no_item=0, has_item=1, reserved_item=2 };
39 protected:
40     typedef size_t size_type;
41     typedef std::pair<item_type, buffer_item_state> aligned_space_item;
42     typedef aligned_space<aligned_space_item> buffer_item_type;
43     typedef typename allocator_traits<A>::template rebind_alloc<buffer_item_type> allocator_type;
44     buffer_item_type *my_array;
45     size_type my_array_size;
46     static const size_type initial_buffer_size = 4;
47     size_type my_head;
48     size_type my_tail;
49 
buffer_empty()50     bool buffer_empty() const { return my_head == my_tail; }
51 
item(size_type i)52     aligned_space_item &item(size_type i) {
53         __TBB_ASSERT(!(size_type(&(my_array[i&(my_array_size-1)].begin()->second))%alignment_of<buffer_item_state>::value), nullptr);
54         __TBB_ASSERT(!(size_type(&(my_array[i&(my_array_size-1)].begin()->first))%alignment_of<item_type>::value), nullptr);
55         return *my_array[i & (my_array_size - 1) ].begin();
56     }
57 
item(size_type i)58     const aligned_space_item &item(size_type i) const {
59         __TBB_ASSERT(!(size_type(&(my_array[i&(my_array_size-1)].begin()->second))%alignment_of<buffer_item_state>::value), nullptr);
60         __TBB_ASSERT(!(size_type(&(my_array[i&(my_array_size-1)].begin()->first))%alignment_of<item_type>::value), nullptr);
61         return *my_array[i & (my_array_size-1)].begin();
62     }
63 
my_item_valid(size_type i)64     bool my_item_valid(size_type i) const { return (i < my_tail) && (i >= my_head) && (item(i).second != no_item); }
65 #if TBB_USE_ASSERT
my_item_reserved(size_type i)66     bool my_item_reserved(size_type i) const { return item(i).second == reserved_item; }
67 #endif
68 
69     // object management in buffer
get_my_item(size_t i)70     const item_type &get_my_item(size_t i) const {
71         __TBB_ASSERT(my_item_valid(i),"attempt to get invalid item");
72         item_type* itm = const_cast<item_type*>(reinterpret_cast<const item_type*>(&item(i).first));
73         return *itm;
74     }
75 
76     // may be called with an empty slot or a slot that has already been constructed into.
set_my_item(size_t i,const item_type & o)77     void set_my_item(size_t i, const item_type &o) {
78         if(item(i).second != no_item) {
79             destroy_item(i);
80         }
81         new(&(item(i).first)) item_type(o);
82         item(i).second = has_item;
83     }
84 
85     // destructively-fetch an object from the buffer
fetch_item(size_t i,item_type & o)86     void fetch_item(size_t i, item_type &o) {
87         __TBB_ASSERT(my_item_valid(i), "Trying to fetch an empty slot");
88         o = get_my_item(i);  // could have std::move assign semantics
89         destroy_item(i);
90     }
91 
92     // move an existing item from one slot to another.  The moved-to slot must be unoccupied,
93     // the moved-from slot must exist and not be reserved.  The after, from will be empty,
94     // to will be occupied but not reserved
move_item(size_t to,size_t from)95     void move_item(size_t to, size_t from) {
96         __TBB_ASSERT(!my_item_valid(to), "Trying to move to a non-empty slot");
97         __TBB_ASSERT(my_item_valid(from), "Trying to move from an empty slot");
98         set_my_item(to, get_my_item(from));   // could have std::move semantics
99         destroy_item(from);
100 
101     }
102 
103     // put an item in an empty slot.  Return true if successful, else false
place_item(size_t here,const item_type & me)104     bool place_item(size_t here, const item_type &me) {
105 #if !TBB_DEPRECATED_SEQUENCER_DUPLICATES
106         if(my_item_valid(here)) return false;
107 #endif
108         set_my_item(here, me);
109         return true;
110     }
111 
112     // could be implemented with std::move semantics
swap_items(size_t i,size_t j)113     void swap_items(size_t i, size_t j) {
114         __TBB_ASSERT(my_item_valid(i) && my_item_valid(j), "attempt to swap invalid item(s)");
115         item_type temp = get_my_item(i);
116         set_my_item(i, get_my_item(j));
117         set_my_item(j, temp);
118     }
119 
destroy_item(size_type i)120     void destroy_item(size_type i) {
121         __TBB_ASSERT(my_item_valid(i), "destruction of invalid item");
122         item(i).first.~item_type();
123         item(i).second = no_item;
124     }
125 
126     // returns the front element
front()127     const item_type& front() const
128     {
129         __TBB_ASSERT(my_item_valid(my_head), "attempt to fetch head non-item");
130         return get_my_item(my_head);
131     }
132 
133     // returns  the back element
back()134     const item_type& back() const
135     {
136         __TBB_ASSERT(my_item_valid(my_tail - 1), "attempt to fetch head non-item");
137         return get_my_item(my_tail - 1);
138     }
139 
140     // following methods are for reservation of the front of a buffer.
reserve_item(size_type i)141     void reserve_item(size_type i) { __TBB_ASSERT(my_item_valid(i) && !my_item_reserved(i), "item cannot be reserved"); item(i).second = reserved_item; }
release_item(size_type i)142     void release_item(size_type i) { __TBB_ASSERT(my_item_reserved(i), "item is not reserved"); item(i).second = has_item; }
143 
destroy_front()144     void destroy_front() { destroy_item(my_head); ++my_head; }
destroy_back()145     void destroy_back() { destroy_item(my_tail-1); --my_tail; }
146 
147     // we have to be able to test against a new tail value without changing my_tail
148     // grow_array doesn't work if we change my_tail when the old array is too small
149     size_type size(size_t new_tail = 0) { return (new_tail ? new_tail : my_tail) - my_head; }
capacity()150     size_type capacity() { return my_array_size; }
151     // sequencer_node does not use this method, so we don't
152     // need a version that passes in the new_tail value.
buffer_full()153     bool buffer_full() { return size() >= capacity(); }
154 
155     //! Grows the internal array.
grow_my_array(size_t minimum_size)156     void grow_my_array( size_t minimum_size ) {
157         // test that we haven't made the structure inconsistent.
158         __TBB_ASSERT(capacity() >= my_tail - my_head, "total items exceed capacity");
159         size_type new_size = my_array_size ? 2*my_array_size : initial_buffer_size;
160         while( new_size<minimum_size )
161             new_size*=2;
162 
163         buffer_item_type* new_array = allocator_type().allocate(new_size);
164 
165         // initialize validity to "no"
166         for( size_type i=0; i<new_size; ++i ) { new_array[i].begin()->second = no_item; }
167 
168         for( size_type i=my_head; i<my_tail; ++i) {
169             if(my_item_valid(i)) {  // sequencer_node may have empty slots
170                 // placement-new copy-construct; could be std::move
171                 char *new_space = (char *)&(new_array[i&(new_size-1)].begin()->first);
172                 (void)new(new_space) item_type(get_my_item(i));
173                 new_array[i&(new_size-1)].begin()->second = item(i).second;
174             }
175         }
176 
177         clean_up_buffer(/*reset_pointers*/false);
178 
179         my_array = new_array;
180         my_array_size = new_size;
181     }
182 
push_back(item_type & v)183     bool push_back(item_type &v) {
184         if(buffer_full()) {
185             grow_my_array(size() + 1);
186         }
187         set_my_item(my_tail, v);
188         ++my_tail;
189         return true;
190     }
191 
pop_back(item_type & v)192     bool pop_back(item_type &v) {
193         if (!my_item_valid(my_tail-1)) {
194             return false;
195         }
196         v = this->back();
197         destroy_back();
198         return true;
199     }
200 
pop_front(item_type & v)201     bool pop_front(item_type &v) {
202         if(!my_item_valid(my_head)) {
203             return false;
204         }
205         v = this->front();
206         destroy_front();
207         return true;
208     }
209 
210     // This is used both for reset and for grow_my_array.  In the case of grow_my_array
211     // we want to retain the values of the head and tail.
clean_up_buffer(bool reset_pointers)212     void clean_up_buffer(bool reset_pointers) {
213         if (my_array) {
214             for( size_type i=my_head; i<my_tail; ++i ) {
215                 if(my_item_valid(i))
216                     destroy_item(i);
217             }
218             allocator_type().deallocate(my_array,my_array_size);
219         }
220         my_array = nullptr;
221         if(reset_pointers) {
222             my_head = my_tail = my_array_size = 0;
223         }
224     }
225 
226 public:
227     //! Constructor
item_buffer()228     item_buffer( ) : my_array(nullptr), my_array_size(0),
229                      my_head(0), my_tail(0) {
230         grow_my_array(initial_buffer_size);
231     }
232 
~item_buffer()233     ~item_buffer() {
234         clean_up_buffer(/*reset_pointers*/true);
235     }
236 
reset()237     void reset() { clean_up_buffer(/*reset_pointers*/true); grow_my_array(initial_buffer_size); }
238 
239 };
240 
241 //! item_buffer with reservable front-end.  NOTE: if reserving, do not
242 //* complete operation with pop_front(); use consume_front().
243 //* No synchronization built-in.
244 template<typename T, typename A=cache_aligned_allocator<T> >
245 class reservable_item_buffer : public item_buffer<T, A> {
246 protected:
247     using item_buffer<T, A>::my_item_valid;
248     using item_buffer<T, A>::my_head;
249 
250 public:
reservable_item_buffer()251     reservable_item_buffer() : item_buffer<T, A>(), my_reserved(false) {}
reset()252     void reset() {my_reserved = false; item_buffer<T,A>::reset(); }
253 protected:
254 
reserve_front(T & v)255     bool reserve_front(T &v) {
256         if(my_reserved || !my_item_valid(this->my_head)) return false;
257         my_reserved = true;
258         // reserving the head
259         v = this->front();
260         this->reserve_item(this->my_head);
261         return true;
262     }
263 
consume_front()264     void consume_front() {
265         __TBB_ASSERT(my_reserved, "Attempt to consume a non-reserved item");
266         this->destroy_front();
267         my_reserved = false;
268     }
269 
release_front()270     void release_front() {
271         __TBB_ASSERT(my_reserved, "Attempt to release a non-reserved item");
272         this->release_item(this->my_head);
273         my_reserved = false;
274     }
275 
276     bool my_reserved;
277 };
278 
279 #endif // __TBB__flow_graph_item_buffer_impl_H
280