gcc/
[official-gcc.git] / libitm / containers.h
blobeec9fafe0549b2d0f0d2127e332b22b62c772ef8
1 /* Copyright (C) 2011-2014 Free Software Foundation, Inc.
2 Contributed by Torvald Riegel <triegel@redhat.com>.
4 This file is part of the GNU Transactional Memory Library (libitm).
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.
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.
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.
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/>. */
25 #ifndef LIBITM_CONTAINERS_H
26 #define LIBITM_CONTAINERS_H 1
28 #include "common.h"
30 namespace GTM HIDDEN {
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
38 private:
39 size_t m_capacity;
40 size_t m_size;
41 T* entries;
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;
50 // Don't try to copy this vector.
51 vector<T, alloc_separate_cl>(const vector<T, alloc_separate_cl>& x);
53 public:
54 typedef T datatype;
55 typedef T* iterator;
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]; }
62 vector<T, alloc_separate_cl>(size_t initial_size = default_initial_capacity)
63 : m_capacity(initial_size),
64 m_size(0)
66 if (m_capacity > 0)
67 entries = (T*) xmalloc(sizeof(T) * m_capacity, alloc_separate_cl);
68 else
69 entries = 0;
71 ~vector<T, alloc_separate_cl>() { if (m_capacity) free(entries); }
73 void resize(size_t additional_capacity)
75 size_t target = m_capacity + additional_capacity;
76 if (target > default_resize_max)
77 m_capacity = ((target - 1 + default_resize_max) / default_resize_max)
78 * default_resize_max;
79 else
80 while (m_capacity < target)
81 m_capacity = m_capacity * 2;
82 if (m_capacity < default_resize_min)
83 m_capacity = default_resize_min;
84 entries = (T*) xrealloc(entries, sizeof(T) * m_capacity, alloc_separate_cl);
86 void resize_noinline() __attribute__((noinline)) { resize(1); }
87 void resize_noinline(size_t elements) __attribute__((noinline))
89 resize(elements);
92 size_t size() const { return m_size; }
93 size_t capacity() const { return this->capacity; }
95 void set_size (size_t size) { m_size = size; }
96 void clear() { m_size = 0; }
98 iterator push() {
99 // We don't want inlining here since push() is often on the fast path.
100 if (unlikely(m_size == m_capacity)) resize_noinline();
101 return &entries[m_size++];
104 iterator push(size_t elements)
106 // We don't want inlining here since push() is often on the fast path.
107 if (unlikely(m_size + elements > m_capacity)) resize_noinline(elements);
108 iterator it = &entries[m_size];
109 m_size += elements;
110 return it;
113 iterator pop() {
114 if (likely(m_size > 0))
116 m_size--;
117 return entries + m_size;
119 else return 0;
123 } // namespace GTM
125 #endif // LIBITM_CONTAINERS_H