1 /* Copyright (C) 2011-2021 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
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
30 namespace GTM HIDDEN
{
32 // A simple vector-like container.
33 // If alloc_seperate_cl is true, allocations will happen on separate cache
35 template <typename T
, bool alloc_separate_cl
= true>
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
);
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
),
67 entries
= (T
*) xmalloc(sizeof(T
) * m_capacity
, alloc_separate_cl
);
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
)
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
))
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; }
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
];
114 if (likely(m_size
> 0))
117 return entries
+ m_size
;
125 #endif // LIBITM_CONTAINERS_H