Thomas Koenig | b18a97e | 2021-09-13 19:49:49 +0200 | [diff] [blame] | 1 | /* Copyright (C) 2011-2021 Free Software Foundation, Inc. |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 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 | |
Torvald Riegel | 11f30bb | 2012-01-08 14:13:49 +0000 | [diff] [blame] | 73 | void resize(size_t additional_capacity) |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 74 | { |
Torvald Riegel | 11f30bb | 2012-01-08 14:13:49 +0000 | [diff] [blame] | 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; |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 79 | else |
Torvald Riegel | 11f30bb | 2012-01-08 14:13:49 +0000 | [diff] [blame] | 80 | while (m_capacity < target) |
| 81 | m_capacity = m_capacity * 2; |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 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); |
| 85 | } |
Torvald Riegel | 11f30bb | 2012-01-08 14:13:49 +0000 | [diff] [blame] | 86 | void resize_noinline() __attribute__((noinline)) { resize(1); } |
| 87 | void resize_noinline(size_t elements) __attribute__((noinline)) |
| 88 | { |
| 89 | resize(elements); |
| 90 | } |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 91 | |
| 92 | size_t size() const { return m_size; } |
| 93 | size_t capacity() const { return this->capacity; } |
| 94 | |
Torvald Riegel | aab74ee | 2012-01-13 23:45:42 +0000 | [diff] [blame] | 95 | void set_size (size_t size) { m_size = size; } |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 96 | void clear() { m_size = 0; } |
| 97 | |
| 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++]; |
| 102 | } |
| 103 | |
Torvald Riegel | 11f30bb | 2012-01-08 14:13:49 +0000 | [diff] [blame] | 104 | iterator push(size_t elements) |
| 105 | { |
| 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; |
| 111 | } |
| 112 | |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 113 | iterator pop() { |
| 114 | if (likely(m_size > 0)) |
| 115 | { |
| 116 | m_size--; |
| 117 | return entries + m_size; |
| 118 | } |
| 119 | else return 0; |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | } // namespace GTM |
| 124 | |
| 125 | #endif // LIBITM_CONTAINERS_H |