blob: 394b6f2508ecb6b100dc50a7b487b6cbbb66a71f [file] [log] [blame]
Torvald Riegel11f30bb2012-01-08 14:13:49 +00001/* Copyright (C) 2011, 2012 Free Software Foundation, Inc.
Aldy Hernandez0a355132011-11-08 11:13:41 +00002 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
30namespace GTM HIDDEN {
31
32// A simple vector-like container.
33// If alloc_seperate_cl is true, allocations will happen on separate cache
34// lines.
35template <typename T, bool alloc_separate_cl = true>
36class 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
Torvald Riegel11f30bb2012-01-08 14:13:49 +000073 void resize(size_t additional_capacity)
Aldy Hernandez0a355132011-11-08 11:13:41 +000074 {
Torvald Riegel11f30bb2012-01-08 14:13:49 +000075 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;
Aldy Hernandez0a355132011-11-08 11:13:41 +000079 else
Torvald Riegel11f30bb2012-01-08 14:13:49 +000080 while (m_capacity < target)
81 m_capacity = m_capacity * 2;
Aldy Hernandez0a355132011-11-08 11:13:41 +000082 if (m_capacity < default_resize_min)
83 m_capacity = default_resize_min;
84 entries = (T*) xrealloc(entries, sizeof(T) * m_capacity, alloc_separate_cl);
85 }
Torvald Riegel11f30bb2012-01-08 14:13:49 +000086 void resize_noinline() __attribute__((noinline)) { resize(1); }
87 void resize_noinline(size_t elements) __attribute__((noinline))
88 {
89 resize(elements);
90 }
Aldy Hernandez0a355132011-11-08 11:13:41 +000091
92 size_t size() const { return m_size; }
93 size_t capacity() const { return this->capacity; }
94
95 void clear() { m_size = 0; }
96
97 iterator push() {
98 // We don't want inlining here since push() is often on the fast path.
99 if (unlikely(m_size == m_capacity)) resize_noinline();
100 return &entries[m_size++];
101 }
102
Torvald Riegel11f30bb2012-01-08 14:13:49 +0000103 iterator push(size_t elements)
104 {
105 // We don't want inlining here since push() is often on the fast path.
106 if (unlikely(m_size + elements > m_capacity)) resize_noinline(elements);
107 iterator it = &entries[m_size];
108 m_size += elements;
109 return it;
110 }
111
Aldy Hernandez0a355132011-11-08 11:13:41 +0000112 iterator pop() {
113 if (likely(m_size > 0))
114 {
115 m_size--;
116 return entries + m_size;
117 }
118 else return 0;
119 }
120};
121
122} // namespace GTM
123
124#endif // LIBITM_CONTAINERS_H