containers.h 3.9 KB

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