OSDN Git Service

libitm: Don't enforce privatization safety if already in serial mode.
[pf3gnuchains/gcc-fork.git] / libitm / containers.h
1 /* Copyright (C) 2011 Free Software Foundation, Inc.
2    Contributed by Torvald Riegel <triegel@redhat.com>.
3
4    This file is part of the GNU Transactional Memory Library (libitm).
5
6    Libitm is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    Libitm is distributed in the hope that it will be useful, but WITHOUT ANY
12    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13    FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14    more details.
15
16    Under Section 7 of GPL version 3, you are granted additional
17    permissions described in the GCC Runtime Library Exception, version
18    3.1, as published by the Free Software Foundation.
19
20    You should have received a copy of the GNU General Public License and
21    a copy of the GCC Runtime Library Exception along with this program;
22    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23    <http://www.gnu.org/licenses/>.  */
24
25 #ifndef LIBITM_CONTAINERS_H
26 #define LIBITM_CONTAINERS_H 1
27
28 #include "common.h"
29
30 namespace GTM HIDDEN {
31
32 // A simple vector-like container.
33 // If alloc_seperate_cl is true, allocations will happen on separate cache
34 // lines.
35 template <typename T, bool alloc_separate_cl = true>
36 class vector
37 {
38  private:
39   size_t m_capacity;
40   size_t m_size;
41   T* entries;
42
43   // Initial capacity of the vector.
44   static const size_t default_initial_capacity = 32;
45   // Above that capacity, grow vector by that size for each call.
46   static const size_t default_resize_max = 2048;
47   // Resize vector to at least this capacity.
48   static const size_t default_resize_min = 32;
49
50   // Don't try to copy this vector.
51   vector<T, alloc_separate_cl>(const vector<T, alloc_separate_cl>& x);
52
53  public:
54   typedef T datatype;
55   typedef T* iterator;
56
57   iterator begin() const { return entries; }
58   iterator end() const { return entries + m_size; }
59   T& operator[] (size_t pos) { return entries[pos]; }
60   const T& operator[] (size_t pos) const  { return entries[pos]; }
61
62   vector<T, alloc_separate_cl>(size_t initial_size = default_initial_capacity)
63     : m_capacity(initial_size),
64       m_size(0)
65   {
66     if (m_capacity > 0)
67       entries = (T*) xmalloc(sizeof(T) * m_capacity, alloc_separate_cl);
68     else
69       entries = 0;
70   }
71   ~vector<T, alloc_separate_cl>() { if (m_capacity) free(entries); }
72
73   void resize()
74   {
75     if (m_capacity >= default_resize_max)
76       m_capacity = m_capacity + default_resize_max;
77     else
78       m_capacity = m_capacity * 2;
79     if (m_capacity < default_resize_min)
80       m_capacity = default_resize_min;
81     entries = (T*) xrealloc(entries, sizeof(T) * m_capacity, alloc_separate_cl);
82   }
83   void resize_noinline() __attribute__((noinline)) { resize(); }
84
85   size_t size() const { return m_size; }
86   size_t capacity() const { return this->capacity; }
87
88   void clear() { m_size = 0; }
89
90   iterator push() {
91     // We don't want inlining here since push() is often on the fast path.
92     if (unlikely(m_size == m_capacity)) resize_noinline();
93     return &entries[m_size++];
94   }
95
96   iterator pop() {
97     if (likely(m_size > 0))
98       {
99         m_size--;
100         return entries + m_size;
101       }
102     else return 0;
103   }
104 };
105
106 } // namespace GTM
107
108 #endif // LIBITM_CONTAINERS_H