annotate libitm/containers.h @ 158:494b0b89df80 default tip

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