diff options
author | aldyh <aldyh@138bc75d-0d04-0410-961f-82ee72b054a4> | 2011-11-08 11:13:41 +0000 |
---|---|---|
committer | aldyh <aldyh@138bc75d-0d04-0410-961f-82ee72b054a4> | 2011-11-08 11:13:41 +0000 |
commit | 4c0315d05fa0f707875686abc4f91f7a979a7c7b (patch) | |
tree | e07de8d0b6265f8d72388d335bd471022e753d57 /libitm/containers.h | |
parent | bf09288ee7b5f264f28081a84fde4c6aa1ac5c82 (diff) | |
download | gcc-4c0315d05fa0f707875686abc4f91f7a979a7c7b.tar.gz |
Merge from transactional-memory branch.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@181154 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libitm/containers.h')
-rw-r--r-- | libitm/containers.h | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/libitm/containers.h b/libitm/containers.h new file mode 100644 index 00000000000..e8aa94bb4f4 --- /dev/null +++ b/libitm/containers.h @@ -0,0 +1,108 @@ +/* Copyright (C) 2011 Free Software Foundation, Inc. + Contributed by Torvald Riegel <triegel@redhat.com>. + + This file is part of the GNU Transactional Memory Library (libitm). + + Libitm is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + Libitm is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU General Public License for + more details. + + Under Section 7 of GPL version 3, you are granted additional + permissions described in the GCC Runtime Library Exception, version + 3.1, as published by the Free Software Foundation. + + You should have received a copy of the GNU General Public License and + a copy of the GCC Runtime Library Exception along with this program; + see the files COPYING3 and COPYING.RUNTIME respectively. If not, see + <http://www.gnu.org/licenses/>. */ + +#ifndef LIBITM_CONTAINERS_H +#define LIBITM_CONTAINERS_H 1 + +#include "common.h" + +namespace GTM HIDDEN { + +// A simple vector-like container. +// If alloc_seperate_cl is true, allocations will happen on separate cache +// lines. +template <typename T, bool alloc_separate_cl = true> +class vector +{ + private: + size_t m_capacity; + size_t m_size; + T* entries; + + // Initial capacity of the vector. + static const size_t default_initial_capacity = 32; + // Above that capacity, grow vector by that size for each call. + static const size_t default_resize_max = 2048; + // Resize vector to at least this capacity. + static const size_t default_resize_min = 32; + + // Don't try to copy this vector. + vector<T, alloc_separate_cl>(const vector<T, alloc_separate_cl>& x); + + public: + typedef T datatype; + typedef T* iterator; + + iterator begin() const { return entries; } + iterator end() const { return entries + m_size; } + T& operator[] (size_t pos) { return entries[pos]; } + const T& operator[] (size_t pos) const { return entries[pos]; } + + vector<T, alloc_separate_cl>(size_t initial_size = default_initial_capacity) + : m_capacity(initial_size), + m_size(0) + { + if (m_capacity > 0) + entries = (T*) xmalloc(sizeof(T) * m_capacity, alloc_separate_cl); + else + entries = 0; + } + ~vector<T, alloc_separate_cl>() { if (m_capacity) free(entries); } + + void resize() + { + if (m_capacity >= default_resize_max) + m_capacity = m_capacity + default_resize_max; + else + m_capacity = m_capacity * 2; + if (m_capacity < default_resize_min) + m_capacity = default_resize_min; + entries = (T*) xrealloc(entries, sizeof(T) * m_capacity, alloc_separate_cl); + } + void resize_noinline() __attribute__((noinline)) { resize(); } + + size_t size() const { return m_size; } + size_t capacity() const { return this->capacity; } + + void clear() { m_size = 0; } + + iterator push() { + // We don't want inlining here since push() is often on the fast path. + if (unlikely(m_size == m_capacity)) resize_noinline(); + return &entries[m_size++]; + } + + iterator pop() { + if (likely(m_size > 0)) + { + m_size--; + return entries + m_size; + } + else return 0; + } +}; + +} // namespace GTM + +#endif // LIBITM_CONTAINERS_H |