2 Copyright 2005-2011 Intel Corporation. All Rights Reserved.
4 This file is part of Threading Building Blocks.
6 Threading Building Blocks is free software; you can redistribute it
7 and/or modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
10 Threading Building Blocks is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with Threading Building Blocks; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 As a special exception, you may use this file as part of a free software
20 library without restriction. Specifically, if other files instantiate
21 templates or use macros or inline functions from this file, or you compile
22 this file and link it with other files to produce an executable, this
23 file does not by itself cause the resulting executable to be covered by
24 the GNU General Public License. This exception does not however
25 invalidate any other reasons why the executable file might be covered by
26 the GNU General Public License.
29 #ifndef __TBB_item_buffer_H
30 #define __TBB_item_buffer_H
32 //! Expandable buffer of items. The possible operations are push, pop,
33 //* tests for empty and so forth. No mutual exclusion is built in.
34 template <typename T, typename A=cache_aligned_allocator<T> >
38 typedef T output_type;
40 typedef size_t size_type;
41 typedef std::pair< T, bool > item_type;
42 typedef typename A::template rebind<item_type>::other allocator_type;
45 size_type my_array_size;
46 static const size_type initial_buffer_size = 4;
50 bool buffer_empty() { return my_head == my_tail; }
52 item_type &item(size_type i) { return my_array[i & (my_array_size - 1) ]; } // may not be marked valid
54 bool item_valid(size_type i) { return item(i).second; }
56 void fetch_front(T &v) { __TBB_ASSERT(item_valid(my_head), "front not valid"); v = item(my_head).first; }
57 void fetch_back(T &v) { __TBB_ASSERT(item_valid(my_tail-1), "back not valid"); v = item(my_tail-1).first; }
59 void invalidate(size_type i) { __TBB_ASSERT(item_valid(i), "Item not valid"); item(i).second = false; }
60 void validate(size_type i) { __TBB_ASSERT(!item_valid(i), "Item already valid"); item(i).second = true; }
62 void invalidate_front() { invalidate(my_head); }
63 void validate_front() { validate(my_head); }
64 void invalidate_back() { invalidate(my_tail-1); }
66 size_type size() { return my_tail - my_head; }
67 size_type capacity() { return my_array_size; }
68 bool buffer_full() { return size() == capacity(); }
70 //! Grows the internal array.
71 void grow_my_array( size_t minimum_size ) {
72 size_type old_size = my_array_size;
73 size_type new_size = old_size ? 2*old_size : initial_buffer_size;
74 while( new_size<minimum_size )
77 item_type* new_array = allocator_type().allocate(new_size);
78 item_type* old_array = my_array;
80 for( size_type i=0; i<new_size; ++i ) {
81 new (&(new_array[i].first)) input_type;
82 new_array[i].second = false;
86 for( size_type i=0; i<old_size; ++i, ++t )
87 new_array[t&(new_size-1)] = old_array[t&(old_size-1)];
89 my_array_size = new_size;
91 for( size_type i=0; i<old_size; ++i, ++t )
92 old_array[i].first.~input_type();
93 allocator_type().deallocate(old_array,old_size);
97 bool push_back(T &v) {
99 grow_my_array(size() + 1);
101 item(my_tail) = std::make_pair( v, true );
106 bool pop_back(T &v) {
107 if (!item_valid(my_tail-1)) {
116 bool pop_front(T &v) {
117 if(!item_valid(my_head)) {
128 item_buffer( ) : my_array(NULL), my_array_size(0),
129 my_head(0), my_tail(0) {
130 grow_my_array(initial_buffer_size);
135 for( size_type i=0; i<my_array_size; ++i ) {
136 my_array[i].first.~input_type();
138 allocator_type().deallocate(my_array,my_array_size);
144 //! item_buffer with reservable front-end. NOTE: if reserving, do not
145 //* complete operation with pop_front(); use consume_front().
146 //* No synchronization built-in.
147 template<typename T, typename A=cache_aligned_allocator<T> >
148 class reservable_item_buffer : public item_buffer<T, A> {
150 using item_buffer<T, A>::buffer_empty;
151 using item_buffer<T, A>::fetch_front;
152 using item_buffer<T, A>::invalidate_front;
153 using item_buffer<T, A>::validate_front;
154 using item_buffer<T, A>::item_valid;
155 using item_buffer<T, A>::my_head;
158 reservable_item_buffer() : item_buffer<T, A>(), my_reserved(false) {}
161 bool reserve_front(T &v) {
162 if(my_reserved || !item_valid(my_head)) return false;
164 // reserving the head
166 // invalidate the head, but don't commit until consume is called
171 void consume_front() {
172 __TBB_ASSERT(my_reserved, "Attempt to consume a non-reserved item");
177 void release_front() {
178 __TBB_ASSERT(my_reserved, "Attempt to release a non-reserved item");
186 #endif // __TBB_item_buffer_H